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)
25 /* We expect this to fail. */
26 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
29 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
32 if (tap_log_messages != 2)
36 if (tap_log_messages != 2)
41 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
45 int main(int argc, char *argv[])
48 struct tdb_context *tdb;
49 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
50 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT };
51 struct tdb_data key = tdb_mkdata("key", 3);
52 struct tdb_data data = tdb_mkdata("data", 4);
54 plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
55 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
60 tdb = tdb_open("run-fork-test.tdb", flags[i],
61 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
65 /* Put a record in here. */
66 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);
68 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
70 /* We expect this to fail. */
71 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
74 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
77 if (tap_log_messages != 2)
80 tdb_chainunlock(tdb, key);
81 if (tap_log_messages != 3)
84 if (tap_log_messages != 3)
89 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
90 tdb_chainunlock(tdb, key);
92 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
94 /* We expect this to fail. */
95 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
98 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
101 if (tap_log_messages != 2)
105 if (tap_log_messages != 2)
108 if (tap_log_messages != 2)
113 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
116 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
118 /* We expect this to fail. */
119 /* This would always fail anyway... */
120 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
123 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
126 if (tap_log_messages != 2)
129 tdb_unlockall_read(tdb);
130 if (tap_log_messages != 2)
133 if (tap_log_messages != 2)
138 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
139 tdb_unlockall_read(tdb);
141 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
142 /* If transactions is empty, noop "commit" succeeds. */
143 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
145 /* We expect this to fail. */
146 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
149 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
152 if (tap_log_messages != 2)
155 if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
159 if (tap_log_messages < 3)
164 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
165 tdb_transaction_cancel(tdb);
167 ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
170 ok1(tap_log_messages == 0);
172 return exit_status();