tdb/test: add mutex related tests
[Samba.git] / lib / tdb / test / run-mutex-allrecord-block.c
blobfcd3b4fadae1dd0dc1508c22f0d32e3c63e05034
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>
19 static TDB_DATA key, data;
21 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level,
22 const char *fmt, ...)
24 va_list ap;
25 va_start(ap, fmt);
26 vfprintf(stderr, fmt, ap);
27 va_end(ap);
30 static int do_child(int tdb_flags, int to, int from)
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;
38 tdb = tdb_open_ex("mutex-allrecord-block.tdb", 3, tdb_flags,
39 O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
40 ok(tdb, "tdb_open_ex should succeed");
42 ret = tdb_allrecord_lock(tdb, F_WRLCK, TDB_LOCK_WAIT, false);
43 ok(ret == 0, "tdb_allrecord_lock should succeed");
45 write(to, &c, sizeof(c));
47 read(from, &c, sizeof(c));
49 ret = tdb_allrecord_unlock(tdb, F_WRLCK, false);
50 ok(ret == 0, "tdb_allrecord_unlock should succeed");
52 return 0;
55 /* The code should barf on TDBs created with rwlocks. */
56 int main(int argc, char *argv[])
58 struct tdb_context *tdb;
59 unsigned int log_count;
60 struct tdb_logging_context log_ctx = { log_fn, &log_count };
61 int ret, status;
62 pid_t child, wait_ret;
63 int fromchild[2];
64 int tochild[2];
65 char c;
66 int tdb_flags;
67 bool runtime_support;
69 runtime_support = tdb_runtime_check_for_robust_mutexes();
71 if (!runtime_support) {
72 skip(1, "No robust mutex support");
73 return exit_status();
76 key.dsize = strlen("hi");
77 key.dptr = discard_const_p(uint8_t, "hi");
78 data.dsize = strlen("world");
79 data.dptr = discard_const_p(uint8_t, "world");
81 pipe(fromchild);
82 pipe(tochild);
84 tdb_flags = TDB_INCOMPATIBLE_HASH|
85 TDB_MUTEX_LOCKING|
86 TDB_CLEAR_IF_FIRST;
88 child = fork();
89 if (child == 0) {
90 close(fromchild[0]);
91 close(tochild[1]);
92 return do_child(tdb_flags, fromchild[1], tochild[0]);
94 close(fromchild[1]);
95 close(tochild[0]);
97 read(fromchild[0], &c, sizeof(c));
99 tdb = tdb_open_ex("mutex-allrecord-block.tdb", 0,
100 tdb_flags, O_RDWR|O_CREAT, 0755,
101 &log_ctx, NULL);
102 ok(tdb, "tdb_open_ex should succeed");
104 ret = tdb_chainlock_nonblock(tdb, key);
105 ok(ret == -1, "tdb_chainlock_nonblock should not succeed");
107 write(tochild[1], &c, sizeof(c));
109 ret = tdb_chainlock(tdb, key);
110 ok(ret == 0, "tdb_chainlock should not succeed");
112 ret = tdb_chainunlock(tdb, key);
113 ok(ret == 0, "tdb_chainunlock should succeed");
115 wait_ret = wait(&status);
116 ok(wait_ret == child, "child should have exited correctly");
118 diag("done");
119 return exit_status();