1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
|
/*
Unix SMB/CIFS implementation.
Test dbwrap_watch API
Copyright (C) Volker Lendecke 2012
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "includes.h"
#include "torture/proto.h"
#include "system/filesys.h"
#include "lib/dbwrap/dbwrap.h"
#include "lib/dbwrap/dbwrap_open.h"
#include "lib/dbwrap/dbwrap_watch.h"
#include "lib/util/util_tdb.h"
static bool test_dbwrap_watch_init(
TALLOC_CTX *mem_ctx,
const char *dbname,
struct tevent_context **pev,
struct messaging_context **pmsg,
struct db_context **pbackend,
struct db_context **pdb)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct db_context *backend = NULL;
struct db_context *db = NULL;
ev = samba_tevent_context_init(mem_ctx);
if (ev == NULL) {
fprintf(stderr, "tevent_context_init failed\n");
goto fail;
}
msg = messaging_init(ev, ev);
if (msg == NULL) {
fprintf(stderr, "messaging_init failed\n");
goto fail;
}
backend = db_open(
msg,
dbname,
0,
TDB_CLEAR_IF_FIRST,
O_CREAT|O_RDWR,
0644,
DBWRAP_LOCK_ORDER_1,
DBWRAP_FLAG_NONE);
if (backend == NULL) {
fprintf(stderr, "db_open failed: %s\n", strerror(errno));
goto fail;
}
{
struct db_context *backend_copy = backend;
db = db_open_watched(ev, &backend_copy, msg);
if (db == NULL) {
fprintf(stderr, "db_open_watched failed\n");
goto fail;
}
}
if (pev != NULL) {
*pev = ev;
}
if (pmsg != NULL) {
*pmsg = msg;
}
if (pbackend != NULL) {
*pbackend = backend;
}
if (pdb != NULL) {
*pdb = db;
}
return true;
fail:
TALLOC_FREE(backend);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return false;
}
bool run_dbwrap_watch1(int dummy)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct db_context *backend = NULL;
struct db_context *db = NULL;
const char *keystr = "key";
TDB_DATA key = string_term_tdb_data(keystr);
struct db_record *rec = NULL;
struct tevent_req *req = NULL;
NTSTATUS status;
bool ret = false;
ret = test_dbwrap_watch_init(
talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
if (!ret) {
goto fail;
}
rec = dbwrap_fetch_locked(db, db, key);
if (rec == NULL) {
fprintf(stderr, "dbwrap_fetch_locked failed\n");
goto fail;
}
req = dbwrap_watched_watch_send(talloc_tos(), ev, rec,
0, /* resume_instance */
(struct server_id){0});
if (req == NULL) {
fprintf(stderr, "dbwrap_record_watch_send failed\n");
goto fail;
}
TALLOC_FREE(rec);
status = dbwrap_store_int32_bystring(db, "different_key", 1);
if (!NT_STATUS_IS_OK(status)) {
fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
nt_errstr(status));
goto fail;
}
status = dbwrap_store_int32_bystring(db, keystr, 1);
if (!NT_STATUS_IS_OK(status)) {
fprintf(stderr, "dbwrap_store_int32 failed: %s\n",
nt_errstr(status));
goto fail;
}
if (!tevent_req_poll(req, ev)) {
fprintf(stderr, "tevent_req_poll failed\n");
goto fail;
}
status = dbwrap_watched_watch_recv(req, NULL, NULL, NULL);
if (!NT_STATUS_IS_OK(status)) {
fprintf(stderr, "dbwrap_record_watch_recv failed: %s\n",
nt_errstr(status));
goto fail;
}
(void)unlink("test_watch.tdb");
ret = true;
fail:
TALLOC_FREE(req);
TALLOC_FREE(rec);
TALLOC_FREE(db);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return ret;
}
/*
* Make sure dbwrap_parse_record does not return NT_STATUS_OK on
* invalid data
*/
bool run_dbwrap_watch2(int dummy)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct db_context *backend = NULL;
struct db_context *db = NULL;
const char *keystr = "key";
TDB_DATA key = string_term_tdb_data(keystr);
NTSTATUS status;
bool ret = false;
ret = test_dbwrap_watch_init(
talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
if (!ret) {
goto fail;
}
/*
* Store invalid data (from the dbwrap_watch point of view)
* directly into the backend database
*/
status = dbwrap_store_uint32_bystring(backend, keystr, UINT32_MAX);
if (!NT_STATUS_IS_OK(status)) {
fprintf(stderr, "dbwrap_store_uint32_bystring failed: %s\n",
nt_errstr(status));
goto fail;
}
status = dbwrap_parse_record(db, key, NULL, NULL);
if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
fprintf(stderr, "dbwrap_parse_record returned %s, expected "
"NT_STATUS_NOT_FOUND\n", nt_errstr(status));
goto fail;
}
(void)unlink("test_watch.tdb");
ret = true;
fail:
TALLOC_FREE(db);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return ret;
}
/*
* Test autocleanup of dead watchers
*/
bool run_dbwrap_watch3(int dummy)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct db_context *backend = NULL;
struct db_context *db = NULL;
const char *keystr = "key";
TDB_DATA key = string_term_tdb_data(keystr);
NTSTATUS status;
bool ret = false;
pid_t child, waited;
int wstatus, exit_status;
BlockSignals(true, SIGCHLD);
child = fork();
if (child == -1) {
fprintf(stderr,
"fork failed: %s\n",
strerror(errno));
goto fail;
}
ret = test_dbwrap_watch_init(
talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
if (!ret) {
goto fail;
}
if (child == 0) {
struct db_record *rec = dbwrap_fetch_locked(db, db, key);
struct tevent_req *req = NULL;
if (rec == NULL) {
fprintf(stderr, "dbwrap_fetch_locked failed\n");
exit(1);
}
req = dbwrap_watched_watch_send(
db, ev, rec, 0, (struct server_id) { 0 });
if (req == NULL) {
fprintf(stderr, "dbwrap_watched_watch_send failed\n");
exit(2);
}
exit(0);
}
waited = waitpid(child, &wstatus, 0);
if (waited == -1) {
fprintf(stderr, "waitpid failed: %s\n", strerror(errno));
goto fail;
}
if (!WIFEXITED(wstatus)) {
fprintf(stderr, "child did not exit normally\n");
goto fail;
}
exit_status = WEXITSTATUS(wstatus);
if (exit_status != 0) {
fprintf(stderr, "exit status is %d\n", exit_status);
goto fail;
}
status = dbwrap_store_uint32_bystring(db, keystr, 1);
if (!NT_STATUS_EQUAL(status, NT_STATUS_OK)) {
fprintf(stderr,
"dbwrap_store_uint32 returned %s\n",
nt_errstr(status));
goto fail;
}
(void)unlink("test_watch.tdb");
ret = true;
fail:
TALLOC_FREE(db);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return ret;
}
/*
* Test that we can't add two watchers in the same
* fetch_lock/do_locked round
*/
struct dbwrap_watch4_state {
TALLOC_CTX *mem_ctx;
struct tevent_context *ev;
struct db_context *db;
TDB_DATA key;
NTSTATUS status;
struct tevent_req *req1;
NTSTATUS status1;
struct tevent_req *req2;
NTSTATUS status2;
};
static void dbwrap_watch4_done1(struct tevent_req *subreq);
static void dbwrap_watch4_done2(struct tevent_req *subreq);
static void dbwrap_watch4_fn(struct db_record *rec,
TDB_DATA value,
void *private_data)
{
struct dbwrap_watch4_state *state = private_data;
bool ok;
state->req1 = dbwrap_watched_watch_send(
state->mem_ctx, state->ev, rec, 0, (struct server_id) { .pid=0 });
if (state->req1 == NULL) {
goto nomem;
}
tevent_req_set_callback(state->req1, dbwrap_watch4_done1, state);
state->status1 = NT_STATUS_EVENT_PENDING;
ok = tevent_req_set_endtime(
state->req1, state->ev, timeval_current_ofs(1, 0));
if (!ok) {
goto nomem;
}
state->req2 = dbwrap_watched_watch_send(
state->mem_ctx, state->ev, rec, 0, (struct server_id) { .pid=0 });
if (state->req2 == NULL) {
goto nomem;
}
tevent_req_set_callback(state->req2, dbwrap_watch4_done2, state);
state->status2 = NT_STATUS_EVENT_PENDING;
ok = tevent_req_set_endtime(
state->req2, state->ev, timeval_current_ofs(1, 0));
if (!ok) {
goto nomem;
}
state->status = NT_STATUS_OK;
return;
nomem:
state->status = NT_STATUS_NO_MEMORY;
}
static void dbwrap_watch4_done1(struct tevent_req *subreq)
{
struct dbwrap_watch4_state *state = tevent_req_callback_data_void(subreq);
state->status1 = dbwrap_watched_watch_recv(subreq, NULL, NULL, NULL);
TALLOC_FREE(subreq);
printf("req1 finished: %s\n", nt_errstr(state->status1));
state->req1 = NULL;
}
static void dbwrap_watch4_done2(struct tevent_req *subreq)
{
struct dbwrap_watch4_state *state = tevent_req_callback_data_void(subreq);
state->status2 = dbwrap_watched_watch_recv(subreq, NULL, NULL, NULL);
TALLOC_FREE(subreq);
printf("req2 finished: %s\n", nt_errstr(state->status2));
state->req2 = NULL;
}
bool run_dbwrap_watch4(int dummy)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct db_context *backend = NULL;
struct db_context *db = NULL;
const char *keystr = "key";
TDB_DATA key = string_term_tdb_data(keystr);
struct dbwrap_watch4_state state = { 0 };
NTSTATUS status;
bool ret = false;
bool ok;
ok = test_dbwrap_watch_init(
talloc_tos(), "test_watch.tdb", &ev, &msg, &backend, &db);
if (!ok) {
goto fail;
}
state = (struct dbwrap_watch4_state) {
.mem_ctx = talloc_tos(),
.ev = ev,
.db = db,
.key = key,
};
status = dbwrap_do_locked(db, key, dbwrap_watch4_fn, &state);
if (!NT_STATUS_IS_OK(status)) {
fprintf(stderr,
"dbwrap_do_locked failed: %s\n",
nt_errstr(status));
goto fail;
}
if (!NT_STATUS_IS_OK(state.status)) {
fprintf(stderr,
"dbwrap_watch4_fn failed: %s\n",
nt_errstr(status));
goto fail;
}
status = dbwrap_store(db, key, key, 0);
if (!NT_STATUS_IS_OK(status)) {
fprintf(stderr,
"dbwrap_store failed: %s\n",
nt_errstr(status));
goto fail;
}
while (NT_STATUS_EQUAL(state.status1, NT_STATUS_EVENT_PENDING) ||
NT_STATUS_EQUAL(state.status2, NT_STATUS_EVENT_PENDING)) {
int res = tevent_loop_once(ev);
if (res != 0) {
fprintf(stderr,
"tevent_loop_once failed: %s\n",
strerror(errno));
goto fail;
}
}
if (!NT_STATUS_IS_OK(state.status1)) {
fprintf(stderr,
"req1 returned %s\n",
nt_errstr(state.status1));
goto fail;
}
if (!NT_STATUS_EQUAL(state.status2, NT_STATUS_REQUEST_NOT_ACCEPTED)) {
fprintf(stderr,
"req2 returned %s\n",
nt_errstr(state.status2));
goto fail;
}
(void)unlink("test_watch.tdb");
ret = true;
fail:
TALLOC_FREE(state.req2);
TALLOC_FREE(state.req1);
TALLOC_FREE(db);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return ret;
}
|