From 0e50da4c7ebede054a9f4cf8580e57a7a2aa0c96 Mon Sep 17 00:00:00 2001 From: Martin Schwenke Date: Fri, 24 Aug 2018 14:44:12 +1000 Subject: [PATCH] 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 (cherry picked from commit dc6040c121c65d5551c686f3f1be2891795f48aa) --- ctdb/common/sock_daemon.c | 26 ++++++++++++++++++++++++++ ctdb/common/sock_daemon.h | 10 ++++++++++ 2 files changed, 36 insertions(+) diff --git a/ctdb/common/sock_daemon.c b/ctdb/common/sock_daemon.c index 7554cd6da07..03d3ac1f1ec 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" @@ -71,6 +72,7 @@ struct sock_daemon_context { struct pidfile_context *pid_ctx; struct sock_socket *socket_list; + int startup_fd; }; /* @@ -483,6 +485,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) { @@ -514,6 +517,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 */ @@ -543,6 +551,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, @@ -669,6 +678,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) @@ -696,6 +707,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, @@ -961,6 +974,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 a071833c2f3..a28f8c6f39c 100644 --- a/ctdb/common/sock_daemon.h +++ b/ctdb/common/sock_daemon.h @@ -207,6 +207,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 * -- 2.34.1