ctdb-common: Allow sock_daemon to daemonise during startup
authorMartin Schwenke <martin@meltin.net>
Wed, 16 Aug 2017 03:52:08 +0000 (13:52 +1000)
committerAmitay Isaacs <amitay@samba.org>
Tue, 29 Aug 2017 09:14:09 +0000 (11:14 +0200)
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
ctdb/server/ctdb_eventd.c
ctdb/tests/src/sock_daemon_test.c

index e8742a06aabe93acf54a773922d93c8c6a0392e7..56205d019ecc4c2980c8a9336c216d6699081df5 100644 (file)
@@ -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;
        }
index 3fdd576e7a142ad8948c01d5747caeefbb9195e2..18210287771f8e6fc1605df115c3fa8d00181c0c 100644 (file)
@@ -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__ */
index cc9256ad7b820fdb039a6f19b328e1dffe7404d9..52a2e757ee7ece062eb460f582ba36dc76625a20 100644 (file)
@@ -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;
        }
index 1c0189428529f99ea14612680fa4c512280e2a56..8a653d9dc835f89b5adf7c8e9010d750099ed52a 100644 (file)
@@ -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);