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