From 94cc5eaddc6cee3d7b24dc57b6194ef0a0a979ba Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Wed, 16 Aug 2017 13:52:08 +1000 Subject: [PATCH] ctdb-common: Allow sock_daemon to daemonise during startup Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- ctdb/common/sock_daemon.c | 8 +++++++- ctdb/common/sock_daemon.h | 6 ++++++ ctdb/server/ctdb_eventd.c | 3 ++- ctdb/tests/src/sock_daemon_test.c | 14 +++++++------- 4 files changed, 22 insertions(+), 9 deletions(-) diff --git a/ctdb/common/sock_daemon.c b/ctdb/common/sock_daemon.c index e8742a06aab..56205d019ec 100644 --- a/ctdb/common/sock_daemon.c +++ b/ctdb/common/sock_daemon.c @@ -30,6 +30,7 @@ #include "lib/util/blocking.h" #include "lib/util/dlinklist.h" #include "lib/util/tevent_unix.h" +#include "lib/util/become_daemon.h" #include "common/logging.h" #include "common/reqid.h" @@ -529,6 +530,7 @@ struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct sock_daemon_context *sockd, const char *pidfile, + bool do_fork, bool create_session, pid_t pid_watch) { struct tevent_req *req, *subreq; @@ -542,6 +544,8 @@ struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx, return NULL; } + become_daemon(do_fork, !create_session, false); + if (pidfile != NULL) { int ret = pidfile_context_create(sockd, pidfile, &sockd->pid_ctx); @@ -782,13 +786,15 @@ bool sock_daemon_run_recv(struct tevent_req *req, int *perr) int sock_daemon_run(struct tevent_context *ev, struct sock_daemon_context *sockd, const char *pidfile, + bool do_fork, bool create_session, pid_t pid_watch) { struct tevent_req *req; int ret; bool status; - req = sock_daemon_run_send(ev, ev, sockd, pidfile, pid_watch); + req = sock_daemon_run_send(ev, ev, sockd, + pidfile, do_fork, create_session, pid_watch); if (req == NULL) { return ENOMEM; } diff --git a/ctdb/common/sock_daemon.h b/ctdb/common/sock_daemon.h index 3fdd576e7a1..18210287771 100644 --- a/ctdb/common/sock_daemon.h +++ b/ctdb/common/sock_daemon.h @@ -181,6 +181,8 @@ int sock_daemon_add_unix(struct sock_daemon_context *sockd, * @param[in] ev Tevent context * @param[in] sockd The socket daemon context * @param[in] pidfile PID file to create, NULL if no PID file required + * @param[in] do_fork Whether the daemon should fork on startup + * @param[in] create_session Whether the daemon should create a new session * @param[in] pid_watch PID to watch. If PID goes away, shutdown. * @return new tevent request, NULL on failure */ @@ -188,6 +190,7 @@ struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct sock_daemon_context *sockd, const char *pidfile, + bool do_fork, bool create_session, pid_t pid_watch); /** @@ -205,6 +208,8 @@ bool sock_daemon_run_recv(struct tevent_req *req, int *perr); * @param[in] ev Tevent context * @param[in] sockd The socket daemon context * @param[in] pidfile PID file to create, NULL if no PID file required + * @param[in] do_fork Whether the daemon should fork on startup + * @param[in] create_session Whether the daemon should create a new session * @param[in] pid_watch PID to watch. If PID goes away, shutdown. * @return 0 on success, errno on failure * @@ -213,6 +218,7 @@ bool sock_daemon_run_recv(struct tevent_req *req, int *perr); int sock_daemon_run(struct tevent_context *ev, struct sock_daemon_context *sockd, const char *pidfile, + bool do_fork, bool create_session, pid_t pid_watch); #endif /* __CTDB_SOCK_DAEMON_H__ */ diff --git a/ctdb/server/ctdb_eventd.c b/ctdb/server/ctdb_eventd.c index cc9256ad7b8..52a2e757ee7 100644 --- a/ctdb/server/ctdb_eventd.c +++ b/ctdb/server/ctdb_eventd.c @@ -1215,7 +1215,8 @@ int main(int argc, const char **argv) goto fail; } - ret = sock_daemon_run(ev, sockd, options.pidfile, options.pid); + ret = sock_daemon_run(ev, sockd, + options.pidfile, false, false, options.pid); if (ret == EINTR) { ret = 0; } diff --git a/ctdb/tests/src/sock_daemon_test.c b/ctdb/tests/src/sock_daemon_test.c index 1c018942852..8a653d9dc83 100644 --- a/ctdb/tests/src/sock_daemon_test.c +++ b/ctdb/tests/src/sock_daemon_test.c @@ -168,7 +168,7 @@ static void test2(TALLOC_CTX *mem_ctx, const char *pidfile, &dummy_socket_funcs, NULL); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, pidfile, -1); + ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1); assert(ret == EINTR); exit(0); @@ -264,7 +264,7 @@ static void test3(TALLOC_CTX *mem_ctx, const char *pidfile, &dummy_socket_funcs, NULL); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, NULL, pid_watch); + ret = sock_daemon_run(ev, sockd, NULL, false, false, pid_watch); assert(ret == ESRCH); exit(0); @@ -374,7 +374,7 @@ static void test4(TALLOC_CTX *mem_ctx, const char *pidfile, &test4_funcs, NULL, &sockd); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, pidfile, -1); + ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1); assert(ret == 0); exit(0); @@ -663,7 +663,7 @@ static void test5(TALLOC_CTX *mem_ctx, const char *pidfile, &test5_client_funcs, &state); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, pidfile, pid); + ret = sock_daemon_run(ev, sockd, pidfile, false, false, pid); assert(ret == EINTR); exit(0); @@ -970,7 +970,7 @@ static void test6(TALLOC_CTX *mem_ctx, const char *pidfile, &test6_client_funcs, &server_state); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, pidfile, pid); + ret = sock_daemon_run(ev, sockd, pidfile, false, false, pid); assert(ret == 0); exit(0); @@ -1024,7 +1024,7 @@ static void test7(TALLOC_CTX *mem_ctx, const char *pidfile, &test2_funcs, &fd[1], &sockd); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, pidfile, -1); + ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1); assert(ret == EINTR); exit(0); @@ -1048,7 +1048,7 @@ static void test7(TALLOC_CTX *mem_ctx, const char *pidfile, &test2_funcs, &fd[1], &sockd); assert(ret == 0); - ret = sock_daemon_run(ev, sockd, pidfile, -1); + ret = sock_daemon_run(ev, sockd, pidfile, false, false, -1); assert(ret == EEXIST); ret = kill(pid, SIGTERM); -- 2.34.1