1 #include "../common/tdb_private.h"
2 #include "../common/io.c"
3 #include "../common/tdb.c"
4 #include "../common/lock.c"
5 #include "../common/freelist.c"
6 #include "../common/traverse.c"
7 #include "../common/transaction.c"
8 #include "../common/error.c"
9 #include "../common/open.c"
10 #include "../common/check.c"
11 #include "../common/hash.c"
12 #include "../common/mutex.c"
13 #include "tap-interface.h"
14 #include <stdlib.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <stdarg.h>
18 
19 static TDB_DATA key, data;
20 
log_fn(struct tdb_context * tdb,enum tdb_debug_level level,const char * fmt,...)21 static void log_fn(struct tdb_context *tdb, enum tdb_debug_level level,
22 		   const char *fmt, ...)
23 {
24 	va_list ap;
25 	va_start(ap, fmt);
26 	vfprintf(stderr, fmt, ap);
27 	va_end(ap);
28 }
29 
do_child(int tdb_flags,int to,int from)30 static int do_child(int tdb_flags, int to, int from)
31 {
32 	struct tdb_context *tdb;
33 	unsigned int log_count;
34 	struct tdb_logging_context log_ctx = { log_fn, &log_count };
35 	int ret;
36 	char c = 0;
37 
38 	tdb = tdb_open_ex("mutex-transaction1.tdb", 3, tdb_flags,
39 			  O_RDWR|O_CREAT, 0755, &log_ctx, NULL);
40 	ok(tdb, "tdb_open_ex should succeed");
41 
42 	ret = tdb_transaction_start(tdb);
43 	ok(ret == 0, "tdb_transaction_start should succeed");
44 
45 	ret = tdb_store(tdb, key, data, TDB_INSERT);
46 	ok(ret == 0, "tdb_store(tdb, key, data, TDB_INSERT) should succeed");
47 
48 	write(to, &c, sizeof(c));
49 	read(from, &c, sizeof(c));
50 
51 	ret = tdb_transaction_cancel(tdb);
52 	ok(ret == 0, "tdb_transaction_cancel should succeed");
53 
54 	write(to, &c, sizeof(c));
55 	read(from, &c, sizeof(c));
56 
57 	ret = tdb_transaction_start(tdb);
58 	ok(ret == 0, "tdb_transaction_start should succeed");
59 
60 	ret = tdb_store(tdb, key, data, TDB_INSERT);
61 	ok(ret == 0, "tdb_store(tdb, key, data, TDB_INSERT) should succeed");
62 
63 	write(to, &c, sizeof(c));
64 	read(from, &c, sizeof(c));
65 
66 	ret = tdb_transaction_commit(tdb);
67 	ok(ret == 0, "tdb_transaction_commit should succeed");
68 
69 	write(to, &c, sizeof(c));
70 	read(from, &c, sizeof(c));
71 
72 	ret = tdb_transaction_start(tdb);
73 	ok(ret == 0, "tdb_transaction_start should succeed");
74 
75 	ret = tdb_store(tdb, key, key, TDB_REPLACE);
76 	ok(ret == 0, "tdb_store(tdb, key, data, TDB_REPLACE) should succeed");
77 
78 	write(to, &c, sizeof(c));
79 	read(from, &c, sizeof(c));
80 
81 	ret = tdb_transaction_commit(tdb);
82 	ok(ret == 0, "tdb_transaction_commit should succeed");
83 
84 	write(to, &c, sizeof(c));
85 	read(from, &c, sizeof(c));
86 
87 	return 0;
88 }
89 
90 /* The code should barf on TDBs created with rwlocks. */
main(int argc,char * argv[])91 int main(int argc, char *argv[])
92 {
93 	struct tdb_context *tdb;
94 	unsigned int log_count;
95 	struct tdb_logging_context log_ctx = { log_fn, &log_count };
96 	int ret, status;
97 	pid_t child, wait_ret;
98 	int fromchild[2];
99 	int tochild[2];
100 	TDB_DATA val;
101 	char c;
102 	int tdb_flags;
103 	bool runtime_support;
104 
105 	runtime_support = tdb_runtime_check_for_robust_mutexes();
106 
107 	if (!runtime_support) {
108 		skip(1, "No robust mutex support");
109 		return exit_status();
110 	}
111 
112 	key.dsize = strlen("hi");
113 	key.dptr = discard_const_p(uint8_t, "hi");
114 	data.dsize = strlen("world");
115 	data.dptr = discard_const_p(uint8_t, "world");
116 
117 	pipe(fromchild);
118 	pipe(tochild);
119 
120 	tdb_flags = TDB_INCOMPATIBLE_HASH|
121 		TDB_MUTEX_LOCKING|
122 		TDB_CLEAR_IF_FIRST;
123 
124 	child = fork();
125 	if (child == 0) {
126 		close(fromchild[0]);
127 		close(tochild[1]);
128 		return do_child(tdb_flags, fromchild[1], tochild[0]);
129 	}
130 	close(fromchild[1]);
131 	close(tochild[0]);
132 
133 	read(fromchild[0], &c, sizeof(c));
134 
135 	tdb = tdb_open_ex("mutex-transaction1.tdb", 0,
136 			  tdb_flags, O_RDWR|O_CREAT, 0755,
137 			  &log_ctx, NULL);
138 	ok(tdb, "tdb_open_ex should succeed");
139 
140 	/*
141 	 * The child has the transaction running
142 	 */
143 	ret = tdb_transaction_start_nonblock(tdb);
144 	ok(ret == -1, "tdb_transaction_start_nonblock not succeed");
145 
146 	ret = tdb_chainlock_nonblock(tdb, key);
147 	ok(ret == -1, "tdb_chainlock_nonblock should not succeed");
148 
149 	/*
150 	 * We can still read
151 	 */
152 	ret = tdb_exists(tdb, key);
153 	ok(ret == 0, "tdb_exists(tdb, key) should return 0");
154 
155 	val = tdb_fetch(tdb, key);
156 	ok(val.dsize == 0, "tdb_fetch(tdb, key) should return an empty value");
157 
158 	write(tochild[1], &c, sizeof(c));
159 
160 	/*
161 	 * When the child canceled we can start...
162 	 */
163 	ret = tdb_transaction_start(tdb);
164 	ok(ret == 0, "tdb_transaction_start should succeed");
165 
166 	read(fromchild[0], &c, sizeof(c));
167 	write(tochild[1], &c, sizeof(c));
168 
169 	ret = tdb_transaction_cancel(tdb);
170 	ok(ret == 0, "tdb_transaction_cancel should succeed");
171 
172 	/*
173 	 * When we canceled the child can start and store...
174 	 */
175 	read(fromchild[0], &c, sizeof(c));
176 
177 	/*
178 	 * We still see the old values before the child commits...
179 	 */
180 	ret = tdb_exists(tdb, key);
181 	ok(ret == 0, "tdb_exists(tdb, key) should return 0");
182 
183 	val = tdb_fetch(tdb, key);
184 	ok(val.dsize == 0, "tdb_fetch(tdb, key) should return an empty value");
185 
186 	write(tochild[1], &c, sizeof(c));
187 	read(fromchild[0], &c, sizeof(c));
188 
189 	/*
190 	 * We see the new values after the commit...
191 	 */
192 	ret = tdb_exists(tdb, key);
193 	ok(ret == 1, "tdb_exists(tdb, key) should return 1");
194 
195 	val = tdb_fetch(tdb, key);
196 	ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value");
197 	ok(val.dsize == data.dsize, "tdb_fetch(tdb, key) should return a value");
198 	ok(memcmp(val.dptr, data.dptr, data.dsize) == 0, "tdb_fetch(tdb, key) should return a value");
199 
200 	write(tochild[1], &c, sizeof(c));
201 	read(fromchild[0], &c, sizeof(c));
202 
203 	/*
204 	 * The child started a new transaction and replaces the value,
205 	 * but we still see the old values before the child commits...
206 	 */
207 	ret = tdb_exists(tdb, key);
208 	ok(ret == 1, "tdb_exists(tdb, key) should return 1");
209 
210 	val = tdb_fetch(tdb, key);
211 	ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value");
212 	ok(val.dsize == data.dsize, "tdb_fetch(tdb, key) should return a value");
213 	ok(memcmp(val.dptr, data.dptr, data.dsize) == 0, "tdb_fetch(tdb, key) should return a value");
214 
215 	write(tochild[1], &c, sizeof(c));
216 	read(fromchild[0], &c, sizeof(c));
217 
218 	/*
219 	 * We see the new values after the commit...
220 	 */
221 	ret = tdb_exists(tdb, key);
222 	ok(ret == 1, "tdb_exists(tdb, key) should return 1");
223 
224 	val = tdb_fetch(tdb, key);
225 	ok(val.dsize != 0, "tdb_fetch(tdb, key) should return a value");
226 	ok(val.dsize == key.dsize, "tdb_fetch(tdb, key) should return a value");
227 	ok(memcmp(val.dptr, key.dptr, key.dsize) == 0, "tdb_fetch(tdb, key) should return a value");
228 
229 	write(tochild[1], &c, sizeof(c));
230 
231 	wait_ret = wait(&status);
232 	ok(wait_ret == child, "child should have exited correctly");
233 
234 	diag("done");
235 	return exit_status();
236 }
237