summaryrefslogtreecommitdiffstats
path: root/test/run-allrecord-traverse-deadlock.c
blob: 2c582062f0469de8b2006dad91c2a5520c749504 (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
#include "../common/tdb_private.h"
#include "../common/io.c"
#include "../common/tdb.c"
#include "../common/lock.c"
#include "../common/freelist.c"
#include "../common/traverse.c"
#include "../common/transaction.c"
#include "../common/error.c"
#include "../common/open.c"
#include "../common/check.c"
#include "../common/hash.c"
#include "../common/mutex.c"
#include "tap-interface.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdarg.h>
#include "logging.h"

static void do_allrecord_lock(const char *name, int tdb_flags, int up,
			      int down)
{
	struct tdb_context *tdb;
	int ret;
	ssize_t nread, nwritten;
	char c = 0;

	tdb = tdb_open_ex(name, 3, tdb_flags,
			  O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
	ok(tdb, "tdb_open_ex should succeed");

	ret = tdb_lockall(tdb);
	ok(ret == 0, "tdb_lockall should succeed");

	nwritten = write(up, &c, sizeof(c));
	ok(nwritten == sizeof(c), "write should succeed");

	nread = read(down, &c, sizeof(c));
	ok(nread == sizeof(c), "read should succeed");

	ret = tdb_traverse(tdb, NULL, NULL);
	ok(ret == -1, "do_allrecord_lock: traverse should fail");

	nwritten = write(up, &c, sizeof(c));
	ok(nwritten == sizeof(c), "write should succeed");

	exit(0);
}

static void do_traverse(const char *name, int tdb_flags, int up, int down)
{
	struct tdb_context *tdb;
	int ret;
	ssize_t nread, nwritten;
	char c = 0;

	tdb = tdb_open_ex(name, 3, tdb_flags,
			  O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
	ok(tdb, "tdb_open_ex should succeed");

	ret = tdb_traverse(tdb, NULL, NULL);
	ok(ret == 1, "do_traverse: tdb_traverse should return 1 record");

	nwritten = write(up, &c, sizeof(c));
	ok(nwritten == sizeof(c), "write should succeed");

	nread = read(down, &c, sizeof(c));
	ok(nread == sizeof(c), "read should succeed");

	exit(0);
}

/*
 * Process 1: get the allrecord_lock on a tdb.
 * Process 2: start a traverse, this will stall waiting for the
 *            first chainlock: That is taken by the allrecord_lock
 * Process 1: start a traverse: This will get EDEADLK in trying to
 *            get the TRANSACTION_LOCK. It will deadlock for mutexes,
 *            which don't have built-in deadlock detection.
 */

static int do_tests(const char *name, int tdb_flags)
{
	struct tdb_context *tdb;
	int ret;
	pid_t traverse_child, allrecord_child;
	int traverse_down[2];
	int traverse_up[2];
	int allrecord_down[2];
	int allrecord_up[2];
	char c;
	ssize_t nread, nwritten;
	TDB_DATA key, data;

	key.dsize = strlen("hi");
	key.dptr = discard_const_p(uint8_t, "hi");
	data.dsize = strlen("world");
	data.dptr = discard_const_p(uint8_t, "world");

	tdb = tdb_open_ex(name, 3, tdb_flags,
			  O_RDWR|O_CREAT, 0755, &taplogctx, NULL);
	ok(tdb, "tdb_open_ex should succeed");

	ret = tdb_store(tdb, key, data, TDB_INSERT);
	ok(ret == 0, "tdb_store should succeed");

	ret = pipe(traverse_down);
	ok(ret == 0, "pipe should succeed");

	ret = pipe(traverse_up);
	ok(ret == 0, "pipe should succeed");

	ret = pipe(allrecord_down);
	ok(ret == 0, "pipe should succeed");

	ret = pipe(allrecord_up);
	ok(ret == 0, "pipe should succeed");

	allrecord_child = fork();
	ok(allrecord_child != -1, "fork should succeed");

	if (allrecord_child == 0) {
		tdb_close(tdb);
		close(traverse_up[0]);
		close(traverse_up[1]);
		close(traverse_down[0]);
		close(traverse_down[1]);
		close(allrecord_up[0]);
		close(allrecord_down[1]);
		do_allrecord_lock(name, tdb_flags,
				  allrecord_up[1], allrecord_down[0]);
		exit(0);
	}
	close(allrecord_up[1]);
	close(allrecord_down[0]);

	nread = read(allrecord_up[0], &c, sizeof(c));
	ok(nread == sizeof(c), "read should succeed");

	traverse_child = fork();
	ok(traverse_child != -1, "fork should succeed");

	if (traverse_child == 0) {
		tdb_close(tdb);
		close(traverse_up[0]);
		close(traverse_down[1]);
		close(allrecord_up[0]);
		close(allrecord_down[1]);
		do_traverse(name, tdb_flags,
			    traverse_up[1], traverse_down[0]);
		exit(0);
	}
	close(traverse_up[1]);
	close(traverse_down[0]);

	poll(NULL, 0, 1000);

	nwritten = write(allrecord_down[1], &c, sizeof(c));
	ok(nwritten == sizeof(c), "write should succeed");

	nread = read(traverse_up[0], &c, sizeof(c));
	ok(nread == sizeof(c), "read should succeed");

	nwritten = write(traverse_down[1], &c, sizeof(c));
	ok(nwritten == sizeof(c), "write should succeed");

	nread = read(allrecord_up[0], &c, sizeof(c));
	ok(nread == sizeof(c), "ret should succeed");

	close(traverse_up[0]);
	close(traverse_down[1]);
	close(allrecord_up[0]);
	close(allrecord_down[1]);
	diag("%s tests done", name);
	return exit_status();
}

int main(int argc, char *argv[])
{
	int ret;
	bool mutex_support;

	mutex_support = tdb_runtime_check_for_robust_mutexes();

	ret = do_tests("marklock-deadlock-fcntl.tdb",
		       TDB_CLEAR_IF_FIRST |
		       TDB_INCOMPATIBLE_HASH);
	ok(ret == 0, "marklock-deadlock-fcntl.tdb tests should succeed");

	if (!mutex_support) {
		skip(1, "No robust mutex support, "
			"skipping marklock-deadlock-mutex.tdb tests");
		return exit_status();
	}

	ret = do_tests("marklock-deadlock-mutex.tdb",
		       TDB_CLEAR_IF_FIRST |
		       TDB_MUTEX_LOCKING |
		       TDB_INCOMPATIBLE_HASH);
	ok(ret == 0, "marklock-deadlock-mutex.tdb tests should succeed");

	return exit_status();
}