summaryrefslogtreecommitdiffstats
path: root/dlm/dlm_kill/dlm_lock_alt.c
blob: a757c6c6ded8ab637c02a02cdfe56b3d38fa06df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Copyright (c) 2010 David Teigland
 * All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License V2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <limits.h>
#include <time.h>
#include <errno.h>
#include <signal.h>
#include <syslog.h>
#include <sys/poll.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <sys/types.h>

#include "libdlm.h"

/* 
 * The general idea of this test is
 * - dlm_lock on node1 gets EX
 * - dlm_lock on node2 blocks on EX
 * - dlm_lock on node1 is killed
 * - dlm_lock on node2 should get EX
 * repeat
 *
 * The dlm_lock process is intentionally killed without an explicit
 * unlock to test the lock cleanup done by the kernel when a process
 * exits while holding locks.
 */

/* even/odd limits this to two instances, but requires no config;
   "offset max" args (like alternate) would allow multiple */

static dlm_lshandle_t *dh;
static struct dlm_lksb counter_lksb;
static char counter_lvb[32];
static uint32_t counter;
static int we_are_even;

static int do_release(void)
{
	dh = dlm_create_lockspace("dlm_lock_alt", 0600);
	if (!dh) {
		printf("alt create lockspace error\n");
		return -1;
	}

	dlm_release_lockspace("dlm_lock_alt", dh, 1);
	return 0;
}

static int get_counter(void)
{
	int rv;

	dh = dlm_create_lockspace("dlm_lock_alt", 0600);
	if (!dh) {
		return -1;
	}

	rv = dlm_ls_pthread_init(dh);
	if (rv < 0) {
		return -1;
	}

	memset(&counter_lksb, 0, sizeof(counter_lksb));
	memset(&counter_lvb, 0, sizeof(counter_lvb));
	counter_lksb.sb_lvbptr = counter_lvb;

	printf("alt counter request NL ...\n");
	rv = dlm_ls_lock_wait(dh, LKM_NLMODE, &counter_lksb,
				LKF_VALBLK,
				"counter", strlen("counter"),
				0, NULL, NULL, NULL);

	printf("alt counter request NL done status %d lkid %x\n",
	       counter_lksb.sb_status, counter_lksb.sb_lkid);

	if (rv) {
		return -1;
	}
	return 0;
}

/* increment lvb of counter_lock */
/* if we are even, counter++ should make counter odd */

static int inc_counter(void)
{
	int rv;

	/* NL->EX */

	rv = dlm_ls_lock_wait(dh, LKM_EXMODE, &counter_lksb,
				LKF_VALBLK | LKF_CONVERT,
				"counter", strlen("counter"),
				0, NULL, NULL, NULL);
	if (rv) {
		return -1;
	}

	counter++;
	memcpy(&counter_lvb, &counter, sizeof(counter));

	/* EX->NL */

	rv = dlm_ls_lock_wait(dh, LKM_NLMODE, &counter_lksb,
				LKF_VALBLK | LKF_CONVERT,
				"counter", strlen("counter"),
				0, NULL, NULL, NULL);
	if (rv) {
		return -1;
	}

	return 0;
}

/* if we are even, wait for counter lock to be even */

static int wait_counter(void)
{
	int rv;

	while (1) {
		/* NL->PR */

		rv = dlm_ls_lock_wait(dh, LKM_PRMODE, &counter_lksb,
					LKF_VALBLK | LKF_CONVERT,
					"counter", strlen("counter"),
					0, NULL, NULL, NULL);
		if (rv) {
			return -1;
		}

		memcpy(&counter, &counter_lvb, sizeof(counter));

		/* PR->NL */

		rv = dlm_ls_lock_wait(dh, LKM_NLMODE, &counter_lksb,
					LKF_VALBLK | LKF_CONVERT,
					"counter", strlen("counter"),
					0, NULL, NULL, NULL);
		if (rv) {
			return -1;
		}

		if (!counter) {
			we_are_even = 1;
			break;
		}

		if (we_are_even && !(counter % 2))
			break;

		if (!we_are_even && (counter % 2))
			break;

		usleep(500000);
	}

	return 0;
}

static int rand_int(int a, int b)
{
	return a + (int) (((float)(b - a + 1)) * random() / (RAND_MAX+1.0)); 
}

#define LINE_LEN 64

static int wait_child_lock(int pr_fd)
{
	char *done_str = "dlm_lock convert EX done";
	char line[LINE_LEN], prog[32], act[32], mode[32], done[32], status[32];
	FILE *file;
	int n, val, rv = -1;

	file = fdopen(pr_fd, "r");
	if (!file) {
		printf("alt fdopen error %d\n", errno);
		return -1;
	}

	while (fgets(line, LINE_LEN, file)) {
		printf("%s", line);
		fflush(stdout);

		n = sscanf(line, "%s %s %s %s %s %d",
			   prog, act, mode, done, status, &val);

		if (n < 6)
			continue;

		if (!strncmp(line, done_str, strlen(done_str))) {
			if (!strcmp(status, "status") && val == 0)
				rv = 0;
			break;
		}
	}
	fclose(file);
	return rv;
}

int main(int argc, char *argv[])
{
	int fda[2];
	int pr_fd, cw_fd; /* parent read fd, child write fd */
	int pid, status, n, rv;

	printf("alt pid %d\n", getpid());

	if (argc > 1 && !strcmp(argv[1], "release")) {
		do_release();
		return 0;
	}

	rv = get_counter();
	if (rv < 0) {
		return -1;
	}

	while (1) {
		/* when this returns, it's our turn (other node has EX) */
		printf("alt wait counter %s %u\n", we_are_even ? "even" : "odd", counter);
		rv = wait_counter();
		if (rv < 0) {
			printf("alt wait_counter error\n");
			break;
		}

		pipe(fda);
		pr_fd = fda[0];
		cw_fd = fda[1];

		pid = fork();
		if (!pid) {
			/* child's stdout and stderr sent to parent's pr_fd */
			close(STDOUT_FILENO);
			dup(cw_fd);
			close(STDERR_FILENO);
			dup(cw_fd);
			close(STDIN_FILENO);

			execl("./dlm_lock", "./dlm_lock", NULL);
			exit(EXIT_FAILURE);

		} else {
			printf("alt fork pid %d\n", pid);

			/* wait for child to get EX */
			printf("alt wait child lock\n");
			rv = wait_child_lock(pr_fd);
			close(pr_fd);
			close(cw_fd);
			if (rv < 0) {
				printf("alt child lock error\n");
				break;
			}

			/* let other node start another dlm_lock process */
			rv = inc_counter();
			if (rv < 0) {
				printf("alt inc_counter error\n");
				break;
			}

			/* sleep random number of seconds */
			n = rand_int(0, 4);
			printf("alt sleep %d\n", n);
			sleep(rand_int(0, 10));

			/* kill child */
			printf("alt kill pid %d\n", pid);
			kill(pid, SIGKILL);

			/* wait for child to exit */
			printf("alt wait pid %d\n", pid);
			waitpid(pid, &status, 0);

			/* sleep random number of seconds */
			n = rand_int(0, 2);
			printf("alt sleep %d\n", n);
			sleep(n);
		}
	}

	return 0;
}