s3-g_lock: Make g_lock_lock more robust
[Samba/gebeck_regimport.git] / source3 / lib / g_lock.c
blob4535b355690a64fc418ed307a24a3e28960f0a91
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"
33 struct g_lock_ctx {
34 struct db_context *db;
35 struct messaging_context *msg;
39 * The "g_lock.tdb" file contains records, indexed by the 0-terminated
40 * lockname. The record contains an array of "struct g_lock_rec"
41 * structures.
44 struct g_lock_rec {
45 enum g_lock_type lock_type;
46 struct server_id pid;
49 struct g_lock_ctx *g_lock_ctx_init(TALLOC_CTX *mem_ctx,
50 struct messaging_context *msg)
52 struct g_lock_ctx *result;
54 result = talloc(mem_ctx, struct g_lock_ctx);
55 if (result == NULL) {
56 return NULL;
58 result->msg = msg;
60 result->db = db_open(result, lock_path("g_lock.tdb"), 0,
61 TDB_CLEAR_IF_FIRST|TDB_INCOMPATIBLE_HASH,
62 O_RDWR|O_CREAT, 0600,
63 DBWRAP_LOCK_ORDER_2);
64 if (result->db == NULL) {
65 DEBUG(1, ("g_lock_init: Could not open g_lock.tdb\n"));
66 TALLOC_FREE(result);
67 return NULL;
69 dbwrap_watch_db(result->db, msg);
70 return result;
73 static bool g_lock_conflicts(enum g_lock_type l1, enum g_lock_type l2)
76 * Only tested write locks so far. Very likely this routine
77 * needs to be fixed for read locks....
79 if ((l1 == G_LOCK_READ) && (l2 == G_LOCK_READ)) {
80 return false;
82 return true;
85 static bool g_lock_parse(TALLOC_CTX *mem_ctx, TDB_DATA data,
86 unsigned *pnum_locks, struct g_lock_rec **plocks)
88 unsigned 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;
95 num_locks = data.dsize / sizeof(struct g_lock_rec);
96 locks = talloc_memdup(mem_ctx, data.dptr, data.dsize);
97 if (locks == NULL) {
98 DEBUG(1, ("talloc_memdup failed\n"));
99 return false;
101 *plocks = locks;
102 *pnum_locks = num_locks;
103 return true;
106 static NTSTATUS g_lock_trylock(struct db_record *rec, struct server_id self,
107 enum g_lock_type type)
109 TDB_DATA data;
110 unsigned i, num_locks;
111 struct g_lock_rec *locks, *tmp;
112 NTSTATUS status;
113 bool modified = false;
115 data = dbwrap_record_get_value(rec);
117 if (!g_lock_parse(talloc_tos(), data, &num_locks, &locks)) {
118 return NT_STATUS_INTERNAL_ERROR;
121 for (i=0; i<num_locks; i++) {
122 if (serverid_equal(&self, &locks[i].pid)) {
123 status = NT_STATUS_INTERNAL_ERROR;
124 goto done;
126 if (g_lock_conflicts(type, locks[i].lock_type)) {
128 if (process_exists(locks[i].pid)) {
129 status = NT_STATUS_LOCK_NOT_GRANTED;
130 goto done;
134 * Delete stale conflicting entry
136 locks[i] = locks[num_locks-1];
137 num_locks -= 1;
138 modified = true;
142 tmp = talloc_realloc(talloc_tos(), locks, struct g_lock_rec,
143 num_locks+1);
144 if (tmp == NULL) {
145 status = NT_STATUS_NO_MEMORY;
146 goto done;
148 locks = tmp;
150 ZERO_STRUCT(locks[num_locks]);
151 locks[num_locks].pid = self;
152 locks[num_locks].lock_type = type;
153 num_locks += 1;
154 modified = true;
156 status = NT_STATUS_OK;
157 done:
158 if (modified) {
159 NTSTATUS store_status;
161 data = make_tdb_data((uint8_t *)locks, num_locks * sizeof(*locks));
162 store_status = dbwrap_record_store(rec, data, 0);
163 if (!NT_STATUS_IS_OK(store_status)) {
164 DEBUG(1, ("rec->store failed: %s\n",
165 nt_errstr(store_status)));
166 status = store_status;
169 TALLOC_FREE(locks);
170 return status;
173 struct g_lock_lock_state {
174 struct tevent_context *ev;
175 struct g_lock_ctx *ctx;
176 const char *name;
177 enum g_lock_type type;
180 static void g_lock_lock_retry(struct tevent_req *subreq);
182 struct tevent_req *g_lock_lock_send(TALLOC_CTX *mem_ctx,
183 struct tevent_context *ev,
184 struct g_lock_ctx *ctx,
185 const char *name,
186 enum g_lock_type type)
188 struct tevent_req *req, *subreq;
189 struct g_lock_lock_state *state;
190 struct db_record *rec;
191 struct server_id self;
192 NTSTATUS status;
194 req = tevent_req_create(mem_ctx, &state, struct g_lock_lock_state);
195 if (req == NULL) {
196 return NULL;
198 state->ev = ev;
199 state->ctx = ctx;
200 state->name = name;
201 state->type = type;
203 rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
204 string_term_tdb_data(state->name));
205 if (rec == NULL) {
206 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
207 tevent_req_nterror(req, NT_STATUS_LOCK_NOT_GRANTED);
208 return tevent_req_post(req, ev);
211 self = messaging_server_id(state->ctx->msg);
213 status = g_lock_trylock(rec, self, state->type);
214 if (NT_STATUS_IS_OK(status)) {
215 TALLOC_FREE(rec);
216 tevent_req_done(req);
217 return tevent_req_post(req, ev);
219 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
220 TALLOC_FREE(rec);
221 tevent_req_nterror(req, status);
222 return tevent_req_post(req, ev);
224 subreq = dbwrap_record_watch_send(state, state->ev, rec,
225 state->ctx->msg);
226 TALLOC_FREE(rec);
227 if (tevent_req_nomem(subreq, req)) {
228 return tevent_req_post(req, ev);
230 if (!tevent_req_set_endtime(
231 subreq, state->ev,
232 timeval_current_ofs(5 + sys_random() % 5, 0))) {
233 tevent_req_oom(req);
234 return tevent_req_post(req, ev);
236 tevent_req_set_callback(subreq, g_lock_lock_retry, req);
237 return req;
240 static void g_lock_lock_retry(struct tevent_req *subreq)
242 struct tevent_req *req = tevent_req_callback_data(
243 subreq, struct tevent_req);
244 struct g_lock_lock_state *state = tevent_req_data(
245 req, struct g_lock_lock_state);
246 struct server_id self = messaging_server_id(state->ctx->msg);
247 struct db_record *rec;
248 NTSTATUS status;
250 status = dbwrap_record_watch_recv(subreq, talloc_tos(), &rec);
251 TALLOC_FREE(subreq);
253 if (NT_STATUS_EQUAL(status, NT_STATUS_IO_TIMEOUT)) {
254 rec = dbwrap_fetch_locked(
255 state->ctx->db, talloc_tos(),
256 string_term_tdb_data(state->name));
257 if (rec == NULL) {
258 status = map_nt_error_from_unix(errno);
259 } else {
260 status = NT_STATUS_OK;
264 if (tevent_req_nterror(req, status)) {
265 return;
267 status = g_lock_trylock(rec, self, state->type);
268 if (NT_STATUS_IS_OK(status)) {
269 TALLOC_FREE(rec);
270 tevent_req_done(req);
271 return;
273 if (!NT_STATUS_EQUAL(status, NT_STATUS_LOCK_NOT_GRANTED)) {
274 TALLOC_FREE(rec);
275 tevent_req_nterror(req, status);
276 return;
278 subreq = dbwrap_record_watch_send(state, state->ev, rec,
279 state->ctx->msg);
280 TALLOC_FREE(rec);
281 if (tevent_req_nomem(subreq, req)) {
282 return;
284 if (!tevent_req_set_endtime(
285 subreq, state->ev,
286 timeval_current_ofs(5 + sys_random() % 5, 0))) {
287 tevent_req_oom(req);
288 return;
290 tevent_req_set_callback(subreq, g_lock_lock_retry, req);
291 return;
295 NTSTATUS g_lock_lock_recv(struct tevent_req *req)
297 return tevent_req_simple_recv_ntstatus(req);
300 NTSTATUS g_lock_lock(struct g_lock_ctx *ctx, const char *name,
301 enum g_lock_type type, struct timeval timeout)
303 TALLOC_CTX *frame = talloc_stackframe();
304 struct tevent_context *ev;
305 struct tevent_req *req;
306 struct timeval end;
307 NTSTATUS status = NT_STATUS_NO_MEMORY;
309 ev = tevent_context_init(frame);
310 if (ev == NULL) {
311 goto fail;
313 req = g_lock_lock_send(frame, ev, ctx, name, type);
314 if (req == NULL) {
315 goto fail;
317 end = timeval_current_ofs(timeout.tv_sec, timeout.tv_usec);
318 if (!tevent_req_set_endtime(req, ev, end)) {
319 goto fail;
321 if (!tevent_req_poll_ntstatus(req, ev, &status)) {
322 goto fail;
324 status = g_lock_lock_recv(req);
325 fail:
326 TALLOC_FREE(frame);
327 return status;
330 NTSTATUS g_lock_unlock(struct g_lock_ctx *ctx, const char *name)
332 struct server_id self = messaging_server_id(ctx->msg);
333 struct db_record *rec = NULL;
334 struct g_lock_rec *locks = NULL;
335 unsigned i, num_locks;
336 NTSTATUS status;
337 TDB_DATA value;
339 rec = dbwrap_fetch_locked(ctx->db, talloc_tos(),
340 string_term_tdb_data(name));
341 if (rec == NULL) {
342 DEBUG(10, ("fetch_locked(\"%s\") failed\n", name));
343 status = NT_STATUS_INTERNAL_ERROR;
344 goto done;
347 value = dbwrap_record_get_value(rec);
349 if (!g_lock_parse(talloc_tos(), value, &num_locks, &locks)) {
350 DEBUG(10, ("g_lock_parse for %s failed\n", name));
351 status = NT_STATUS_FILE_INVALID;
352 goto done;
354 for (i=0; i<num_locks; i++) {
355 if (serverid_equal(&self, &locks[i].pid)) {
356 break;
359 if (i == num_locks) {
360 DEBUG(10, ("g_lock_force_unlock: Lock not found\n"));
361 status = NT_STATUS_NOT_FOUND;
362 goto done;
365 locks[i] = locks[num_locks-1];
366 num_locks -= 1;
368 if (num_locks == 0) {
369 status = dbwrap_record_delete(rec);
370 } else {
371 TDB_DATA data;
372 data = make_tdb_data((uint8_t *)locks,
373 sizeof(struct g_lock_rec) * num_locks);
374 status = dbwrap_record_store(rec, data, 0);
376 if (!NT_STATUS_IS_OK(status)) {
377 DEBUG(1, ("g_lock_force_unlock: Could not store record: %s\n",
378 nt_errstr(status)));
379 goto done;
382 status = NT_STATUS_OK;
383 done:
384 TALLOC_FREE(rec);
385 TALLOC_FREE(locks);
386 return status;
389 struct g_lock_locks_state {
390 int (*fn)(const char *name, void *private_data);
391 void *private_data;
394 static int g_lock_locks_fn(struct db_record *rec, void *priv)
396 TDB_DATA key;
397 struct g_lock_locks_state *state = (struct g_lock_locks_state *)priv;
399 key = dbwrap_record_get_key(rec);
400 if ((key.dsize == 0) || (key.dptr[key.dsize-1] != 0)) {
401 DEBUG(1, ("invalid key in g_lock.tdb, ignoring\n"));
402 return 0;
404 return state->fn((char *)key.dptr, state->private_data);
407 int g_lock_locks(struct g_lock_ctx *ctx,
408 int (*fn)(const char *name, void *private_data),
409 void *private_data)
411 struct g_lock_locks_state state;
412 NTSTATUS status;
413 int count;
415 state.fn = fn;
416 state.private_data = private_data;
418 status = dbwrap_traverse_read(ctx->db, g_lock_locks_fn, &state, &count);
419 if (!NT_STATUS_IS_OK(status)) {
420 return -1;
421 } else {
422 return count;
426 NTSTATUS g_lock_dump(struct g_lock_ctx *ctx, const char *name,
427 int (*fn)(struct server_id pid,
428 enum g_lock_type lock_type,
429 void *private_data),
430 void *private_data)
432 TDB_DATA data;
433 unsigned i, num_locks;
434 struct g_lock_rec *locks = NULL;
435 bool ret;
436 NTSTATUS status;
438 status = dbwrap_fetch_bystring(ctx->db, talloc_tos(), name, &data);
439 if (!NT_STATUS_IS_OK(status)) {
440 return status;
443 if ((data.dsize == 0) || (data.dptr == NULL)) {
444 return NT_STATUS_OK;
447 ret = g_lock_parse(talloc_tos(), data, &num_locks, &locks);
449 TALLOC_FREE(data.dptr);
451 if (!ret) {
452 DEBUG(10, ("g_lock_parse for %s failed\n", name));
453 return NT_STATUS_INTERNAL_ERROR;
456 for (i=0; i<num_locks; i++) {
457 if (fn(locks[i].pid, locks[i].lock_type, private_data) != 0) {
458 break;
461 TALLOC_FREE(locks);
462 return NT_STATUS_OK;
465 struct g_lock_get_state {
466 bool found;
467 struct server_id *pid;
470 static int g_lock_get_fn(struct server_id pid, enum g_lock_type lock_type,
471 void *priv)
473 struct g_lock_get_state *state = (struct g_lock_get_state *)priv;
474 state->found = true;
475 *state->pid = pid;
476 return 1;
479 NTSTATUS g_lock_get(struct g_lock_ctx *ctx, const char *name,
480 struct server_id *pid)
482 struct g_lock_get_state state;
483 NTSTATUS status;
485 state.found = false;
486 state.pid = pid;
488 status = g_lock_dump(ctx, name, g_lock_get_fn, &state);
489 if (!NT_STATUS_IS_OK(status)) {
490 return status;
492 if (!state.found) {
493 return NT_STATUS_NOT_FOUND;
495 return NT_STATUS_OK;
498 static bool g_lock_init_all(TALLOC_CTX *mem_ctx,
499 struct tevent_context **pev,
500 struct messaging_context **pmsg,
501 struct g_lock_ctx **pg_ctx)
503 struct tevent_context *ev = NULL;
504 struct messaging_context *msg = NULL;
505 struct g_lock_ctx *g_ctx = NULL;
507 ev = tevent_context_init(mem_ctx);
508 if (ev == NULL) {
509 d_fprintf(stderr, "ERROR: could not init event context\n");
510 goto fail;
512 msg = messaging_init(mem_ctx, ev);
513 if (msg == NULL) {
514 d_fprintf(stderr, "ERROR: could not init messaging context\n");
515 goto fail;
517 g_ctx = g_lock_ctx_init(mem_ctx, msg);
518 if (g_ctx == NULL) {
519 d_fprintf(stderr, "ERROR: could not init g_lock context\n");
520 goto fail;
523 *pev = ev;
524 *pmsg = msg;
525 *pg_ctx = g_ctx;
526 return true;
527 fail:
528 TALLOC_FREE(g_ctx);
529 TALLOC_FREE(msg);
530 TALLOC_FREE(ev);
531 return false;
534 NTSTATUS g_lock_do(const char *name, enum g_lock_type lock_type,
535 struct timeval timeout,
536 void (*fn)(void *private_data), void *private_data)
538 struct tevent_context *ev = NULL;
539 struct messaging_context *msg = NULL;
540 struct g_lock_ctx *g_ctx = NULL;
541 NTSTATUS status;
543 if (!g_lock_init_all(talloc_tos(), &ev, &msg, &g_ctx)) {
544 status = NT_STATUS_ACCESS_DENIED;
545 goto done;
548 status = g_lock_lock(g_ctx, name, lock_type, timeout);
549 if (!NT_STATUS_IS_OK(status)) {
550 goto done;
552 fn(private_data);
553 g_lock_unlock(g_ctx, name);
555 done:
556 TALLOC_FREE(g_ctx);
557 TALLOC_FREE(msg);
558 TALLOC_FREE(ev);
559 return status;