mirror of
https://github.com/samba-team/samba.git
synced 2025-01-13 13:18:06 +03:00
542b76136e
- use the tdb_chainlock_mark() call to allow us to guarantee forward progress in the ctdb_lockwait code (This used to be ctdb commit e201e98aad0fef6a779a80f3b1ae7792953e2d6b)
154 lines
4.0 KiB
C
154 lines
4.0 KiB
C
/*
|
|
wait for a tdb chain lock
|
|
|
|
Copyright (C) Andrew Tridgell 2006
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
modify it under the terms of the GNU Lesser General Public
|
|
License as published by the Free Software Foundation; either
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
This library 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
|
|
Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
License along with this library; if not, write to the Free Software
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
*/
|
|
|
|
#include "includes.h"
|
|
#include "lib/events/events.h"
|
|
#include "system/filesys.h"
|
|
#include "system/wait.h"
|
|
#include "db_wrap.h"
|
|
#include "lib/tdb/include/tdb.h"
|
|
#include "../include/ctdb_private.h"
|
|
|
|
|
|
struct lockwait_handle {
|
|
struct ctdb_context *ctdb;
|
|
struct ctdb_db_context *ctdb_db;
|
|
struct fd_event *fde;
|
|
int fd[2];
|
|
pid_t child;
|
|
void *private_data;
|
|
void (*callback)(void *);
|
|
TDB_DATA key;
|
|
struct timeval start_time;
|
|
};
|
|
|
|
static void lockwait_handler(struct event_context *ev, struct fd_event *fde,
|
|
uint16_t flags, void *private_data)
|
|
{
|
|
struct lockwait_handle *h = talloc_get_type(private_data,
|
|
struct lockwait_handle);
|
|
void (*callback)(void *) = h->callback;
|
|
void *p = h->private_data;
|
|
pid_t child = h->child;
|
|
TDB_DATA key = h->key;
|
|
struct tdb_context *tdb = h->ctdb_db->ltdb->tdb;
|
|
TALLOC_CTX *tmp_ctx = talloc_new(ev);
|
|
|
|
talloc_free(fde);
|
|
|
|
key.dptr = talloc_memdup(tmp_ctx, key.dptr, key.dsize);
|
|
|
|
talloc_set_destructor(h, NULL);
|
|
ctdb_latency(&h->ctdb->status.max_lockwait_latency, h->start_time);
|
|
h->ctdb->status.pending_lockwait_calls--;
|
|
|
|
tdb_chainlock_mark(tdb, key);
|
|
callback(p);
|
|
tdb_chainlock_unmark(tdb, key);
|
|
|
|
kill(child, SIGKILL);
|
|
waitpid(child, NULL, 0);
|
|
talloc_free(tmp_ctx);
|
|
}
|
|
|
|
static int lockwait_destructor(struct lockwait_handle *h)
|
|
{
|
|
h->ctdb->status.pending_lockwait_calls--;
|
|
kill(h->child, SIGKILL);
|
|
waitpid(h->child, NULL, 0);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
setup a non-blocking chainlock on a tdb record. If this function
|
|
returns NULL then it could not get the chainlock. Otherwise it
|
|
returns a opaque handle, and will call callback() once it has
|
|
managed to get the chainlock. You can cancel it by using talloc_free
|
|
on the returned handle.
|
|
|
|
It is the callers responsibility to unlock the chainlock once
|
|
acquired
|
|
*/
|
|
struct lockwait_handle *ctdb_lockwait(struct ctdb_db_context *ctdb_db,
|
|
TDB_DATA key,
|
|
void (*callback)(void *private_data),
|
|
void *private_data)
|
|
{
|
|
struct lockwait_handle *result;
|
|
int ret;
|
|
|
|
ctdb_db->ctdb->status.lockwait_calls++;
|
|
ctdb_db->ctdb->status.pending_lockwait_calls++;
|
|
|
|
if (!(result = talloc_zero(private_data, struct lockwait_handle))) {
|
|
ctdb_db->ctdb->status.pending_lockwait_calls--;
|
|
return NULL;
|
|
}
|
|
|
|
ret = pipe(result->fd);
|
|
|
|
if (ret != 0) {
|
|
talloc_free(result);
|
|
ctdb_db->ctdb->status.pending_lockwait_calls--;
|
|
return NULL;
|
|
}
|
|
|
|
result->child = fork();
|
|
|
|
if (result->child == (pid_t)-1) {
|
|
close(result->fd[0]);
|
|
close(result->fd[1]);
|
|
talloc_free(result);
|
|
ctdb_db->ctdb->status.pending_lockwait_calls--;
|
|
return NULL;
|
|
}
|
|
|
|
result->callback = callback;
|
|
result->private_data = private_data;
|
|
result->ctdb = ctdb_db->ctdb;
|
|
result->ctdb_db = ctdb_db;
|
|
result->key = key;
|
|
|
|
if (result->child == 0) {
|
|
char c = 0;
|
|
close(result->fd[0]);
|
|
tdb_chainlock(ctdb_db->ltdb->tdb, key);
|
|
write(result->fd[1], &c, 1);
|
|
pause();
|
|
_exit(0);
|
|
}
|
|
|
|
close(result->fd[1]);
|
|
talloc_set_destructor(result, lockwait_destructor);
|
|
|
|
result->fde = event_add_fd(ctdb_db->ctdb->ev, result, result->fd[0],
|
|
EVENT_FD_READ|EVENT_FD_AUTOCLOSE, lockwait_handler,
|
|
(void *)result);
|
|
if (result->fde == NULL) {
|
|
talloc_free(result);
|
|
ctdb_db->ctdb->status.pending_lockwait_calls--;
|
|
return NULL;
|
|
}
|
|
|
|
result->start_time = timeval_current();
|
|
|
|
return result;
|
|
}
|