From: Martin Schwenke Date: Fri, 24 Aug 2018 04:44:12 +0000 (+1000) Subject: ctdb-common: Add support for sock daemon to notify of successful startup X-Git-Tag: tdb-1.3.17~1988 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=dc6040c121c65d5551c686f3f1be2891795f48aa;p=thirdparty%2Fsamba.git ctdb-common: Add support for sock daemon to notify of successful startup The daemon writes 0 into the specified file descriptor when it is up and listening. This can be used to avoid loops in clients that attempt to connect until they succeed. BUG: https://bugzilla.samba.org/show_bug.cgi?id=13592 Signed-off-by: Martin Schwenke Reviewed-by: Amitay Isaacs --- diff --git a/ctdb/common/sock_daemon.c b/ctdb/common/sock_daemon.c index 3c17519ff89..90f6bce2fd3 100644 --- a/ctdb/common/sock_daemon.c +++ b/ctdb/common/sock_daemon.c @@ -31,6 +31,7 @@ #include "lib/util/dlinklist.h" #include "lib/util/tevent_unix.h" #include "lib/util/become_daemon.h" +#include "lib/util/sys_rw.h" #include "common/logging.h" #include "common/reqid.h" @@ -72,6 +73,7 @@ struct sock_daemon_context { struct pidfile_context *pid_ctx; struct sock_socket *socket_list; + int startup_fd; }; /* @@ -489,6 +491,7 @@ int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name, sockd->funcs = funcs; sockd->private_data = private_data; + sockd->startup_fd = -1; ret = logging_init(sockd, logging, debug_level, daemon_name); if (ret != 0) { @@ -520,6 +523,11 @@ int sock_daemon_add_unix(struct sock_daemon_context *sockd, return 0; } +void sock_daemon_set_startup_fd(struct sock_daemon_context *sockd, int fd) +{ + sockd->startup_fd = fd; +} + /* * Run socket daemon */ @@ -549,6 +557,7 @@ static void sock_daemon_run_socket_fail(struct tevent_req *subreq); static void sock_daemon_run_watch_pid(struct tevent_req *subreq); static void sock_daemon_run_wait(struct tevent_req *req); static void sock_daemon_run_wait_done(struct tevent_req *subreq); +static void sock_daemon_startup_notify(struct sock_daemon_context *sockd); struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx, struct tevent_context *ev, @@ -675,6 +684,8 @@ static void sock_daemon_run_started(struct tevent_req *subreq) return; } sock_daemon_run_wait(req); + + sock_daemon_startup_notify(sockd); } static void sock_daemon_run_startup_done(struct tevent_req *subreq) @@ -702,6 +713,8 @@ static void sock_daemon_run_startup_done(struct tevent_req *subreq) return; } sock_daemon_run_wait(req); + + sock_daemon_startup_notify(sockd); } static void sock_daemon_run_signal_handler(struct tevent_context *ev, @@ -967,6 +980,19 @@ static void sock_daemon_run_wait_done(struct tevent_req *subreq) sock_daemon_run_shutdown(req); } +static void sock_daemon_startup_notify(struct sock_daemon_context *sockd) +{ + if (sockd->startup_fd != -1) { + unsigned int zero = 0; + ssize_t num; + + num = sys_write(sockd->startup_fd, &zero, sizeof(zero)); + if (num != sizeof(zero)) { + D_WARNING("Failed to write zero to pipe FD\n"); + } + } +} + bool sock_daemon_run_recv(struct tevent_req *req, int *perr) { int ret; diff --git a/ctdb/common/sock_daemon.h b/ctdb/common/sock_daemon.h index 705c4fab359..972245a9650 100644 --- a/ctdb/common/sock_daemon.h +++ b/ctdb/common/sock_daemon.h @@ -209,6 +209,16 @@ int sock_daemon_add_unix(struct sock_daemon_context *sockd, struct sock_socket_funcs *funcs, void *private_data); +/** + * @brief Set file descriptor for indicating startup success + * + * On successful completion, 0 (unsigned int) will be written to the fd. + * + * @param[in] sockd Socket daemon context + * @param[in] fd File descriptor + */ +void sock_daemon_set_startup_fd(struct sock_daemon_context *sockd, int fd); + /** * @brief Async computation start to run a socket daemon *