ctdb-tests: Strengthen some tests
[samba.git] / ctdb / common / sock_daemon.c
index b53b4d85333f087dcc8a67e2b8fae279ed786fc6..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"
@@ -452,7 +453,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 +476,6 @@ int sock_daemon_setup(TALLOC_CTX *mem_ctx, const char *daemon_name,
                return ret;
        }
 
-       if (pidfile != NULL) {
-               ret = pidfile_create(sockd, pidfile, &sockd->pid_ctx);
-               if (ret != 0) {
-                       talloc_free(sockd);
-                       return EEXIST;
-               }
-       }
-
        *out = sockd;
        return 0;
 }
@@ -537,6 +529,8 @@ 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,
+                                       bool do_fork, bool create_session,
                                        pid_t pid_watch)
 {
        struct tevent_req *req, *subreq;
@@ -550,6 +544,17 @@ 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);
+               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 +785,16 @@ 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, pid_watch);
+       req = sock_daemon_run_send(ev, ev, sockd,
+                                  pidfile, do_fork, create_session, pid_watch);
        if (req == NULL) {
                return ENOMEM;
        }