]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
ctdb-common: Add support for sock daemon to notify of successful startup
authorMartin Schwenke <martin@meltin.net>
Fri, 24 Aug 2018 04:44:12 +0000 (14:44 +1000)
committerAmitay Isaacs <amitay@samba.org>
Thu, 30 Aug 2018 02:48:56 +0000 (04:48 +0200)
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 <martin@meltin.net>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/common/sock_daemon.c
ctdb/common/sock_daemon.h

index 3c17519ff89386c6412bbcfcea1f012ba99728dd..90f6bce2fd394df8cf1486334a9e85789b045d28 100644 (file)
@@ -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;
index 705c4fab359e8cd430f3d12eaa4a9f7b3ba414a9..972245a965091050ffc82f2d808385acbc310624 100644 (file)
@@ -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
  *