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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
|
/*
Unix SMB/CIFS implementation.
global locks based on dbwrap and messaging
Copyright (C) 2009 by Volker Lendecke
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 "system/filesys.h"
#include "lib/util/server_id.h"
#include "dbwrap/dbwrap.h"
#include "dbwrap/dbwrap_open.h"
#include "dbwrap/dbwrap_watch.h"
#include "g_lock.h"
#include "util_tdb.h"
#include "../lib/util/tevent_ntstatus.h"
#include "messages.h"
#include "serverid.h"
struct g_lock_ctx {
struct db_context *db;
struct messaging_context *msg;
};
/*
* The "g_lock.tdb" file contains records, indexed by the 0-terminated
* lockname. The record contains an array of "struct g_lock_rec"
* structures.
*/
#define G_LOCK_REC_LENGTH (SERVER_ID_BUF_LENGTH+1)
static void g_lock_rec_put(uint8_t buf[G_LOCK_REC_LENGTH],
const struct g_lock_rec rec)
{
SCVAL(buf, 0, rec.lock_type);
server_id_put(buf+1, rec.pid);
}
static void g_lock_rec_get(struct g_lock_rec *rec,
const uint8_t buf[G_LOCK_REC_LENGTH])
{
rec->lock_type = CVAL(buf, 0);
server_id_get(&rec->pid, buf+1);
}
struct g_lock {
uint8_t *recsbuf;
size_t num_recs;
uint8_t *data;
size_t datalen;
};
static bool g_lock_parse(uint8_t *buf, size_t buflen, struct g_lock *lck)
{
size_t found_recs, data_ofs;
if (buflen < sizeof(uint32_t)) {
*lck = (struct g_lock) {0};
return true;
}
found_recs = IVAL(buf, 0);
if (found_recs > buflen/G_LOCK_REC_LENGTH) {
return false;
}
buf += sizeof(uint32_t);
buflen -= sizeof(uint32_t);
data_ofs = found_recs * G_LOCK_REC_LENGTH;
*lck = (struct g_lock) {
.recsbuf = buf, .num_recs = found_recs,
.data = buf+data_ofs, .datalen = buflen-data_ofs
};
return true;
}
static void g_lock_get_rec(struct g_lock *lck, size_t i,
struct g_lock_rec *rec)
{
if (i >= lck->num_recs) {
abort();
}
g_lock_rec_get(rec, lck->recsbuf + i*G_LOCK_REC_LENGTH);
}
static void g_lock_rec_del(struct g_lock *lck, size_t i)
{
if (i >= lck->num_recs) {
abort();
}
lck->num_recs -= 1;
if (i < lck->num_recs) {
uint8_t *recptr = lck->recsbuf + i*G_LOCK_REC_LENGTH;
memcpy(recptr, lck->recsbuf + lck->num_recs*G_LOCK_REC_LENGTH,
G_LOCK_REC_LENGTH);
}
}
static NTSTATUS g_lock_store(struct db_record *rec, struct g_lock *lck,
struct g_lock_rec *add)
{
uint8_t sizebuf[4];
uint8_t addbuf[G_LOCK_REC_LENGTH];
struct TDB_DATA dbufs[] = {
{ .dptr = sizebuf, .dsize = sizeof(sizebuf) },
{ .dptr = lck->recsbuf,
.dsize = lck->num_recs * G_LOCK_REC_LENGTH },
{ 0 },
{ .dptr = lck->data, .dsize = lck->datalen }
};
if (add != NULL) {
g_lock_rec_put(addbuf, *add);
dbufs[2] = (TDB_DATA) {
.dptr = addbuf, .dsize = G_LOCK_REC_LENGTH
};
lck->num_recs += 1;
}
SIVAL(sizebuf, 0, lck->num_recs);
return dbwrap_record_storev(rec, dbufs, ARRAY_SIZE(dbufs), 0);
}
struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
struct messaging_context *msg)
{
struct g_lock_ctx *result;
struct db_context *backend;
char *db_path;
result = talloc(mem_ctx, struct g_lock_ctx);
if (result == NULL) {
return NULL;
}
result->msg = msg;
db_path = lock_path("g_lock.tdb");
if (db_path == NULL) {
TALLOC_FREE(result);
return NULL;
}
backend = db_open(result, db_path, 0,
TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
O_RDWR|O_CREAT, 0600,
DBWRAP_LOCK_ORDER_3,
DBWRAP_FLAG_NONE);
TALLOC_FREE(db_path);
if (backend == NULL) {
DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
TALLOC_FREE(result);
return NULL;
}
result->db = db_open_watched(result, backend, msg);
if (result->db == NULL) {
DBG_WARNING("g_lock_init: db_open_watched failed\n");
TALLOC_FREE(result);
return NULL;
}
return result;
}
static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
{
/*
* Only tested write locks so far. Very likely this routine
* needs to be fixed for read locks....
*/
if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
return false;
}
return true;
}
static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
enum g_lock_type type,
struct server_id *blocker)
{
TDB_DATA data;
size_t i;
struct g_lock lck;
NTSTATUS status;
bool modified = false;
bool ok;
data = dbwrap_record_get_value(rec);
ok = g_lock_parse(data.dptr, data.dsize, &lck);
if (!ok) {
return NT_STATUS_INTERNAL_DB_CORRUPTION;
}
if ((type == G_LOCK_READ) && (lck.num_recs > 0)) {
struct g_lock_rec check_rec;
/*
* Read locks can stay around forever if the process
* dies. Do a heuristic check for process existence:
* Check one random process for existence. Hopefully
* this will keep runaway read locks under control.
*/
i = generate_random() % lck.num_recs;
g_lock_get_rec(&lck, i, &check_rec);
if (!serverid_exists(&check_rec.pid)) {
g_lock_rec_del(&lck, i);
modified = true;
}
}
for (i=0; i<lck.num_recs; i++) {
struct g_lock_rec lock;
g_lock_get_rec(&lck, i, &lock);
if (serverid_equal(&self, &lock.pid)) {
if (lock.lock_type == type) {
status = NT_STATUS_WAS_LOCKED;
goto done;
}
/*
* Remove "our" lock entry. Re-add it later
* with our new lock type.
*/
g_lock_rec_del(&lck, i);
continue;
}
if (g_lock_conflicts(type, lock.lock_type)) {
struct server_id pid = lock.pid;
/*
* As the serverid_exists might recurse into
* the g_lock code, we use
* SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
*/
pid.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
if (serverid_exists(&pid)) {
status = NT_STATUS_LOCK_NOT_GRANTED;
*blocker = lock.pid;
goto done;
}
/*
* Delete stale conflicting entry
*/
g_lock_rec_del(&lck, i);
modified = true;
}
}
modified = true;
status = NT_STATUS_OK;
done:
if (modified) {
struct g_lock_rec mylock = { .pid = self, .lock_type = type };
NTSTATUS store_status;
store_status = g_lock_store(rec, &lck, &mylock);
if (!NT_STATUS_IS_OK(store_status)) {
DBG_WARNING("g_lock_record_store failed: %s\n",
nt_errstr(store_status));
status = store_status;
}
}
return status;
}
struct g_lock_lock_state {
struct tevent_context *ev;
struct g_lock_ctx *ctx;
const char *name;
enum g_lock_type type;
};
static void g_lock_lock_retry(struct tevent_req *subreq);
struct g_lock_lock_fn_state {
struct g_lock_lock_state *state;
struct server_id self;
struct tevent_req *watch_req;
NTSTATUS status;
};
static void g_lock_lock_fn(struct db_record *rec, void *private_data)
{
struct g_lock_lock_fn_state *state = private_data;
struct server_id blocker;
state->status = g_lock_trylock(rec, state->self, state->state->type,
&blocker);
if (!NT_STATUS_EQUAL(state->status, NT_STATUS_LOCK_NOT_GRANTED)) {
return;
}
state->watch_req = dbwrap_watched_watch_send(
state->state, state->state->ev, rec, blocker);
}
struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
struct tevent_context *ev,
struct g_lock_ctx *ctx,
const char *name,
enum g_lock_type type)
{
struct tevent_req *req;
struct g_lock_lock_state *state;
struct g_lock_lock_fn_state fn_state;
NTSTATUS status;
req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
if (req == NULL) {
return NULL;
}
state->ev = ev;
state->ctx = ctx;
state->name = name;
state->type = type;
fn_state = (struct g_lock_lock_fn_state) {
.state = state, .self = messaging_server_id(ctx->msg)
};
status = dbwrap_do_locked(ctx->db, string_term_tdb_data(name),
g_lock_lock_fn, &fn_state);
if (tevent_req_nterror(req, status)) {
DBG_DEBUG("dbwrap_do_locked failed: %s\n",
nt_errstr(status));
return tevent_req_post(req, ev);
}
if (NT_STATUS_IS_OK(fn_state.status)) {
tevent_req_done(req);
return tevent_req_post(req, ev);
}
if (!NT_STATUS_EQUAL(fn_state.status, NT_STATUS_LOCK_NOT_GRANTED)) {
tevent_req_nterror(req, fn_state.status);
return tevent_req_post(req, ev);
}
if (tevent_req_nomem(fn_state.watch_req, req)) {
return tevent_req_post(req, ev);
}
if (!tevent_req_set_endtime(
fn_state.watch_req, state->ev,
timeval_current_ofs(5 + sys_random() % 5, 0))) {
tevent_req_oom(req);
return tevent_req_post(req, ev);
}
tevent_req_set_callback(fn_state.watch_req, g_lock_lock_retry, req);
return req;
}
static void g_lock_lock_retry(struct tevent_req *subreq)
{
struct tevent_req *req = tevent_req_callback_data(
subreq, struct tevent_req);
struct g_lock_lock_state *state = tevent_req_data(
req, struct g_lock_lock_state);
struct g_lock_lock_fn_state fn_state;
NTSTATUS status;
status = dbwrap_watched_watch_recv(subreq, NULL, NULL, NULL, NULL);
DBG_DEBUG("watch_recv returned %s\n", nt_errstr(status));
TALLOC_FREE(subreq);
if (!NT_STATUS_IS_OK(status) &&
!NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
tevent_req_nterror(req, status);
return;
}
fn_state = (struct g_lock_lock_fn_state) {
.state = state, .self = messaging_server_id(state->ctx->msg)
};
status = dbwrap_do_locked(state->ctx->db,
string_term_tdb_data(state->name),
g_lock_lock_fn, &fn_state);
if (tevent_req_nterror(req, status)) {
DBG_DEBUG("dbwrap_do_locked failed: %s\n",
nt_errstr(status));
return;
}
if (NT_STATUS_IS_OK(fn_state.status)) {
tevent_req_done(req);
return;
}
if (!NT_STATUS_EQUAL(fn_state.status, NT_STATUS_LOCK_NOT_GRANTED)) {
tevent_req_nterror(req, fn_state.status);
return;
}
if (tevent_req_nomem(fn_state.watch_req, req)) {
return;
}
if (!tevent_req_set_endtime(
fn_state.watch_req, state->ev,
timeval_current_ofs(5 + sys_random() % 5, 0))) {
tevent_req_oom(req);
return;
}
tevent_req_set_callback(fn_state.watch_req, g_lock_lock_retry, req);
}
NTSTATUS g_lock_lock_recv(struct tevent_req *req)
{
return tevent_req_simple_recv_ntstatus(req);
}
NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
enum g_lock_type type, struct timeval timeout)
{
TALLOC_CTX *frame = talloc_stackframe();
struct tevent_context *ev;
struct tevent_req *req;
struct timeval end;
NTSTATUS status = NT_STATUS_NO_MEMORY;
ev = samba_tevent_context_init(frame);
if (ev == NULL) {
goto fail;
}
req = g_lock_lock_send(frame, ev, ctx, name, type);
if (req == NULL) {
goto fail;
}
end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
if (!tevent_req_set_endtime(req, ev, end)) {
goto fail;
}
if (!tevent_req_poll_ntstatus(req, ev, &status)) {
goto fail;
}
status = g_lock_lock_recv(req);
fail:
TALLOC_FREE(frame);
return status;
}
struct g_lock_unlock_state {
const char *name;
struct server_id self;
NTSTATUS status;
};
static void g_lock_unlock_fn(struct db_record *rec,
void *private_data)
{
struct g_lock_unlock_state *state = private_data;
TDB_DATA value;
struct g_lock lck;
size_t i;
bool ok;
value = dbwrap_record_get_value(rec);
ok = g_lock_parse(value.dptr, value.dsize, &lck);
if (!ok) {
DBG_DEBUG("g_lock_get for %s failed\n", state->name);
state->status = NT_STATUS_FILE_INVALID;
return;
}
for (i=0; i<lck.num_recs; i++) {
struct g_lock_rec lockrec;
g_lock_get_rec(&lck, i, &lockrec);
if (serverid_equal(&state->self, &lockrec.pid)) {
break;
}
}
if (i == lck.num_recs) {
DBG_DEBUG("Lock not found, num_rec=%zu\n", lck.num_recs);
state->status = NT_STATUS_NOT_FOUND;
return;
}
g_lock_rec_del(&lck, i);
if ((lck.num_recs == 0) && (lck.datalen == 0)) {
state->status = dbwrap_record_delete(rec);
return;
}
state->status = g_lock_store(rec, &lck, NULL);
}
NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
{
struct g_lock_unlock_state state = {
.self = messaging_server_id(ctx->msg), .name = name
};
NTSTATUS status;
status = dbwrap_do_locked(ctx->db, string_term_tdb_data(name),
g_lock_unlock_fn, &state);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("dbwrap_do_locked failed: %s\n",
nt_errstr(status));
return status;
}
if (!NT_STATUS_IS_OK(state.status)) {
DBG_WARNING("g_lock_unlock_fn failed: %s\n",
nt_errstr(state.status));
return state.status;
}
return NT_STATUS_OK;
}
struct g_lock_write_data_state {
const char *name;
struct server_id self;
const uint8_t *data;
size_t datalen;
NTSTATUS status;
};
static void g_lock_write_data_fn(struct db_record *rec,
void *private_data)
{
struct g_lock_write_data_state *state = private_data;
TDB_DATA value;
struct g_lock lck;
size_t i;
bool ok;
value = dbwrap_record_get_value(rec);
ok = g_lock_parse(value.dptr, value.dsize, &lck);
if (!ok) {
DBG_DEBUG("g_lock_parse for %s failed\n", state->name);
state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
return;
}
for (i=0; i<lck.num_recs; i++) {
struct g_lock_rec lockrec;
g_lock_get_rec(&lck, i, &lockrec);
if ((lockrec.lock_type == G_LOCK_WRITE) &&
serverid_equal(&state->self, &lockrec.pid)) {
break;
}
}
if (i == lck.num_recs) {
DBG_DEBUG("Not locked by us\n");
state->status = NT_STATUS_NOT_LOCKED;
return;
}
lck.data = discard_const_p(uint8_t, state->data);
lck.datalen = state->datalen;
state->status = g_lock_store(rec, &lck, NULL);
}
NTSTATUS g_lock_write_data(struct g_lock_ctx *ctx, const char *name,
const uint8_t *buf, size_t buflen)
{
struct g_lock_write_data_state state = {
.name = name, .self = messaging_server_id(ctx->msg),
.data = buf, .datalen = buflen
};
NTSTATUS status;
status = dbwrap_do_locked(ctx->db, string_term_tdb_data(name),
g_lock_write_data_fn, &state);
if (!NT_STATUS_IS_OK(status)) {
DBG_WARNING("dbwrap_do_locked failed: %s\n",
nt_errstr(status));
return status;
}
if (!NT_STATUS_IS_OK(state.status)) {
DBG_WARNING("g_lock_write_data_fn failed: %s\n",
nt_errstr(state.status));
return state.status;
}
return NT_STATUS_OK;
}
struct g_lock_locks_state {
int (*fn)(const char *name, void *private_data);
void *private_data;
};
static int g_lock_locks_fn(struct db_record *rec, void *priv)
{
TDB_DATA key;
struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
key = dbwrap_record_get_key(rec);
if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
return 0;
}
return state->fn((char *)key.dptr, state->private_data);
}
int g_lock_locks(struct g_lock_ctx *ctx,
int (*fn)(const char *name, void *private_data),
void *private_data)
{
struct g_lock_locks_state state;
NTSTATUS status;
int count;
state.fn = fn;
state.private_data = private_data;
status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
if (!NT_STATUS_IS_OK(status)) {
return -1;
}
return count;
}
struct g_lock_dump_state {
TALLOC_CTX *mem_ctx;
const char *name;
void (*fn)(const struct g_lock_rec *locks,
size_t num_locks,
const uint8_t *data,
size_t datalen,
void *private_data);
void *private_data;
NTSTATUS status;
};
static void g_lock_dump_fn(TDB_DATA key, TDB_DATA data,
void *private_data)
{
struct g_lock_dump_state *state = private_data;
struct g_lock_rec *recs;
struct g_lock lck;
size_t i;
bool ok;
ok = g_lock_parse(data.dptr, data.dsize, &lck);
if (!ok) {
DBG_DEBUG("g_lock_parse failed for %s\n",
state->name);
state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
return;
}
recs = talloc_array(state->mem_ctx, struct g_lock_rec, lck.num_recs);
if (recs == NULL) {
DBG_DEBUG("talloc failed\n");
state->status = NT_STATUS_NO_MEMORY;
return;
}
for (i=0; i<lck.num_recs; i++) {
g_lock_get_rec(&lck, i, &recs[i]);
}
state->fn(recs, lck.num_recs, lck.data, lck.datalen,
state->private_data);
TALLOC_FREE(recs);
state->status = NT_STATUS_OK;
}
NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
void (*fn)(const struct g_lock_rec *locks,
size_t num_locks,
const uint8_t *data,
size_t datalen,
void *private_data),
void *private_data)
{
struct g_lock_dump_state state = {
.mem_ctx = ctx, .name = name,
.fn = fn, .private_data = private_data
};
NTSTATUS status;
status = dbwrap_parse_record(ctx->db, string_term_tdb_data(name),
g_lock_dump_fn, &state);
if (!NT_STATUS_IS_OK(status)) {
DBG_DEBUG("dbwrap_parse_record returned %s\n",
nt_errstr(status));
return status;
}
if (!NT_STATUS_IS_OK(state.status)) {
DBG_DEBUG("g_lock_dump_fn returned %s\n",
nt_errstr(state.status));
return state.status;
}
return NT_STATUS_OK;
}
static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
struct tevent_context **pev,
struct messaging_context **pmsg,
struct g_lock_ctx **pg_ctx)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct g_lock_ctx *g_ctx = NULL;
ev = samba_tevent_context_init(mem_ctx);
if (ev == NULL) {
d_fprintf(stderr, "ERROR: could not init event context\n");
goto fail;
}
msg = messaging_init(mem_ctx, ev);
if (msg == NULL) {
d_fprintf(stderr, "ERROR: could not init messaging context\n");
goto fail;
}
g_ctx = g_lock_ctx_init(mem_ctx, msg);
if (g_ctx == NULL) {
d_fprintf(stderr, "ERROR: could not init g_lock context\n");
goto fail;
}
*pev = ev;
*pmsg = msg;
*pg_ctx = g_ctx;
return true;
fail:
TALLOC_FREE(g_ctx);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return false;
}
NTSTATUS g_lock_do(const char *name, enum g_lock_type lock_type,
struct timeval timeout,
void (*fn)(void *private_data), void *private_data)
{
struct tevent_context *ev = NULL;
struct messaging_context *msg = NULL;
struct g_lock_ctx *g_ctx = NULL;
NTSTATUS status;
if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
status = NT_STATUS_ACCESS_DENIED;
goto done;
}
status = g_lock_lock(g_ctx, name, lock_type, timeout);
if (!NT_STATUS_IS_OK(status)) {
goto done;
}
fn(private_data);
g_lock_unlock(g_ctx, name);
done:
TALLOC_FREE(g_ctx);
TALLOC_FREE(msg);
TALLOC_FREE(ev);
return status;
}
|