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