selftest: remove samba3.rpc.spoolss.*printserver.openprinter_badnamelist from flapping
[Samba.git] / lib / tdb2 / test / api-fork-test.c
blob988004e90a2004c0bb7f146ef82772e4636ef37c
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 "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"
25 static enum TDB_ERROR fork_in_parse(TDB_DATA key, TDB_DATA data,
26 struct tdb_context *tdb)
28 int status, extra_messages;
30 if (tdb_get_flags(tdb) & TDB_VERSION1) {
31 extra_messages = 1;
32 } else {
33 extra_messages = 0;
36 if (fork() == 0) {
37 /* We expect this to fail. */
38 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
39 exit(1);
40 tap_log_messages -= extra_messages;
42 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
43 exit(1);
45 tap_log_messages -= extra_messages;
46 if (tap_log_messages != 2)
47 exit(2);
49 tdb_close(tdb);
50 if (tap_log_messages != 2)
51 exit(3);
52 exit(0);
54 wait(&status);
55 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
56 return TDB_SUCCESS;
59 int main(int argc, char *argv[])
61 unsigned int i;
62 struct tdb_context *tdb;
63 int flags[] = { TDB_DEFAULT, TDB_NOMMAP,
64 TDB_CONVERT, TDB_NOMMAP|TDB_CONVERT,
65 TDB_VERSION1, TDB_NOMMAP|TDB_VERSION1,
66 TDB_CONVERT|TDB_VERSION1,
67 TDB_NOMMAP|TDB_CONVERT|TDB_VERSION1 };
68 struct tdb_data key = tdb_mkdata("key", 3);
69 struct tdb_data data = tdb_mkdata("data", 4);
71 plan_tests(sizeof(flags) / sizeof(flags[0]) * 14);
72 for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
73 int status, extra_messages;
75 if (flags[i] & TDB_VERSION1) {
76 extra_messages = 1;
77 } else {
78 extra_messages = 0;
81 tap_log_messages = 0;
83 tdb = tdb_open("run-fork-test.tdb", flags[i],
84 O_RDWR|O_CREAT|O_TRUNC, 0600, &tap_log_attr);
85 if (!ok1(tdb))
86 continue;
88 /* Put a record in here. */
89 ok1(tdb_store(tdb, key, data, TDB_REPLACE) == TDB_SUCCESS);
91 ok1(tdb_chainlock(tdb, key) == TDB_SUCCESS);
92 if (fork() == 0) {
93 /* We expect this to fail. */
94 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
95 return 1;
96 tap_log_messages -= extra_messages;
98 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
99 return 1;
100 tap_log_messages -= extra_messages;
102 if (tap_log_messages != 2)
103 return 2;
105 tdb_chainunlock(tdb, key);
106 if (tap_log_messages != 3)
107 return 3;
108 tdb_close(tdb);
109 if (tap_log_messages != 3)
110 return 4;
111 return 0;
113 wait(&status);
114 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
115 tdb_chainunlock(tdb, key);
117 ok1(tdb_lockall(tdb) == TDB_SUCCESS);
118 if (fork() == 0) {
119 /* We expect this to fail. */
120 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
121 return 1;
122 tap_log_messages -= extra_messages;
124 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
125 return 1;
126 tap_log_messages -= extra_messages;
128 if (tap_log_messages != 2)
129 return 2;
131 tdb_unlockall(tdb);
132 if (tap_log_messages != 2)
133 return 3;
134 tdb_close(tdb);
135 if (tap_log_messages != 2)
136 return 4;
137 return 0;
139 wait(&status);
140 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
141 tdb_unlockall(tdb);
143 ok1(tdb_lockall_read(tdb) == TDB_SUCCESS);
144 if (fork() == 0) {
145 /* We expect this to fail. */
146 /* This would always fail anyway... */
147 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
148 return 1;
149 tap_log_messages -= extra_messages;
151 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
152 return 1;
153 tap_log_messages -= extra_messages;
155 if (tap_log_messages != 2)
156 return 2;
158 tdb_unlockall_read(tdb);
159 if (tap_log_messages != 2)
160 return 3;
161 tdb_close(tdb);
162 if (tap_log_messages != 2)
163 return 4;
164 return 0;
166 wait(&status);
167 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
168 tdb_unlockall_read(tdb);
170 ok1(tdb_transaction_start(tdb) == TDB_SUCCESS);
171 /* If transactions is empty, noop "commit" succeeds. */
172 ok1(tdb_delete(tdb, key) == TDB_SUCCESS);
173 if (fork() == 0) {
174 /* We expect this to fail. */
175 if (tdb_store(tdb, key, data, TDB_REPLACE) != TDB_ERR_LOCK)
176 return 1;
177 tap_log_messages -= extra_messages;
179 if (tdb_fetch(tdb, key, &data) != TDB_ERR_LOCK)
180 return 1;
181 tap_log_messages -= extra_messages;
183 if (tap_log_messages != 2)
184 return 2;
186 if (tdb_transaction_commit(tdb) != TDB_ERR_LOCK)
187 return 3;
188 tap_log_messages -= extra_messages;
190 tdb_close(tdb);
191 if (tap_log_messages < 3)
192 return 4;
193 return 0;
195 wait(&status);
196 ok1(WIFEXITED(status) && WEXITSTATUS(status) == 0);
197 tdb_transaction_cancel(tdb);
199 ok1(tdb_parse_record(tdb, key, fork_in_parse, tdb)
200 == TDB_SUCCESS);
201 tdb_close(tdb);
202 ok1(tap_log_messages == 0);
204 return exit_status();