s3: Call printer_list_parent_init in the parent
[Samba/gbeck.git] / source3 / lib / g_lock.c
blob378e464b5ce3732143def20524c4170d9e6ff793
1 /*
2 Unix SMB/CIFS implementation.
3 global locks based on dbwrap and messaging
4 Copyright (C) 2009 by Volker Lendecke
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.
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.
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/>.
20 #include "includes.h"
21 #include "g_lock.h"
22 #include "librpc/gen_ndr/messaging.h"
23 #include "ctdbd_conn.h"
24 #include "../lib/util/select.h"
26 static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
27 struct server_id pid);
29 struct g_lock_ctx {
30 struct db_context *db;
31 struct messaging_context *msg;
35 * The "g_lock.tdb" file contains records, indexed by the 0-terminated
36 * lockname. The record contains an array of "struct g_lock_rec"
37 * structures. Waiters have the lock_type with G_LOCK_PENDING or'ed.
40 struct g_lock_rec {
41 enum g_lock_type lock_type;
42 struct server_id pid;
45 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
46 struct messaging_context *msg)
48 struct g_lock_ctx *result;
50 result = talloc(mem_ctx, struct g_lock_ctx);
51 if (result == NULL) {
52 return NULL;
54 result->msg = msg;
56 result->db = db_open(result, lock_path("g_lock.tdb"), 0,
57 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH, O_RDWR|O_CREAT, 0700);
58 if (result->db == NULL) {
59 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb"));
60 TALLOC_FREE(result);
61 return NULL;
63 return result;
66 static bool g_lock_conflicts(enum g_lock_type lock_type,
67 const struct g_lock_rec *rec)
69 enum g_lock_type rec_lock = rec->lock_type;
71 if ((rec_lock & G_LOCK_PENDING) != 0) {
72 return false;
76 * Only tested write locks so far. Very likely this routine
77 * needs to be fixed for read locks....
79 if ((lock_type == G_LOCK_READ) && (rec_lock == G_LOCK_READ)) {
80 return false;
82 return true;
85 static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
86 int *pnum_locks, struct g_lock_rec **plocks)
88 int i, num_locks;
89 struct g_lock_rec *locks;
91 if ((data.dsize % sizeof(struct g_lock_rec)) != 0) {
92 DEBUG(1, ("invalid lock record length %d\n", (int)data.dsize));
93 return false;
96 num_locks = data.dsize / sizeof(struct g_lock_rec);
97 locks = talloc_array(mem_ctx, struct g_lock_rec, num_locks);
98 if (locks == NULL) {
99 DEBUG(1, ("talloc failed\n"));
100 return false;
103 memcpy(locks, data.dptr, data.dsize);
105 DEBUG(10, ("locks:\n"));
106 for (i=0; i<num_locks; i++) {
107 DEBUGADD(10, ("%s: %s %s\n",
108 procid_str(talloc_tos(), &locks[i].pid),
109 ((locks[i].lock_type & 1) == G_LOCK_READ) ?
110 "read" : "write",
111 (locks[i].lock_type & G_LOCK_PENDING) ?
112 "(pending)" : "(owner)"));
114 if (((locks[i].lock_type & G_LOCK_PENDING) == 0)
115 && !process_exists(locks[i].pid)) {
117 DEBUGADD(10, ("lock owner %s died -- discarding\n",
118 procid_str(talloc_tos(),
119 &locks[i].pid)));
121 if (i < (num_locks-1)) {
122 locks[i] = locks[num_locks-1];
124 num_locks -= 1;
128 *plocks = locks;
129 *pnum_locks = num_locks;
130 return true;
133 static void g_lock_cleanup(int *pnum_locks, struct g_lock_rec *locks)
135 int i, num_locks;
137 num_locks = *pnum_locks;
139 DEBUG(10, ("g_lock_cleanup: %d locks\n", num_locks));
141 for (i=0; i<num_locks; i++) {
142 if (process_exists(locks[i].pid)) {
143 continue;
145 DEBUGADD(10, ("%s does not exist -- discarding\n",
146 procid_str(talloc_tos(), &locks[i].pid)));
148 if (i < (num_locks-1)) {
149 locks[i] = locks[num_locks-1];
151 num_locks -= 1;
153 *pnum_locks = num_locks;
154 return;
157 static struct g_lock_rec *g_lock_addrec(TALLOC_CTX *mem_ctx,
158 struct g_lock_rec *locks,
159 int *pnum_locks,
160 const struct server_id pid,
161 enum g_lock_type lock_type)
163 struct g_lock_rec *result;
164 int num_locks = *pnum_locks;
166 result = talloc_realloc(mem_ctx, locks, struct g_lock_rec,
167 num_locks+1);
168 if (result == NULL) {
169 return NULL;
172 result[num_locks].pid = pid;
173 result[num_locks].lock_type = lock_type;
174 *pnum_locks += 1;
175 return result;
178 static void g_lock_got_retry(struct messaging_context *msg,
179 void *private_data,
180 uint32_t msg_type,
181 struct server_id server_id,
182 DATA_BLOB *data);
184 static NTSTATUS g_lock_trylock(struct g_lock_ctx *ctx, const char *name,
185 enum g_lock_type lock_type)
187 struct db_record *rec = NULL;
188 struct g_lock_rec *locks = NULL;
189 int i, num_locks;
190 struct server_id self;
191 int our_index;
192 TDB_DATA data;
193 NTSTATUS status = NT_STATUS_OK;
194 NTSTATUS store_status;
196 again:
197 rec = ctx->db->fetch_locked(ctx->db, talloc_tos(),
198 string_term_tdb_data(name));
199 if (rec == NULL) {
200 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
201 status = NT_STATUS_LOCK_NOT_GRANTED;
202 goto done;
205 if (!g_lock_parse(talloc_tos(), rec->value, &num_locks, &locks)) {
206 DEBUG(10, ("g_lock_parse for %s failed\n", name));
207 status = NT_STATUS_INTERNAL_ERROR;
208 goto done;
211 self = messaging_server_id(ctx->msg);
212 our_index = -1;
214 for (i=0; i<num_locks; i++) {
215 if (procid_equal(&self, &locks[i].pid)) {
216 if (our_index != -1) {
217 DEBUG(1, ("g_lock_trylock: Added ourself "
218 "twice!\n"));
219 status = NT_STATUS_INTERNAL_ERROR;
220 goto done;
222 if ((locks[i].lock_type & G_LOCK_PENDING) == 0) {
223 DEBUG(1, ("g_lock_trylock: Found ourself not "
224 "pending!\n"));
225 status = NT_STATUS_INTERNAL_ERROR;
226 goto done;
229 our_index = i;
231 /* never conflict with ourself */
232 continue;
234 if (g_lock_conflicts(lock_type, &locks[i])) {
235 struct server_id pid = locks[i].pid;
237 if (!process_exists(pid)) {
238 TALLOC_FREE(locks);
239 TALLOC_FREE(rec);
240 status = g_lock_force_unlock(ctx, name, pid);
241 if (!NT_STATUS_IS_OK(status)) {
242 DEBUG(1, ("Could not unlock dead lock "
243 "holder!\n"));
244 goto done;
246 goto again;
248 lock_type |= G_LOCK_PENDING;
252 if (our_index == -1) {
253 /* First round, add ourself */
255 locks = g_lock_addrec(talloc_tos(), locks, &num_locks,
256 self, lock_type);
257 if (locks == NULL) {
258 DEBUG(10, ("g_lock_addrec failed\n"));
259 status = NT_STATUS_NO_MEMORY;
260 goto done;
262 } else {
264 * Retry. We were pending last time. Overwrite the
265 * stored lock_type with what we calculated, we might
266 * have acquired the lock this time.
268 locks[our_index].lock_type = lock_type;
271 if (NT_STATUS_IS_OK(status) && ((lock_type & G_LOCK_PENDING) == 0)) {
273 * Walk through the list of locks, search for dead entries
275 g_lock_cleanup(&num_locks, locks);
278 data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
279 store_status = rec->store(rec, data, 0);
280 if (!NT_STATUS_IS_OK(store_status)) {
281 DEBUG(1, ("rec->store failed: %s\n",
282 nt_errstr(store_status)));
283 status = store_status;
286 done:
287 TALLOC_FREE(locks);
288 TALLOC_FREE(rec);
290 if (NT_STATUS_IS_OK(status) && (lock_type & G_LOCK_PENDING) != 0) {
291 return STATUS_PENDING;
294 return NT_STATUS_OK;
297 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
298 enum g_lock_type lock_type, struct timeval timeout)
300 struct tevent_timer *te = NULL;
301 NTSTATUS status;
302 bool retry = false;
303 struct timeval timeout_end;
304 struct timeval time_now;
306 DEBUG(10, ("Trying to acquire lock %d for %s\n", (int)lock_type,
307 name));
309 if (lock_type & ~1) {
310 DEBUG(1, ("Got invalid lock type %d for %s\n",
311 (int)lock_type, name));
312 return NT_STATUS_INVALID_PARAMETER;
315 #ifdef CLUSTER_SUPPORT
316 if (lp_clustering()) {
317 status = ctdb_watch_us(messaging_ctdbd_connection());
318 if (!NT_STATUS_IS_OK(status)) {
319 DEBUG(10, ("could not register retry with ctdb: %s\n",
320 nt_errstr(status)));
321 goto done;
324 #endif
326 status = messaging_register(ctx->msg, &retry, MSG_DBWRAP_G_LOCK_RETRY,
327 g_lock_got_retry);
328 if (!NT_STATUS_IS_OK(status)) {
329 DEBUG(10, ("messaging_register failed: %s\n",
330 nt_errstr(status)));
331 return status;
334 time_now = timeval_current();
335 timeout_end = timeval_sum(&time_now, &timeout);
337 while (true) {
338 #ifdef CLUSTER_SUPPORT
339 fd_set _r_fds;
340 #endif
341 fd_set *r_fds = NULL;
342 int max_fd = 0;
343 int ret;
344 struct timeval timeout_remaining, select_timeout;
346 status = g_lock_trylock(ctx, name, lock_type);
347 if (NT_STATUS_IS_OK(status)) {
348 DEBUG(10, ("Got lock %s\n", name));
349 break;
351 if (!NT_STATUS_EQUAL(status, STATUS_PENDING)) {
352 DEBUG(10, ("g_lock_trylock failed: %s\n",
353 nt_errstr(status)));
354 break;
357 DEBUG(10, ("g_lock_trylock: Did not get lock, waiting...\n"));
359 /* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360 * !!! HACK ALERT --- FIX ME !!!
361 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
362 * What we really want to do here is to react to
363 * MSG_DBWRAP_G_LOCK_RETRY messages that are either sent
364 * by a client doing g_lock_unlock or by ourselves when
365 * we receive a CTDB_SRVID_SAMBA_NOTIFY or
366 * CTDB_SRVID_RECONFIGURE message from ctdbd, i.e. when
367 * either a client holding a lock or a complete node
368 * has died.
370 * Doing this properly involves calling tevent_loop_once(),
371 * but doing this here with the main ctdbd messaging context
372 * creates a nested event loop when g_lock_lock() is called
373 * from the main event loop, e.g. in a tcon_and_X where the
374 * share_info.tdb needs to be initialized and is locked by
375 * another process, or when the remore registry is accessed
376 * for writing and some other process already holds a lock
377 * on the registry.tdb.
379 * So as a quick fix, we act a little coarsely here: we do
380 * a select on the ctdb connection fd and when it is readable
381 * or we get EINTR, then we retry without actually parsing
382 * any ctdb packages or dispatching messages. This means that
383 * we retry more often than intended by design, but this does
384 * not harm and it is unobtrusive. When we have finished,
385 * the main loop will pick up all the messages and ctdb
386 * packets. The only extra twist is that we cannot use timed
387 * events here but have to handcode a timeout.
390 #ifdef CLUSTER_SUPPORT
391 if (lp_clustering()) {
392 struct ctdbd_connection *conn;
393 conn = messaging_ctdbd_connection();
395 r_fds = &_r_fds;
396 FD_ZERO(r_fds);
397 max_fd = ctdbd_conn_get_fd(conn);
398 FD_SET(max_fd, r_fds);
400 #endif
402 time_now = timeval_current();
403 timeout_remaining = timeval_until(&time_now, &timeout_end);
404 select_timeout = timeval_set(60, 0);
406 select_timeout = timeval_min(&select_timeout,
407 &timeout_remaining);
409 ret = sys_select(max_fd + 1, r_fds, NULL, NULL,
410 &select_timeout);
411 if (ret == -1) {
412 if (errno != EINTR) {
413 DEBUG(1, ("error calling select: %s\n",
414 strerror(errno)));
415 status = NT_STATUS_INTERNAL_ERROR;
416 break;
419 * errno == EINTR:
420 * This means a signal was received.
421 * It might have been a MSG_DBWRAP_G_LOCK_RETRY message.
422 * ==> retry
424 } else if (ret == 0) {
425 if (timeval_expired(&timeout_end)) {
426 DEBUG(10, ("g_lock_lock timed out\n"));
427 status = NT_STATUS_LOCK_NOT_GRANTED;
428 break;
429 } else {
430 DEBUG(10, ("select returned 0 but timeout not "
431 "not expired, retrying\n"));
433 } else if (ret != 1) {
434 DEBUG(1, ("invalid return code of select: %d\n", ret));
435 status = NT_STATUS_INTERNAL_ERROR;
436 break;
439 * ret == 1:
440 * This means ctdbd has sent us some data.
441 * Might be a CTDB_SRVID_RECONFIGURE or a
442 * CTDB_SRVID_SAMBA_NOTIFY message.
443 * ==> retry
447 #ifdef CLUSTER_SUPPORT
448 done:
449 #endif
451 if (!NT_STATUS_IS_OK(status)) {
452 NTSTATUS unlock_status;
454 unlock_status = g_lock_unlock(ctx, name);
456 if (!NT_STATUS_IS_OK(unlock_status)) {
457 DEBUG(1, ("Could not remove ourself from the locking "
458 "db: %s\n", nt_errstr(status)));
462 messaging_deregister(ctx->msg, MSG_DBWRAP_G_LOCK_RETRY, &retry);
463 TALLOC_FREE(te);
465 return status;
468 static void g_lock_got_retry(struct messaging_context *msg,
469 void *private_data,
470 uint32_t msg_type,
471 struct server_id server_id,
472 DATA_BLOB *data)
474 bool *pretry = (bool *)private_data;
476 DEBUG(10, ("Got retry message from pid %s\n",
477 procid_str(talloc_tos(), &server_id)));
479 *pretry = true;
482 static NTSTATUS g_lock_force_unlock(struct g_lock_ctx *ctx, const char *name,
483 struct server_id pid)
485 struct db_record *rec = NULL;
486 struct g_lock_rec *locks = NULL;
487 int i, num_locks;
488 enum g_lock_type lock_type;
489 NTSTATUS status;
491 rec = ctx->db->fetch_locked(ctx->db, talloc_tos(),
492 string_term_tdb_data(name));
493 if (rec == NULL) {
494 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
495 status = NT_STATUS_INTERNAL_ERROR;
496 goto done;
499 if (!g_lock_parse(talloc_tos(), rec->value, &num_locks, &locks)) {
500 DEBUG(10, ("g_lock_parse for %s failed\n", name));
501 status = NT_STATUS_INTERNAL_ERROR;
502 goto done;
505 for (i=0; i<num_locks; i++) {
506 if (procid_equal(&pid, &locks[i].pid)) {
507 break;
511 if (i == num_locks) {
512 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
513 status = NT_STATUS_INTERNAL_ERROR;
514 goto done;
517 lock_type = locks[i].lock_type;
519 if (i < (num_locks-1)) {
520 locks[i] = locks[num_locks-1];
522 num_locks -= 1;
524 if (num_locks == 0) {
525 status = rec->delete_rec(rec);
526 } else {
527 TDB_DATA data;
528 data = make_tdb_data((uint8_t *)locks,
529 sizeof(struct g_lock_rec) * num_locks);
530 status = rec->store(rec, data, 0);
533 if (!NT_STATUS_IS_OK(status)) {
534 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
535 nt_errstr(status)));
536 goto done;
539 TALLOC_FREE(rec);
541 if ((lock_type & G_LOCK_PENDING) == 0) {
542 int num_wakeups = 0;
545 * We've been the lock holder. Others to retry. Don't
546 * tell all others to avoid a thundering herd. In case
547 * this leads to a complete stall because we miss some
548 * processes, the loop in g_lock_lock tries at least
549 * once a minute.
552 for (i=0; i<num_locks; i++) {
553 if ((locks[i].lock_type & G_LOCK_PENDING) == 0) {
554 continue;
556 if (!process_exists(locks[i].pid)) {
557 continue;
561 * Ping all waiters to retry
563 status = messaging_send(ctx->msg, locks[i].pid,
564 MSG_DBWRAP_G_LOCK_RETRY,
565 &data_blob_null);
566 if (!NT_STATUS_IS_OK(status)) {
567 DEBUG(1, ("sending retry to %s failed: %s\n",
568 procid_str(talloc_tos(),
569 &locks[i].pid),
570 nt_errstr(status)));
571 } else {
572 num_wakeups += 1;
574 if (num_wakeups > 5) {
575 break;
579 done:
581 * For the error path, TALLOC_FREE(rec) as well. In the good
582 * path we have already freed it.
584 TALLOC_FREE(rec);
586 TALLOC_FREE(locks);
587 return status;
590 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
592 NTSTATUS status;
594 status = g_lock_force_unlock(ctx, name, messaging_server_id(ctx->msg));
596 #ifdef CLUSTER_SUPPORT
597 if (lp_clustering()) {
598 ctdb_unwatch(messaging_ctdbd_connection());
600 #endif
601 return status;
604 struct g_lock_locks_state {
605 int (*fn)(const char *name, void *private_data);
606 void *private_data;
609 static int g_lock_locks_fn(struct db_record *rec, void *priv)
611 struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
613 if ((rec->key.dsize == 0) || (rec->key.dptr[rec->key.dsize-1] != 0)) {
614 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
615 return 0;
617 return state->fn((char *)rec->key.dptr, state->private_data);
620 int g_lock_locks(struct g_lock_ctx *ctx,
621 int (*fn)(const char *name, void *private_data),
622 void *private_data)
624 struct g_lock_locks_state state;
626 state.fn = fn;
627 state.private_data = private_data;
629 return ctx->db->traverse_read(ctx->db, g_lock_locks_fn, &state);
632 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
633 int (*fn)(struct server_id pid,
634 enum g_lock_type lock_type,
635 void *private_data),
636 void *private_data)
638 TDB_DATA data;
639 int i, num_locks;
640 struct g_lock_rec *locks = NULL;
641 bool ret;
643 if (ctx->db->fetch(ctx->db, talloc_tos(), string_term_tdb_data(name),
644 &data) != 0) {
645 return NT_STATUS_NOT_FOUND;
648 if ((data.dsize == 0) || (data.dptr == NULL)) {
649 return NT_STATUS_OK;
652 ret = g_lock_parse(talloc_tos(), data, &num_locks, &locks);
654 TALLOC_FREE(data.dptr);
656 if (!ret) {
657 DEBUG(10, ("g_lock_parse for %s failed\n", name));
658 return NT_STATUS_INTERNAL_ERROR;
661 for (i=0; i<num_locks; i++) {
662 if (fn(locks[i].pid, locks[i].lock_type, private_data) != 0) {
663 break;
666 TALLOC_FREE(locks);
667 return NT_STATUS_OK;
670 struct g_lock_get_state {
671 bool found;
672 struct server_id *pid;
675 static int g_lock_get_fn(struct server_id pid, enum g_lock_type lock_type,
676 void *priv)
678 struct g_lock_get_state *state = (struct g_lock_get_state *)priv;
680 if ((lock_type & G_LOCK_PENDING) != 0) {
681 return 0;
684 state->found = true;
685 *state->pid = pid;
686 return 1;
689 NTSTATUS g_lock_get(struct g_lock_ctx *ctx, const char *name,
690 struct server_id *pid)
692 struct g_lock_get_state state;
693 NTSTATUS status;
695 state.found = false;
696 state.pid = pid;
698 status = g_lock_dump(ctx, name, g_lock_get_fn, &state);
699 if (!NT_STATUS_IS_OK(status)) {
700 return status;
702 if (!state.found) {
703 return NT_STATUS_NOT_FOUND;
705 return NT_STATUS_OK;
708 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
709 struct tevent_context **pev,
710 struct messaging_context **pmsg,
711 const struct server_id self,
712 struct g_lock_ctx **pg_ctx)
714 struct tevent_context *ev = NULL;
715 struct messaging_context *msg = NULL;
716 struct g_lock_ctx *g_ctx = NULL;
718 ev = tevent_context_init(mem_ctx);
719 if (ev == NULL) {
720 d_fprintf(stderr, "ERROR: could not init event context\n");
721 goto fail;
723 msg = messaging_init(mem_ctx, self, ev);
724 if (msg == NULL) {
725 d_fprintf(stderr, "ERROR: could not init messaging context\n");
726 goto fail;
728 g_ctx = g_lock_ctx_init(mem_ctx, msg);
729 if (g_ctx == NULL) {
730 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
731 goto fail;
734 *pev = ev;
735 *pmsg = msg;
736 *pg_ctx = g_ctx;
737 return true;
738 fail:
739 TALLOC_FREE(g_ctx);
740 TALLOC_FREE(msg);
741 TALLOC_FREE(ev);
742 return false;
745 NTSTATUS g_lock_do(const char *name, enum g_lock_type lock_type,
746 struct timeval timeout, const struct server_id self,
747 void (*fn)(void *private_data), void *private_data)
749 struct tevent_context *ev = NULL;
750 struct messaging_context *msg = NULL;
751 struct g_lock_ctx *g_ctx = NULL;
752 NTSTATUS status;
754 if (!g_lock_init_all(talloc_tos(), &ev, &msg, self, &g_ctx)) {
755 status = NT_STATUS_ACCESS_DENIED;
756 goto done;
759 status = g_lock_lock(g_ctx, name, lock_type, timeout);
760 if (!NT_STATUS_IS_OK(status)) {
761 goto done;
763 fn(private_data);
764 g_lock_unlock(g_ctx, name);
766 done:
767 TALLOC_FREE(g_ctx);
768 TALLOC_FREE(msg);
769 TALLOC_FREE(ev);
770 return status;