Describe implication of upstream ICU-22610
[Samba.git] / lib / tdb / test / run-mutex-trylock.c
blobc96b635546576ac2ae1146415428b1637198601e
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-trylock.tdb", 0, tdb_flags,
39 O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
40 ok(tdb, "tdb_open_ex should succeed");
42 ret = tdb_chainlock(tdb, key);
43 ok(ret == 0, "tdb_chainlock should succeed");
45 write(to, &c, sizeof(c));
47 read(from, &c, sizeof(c));
49 ret = tdb_chainunlock(tdb, key);
50 ok(ret == 0, "tdb_chainunlock should succeed");
52 write(to, &c, sizeof(c));
54 return 0;
57 /* The code should barf on TDBs created with rwlocks. */
58 int main(int argc, char *argv[])
60 struct tdb_context *tdb;
61 unsigned int log_count;
62 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;
71 runtime_support = tdb_runtime_check_for_robust_mutexes();
73 if (!runtime_support) {
74 skip(1, "No robust mutex support");
75 return exit_status();
78 key.dsize = strlen("hi");
79 key.dptr = discard_const_p(uint8_t, "hi");
80 data.dsize = strlen("world");
81 data.dptr = discard_const_p(uint8_t, "world");
83 pipe(fromchild);
84 pipe(tochild);
86 tdb_flags = TDB_INCOMPATIBLE_HASH|
87 TDB_MUTEX_LOCKING|
88 TDB_CLEAR_IF_FIRST;
90 child = fork();
91 if (child == 0) {
92 close(fromchild[0]);
93 close(tochild[1]);
94 return do_child(tdb_flags, fromchild[1], tochild[0]);
96 close(fromchild[1]);
97 close(tochild[0]);
99 read(fromchild[0], &c, sizeof(c));
101 tdb = tdb_open_ex("mutex-trylock.tdb", 0, tdb_flags,
102 O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
103 ok(tdb, "tdb_open_ex should succeed");
105 ret = tdb_chainlock_nonblock(tdb, key);
106 ok(ret == -1, "tdb_chainlock_nonblock should not succeed");
108 write(tochild[1], &c, sizeof(c));
110 read(fromchild[0], &c, sizeof(c));
112 ret = tdb_chainlock_nonblock(tdb, key);
113 ok(ret == 0, "tdb_chainlock_nonblock should succeed");
114 ret = tdb_chainunlock(tdb, key);
115 ok(ret == 0, "tdb_chainunlock should succeed");
117 wait_ret = wait(&status);
118 ok(wait_ret == child, "child should have exited correctly");
120 diag("done");
121 return exit_status();