ctdb-daemon: Do not allow database detach if AllowClientDBAttach=1
[Samba.git] / source3 / lib / g_lock.c
blob6813f0641f26f3925b4156ea9412d08cb3ca1a81
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 "system/filesys.h"
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_open.h"
24 #include "dbwrap/dbwrap_watch.h"
25 #include "g_lock.h"
26 #include "util_tdb.h"
27 #include "ctdbd_conn.h"
28 #include "../lib/util/select.h"
29 #include "../lib/util/tevent_ntstatus.h"
30 #include "system/select.h"
31 #include "messages.h"
32 #include "serverid.h"
34 struct g_lock_ctx {
35 struct db_context *db;
36 struct messaging_context *msg;
40 * The "g_lock.tdb" file contains records, indexed by the 0-terminated
41 * lockname. The record contains an array of "struct g_lock_rec"
42 * structures.
45 struct g_lock_rec {
46 enum g_lock_type lock_type;
47 struct server_id pid;
50 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
51 struct messaging_context *msg)
53 struct g_lock_ctx *result;
55 result = talloc(mem_ctx, struct g_lock_ctx);
56 if (result == NULL) {
57 return NULL;
59 result->msg = msg;
61 result->db = db_open(result, lock_path("g_lock.tdb"), 0,
62 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
63 O_RDWR|O_CREAT, 0600,
64 DBWRAP_LOCK_ORDER_2,
65 DBWRAP_FLAG_NONE);
66 if (result->db == NULL) {
67 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
68 TALLOC_FREE(result);
69 return NULL;
71 dbwrap_watch_db(result->db, msg);
72 return result;
75 static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
78 * Only tested write locks so far. Very likely this routine
79 * needs to be fixed for read locks....
81 if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
82 return false;
84 return true;
87 static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
88 unsigned *pnum_locks, struct g_lock_rec **plocks)
90 unsigned num_locks;
91 struct g_lock_rec *locks;
93 if ((data.dsize % sizeof(struct g_lock_rec)) != 0) {
94 DEBUG(1, ("invalid lock record length %d\n", (int)data.dsize));
95 return false;
97 num_locks = data.dsize / sizeof(struct g_lock_rec);
98 locks = talloc_memdup(mem_ctx, data.dptr, data.dsize);
99 if (locks == NULL) {
100 DEBUG(1, ("talloc_memdup failed\n"));
101 return false;
103 *plocks = locks;
104 *pnum_locks = num_locks;
105 return true;
108 static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
109 enum g_lock_type type)
111 TDB_DATA data;
112 unsigned i, num_locks;
113 struct g_lock_rec *locks, *tmp;
114 NTSTATUS status;
115 bool modified = false;
117 data = dbwrap_record_get_value(rec);
119 if (!g_lock_parse(talloc_tos(), data, &num_locks, &locks)) {
120 return NT_STATUS_INTERNAL_ERROR;
123 for (i=0; i<num_locks; i++) {
124 if (serverid_equal(&self, &locks[i].pid)) {
125 status = NT_STATUS_INTERNAL_ERROR;
126 goto done;
128 if (g_lock_conflicts(type, locks[i].lock_type)) {
129 struct server_id pid = locks[i].pid;
132 * As the serverid_exists might recurse into
133 * the g_lock code, we use
134 * SERVERID_UNIQUE_ID_NOT_TO_VERIFY to avoid the loop
136 pid.unique_id = SERVERID_UNIQUE_ID_NOT_TO_VERIFY;
138 if (serverid_exists(&pid)) {
139 status = NT_STATUS_LOCK_NOT_GRANTED;
140 goto done;
144 * Delete stale conflicting entry
146 locks[i] = locks[num_locks-1];
147 num_locks -= 1;
148 modified = true;
152 tmp = talloc_realloc(talloc_tos(), locks, struct g_lock_rec,
153 num_locks+1);
154 if (tmp == NULL) {
155 status = NT_STATUS_NO_MEMORY;
156 goto done;
158 locks = tmp;
160 ZERO_STRUCT(locks[num_locks]);
161 locks[num_locks].pid = self;
162 locks[num_locks].lock_type = type;
163 num_locks += 1;
164 modified = true;
166 status = NT_STATUS_OK;
167 done:
168 if (modified) {
169 NTSTATUS store_status;
171 data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
172 store_status = dbwrap_record_store(rec, data, 0);
173 if (!NT_STATUS_IS_OK(store_status)) {
174 DEBUG(1, ("rec->store failed: %s\n",
175 nt_errstr(store_status)));
176 status = store_status;
179 TALLOC_FREE(locks);
180 return status;
183 struct g_lock_lock_state {
184 struct tevent_context *ev;
185 struct g_lock_ctx *ctx;
186 const char *name;
187 enum g_lock_type type;
190 static void g_lock_lock_retry(struct tevent_req *subreq);
192 struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
193 struct tevent_context *ev,
194 struct g_lock_ctx *ctx,
195 const char *name,
196 enum g_lock_type type)
198 struct tevent_req *req, *subreq;
199 struct g_lock_lock_state *state;
200 struct db_record *rec;
201 struct server_id self;
202 NTSTATUS status;
204 req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
205 if (req == NULL) {
206 return NULL;
208 state->ev = ev;
209 state->ctx = ctx;
210 state->name = name;
211 state->type = type;
213 rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
214 string_term_tdb_data(state->name));
215 if (rec == NULL) {
216 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
217 tevent_req_nterror(req, NT_STATUS_LOCK_NOT_GRANTED);
218 return tevent_req_post(req, ev);
221 self = messaging_server_id(state->ctx->msg);
223 status = g_lock_trylock(rec, self, state->type);
224 if (NT_STATUS_IS_OK(status)) {
225 TALLOC_FREE(rec);
226 tevent_req_done(req);
227 return tevent_req_post(req, ev);
229 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
230 TALLOC_FREE(rec);
231 tevent_req_nterror(req, status);
232 return tevent_req_post(req, ev);
234 subreq = dbwrap_record_watch_send(state, state->ev, rec,
235 state->ctx->msg);
236 TALLOC_FREE(rec);
237 if (tevent_req_nomem(subreq, req)) {
238 return tevent_req_post(req, ev);
240 if (!tevent_req_set_endtime(
241 subreq, state->ev,
242 timeval_current_ofs(5 + sys_random() % 5, 0))) {
243 tevent_req_oom(req);
244 return tevent_req_post(req, ev);
246 tevent_req_set_callback(subreq, g_lock_lock_retry, req);
247 return req;
250 static void g_lock_lock_retry(struct tevent_req *subreq)
252 struct tevent_req *req = tevent_req_callback_data(
253 subreq, struct tevent_req);
254 struct g_lock_lock_state *state = tevent_req_data(
255 req, struct g_lock_lock_state);
256 struct server_id self = messaging_server_id(state->ctx->msg);
257 struct db_record *rec;
258 NTSTATUS status;
260 status = dbwrap_record_watch_recv(subreq, talloc_tos(), &rec);
261 TALLOC_FREE(subreq);
263 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
264 rec = dbwrap_fetch_locked(
265 state->ctx->db, talloc_tos(),
266 string_term_tdb_data(state->name));
267 if (rec == NULL) {
268 status = map_nt_error_from_unix(errno);
269 } else {
270 status = NT_STATUS_OK;
274 if (tevent_req_nterror(req, status)) {
275 return;
277 status = g_lock_trylock(rec, self, state->type);
278 if (NT_STATUS_IS_OK(status)) {
279 TALLOC_FREE(rec);
280 tevent_req_done(req);
281 return;
283 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
284 TALLOC_FREE(rec);
285 tevent_req_nterror(req, status);
286 return;
288 subreq = dbwrap_record_watch_send(state, state->ev, rec,
289 state->ctx->msg);
290 TALLOC_FREE(rec);
291 if (tevent_req_nomem(subreq, req)) {
292 return;
294 if (!tevent_req_set_endtime(
295 subreq, state->ev,
296 timeval_current_ofs(5 + sys_random() % 5, 0))) {
297 tevent_req_oom(req);
298 return;
300 tevent_req_set_callback(subreq, g_lock_lock_retry, req);
301 return;
305 NTSTATUS g_lock_lock_recv(struct tevent_req *req)
307 return tevent_req_simple_recv_ntstatus(req);
310 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
311 enum g_lock_type type, struct timeval timeout)
313 TALLOC_CTX *frame = talloc_stackframe();
314 struct tevent_context *ev;
315 struct tevent_req *req;
316 struct timeval end;
317 NTSTATUS status = NT_STATUS_NO_MEMORY;
319 ev = samba_tevent_context_init(frame);
320 if (ev == NULL) {
321 goto fail;
323 req = g_lock_lock_send(frame, ev, ctx, name, type);
324 if (req == NULL) {
325 goto fail;
327 end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
328 if (!tevent_req_set_endtime(req, ev, end)) {
329 goto fail;
331 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
332 goto fail;
334 status = g_lock_lock_recv(req);
335 fail:
336 TALLOC_FREE(frame);
337 return status;
340 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
342 struct server_id self = messaging_server_id(ctx->msg);
343 struct db_record *rec = NULL;
344 struct g_lock_rec *locks = NULL;
345 unsigned i, num_locks;
346 NTSTATUS status;
347 TDB_DATA value;
349 rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
350 string_term_tdb_data(name));
351 if (rec == NULL) {
352 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
353 status = NT_STATUS_INTERNAL_ERROR;
354 goto done;
357 value = dbwrap_record_get_value(rec);
359 if (!g_lock_parse(talloc_tos(), value, &num_locks, &locks)) {
360 DEBUG(10, ("g_lock_parse for %s failed\n", name));
361 status = NT_STATUS_FILE_INVALID;
362 goto done;
364 for (i=0; i<num_locks; i++) {
365 if (serverid_equal(&self, &locks[i].pid)) {
366 break;
369 if (i == num_locks) {
370 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
371 status = NT_STATUS_NOT_FOUND;
372 goto done;
375 locks[i] = locks[num_locks-1];
376 num_locks -= 1;
378 if (num_locks == 0) {
379 status = dbwrap_record_delete(rec);
380 } else {
381 TDB_DATA data;
382 data = make_tdb_data((uint8_t *)locks,
383 sizeof(struct g_lock_rec) * num_locks);
384 status = dbwrap_record_store(rec, data, 0);
386 if (!NT_STATUS_IS_OK(status)) {
387 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
388 nt_errstr(status)));
389 goto done;
392 status = NT_STATUS_OK;
393 done:
394 TALLOC_FREE(rec);
395 TALLOC_FREE(locks);
396 return status;
399 struct g_lock_locks_state {
400 int (*fn)(const char *name, void *private_data);
401 void *private_data;
404 static int g_lock_locks_fn(struct db_record *rec, void *priv)
406 TDB_DATA key;
407 struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
409 key = dbwrap_record_get_key(rec);
410 if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
411 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
412 return 0;
414 return state->fn((char *)key.dptr, state->private_data);
417 int g_lock_locks(struct g_lock_ctx *ctx,
418 int (*fn)(const char *name, void *private_data),
419 void *private_data)
421 struct g_lock_locks_state state;
422 NTSTATUS status;
423 int count;
425 state.fn = fn;
426 state.private_data = private_data;
428 status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
429 if (!NT_STATUS_IS_OK(status)) {
430 return -1;
431 } else {
432 return count;
436 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
437 int (*fn)(struct server_id pid,
438 enum g_lock_type lock_type,
439 void *private_data),
440 void *private_data)
442 TDB_DATA data;
443 unsigned i, num_locks;
444 struct g_lock_rec *locks = NULL;
445 bool ret;
446 NTSTATUS status;
448 status = dbwrap_fetch_bystring(ctx->db, talloc_tos(), name, &data);
449 if (!NT_STATUS_IS_OK(status)) {
450 return status;
453 if ((data.dsize == 0) || (data.dptr == NULL)) {
454 return NT_STATUS_OK;
457 ret = g_lock_parse(talloc_tos(), data, &num_locks, &locks);
459 TALLOC_FREE(data.dptr);
461 if (!ret) {
462 DEBUG(10, ("g_lock_parse for %s failed\n", name));
463 return NT_STATUS_INTERNAL_ERROR;
466 for (i=0; i<num_locks; i++) {
467 if (fn(locks[i].pid, locks[i].lock_type, private_data) != 0) {
468 break;
471 TALLOC_FREE(locks);
472 return NT_STATUS_OK;
475 struct g_lock_get_state {
476 bool found;
477 struct server_id *pid;
480 static int g_lock_get_fn(struct server_id pid, enum g_lock_type lock_type,
481 void *priv)
483 struct g_lock_get_state *state = (struct g_lock_get_state *)priv;
484 state->found = true;
485 *state->pid = pid;
486 return 1;
489 NTSTATUS g_lock_get(struct g_lock_ctx *ctx, const char *name,
490 struct server_id *pid)
492 struct g_lock_get_state state;
493 NTSTATUS status;
495 state.found = false;
496 state.pid = pid;
498 status = g_lock_dump(ctx, name, g_lock_get_fn, &state);
499 if (!NT_STATUS_IS_OK(status)) {
500 return status;
502 if (!state.found) {
503 return NT_STATUS_NOT_FOUND;
505 return NT_STATUS_OK;
508 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
509 struct tevent_context **pev,
510 struct messaging_context **pmsg,
511 struct g_lock_ctx **pg_ctx)
513 struct tevent_context *ev = NULL;
514 struct messaging_context *msg = NULL;
515 struct g_lock_ctx *g_ctx = NULL;
517 ev = samba_tevent_context_init(mem_ctx);
518 if (ev == NULL) {
519 d_fprintf(stderr, "ERROR: could not init event context\n");
520 goto fail;
522 msg = messaging_init(mem_ctx, ev);
523 if (msg == NULL) {
524 d_fprintf(stderr, "ERROR: could not init messaging context\n");
525 goto fail;
527 g_ctx = g_lock_ctx_init(mem_ctx, msg);
528 if (g_ctx == NULL) {
529 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
530 goto fail;
533 *pev = ev;
534 *pmsg = msg;
535 *pg_ctx = g_ctx;
536 return true;
537 fail:
538 TALLOC_FREE(g_ctx);
539 TALLOC_FREE(msg);
540 TALLOC_FREE(ev);
541 return false;
544 NTSTATUS g_lock_do(const char *name, enum g_lock_type lock_type,
545 struct timeval timeout,
546 void (*fn)(void *private_data), void *private_data)
548 struct tevent_context *ev = NULL;
549 struct messaging_context *msg = NULL;
550 struct g_lock_ctx *g_ctx = NULL;
551 NTSTATUS status;
553 if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
554 status = NT_STATUS_ACCESS_DENIED;
555 goto done;
558 status = g_lock_lock(g_ctx, name, lock_type, timeout);
559 if (!NT_STATUS_IS_OK(status)) {
560 goto done;
562 fn(private_data);
563 g_lock_unlock(g_ctx, name);
565 done:
566 TALLOC_FREE(g_ctx);
567 TALLOC_FREE(msg);
568 TALLOC_FREE(ev);
569 return status;