Line data Source code
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"
14 : #include <stdlib.h>
15 : #include <sys/types.h>
16 : #include <sys/wait.h>
17 : #include <stdarg.h>
18 :
19 : static TDB_DATA key, data;
20 :
21 0 : static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level,
22 : const char *fmt, ...)
23 : {
24 : va_list ap;
25 0 : va_start(ap, fmt);
26 0 : vfprintf(stderr, fmt, ap);
27 0 : va_end(ap);
28 0 : }
29 :
30 1 : static int do_child(int tdb_flags, int to, int from)
31 : {
32 : struct tdb_context *tdb;
33 : unsigned int log_count;
34 1 : struct tdb_logging_context log_ctx = { log_fn, &log_count };
35 : int ret;
36 1 : char c = 0;
37 :
38 1 : tdb = tdb_open_ex("mutex-trylock.tdb", 0, tdb_flags,
39 : O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
40 1 : ok(tdb, "tdb_open_ex should succeed");
41 :
42 1 : ret = tdb_chainlock(tdb, key);
43 1 : ok(ret == 0, "tdb_chainlock should succeed");
44 :
45 1 : write(to, &c, sizeof(c));
46 :
47 1 : read(from, &c, sizeof(c));
48 :
49 1 : ret = tdb_chainunlock(tdb, key);
50 1 : ok(ret == 0, "tdb_chainunlock should succeed");
51 :
52 1 : write(to, &c, sizeof(c));
53 :
54 1 : return 0;
55 : }
56 :
57 : /* The code should barf on TDBs created with rwlocks. */
58 1 : int main(int argc, char *argv[])
59 : {
60 : struct tdb_context *tdb;
61 : unsigned int log_count;
62 1 : struct tdb_logging_context log_ctx = { log_fn, &log_count };
63 : int ret, status;
64 : pid_t child, wait_ret;
65 : int fromchild[2];
66 : int tochild[2];
67 : char c;
68 : int tdb_flags;
69 : bool runtime_support;
70 :
71 1 : runtime_support = tdb_runtime_check_for_robust_mutexes();
72 :
73 1 : if (!runtime_support) {
74 0 : skip(1, "No robust mutex support");
75 0 : return exit_status();
76 : }
77 :
78 1 : key.dsize = strlen("hi");
79 1 : key.dptr = discard_const_p(uint8_t, "hi");
80 1 : data.dsize = strlen("world");
81 1 : data.dptr = discard_const_p(uint8_t, "world");
82 :
83 1 : pipe(fromchild);
84 1 : pipe(tochild);
85 :
86 1 : tdb_flags = TDB_INCOMPATIBLE_HASH|
87 : TDB_MUTEX_LOCKING|
88 : TDB_CLEAR_IF_FIRST;
89 :
90 1 : child = fork();
91 2 : if (child == 0) {
92 1 : close(fromchild[0]);
93 1 : close(tochild[1]);
94 1 : return do_child(tdb_flags, fromchild[1], tochild[0]);
95 : }
96 1 : close(fromchild[1]);
97 1 : close(tochild[0]);
98 :
99 1 : read(fromchild[0], &c, sizeof(c));
100 :
101 1 : tdb = tdb_open_ex("mutex-trylock.tdb", 0, tdb_flags,
102 : O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
103 1 : ok(tdb, "tdb_open_ex should succeed");
104 :
105 1 : ret = tdb_chainlock_nonblock(tdb, key);
106 1 : ok(ret == -1, "tdb_chainlock_nonblock should not succeed");
107 :
108 1 : write(tochild[1], &c, sizeof(c));
109 :
110 1 : read(fromchild[0], &c, sizeof(c));
111 :
112 1 : ret = tdb_chainlock_nonblock(tdb, key);
113 1 : ok(ret == 0, "tdb_chainlock_nonblock should succeed");
114 1 : ret = tdb_chainunlock(tdb, key);
115 1 : ok(ret == 0, "tdb_chainunlock should succeed");
116 :
117 1 : wait_ret = wait(&status);
118 1 : ok(wait_ret == child, "child should have exited correctly");
119 :
120 1 : diag("done");
121 1 : return exit_status();
122 : }
|