tdb2: allow multiple chain locks.
[ccan] / ccan / tdb2 / test / run-fork-test.c
1 /* Test forking while holding lock.
2  *
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.
9  *
10  * Note that we don't hold a lock across tdb_traverse callbacks, so
11  * that doesn't matter.
12  */
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>
23 #include <sys/wait.h>
24 #include "logging.h"
25
26 static enum TDB_ERROR fork_in_parse(TDB_DATA key, TDB_DATA data,
27                                     struct tdb_context *tdb)
28 {
29         int status;
30
31         if (fork() == 0) {
32                 /* We expect this to fail. */
33                 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
34                         exit(1);
35
36                 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
37                         exit(1);
38
39                 if (tap_log_messages != 2)
40                         exit(2);
41
42                 tdb_close(tdb);
43                 if (tap_log_messages != 2)
44                         exit(3);
45                 exit(0);
46         }
47         wait(&status);
48         ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
49         return TDB_SUCCESS;
50 }
51
52 int main(int argc, char *argv[])
53 {
54         unsigned int i;
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);
60
61         plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
62         for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
63                 int status;
64
65                 tap_log_messages = 0;
66
67                 tdb = tdb_open("run-fork-test.tdb", flags[i],
68                                O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
69                 if (!ok1(tdb))
70                         continue;
71
72                 /* Put a record in here. */
73                 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);
74
75                 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
76                 if (fork() == 0) {
77                         /* We expect this to fail. */
78                         if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
79                                 return 1;
80
81                         if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
82                                 return 1;
83
84                         if (tap_log_messages != 2)
85                                 return 2;
86
87                         tdb_chainunlock(tdb, key);
88                         if (tap_log_messages != 2)
89                                 return 3;
90                         tdb_close(tdb);
91                         if (tap_log_messages != 2)
92                                 return 4;
93                         return 0;
94                 }
95                 wait(&status);
96                 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
97                 tdb_chainunlock(tdb, key);
98
99                 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
100                 if (fork() == 0) {
101                         /* We expect this to fail. */
102                         if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
103                                 return 1;
104
105                         if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
106                                 return 1;
107
108                         if (tap_log_messages != 2)
109                                 return 2;
110
111                         tdb_unlockall(tdb);
112                         if (tap_log_messages != 2)
113                                 return 3;
114                         tdb_close(tdb);
115                         if (tap_log_messages != 2)
116                                 return 4;
117                         return 0;
118                 }
119                 wait(&status);
120                 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
121                 tdb_unlockall(tdb);
122
123                 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
124                 if (fork() == 0) {
125                         /* We expect this to fail. */
126                         /* This would always fail anyway... */
127                         if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
128                                 return 1;
129
130                         if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
131                                 return 1;
132
133                         if (tap_log_messages != 2)
134                                 return 2;
135
136                         tdb_unlockall_read(tdb);
137                         if (tap_log_messages != 2)
138                                 return 3;
139                         tdb_close(tdb);
140                         if (tap_log_messages != 2)
141                                 return 4;
142                         return 0;
143                 }
144                 wait(&status);
145                 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
146                 tdb_unlockall_read(tdb);
147
148                 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
149                 /* If transactions is empty, noop "commit" succeeds. */
150                 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
151                 if (fork() == 0) {
152                         /* We expect this to fail. */
153                         if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
154                                 return 1;
155
156                         if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
157                                 return 1;
158
159                         if (tap_log_messages != 2)
160                                 return 2;
161
162                         if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
163                                 return 3;
164
165                         tdb_close(tdb);
166                         if (tap_log_messages < 3)
167                                 return 4;
168                         return 0;
169                 }
170                 wait(&status);
171                 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
172                 tdb_transaction_cancel(tdb);
173
174                 ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
175                     == TDB_SUCCESS);
176                 tdb_close(tdb);
177                 ok1(tap_log_messages == 0);
178         }
179         return exit_status();
180 }