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/tdb2.h>
14 #include <ccan/tap/tap.h>
15 #include <sys/types.h>
18 #include <sys/types.h>
24 static enum TDB_ERROR fork_in_parse(TDB_DATA key, TDB_DATA data,
25 struct tdb_context *tdb)
27 int status, extra_messages;
29 if (tdb_get_flags(tdb) & TDB_VERSION1) {
36 /* We expect this to fail. */
37 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
39 tap_log_messages -= extra_messages;
41 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
44 tap_log_messages -= extra_messages;
45 if (tap_log_messages != 2)
49 if (tap_log_messages != 2)
54 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
58 int main(int argc, char *argv[])
61 struct tdb_context *tdb;
62 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
63 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
64 TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
65 TDB_CONVERT|TDB_VERSION1,
66 TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
67 struct tdb_data key = tdb_mkdata("key", 3);
68 struct tdb_data data = tdb_mkdata("data", 4);
70 plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
71 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
72 int status, extra_messages;
74 if (flags[i] & TDB_VERSION1) {
82 tdb = tdb_open("run-fork-test.tdb", flags[i],
83 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
87 /* Put a record in here. */
88 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);
90 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
92 /* We expect this to fail. */
93 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
95 tap_log_messages -= extra_messages;
97 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
99 tap_log_messages -= extra_messages;
101 if (tap_log_messages != 2)
104 tdb_chainunlock(tdb, key);
105 if (tap_log_messages != 3)
108 if (tap_log_messages != 3)
113 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
114 tdb_chainunlock(tdb, key);
116 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
118 /* We expect this to fail. */
119 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
121 tap_log_messages -= extra_messages;
123 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
125 tap_log_messages -= extra_messages;
127 if (tap_log_messages != 2)
131 if (tap_log_messages != 2)
134 if (tap_log_messages != 2)
139 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
142 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
144 /* We expect this to fail. */
145 /* This would always fail anyway... */
146 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
148 tap_log_messages -= extra_messages;
150 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
152 tap_log_messages -= extra_messages;
154 if (tap_log_messages != 2)
157 tdb_unlockall_read(tdb);
158 if (tap_log_messages != 2)
161 if (tap_log_messages != 2)
166 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
167 tdb_unlockall_read(tdb);
169 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
170 /* If transactions is empty, noop "commit" succeeds. */
171 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
173 /* We expect this to fail. */
174 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
176 tap_log_messages -= extra_messages;
178 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
180 tap_log_messages -= extra_messages;
182 if (tap_log_messages != 2)
185 if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
187 tap_log_messages -= extra_messages;
190 if (tap_log_messages < 3)
195 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
196 tdb_transaction_cancel(tdb);
198 ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
201 ok1(tap_log_messages == 0);
203 return exit_status();