ctdb-common: Move PID file creation to sock_daemon_run_send()
authorAmitay Isaacs <amitay@gmail.com>
Mon, 28 Aug 2017 08:39:40 +0000 (18:39 +1000)
committerAmitay Isaacs <amitay@samba.org>
Tue, 29 Aug 2017 09:14:09 +0000 (11:14 +0200)
Only create PID file when actually starting the daemon, rather than
when setting up the context.  This will facilitate future changes.

Tweak test to confirm that PID file is no longer created during setup.

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 a6d430119a94a6c612d21deb5334d2c16dc088a9..e8742a06aabe93acf54a773922d93c8c6a0392e7 100644 (file)
@@ -452,7 +452,6 @@ bool sock_socket_write_recv(struct tevent_req *req, int *perr)
 
 int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
                      const char *logging, const char *debug_level,
-                     const char *pidfile,
                      struct sock_daemon_funcs *funcs,
                      void *private_data,
                      struct sock_daemon_context **out)
@@ -476,14 +475,6 @@ int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
                return ret;
        }
 
-       if (pidfile != NULL) {
-               ret = pidfile_context_create(sockd, pidfile, &sockd->pid_ctx);
-               if (ret != 0) {
-                       talloc_free(sockd);
-                       return EEXIST;
-               }
-       }
-
        *out = sockd;
        return 0;
 }
@@ -537,6 +528,7 @@ static void sock_daemon_run_wait_done(struct tevent_req *subreq);
 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
                                        struct sock_daemon_context *sockd,
+                                       const char *pidfile,
                                        pid_t pid_watch)
 {
        struct tevent_req *req, *subreq;
@@ -550,6 +542,15 @@ struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
                return NULL;
        }
 
+       if (pidfile != NULL) {
+               int ret = pidfile_context_create(sockd, pidfile,
+                                                &sockd->pid_ctx);
+               if (ret != 0) {
+                       tevent_req_error(req, EEXIST);
+                       return tevent_req_post(req, ev);
+               }
+       }
+
        state->ev = ev;
        state->sockd = sockd;
        state->pid_watch = pid_watch;
@@ -780,13 +781,14 @@ 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,
                    pid_t pid_watch)
 {
        struct tevent_req *req;
        int ret;
        bool status;
 
-       req = sock_daemon_run_send(ev, ev, sockd, pid_watch);
+       req = sock_daemon_run_send(ev, ev, sockd, pidfile, pid_watch);
        if (req == NULL) {
                return ENOMEM;
        }
index 81853f66446aa42814a91ab065ca6ee9f2b067c5..3fdd576e7a142ad8948c01d5747caeefbb9195e2 100644 (file)
@@ -149,7 +149,6 @@ bool sock_socket_write_recv(struct tevent_req *req, int *perr);
  * @param[in] daemon_name Name of the daemon, used for logging
  * @param[in] logging Logging setup string
  * @param[in] debug_level Debug level to log at
- * @param[in] pidfile PID file to create, NULL if no PID file required
  * @param[in] funcs Socket daemon callback routines
  * @param[in] private_data Private data associated with callback routines
  * @param[out] result New socket daemon context
@@ -157,7 +156,6 @@ bool sock_socket_write_recv(struct tevent_req *req, int *perr);
  */
 int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
                      const char *logging, const char *debug_level,
-                     const char *pidfile,
                      struct sock_daemon_funcs *funcs,
                      void *private_data,
                      struct sock_daemon_context **result);
@@ -182,12 +180,14 @@ int sock_daemon_add_unix(struct sock_daemon_context *sockd,
  * @param[in] mem_ctx Talloc memory context
  * @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] pid_watch PID to watch. If PID goes away, shutdown.
  * @return new tevent request, NULL on failure
  */
 struct tevent_req *sock_daemon_run_send(TALLOC_CTX *mem_ctx,
                                        struct tevent_context *ev,
                                        struct sock_daemon_context *sockd,
+                                       const char *pidfile,
                                        pid_t pid_watch);
 
 /**
@@ -204,6 +204,7 @@ 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] pid_watch PID to watch. If PID goes away, shutdown.
  * @return 0 on success, errno on failure
  *
@@ -211,6 +212,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,
                    pid_t pid_watch);
 
 #endif /* __CTDB_SOCK_DAEMON_H__ */
index a6ab2197e74c932eda38c48fe6b127db513769a3..cc9256ad7b820fdb039a6f19b328e1dffe7404d9 100644 (file)
@@ -1197,7 +1197,7 @@ int main(int argc, const char **argv)
        };
 
        ret = sock_daemon_setup(mem_ctx, "ctdb-eventd", options.logging,
-                               options.debug_level, options.pidfile,
+                               options.debug_level,
                                &daemon_funcs, ectx, &sockd);
        if (ret != 0) {
                goto fail;
@@ -1215,7 +1215,7 @@ int main(int argc, const char **argv)
                goto fail;
        }
 
-       ret = sock_daemon_run(ev, sockd, options.pid);
+       ret = sock_daemon_run(ev, sockd, options.pidfile, options.pid);
        if (ret == EINTR) {
                ret = 0;
        }
index 988bb94e5459eb3988597895674bd71d06c7b9d1..c0e09a409f55e7b2f37bacfcd32c2a7014dd08dc 100644 (file)
@@ -67,14 +67,13 @@ static void test1(TALLOC_CTX *mem_ctx, const char *pidfile,
        struct stat st;
        int ret;
 
-       ret = sock_daemon_setup(mem_ctx, "test1", "file:", "NOTICE", pidfile,
+       ret = sock_daemon_setup(mem_ctx, "test1", "file:", "NOTICE",
                                NULL, NULL, &sockd);
        assert(ret == 0);
        assert(sockd != NULL);
 
        ret = stat(pidfile, &st);
-       assert(ret == 0);
-       assert(S_ISREG(st.st_mode));
+       assert(ret == -1);
 
        ret = sock_daemon_add_unix(sockd, sockpath, &dummy_socket_funcs, NULL);
        assert(ret == 0);
@@ -162,14 +161,14 @@ static void test2(TALLOC_CTX *mem_ctx, const char *pidfile,
                assert(ev != NULL);
 
                ret = sock_daemon_setup(mem_ctx, "test2", "file:", "NOTICE",
-                                       pidfile, &test2_funcs, &fd[1], &sockd);
+                                       &test2_funcs, &fd[1], &sockd);
                assert(ret == 0);
 
                ret = sock_daemon_add_unix(sockd, sockpath,
                                           &dummy_socket_funcs, NULL);
                assert(ret == 0);
 
-               ret = sock_daemon_run(ev, sockd, -1);
+               ret = sock_daemon_run(ev, sockd, pidfile, -1);
                assert(ret == EINTR);
 
                exit(0);
@@ -258,14 +257,14 @@ static void test3(TALLOC_CTX *mem_ctx, const char *pidfile,
                assert(ev != NULL);
 
                ret = sock_daemon_setup(mem_ctx, "test3", "file:", "NOTICE",
-                                       NULL, NULL, NULL, &sockd);
+                                       NULL, NULL, &sockd);
                assert(ret == 0);
 
                ret = sock_daemon_add_unix(sockd, sockpath,
                                           &dummy_socket_funcs, NULL);
                assert(ret == 0);
 
-               ret = sock_daemon_run(ev, sockd, pid_watch);
+               ret = sock_daemon_run(ev, sockd, NULL, pid_watch);
                assert(ret == ESRCH);
 
                exit(0);
@@ -372,10 +371,10 @@ static void test4(TALLOC_CTX *mem_ctx, const char *pidfile,
                assert(ev != NULL);
 
                ret = sock_daemon_setup(mem_ctx, "test4", "file:", "NOTICE",
-                                       pidfile, &test4_funcs, NULL, &sockd);
+                                       &test4_funcs, NULL, &sockd);
                assert(ret == 0);
 
-               ret = sock_daemon_run(ev, sockd, -1);
+               ret = sock_daemon_run(ev, sockd, pidfile, -1);
                assert(ret == 0);
 
                exit(0);
@@ -655,7 +654,7 @@ static void test5(TALLOC_CTX *mem_ctx, const char *pidfile,
                assert(ev != NULL);
 
                ret = sock_daemon_setup(mem_ctx, "test5", "file:", "NOTICE",
-                                       pidfile, &test5_funcs, &fd[1], &sockd);
+                                       &test5_funcs, &fd[1], &sockd);
                assert(ret == 0);
 
                state.num_clients = 0;
@@ -664,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, pid);
+               ret = sock_daemon_run(ev, sockd, pidfile, pid);
                assert(ret == EINTR);
 
                exit(0);
@@ -960,7 +959,7 @@ static void test6(TALLOC_CTX *mem_ctx, const char *pidfile,
                server_state.fd = fd[1];
 
                ret = sock_daemon_setup(mem_ctx, "test6", "file:", "NOTICE",
-                                       pidfile, &test6_funcs, &server_state,
+                                       &test6_funcs, &server_state,
                                        &sockd);
                assert(ret == 0);
 
@@ -971,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, pid);
+               ret = sock_daemon_run(ev, sockd, pidfile, pid);
                assert(ret == 0);
 
                exit(0);