echo.idl frstrans examples
[metze/samba/wip.git] / source3 / lib / g_lock.c
1 /*
2    Unix SMB/CIFS implementation.
3    global locks based on dbwrap and messaging
4    Copyright (C) 2009 by Volker Lendecke
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "lib/util/server_id.h"
23 #include "dbwrap/dbwrap.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "dbwrap/dbwrap_watch.h"
26 #include "g_lock.h"
27 #include "util_tdb.h"
28 #include "../lib/util/tevent_ntstatus.h"
29 #include "messages.h"
30 #include "serverid.h"
31
32 struct g_lock_ctx {
33         struct db_context *db;
34         struct messaging_context *msg;
35 };
36
37 /*
38  * The "g_lock.tdb" file contains records, indexed by the 0-terminated
39  * lockname. The record contains an array of "struct g_lock_rec"
40  * structures.
41  */
42
43 #define G_LOCK_REC_LENGTH (SERVER_ID_BUF_LENGTH+1)
44
45 static void g_lock_rec_put(uint8_t buf[G_LOCK_REC_LENGTH],
46                            const struct g_lock_rec rec)
47 {
48         SCVAL(buf, 0, rec.lock_type);
49         server_id_put(buf+1, rec.pid);
50 }
51
52 static void g_lock_rec_get(struct g_lock_rec *rec,
53                            const uint8_t buf[G_LOCK_REC_LENGTH])
54 {
55         rec->lock_type = CVAL(buf, 0);
56         server_id_get(&rec->pid, buf+1);
57 }
58
59 struct g_lock {
60         uint8_t *recsbuf;
61         size_t num_recs;
62         uint8_t *data;
63         size_t datalen;
64 };
65
66 static bool g_lock_parse(uint8_t *buf, size_t buflen, struct g_lock *lck)
67 {
68         size_t found_recs, data_ofs;
69
70         if (buflen < sizeof(uint32_t)) {
71                 *lck = (struct g_lock) {0};
72                 return true;
73         }
74
75         found_recs = IVAL(buf, 0);
76
77         buf += sizeof(uint32_t);
78         buflen -= sizeof(uint32_t);
79         if (found_recs > buflen/G_LOCK_REC_LENGTH) {
80                 return false;
81         }
82
83         data_ofs = found_recs * G_LOCK_REC_LENGTH;
84
85         *lck = (struct g_lock) {
86                 .recsbuf = buf, .num_recs = found_recs,
87                 .data = buf+data_ofs, .datalen = buflen-data_ofs
88         };
89
90         return true;
91 }
92
93 static void g_lock_get_rec(const struct g_lock *lck,
94                            size_t i,
95                            struct g_lock_rec *rec)
96 {
97         if (i >= lck->num_recs) {
98                 abort();
99         }
100         g_lock_rec_get(rec, lck->recsbuf + i*G_LOCK_REC_LENGTH);
101 }
102
103 static void g_lock_rec_del(struct g_lock *lck, size_t i)
104 {
105         if (i >= lck->num_recs) {
106                 abort();
107         }
108         lck->num_recs -= 1;
109         if (i < lck->num_recs) {
110                 uint8_t *recptr = lck->recsbuf + i*G_LOCK_REC_LENGTH;
111                 memcpy(recptr, lck->recsbuf + lck->num_recs*G_LOCK_REC_LENGTH,
112                        G_LOCK_REC_LENGTH);
113         }
114 }
115
116 static NTSTATUS g_lock_store(struct db_record *rec, struct g_lock *lck,
117                              struct g_lock_rec *add)
118 {
119         uint8_t sizebuf[4];
120         uint8_t addbuf[G_LOCK_REC_LENGTH];
121
122         struct TDB_DATA dbufs[] = {
123                 { .dptr = sizebuf, .dsize = sizeof(sizebuf) },
124                 { .dptr = lck->recsbuf,
125                   .dsize = lck->num_recs * G_LOCK_REC_LENGTH },
126                 { 0 },
127                 { .dptr = lck->data, .dsize = lck->datalen }
128         };
129
130         if (add != NULL) {
131                 g_lock_rec_put(addbuf, *add);
132
133                 dbufs[2] = (TDB_DATA) {
134                         .dptr = addbuf, .dsize = G_LOCK_REC_LENGTH
135                 };
136
137                 lck->num_recs += 1;
138         }
139
140         SIVAL(sizebuf, 0, lck->num_recs);
141
142         return dbwrap_record_storev(rec, dbufs, ARRAY_SIZE(dbufs), 0);
143 }
144
145 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
146                                    struct messaging_context *msg)
147 {
148         struct g_lock_ctx *result;
149         struct db_context *backend;
150         char *db_path;
151
152         result = talloc(mem_ctx, struct g_lock_ctx);
153         if (result == NULL) {
154                 return NULL;
155         }
156         result->msg = msg;
157
158         db_path = lock_path(talloc_tos(), "g_lock.tdb");
159         if (db_path == NULL) {
160                 TALLOC_FREE(result);
161                 return NULL;
162         }
163
164         backend = db_open(result, db_path, 0,
165                           TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
166                           O_RDWR|O_CREAT, 0600,
167                           DBWRAP_LOCK_ORDER_3,
168                           DBWRAP_FLAG_NONE);
169         TALLOC_FREE(db_path);
170         if (backend == NULL) {
171                 DBG_WARNING("Could not open g_lock.tdb\n");
172                 TALLOC_FREE(result);
173                 return NULL;
174         }
175
176         result->db = db_open_watched(result, &backend, msg);
177         if (result->db == NULL) {
178                 DBG_WARNING("db_open_watched failed\n");
179                 TALLOC_FREE(result);
180                 return NULL;
181         }
182         return result;
183 }
184
185 static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
186 {
187         /*
188          * Only tested write locks so far. Very likely this routine
189          * needs to be fixed for read locks....
190          */
191         if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
192                 return false;
193         }
194         return true;
195 }
196
197 static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
198                                enum g_lock_type type,
199                                struct server_id *blocker)
200 {
201         TDB_DATA data;
202         size_t i;
203         struct g_lock lck;
204         struct g_lock_rec mylock = {0};
205         NTSTATUS status;
206         bool modified = false;
207         bool ok;
208
209         data = dbwrap_record_get_value(rec);
210
211         ok = g_lock_parse(data.dptr, data.dsize, &lck);
212         if (!ok) {
213                 return NT_STATUS_INTERNAL_DB_CORRUPTION;
214         }
215
216         if ((type == G_LOCK_READ) && (lck.num_recs > 0)) {
217                 struct g_lock_rec check_rec;
218
219                 /*
220                  * Read locks can stay around forever if the process
221                  * dies. Do a heuristic check for process existence:
222                  * Check one random process for existence. Hopefully
223                  * this will keep runaway read locks under control.
224                  */
225                 i = generate_random() % lck.num_recs;
226
227                 g_lock_get_rec(&lck, i, &check_rec);
228
229                 if ((check_rec.lock_type == G_LOCK_READ) &&
230                     !serverid_exists(&check_rec.pid)) {
231                         g_lock_rec_del(&lck, i);
232                         modified = true;
233                 }
234         }
235
236         /*
237          * For the lock upgrade/downgrade case, remove ourselves from
238          * the list. We re-add ourselves later after we checked the
239          * other entries for conflict.
240          */
241
242         for (i=0; i<lck.num_recs; i++) {
243                 struct g_lock_rec lock;
244
245                 g_lock_get_rec(&lck, i, &lock);
246
247                 if (serverid_equal(&self, &lock.pid)) {
248                         if (lock.lock_type == type) {
249                                 status = NT_STATUS_WAS_LOCKED;
250                                 goto done;
251                         }
252
253                         mylock = lock;
254                         g_lock_rec_del(&lck, i);
255                         modified = true;
256                         break;
257                 }
258         }
259
260         /*
261          * Check for conflicts with everybody else. Not a for-loop
262          * because we remove stale entries in the meantime,
263          * decrementing lck.num_recs.
264          */
265
266         i = 0;
267
268         while (i < lck.num_recs) {
269                 struct g_lock_rec lock;
270
271                 g_lock_get_rec(&lck, i, &lock);
272
273                 if (g_lock_conflicts(type, lock.lock_type)) {
274                         struct server_id pid = lock.pid;
275
276                         /*
277                          * As the serverid_exists might recurse into
278                          * the g_lock code, we use
279                          * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
280                          */
281                         pid.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
282
283                         if (serverid_exists(&pid)) {
284                                 status = NT_STATUS_LOCK_NOT_GRANTED;
285                                 *blocker = lock.pid;
286                                 goto done;
287                         }
288
289                         /*
290                          * Delete stale conflicting entry
291                          */
292                         g_lock_rec_del(&lck, i);
293                         modified = true;
294                         continue;
295                 }
296                 i++;
297         }
298
299         modified = true;
300
301         mylock = (struct g_lock_rec) {
302                 .pid = self,
303                 .lock_type = type
304         };
305
306         status = NT_STATUS_OK;
307 done:
308         if (modified) {
309                 NTSTATUS store_status;
310
311                 /*
312                  * (Re-)add ourselves if needed via non-NULL
313                  * g_lock_store argument
314                  */
315
316                 store_status = g_lock_store(
317                         rec,
318                         &lck,
319                         mylock.pid.pid != 0 ? &mylock : NULL);
320
321                 if (!NT_STATUS_IS_OK(store_status)) {
322                         DBG_WARNING("g_lock_record_store failed: %s\n",
323                                     nt_errstr(store_status));
324                         status = store_status;
325                 }
326         }
327         return status;
328 }
329
330 struct g_lock_lock_state {
331         struct tevent_context *ev;
332         struct g_lock_ctx *ctx;
333         TDB_DATA key;
334         enum g_lock_type type;
335 };
336
337 static void g_lock_lock_retry(struct tevent_req *subreq);
338
339 struct g_lock_lock_fn_state {
340         struct g_lock_lock_state *state;
341         struct server_id self;
342
343         struct tevent_req *watch_req;
344         NTSTATUS status;
345 };
346
347 static void g_lock_lock_fn(struct db_record *rec, void *private_data)
348 {
349         struct g_lock_lock_fn_state *state = private_data;
350         struct server_id blocker;
351
352         state->status = g_lock_trylock(rec, state->self, state->state->type,
353                                        &blocker);
354         if (!NT_STATUS_EQUAL(state->status, NT_STATUS_LOCK_NOT_GRANTED)) {
355                 return;
356         }
357
358         state->watch_req = dbwrap_watched_watch_send(
359                 state->state, state->state->ev, rec, blocker);
360 }
361
362 struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
363                                     struct tevent_context *ev,
364                                     struct g_lock_ctx *ctx,
365                                     TDB_DATA key,
366                                     enum g_lock_type type)
367 {
368         struct tevent_req *req;
369         struct g_lock_lock_state *state;
370         struct g_lock_lock_fn_state fn_state;
371         NTSTATUS status;
372
373         req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
374         if (req == NULL) {
375                 return NULL;
376         }
377         state->ev = ev;
378         state->ctx = ctx;
379         state->key = key;
380         state->type = type;
381
382         fn_state = (struct g_lock_lock_fn_state) {
383                 .state = state, .self = messaging_server_id(ctx->msg)
384         };
385
386         status = dbwrap_do_locked(ctx->db, key, g_lock_lock_fn, &fn_state);
387         if (tevent_req_nterror(req, status)) {
388                 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
389                           nt_errstr(status));
390                 return tevent_req_post(req, ev);
391         }
392
393         if (NT_STATUS_IS_OK(fn_state.status)) {
394                 tevent_req_done(req);
395                 return tevent_req_post(req, ev);
396         }
397         if (!NT_STATUS_EQUAL(fn_state.status, NT_STATUS_LOCK_NOT_GRANTED)) {
398                 tevent_req_nterror(req, fn_state.status);
399                 return tevent_req_post(req, ev);
400         }
401
402         if (tevent_req_nomem(fn_state.watch_req, req)) {
403                 return tevent_req_post(req, ev);
404         }
405
406         if (!tevent_req_set_endtime(
407                     fn_state.watch_req, state->ev,
408                     timeval_current_ofs(5 + sys_random() % 5, 0))) {
409                 return tevent_req_post(req, ev);
410         }
411         tevent_req_set_callback(fn_state.watch_req, g_lock_lock_retry, req);
412         return req;
413 }
414
415 static void g_lock_lock_retry(struct tevent_req *subreq)
416 {
417         struct tevent_req *req = tevent_req_callback_data(
418                 subreq, struct tevent_req);
419         struct g_lock_lock_state *state = tevent_req_data(
420                 req, struct g_lock_lock_state);
421         struct g_lock_lock_fn_state fn_state;
422         NTSTATUS status;
423
424         status = dbwrap_watched_watch_recv(subreq, NULL, NULL);
425         DBG_DEBUG("watch_recv returned %s\n", nt_errstr(status));
426         TALLOC_FREE(subreq);
427
428         if (!NT_STATUS_IS_OK(status) &&
429             !NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
430                 tevent_req_nterror(req, status);
431                 return;
432         }
433
434         fn_state = (struct g_lock_lock_fn_state) {
435                 .state = state, .self = messaging_server_id(state->ctx->msg)
436         };
437
438         status = dbwrap_do_locked(state->ctx->db, state->key,
439                                   g_lock_lock_fn, &fn_state);
440         if (tevent_req_nterror(req, status)) {
441                 DBG_DEBUG("dbwrap_do_locked failed: %s\n",
442                           nt_errstr(status));
443                 return;
444         }
445
446         if (NT_STATUS_IS_OK(fn_state.status)) {
447                 tevent_req_done(req);
448                 return;
449         }
450         if (!NT_STATUS_EQUAL(fn_state.status, NT_STATUS_LOCK_NOT_GRANTED)) {
451                 tevent_req_nterror(req, fn_state.status);
452                 return;
453         }
454
455         if (tevent_req_nomem(fn_state.watch_req, req)) {
456                 return;
457         }
458
459         if (!tevent_req_set_endtime(
460                     fn_state.watch_req, state->ev,
461                     timeval_current_ofs(5 + sys_random() % 5, 0))) {
462                 return;
463         }
464         tevent_req_set_callback(fn_state.watch_req, g_lock_lock_retry, req);
465 }
466
467 NTSTATUS g_lock_lock_recv(struct tevent_req *req)
468 {
469         return tevent_req_simple_recv_ntstatus(req);
470 }
471
472 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, TDB_DATA key,
473                      enum g_lock_type type, struct timeval timeout)
474 {
475         TALLOC_CTX *frame = talloc_stackframe();
476         struct tevent_context *ev;
477         struct tevent_req *req;
478         struct timeval end;
479         NTSTATUS status = NT_STATUS_NO_MEMORY;
480
481         ev = samba_tevent_context_init(frame);
482         if (ev == NULL) {
483                 goto fail;
484         }
485         req = g_lock_lock_send(frame, ev, ctx, key, type);
486         if (req == NULL) {
487                 goto fail;
488         }
489         end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
490         if (!tevent_req_set_endtime(req, ev, end)) {
491                 goto fail;
492         }
493         if (!tevent_req_poll_ntstatus(req, ev, &status)) {
494                 goto fail;
495         }
496         status = g_lock_lock_recv(req);
497  fail:
498         TALLOC_FREE(frame);
499         return status;
500 }
501
502 struct g_lock_unlock_state {
503         TDB_DATA key;
504         struct server_id self;
505         NTSTATUS status;
506 };
507
508 static void g_lock_unlock_fn(struct db_record *rec,
509                              void *private_data)
510 {
511         struct g_lock_unlock_state *state = private_data;
512         TDB_DATA value;
513         struct g_lock lck;
514         size_t i;
515         bool ok;
516
517         value = dbwrap_record_get_value(rec);
518
519         ok = g_lock_parse(value.dptr, value.dsize, &lck);
520         if (!ok) {
521                 DBG_DEBUG("g_lock_parse for %s failed\n",
522                           hex_encode_talloc(talloc_tos(),
523                                             state->key.dptr,
524                                             state->key.dsize));
525                 state->status = NT_STATUS_FILE_INVALID;
526                 return;
527         }
528         for (i=0; i<lck.num_recs; i++) {
529                 struct g_lock_rec lockrec;
530                 g_lock_get_rec(&lck, i, &lockrec);
531                 if (serverid_equal(&state->self, &lockrec.pid)) {
532                         break;
533                 }
534         }
535         if (i == lck.num_recs) {
536                 DBG_DEBUG("Lock not found, num_rec=%zu\n", lck.num_recs);
537                 state->status = NT_STATUS_NOT_FOUND;
538                 return;
539         }
540
541         g_lock_rec_del(&lck, i);
542
543         if ((lck.num_recs == 0) && (lck.datalen == 0)) {
544                 state->status = dbwrap_record_delete(rec);
545                 return;
546         }
547         state->status = g_lock_store(rec, &lck, NULL);
548 }
549
550 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, TDB_DATA key)
551 {
552         struct g_lock_unlock_state state = {
553                 .self = messaging_server_id(ctx->msg), .key = key
554         };
555         NTSTATUS status;
556
557         status = dbwrap_do_locked(ctx->db, key, g_lock_unlock_fn, &state);
558         if (!NT_STATUS_IS_OK(status)) {
559                 DBG_WARNING("dbwrap_do_locked failed: %s\n",
560                             nt_errstr(status));
561                 return status;
562         }
563         if (!NT_STATUS_IS_OK(state.status)) {
564                 DBG_WARNING("g_lock_unlock_fn failed: %s\n",
565                             nt_errstr(state.status));
566                 return state.status;
567         }
568
569         return NT_STATUS_OK;
570 }
571
572 struct g_lock_write_data_state {
573         TDB_DATA key;
574         struct server_id self;
575         const uint8_t *data;
576         size_t datalen;
577         NTSTATUS status;
578 };
579
580 static void g_lock_write_data_fn(struct db_record *rec,
581                                  void *private_data)
582 {
583         struct g_lock_write_data_state *state = private_data;
584         TDB_DATA value;
585         struct g_lock lck;
586         size_t i;
587         bool ok;
588
589         value = dbwrap_record_get_value(rec);
590
591         ok = g_lock_parse(value.dptr, value.dsize, &lck);
592         if (!ok) {
593                 DBG_DEBUG("g_lock_parse for %s failed\n",
594                           hex_encode_talloc(talloc_tos(),
595                                             state->key.dptr,
596                                             state->key.dsize));
597                 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
598                 return;
599         }
600         for (i=0; i<lck.num_recs; i++) {
601                 struct g_lock_rec lockrec;
602                 g_lock_get_rec(&lck, i, &lockrec);
603                 if ((lockrec.lock_type == G_LOCK_WRITE) &&
604                     serverid_equal(&state->self, &lockrec.pid)) {
605                         break;
606                 }
607         }
608         if (i == lck.num_recs) {
609                 DBG_DEBUG("Not locked by us\n");
610                 state->status = NT_STATUS_NOT_LOCKED;
611                 return;
612         }
613
614         lck.data = discard_const_p(uint8_t, state->data);
615         lck.datalen = state->datalen;
616         state->status = g_lock_store(rec, &lck, NULL);
617 }
618
619 NTSTATUS g_lock_write_data(struct g_lock_ctx *ctx, TDB_DATA key,
620                            const uint8_t *buf, size_t buflen)
621 {
622         struct g_lock_write_data_state state = {
623                 .key = key, .self = messaging_server_id(ctx->msg),
624                 .data = buf, .datalen = buflen
625         };
626         NTSTATUS status;
627
628         status = dbwrap_do_locked(ctx->db, key,
629                                   g_lock_write_data_fn, &state);
630         if (!NT_STATUS_IS_OK(status)) {
631                 DBG_WARNING("dbwrap_do_locked failed: %s\n",
632                             nt_errstr(status));
633                 return status;
634         }
635         if (!NT_STATUS_IS_OK(state.status)) {
636                 DBG_WARNING("g_lock_write_data_fn failed: %s\n",
637                             nt_errstr(state.status));
638                 return state.status;
639         }
640
641         return NT_STATUS_OK;
642 }
643
644 struct g_lock_locks_state {
645         int (*fn)(TDB_DATA key, void *private_data);
646         void *private_data;
647 };
648
649 static int g_lock_locks_fn(struct db_record *rec, void *priv)
650 {
651         TDB_DATA key;
652         struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
653
654         key = dbwrap_record_get_key(rec);
655         return state->fn(key, state->private_data);
656 }
657
658 int g_lock_locks(struct g_lock_ctx *ctx,
659                  int (*fn)(TDB_DATA key, void *private_data),
660                  void *private_data)
661 {
662         struct g_lock_locks_state state;
663         NTSTATUS status;
664         int count;
665
666         state.fn = fn;
667         state.private_data = private_data;
668
669         status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
670         if (!NT_STATUS_IS_OK(status)) {
671                 return -1;
672         }
673         return count;
674 }
675
676 struct g_lock_dump_state {
677         TALLOC_CTX *mem_ctx;
678         TDB_DATA key;
679         void (*fn)(const struct g_lock_rec *locks,
680                    size_t num_locks,
681                    const uint8_t *data,
682                    size_t datalen,
683                    void *private_data);
684         void *private_data;
685         NTSTATUS status;
686 };
687
688 static void g_lock_dump_fn(TDB_DATA key, TDB_DATA data,
689                            void *private_data)
690 {
691         struct g_lock_dump_state *state = private_data;
692         struct g_lock_rec *recs;
693         struct g_lock lck;
694         size_t i;
695         bool ok;
696
697         ok = g_lock_parse(data.dptr, data.dsize, &lck);
698         if (!ok) {
699                 DBG_DEBUG("g_lock_parse failed for %s\n",
700                           hex_encode_talloc(talloc_tos(),
701                                             state->key.dptr,
702                                             state->key.dsize));
703                 state->status = NT_STATUS_INTERNAL_DB_CORRUPTION;
704                 return;
705         }
706
707         recs = talloc_array(state->mem_ctx, struct g_lock_rec, lck.num_recs);
708         if (recs == NULL) {
709                 DBG_DEBUG("talloc failed\n");
710                 state->status = NT_STATUS_NO_MEMORY;
711                 return;
712         }
713
714         for (i=0; i<lck.num_recs; i++) {
715                 g_lock_get_rec(&lck, i, &recs[i]);
716         }
717
718         state->fn(recs, lck.num_recs, lck.data, lck.datalen,
719                   state->private_data);
720
721         TALLOC_FREE(recs);
722
723         state->status = NT_STATUS_OK;
724 }
725
726 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, TDB_DATA key,
727                      void (*fn)(const struct g_lock_rec *locks,
728                                 size_t num_locks,
729                                 const uint8_t *data,
730                                 size_t datalen,
731                                 void *private_data),
732                      void *private_data)
733 {
734         struct g_lock_dump_state state = {
735                 .mem_ctx = ctx, .key = key,
736                 .fn = fn, .private_data = private_data
737         };
738         NTSTATUS status;
739
740         status = dbwrap_parse_record(ctx->db, key, g_lock_dump_fn, &state);
741         if (!NT_STATUS_IS_OK(status)) {
742                 DBG_DEBUG("dbwrap_parse_record returned %s\n",
743                           nt_errstr(status));
744                 return status;
745         }
746         if (!NT_STATUS_IS_OK(state.status)) {
747                 DBG_DEBUG("g_lock_dump_fn returned %s\n",
748                           nt_errstr(state.status));
749                 return state.status;
750         }
751         return NT_STATUS_OK;
752 }
753
754 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
755                             struct tevent_context **pev,
756                             struct messaging_context **pmsg,
757                             struct g_lock_ctx **pg_ctx)
758 {
759         struct tevent_context *ev = NULL;
760         struct messaging_context *msg = NULL;
761         struct g_lock_ctx *g_ctx = NULL;
762
763         ev = samba_tevent_context_init(mem_ctx);
764         if (ev == NULL) {
765                 d_fprintf(stderr, "ERROR: could not init event context\n");
766                 goto fail;
767         }
768         msg = messaging_init(mem_ctx, ev);
769         if (msg == NULL) {
770                 d_fprintf(stderr, "ERROR: could not init messaging context\n");
771                 goto fail;
772         }
773         g_ctx = g_lock_ctx_init(mem_ctx, msg);
774         if (g_ctx == NULL) {
775                 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
776                 goto fail;
777         }
778
779         *pev = ev;
780         *pmsg = msg;
781         *pg_ctx = g_ctx;
782         return true;
783 fail:
784         TALLOC_FREE(g_ctx);
785         TALLOC_FREE(msg);
786         TALLOC_FREE(ev);
787         return false;
788 }
789
790 NTSTATUS g_lock_do(TDB_DATA key, enum g_lock_type lock_type,
791                    struct timeval timeout,
792                    void (*fn)(void *private_data), void *private_data)
793 {
794         struct tevent_context *ev = NULL;
795         struct messaging_context *msg = NULL;
796         struct g_lock_ctx *g_ctx = NULL;
797         NTSTATUS status;
798
799         if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
800                 status = NT_STATUS_ACCESS_DENIED;
801                 goto done;
802         }
803
804         status = g_lock_lock(g_ctx, key, lock_type, timeout);
805         if (!NT_STATUS_IS_OK(status)) {
806                 goto done;
807         }
808         fn(private_data);
809         g_lock_unlock(g_ctx, key);
810
811 done:
812         TALLOC_FREE(g_ctx);
813         TALLOC_FREE(msg);
814         TALLOC_FREE(ev);
815         return status;
816 }