1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "../common/mutex.c"
13 #include "tap-interface.h"
15 #include <sys/types.h>
20 static void do_allrecord_lock(const char *name
, int tdb_flags
, int up
,
23 struct tdb_context
*tdb
;
25 ssize_t nread
, nwritten
;
28 tdb
= tdb_open_ex(name
, 3, tdb_flags
,
29 O_RDWR
|O_CREAT
, 0755, &taplogctx
, NULL
);
30 ok(tdb
, "tdb_open_ex should succeed");
32 ret
= tdb_lockall(tdb
);
33 ok(ret
== 0, "tdb_lockall should succeed");
35 nwritten
= write(up
, &c
, sizeof(c
));
36 ok(nwritten
== sizeof(c
), "write should succeed");
38 nread
= read(down
, &c
, sizeof(c
));
39 ok(nread
== sizeof(c
), "read should succeed");
41 ret
= tdb_traverse(tdb
, NULL
, NULL
);
42 ok(ret
== -1, "do_allrecord_lock: traverse should fail");
44 nwritten
= write(up
, &c
, sizeof(c
));
45 ok(nwritten
== sizeof(c
), "write should succeed");
50 static void do_traverse(const char *name
, int tdb_flags
, int up
, int down
)
52 struct tdb_context
*tdb
;
54 ssize_t nread
, nwritten
;
57 tdb
= tdb_open_ex(name
, 3, tdb_flags
,
58 O_RDWR
|O_CREAT
, 0755, &taplogctx
, NULL
);
59 ok(tdb
, "tdb_open_ex should succeed");
61 ret
= tdb_traverse(tdb
, NULL
, NULL
);
62 ok(ret
== 1, "do_traverse: tdb_traverse should return 1 record");
64 nwritten
= write(up
, &c
, sizeof(c
));
65 ok(nwritten
== sizeof(c
), "write should succeed");
67 nread
= read(down
, &c
, sizeof(c
));
68 ok(nread
== sizeof(c
), "read should succeed");
74 * Process 1: get the allrecord_lock on a tdb.
75 * Process 2: start a traverse, this will stall waiting for the
76 * first chainlock: That is taken by the allrecord_lock
77 * Process 1: start a traverse: This will get EDEADLK in trying to
78 * get the TRANSACTION_LOCK. It will deadlock for mutexes,
79 * which don't have built-in deadlock detection.
82 static int do_tests(const char *name
, int tdb_flags
)
84 struct tdb_context
*tdb
;
86 pid_t traverse_child
, allrecord_child
;
89 int allrecord_down
[2];
92 ssize_t nread
, nwritten
;
95 key
.dsize
= strlen("hi");
96 key
.dptr
= discard_const_p(uint8_t, "hi");
97 data
.dsize
= strlen("world");
98 data
.dptr
= discard_const_p(uint8_t, "world");
100 tdb
= tdb_open_ex(name
, 3, tdb_flags
,
101 O_RDWR
|O_CREAT
, 0755, &taplogctx
, NULL
);
102 ok(tdb
, "tdb_open_ex should succeed");
104 ret
= tdb_store(tdb
, key
, data
, TDB_INSERT
);
105 ok(ret
== 0, "tdb_store should succeed");
107 ret
= pipe(traverse_down
);
108 ok(ret
== 0, "pipe should succeed");
110 ret
= pipe(traverse_up
);
111 ok(ret
== 0, "pipe should succeed");
113 ret
= pipe(allrecord_down
);
114 ok(ret
== 0, "pipe should succeed");
116 ret
= pipe(allrecord_up
);
117 ok(ret
== 0, "pipe should succeed");
119 allrecord_child
= fork();
120 ok(allrecord_child
!= -1, "fork should succeed");
122 if (allrecord_child
== 0) {
124 close(traverse_up
[0]);
125 close(traverse_up
[1]);
126 close(traverse_down
[0]);
127 close(traverse_down
[1]);
128 close(allrecord_up
[0]);
129 close(allrecord_down
[1]);
130 do_allrecord_lock(name
, tdb_flags
,
131 allrecord_up
[1], allrecord_down
[0]);
134 close(allrecord_up
[1]);
135 close(allrecord_down
[0]);
137 nread
= read(allrecord_up
[0], &c
, sizeof(c
));
138 ok(nread
== sizeof(c
), "read should succeed");
140 traverse_child
= fork();
141 ok(traverse_child
!= -1, "fork should succeed");
143 if (traverse_child
== 0) {
145 close(traverse_up
[0]);
146 close(traverse_down
[1]);
147 close(allrecord_up
[0]);
148 close(allrecord_down
[1]);
149 do_traverse(name
, tdb_flags
,
150 traverse_up
[1], traverse_down
[0]);
153 close(traverse_up
[1]);
154 close(traverse_down
[0]);
158 nwritten
= write(allrecord_down
[1], &c
, sizeof(c
));
159 ok(nwritten
== sizeof(c
), "write should succeed");
161 nread
= read(traverse_up
[0], &c
, sizeof(c
));
162 ok(nread
== sizeof(c
), "read should succeed");
164 nwritten
= write(traverse_down
[1], &c
, sizeof(c
));
165 ok(nwritten
== sizeof(c
), "write should succeed");
167 nread
= read(allrecord_up
[0], &c
, sizeof(c
));
168 ok(nread
== sizeof(c
), "ret should succeed");
170 close(traverse_up
[0]);
171 close(traverse_down
[1]);
172 close(allrecord_up
[0]);
173 close(allrecord_down
[1]);
174 diag("%s tests done", name
);
175 return exit_status();
178 int main(int argc
, char *argv
[])
183 mutex_support
= tdb_runtime_check_for_robust_mutexes();
185 ret
= do_tests("marklock-deadlock-fcntl.tdb",
187 TDB_INCOMPATIBLE_HASH
);
188 ok(ret
== 0, "marklock-deadlock-fcntl.tdb tests should succeed");
190 if (!mutex_support
) {
191 skip(1, "No robust mutex support, "
192 "skipping marklock-deadlock-mutex.tdb tests");
193 return exit_status();
196 ret
= do_tests("marklock-deadlock-mutex.tdb",
199 TDB_INCOMPATIBLE_HASH
);
200 ok(ret
== 0, "marklock-deadlock-mutex.tdb tests should succeed");
202 return exit_status();