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 <ccan/tdb2/tdb.c>
14 #include <ccan/tdb2/open.c>
15 #include <ccan/tdb2/free.c>
16 #include <ccan/tdb2/lock.c>
17 #include <ccan/tdb2/io.c>
18 #include <ccan/tdb2/hash.c>
19 #include <ccan/tdb2/check.c>
20 #include <ccan/tdb2/transaction.c>
21 #include <ccan/tap/tap.h>
22 #include <sys/types.h>
26 static enum TDB_ERROR fork_in_parse(TDB_DATA key, TDB_DATA data,
27 struct tdb_context *tdb)
32 /* We expect this to fail. */
33 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
36 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
39 if (tap_log_messages != 2)
43 if (tap_log_messages != 2)
48 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
52 int main(int argc, char *argv[])
55 struct tdb_context *tdb;
56 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
57 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
58 struct tdb_data key = tdb_mkdata("key", 3);
59 struct tdb_data data = tdb_mkdata("data", 4);
61 plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
62 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
67 tdb = tdb_open("run-fork-test.tdb", flags[i],
68 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
72 /* Put a record in here. */
73 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);
75 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
77 /* We expect this to fail. */
78 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
81 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
84 if (tap_log_messages != 2)
87 tdb_chainunlock(tdb, key);
88 if (tap_log_messages != 3)
91 if (tap_log_messages != 3)
96 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
97 tdb_chainunlock(tdb, key);
99 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
101 /* We expect this to fail. */
102 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
105 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
108 if (tap_log_messages != 2)
112 if (tap_log_messages != 2)
115 if (tap_log_messages != 2)
120 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
123 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
125 /* We expect this to fail. */
126 /* This would always fail anyway... */
127 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
130 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
133 if (tap_log_messages != 2)
136 tdb_unlockall_read(tdb);
137 if (tap_log_messages != 2)
140 if (tap_log_messages != 2)
145 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
146 tdb_unlockall_read(tdb);
148 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
149 /* If transactions is empty, noop "commit" succeeds. */
150 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
152 /* We expect this to fail. */
153 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
156 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
159 if (tap_log_messages != 2)
162 if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
166 if (tap_log_messages < 3)
171 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
172 tdb_transaction_cancel(tdb);
174 ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
177 ok1(tap_log_messages == 0);
179 return exit_status();