From 16f31786a031255ab5b3099a0a3c745de973347a Mon Sep 17 00:00:00 2001 From: root Date: Mon, 23 Mar 2009 12:37:30 +1100 Subject: [PATCH] create a helper function that converts a ctdb instance in daemon mode to become a ctdb client instance. use this from the recovery daemon child process to switch to client mode and connect back to the main daemon --- client/ctdb_client.c | 36 ++++++++++++++++++++++++++++++++++++ include/ctdb.h | 2 ++ server/ctdb_recoverd.c | 30 +++++------------------------- 3 files changed, 43 insertions(+), 25 deletions(-) diff --git a/client/ctdb_client.c b/client/ctdb_client.c index 16fc03b4..1f68c242 100644 --- a/client/ctdb_client.c +++ b/client/ctdb_client.c @@ -3472,3 +3472,39 @@ int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb) return 0; } + +/* when forking the main daemon and the child process needs to connect back + * to the daemon as a client process, this function can be used to change + * the ctdb context from daemon into client mode + */ +int switch_from_server_to_client(struct ctdb_context *ctdb) +{ + int ret; + + /* shutdown the transport */ + if (ctdb->methods) { + ctdb->methods->shutdown(ctdb); + } + + /* get a new event context */ + talloc_free(ctdb->ev); + ctdb->ev = event_context_init(ctdb); + + close(ctdb->daemon.sd); + ctdb->daemon.sd = -1; + + /* the client does not need to be realtime */ + if (ctdb->do_setsched) { + ctdb_restore_scheduler(ctdb); + } + + /* initialise ctdb */ + ret = ctdb_socket_connect(ctdb); + if (ret != 0) { + DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb client\n")); + return -1; + } + + return 0; +} + diff --git a/include/ctdb.h b/include/ctdb.h index 2ec477f6..f0f510ab 100644 --- a/include/ctdb.h +++ b/include/ctdb.h @@ -610,4 +610,6 @@ int ctdb_transaction_commit(struct ctdb_transaction_handle *h); int ctdb_ctrl_recd_ping(struct ctdb_context *ctdb); +int switch_from_server_to_client(struct ctdb_context *ctdb); + #endif diff --git a/server/ctdb_recoverd.c b/server/ctdb_recoverd.c index 540749d1..28be460c 100644 --- a/server/ctdb_recoverd.c +++ b/server/ctdb_recoverd.c @@ -2906,7 +2906,6 @@ static void recd_sig_child_handler(struct event_context *ev, */ int ctdb_start_recoverd(struct ctdb_context *ctdb) { - int ret; int fd[2]; struct signal_event *se; @@ -2931,35 +2930,16 @@ int ctdb_start_recoverd(struct ctdb_context *ctdb) close(fd[1]); - /* shutdown the transport */ - if (ctdb->methods) { - ctdb->methods->shutdown(ctdb); - } - - /* get a new event context */ - talloc_free(ctdb->ev); - ctdb->ev = event_context_init(ctdb); - - event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE, - ctdb_recoverd_parent, &fd[0]); - - close(ctdb->daemon.sd); - ctdb->daemon.sd = -1; - srandom(getpid() ^ time(NULL)); - /* the recovery daemon does not need to be realtime */ - if (ctdb->do_setsched) { - ctdb_restore_scheduler(ctdb); - } - - /* initialise ctdb */ - ret = ctdb_socket_connect(ctdb); - if (ret != 0) { - DEBUG(DEBUG_ALERT, (__location__ " Failed to init ctdb\n")); + if (switch_from_server_to_client(ctdb) != 0) { + DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch recovery daemon into client mode. shutting down.\n")); exit(1); } + event_add_fd(ctdb->ev, ctdb, fd[0], EVENT_FD_READ|EVENT_FD_AUTOCLOSE, + ctdb_recoverd_parent, &fd[0]); + /* set up a handler to pick up sigchld */ se = event_add_signal(ctdb->ev, ctdb, SIGCHLD, 0, -- 2.34.1