1 /* Test forking while holding lock.
3 * There are only five ways to do this currently:
4 * (1) grab a tdb_chainlock, then fork.
5 * (2) grab a tdb_lockall, then fork.
6 * (3) grab a tdb_lockall_read, then fork.
7 * (4) start a transaction, then fork.
8 * (5) fork from inside a tdb_parse() callback.
10 * Note that we don't hold a lock across tdb_traverse callbacks, so
11 * that doesn't matter.
13 #include "tdb2-source.h"
14 #include <ccan/tap/tap.h>
15 #include <sys/types.h>
19 static enum TDB_ERROR fork_in_parse(TDB_DATA key, TDB_DATA data,
20 struct tdb_context *tdb)
22 int status, extra_messages;
24 if (tdb_get_flags(tdb) & TDB_VERSION1) {
31 /* We expect this to fail. */
32 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
34 tap_log_messages -= extra_messages;
36 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
39 tap_log_messages -= extra_messages;
40 if (tap_log_messages != 2)
44 if (tap_log_messages != 2)
49 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
53 int main(int argc, char *argv[])
56 struct tdb_context *tdb;
57 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
58 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
59 TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
60 TDB_CONVERT|TDB_VERSION1,
61 TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
62 struct tdb_data key = tdb_mkdata("key", 3);
63 struct tdb_data data = tdb_mkdata("data", 4);
65 plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
66 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
67 int status, extra_messages;
69 if (flags[i] & TDB_VERSION1) {
77 tdb = tdb_open("run-fork-test.tdb", flags[i],
78 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
82 /* Put a record in here. */
83 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);
85 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
87 /* We expect this to fail. */
88 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
90 tap_log_messages -= extra_messages;
92 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
94 tap_log_messages -= extra_messages;
96 if (tap_log_messages != 2)
99 tdb_chainunlock(tdb, key);
100 if (tap_log_messages != 3)
103 if (tap_log_messages != 3)
108 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
109 tdb_chainunlock(tdb, key);
111 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
113 /* We expect this to fail. */
114 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
116 tap_log_messages -= extra_messages;
118 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
120 tap_log_messages -= extra_messages;
122 if (tap_log_messages != 2)
126 if (tap_log_messages != 2)
129 if (tap_log_messages != 2)
134 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
137 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
139 /* We expect this to fail. */
140 /* This would always fail anyway... */
141 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
143 tap_log_messages -= extra_messages;
145 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
147 tap_log_messages -= extra_messages;
149 if (tap_log_messages != 2)
152 tdb_unlockall_read(tdb);
153 if (tap_log_messages != 2)
156 if (tap_log_messages != 2)
161 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
162 tdb_unlockall_read(tdb);
164 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
165 /* If transactions is empty, noop "commit" succeeds. */
166 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
168 /* We expect this to fail. */
169 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
171 tap_log_messages -= extra_messages;
173 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
175 tap_log_messages -= extra_messages;
177 if (tap_log_messages != 2)
180 if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
182 tap_log_messages -= extra_messages;
185 if (tap_log_messages < 3)
190 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
191 tdb_transaction_cancel(tdb);
193 ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
196 ok1(tap_log_messages == 0);
198 return exit_status();