echo_srv: Write pidfile after we setup the listeners.
authorAndreas Schneider <asn@samba.org>
Fri, 30 May 2014 11:57:25 +0000 (13:57 +0200)
committerMichael Adam <obnox@samba.org>
Sat, 31 May 2014 01:42:19 +0000 (03:42 +0200)
Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Michael Adam <obnox@samba.org>
tests/echo_srv.c

index 88d8170c38701d0c8869158a27deb97754292049..e3973f570e49bdccbd23fa2fe4b2d44a7a7bb8f9 100644 (file)
@@ -153,7 +153,7 @@ static int pidfile(const char *path)
     return 0;
 }
 
-static int become_daemon(struct echo_srv_opts *opts)
+static int become_daemon(void)
 {
     int ret;
     pid_t child_pid;
@@ -178,13 +178,6 @@ static int become_daemon(struct echo_srv_opts *opts)
     chdir(WORKING_DIR);
 #endif
 
-    ret = pidfile(opts->pidfile);
-    if (ret != 0) {
-        fprintf(stderr, "Cannot create pidfile %s: %s\n",
-                        opts->pidfile, strerror(ret));
-        return ret;
-    }
-
     ret = setsid();
     if (ret == -1) {
         ret = errno;
@@ -895,7 +888,7 @@ int main(int argc, char **argv)
     }
 
     if (opts.daemon) {
-        ret = become_daemon(&opts);
+        ret = become_daemon();
         if (ret != 0) {
             fprintf(stderr, "Cannot become daemon: %s\n", strerror(ret));
             goto done;
@@ -908,6 +901,15 @@ int main(int argc, char **argv)
         goto done;
     }
 
+    if (opts.daemon && opts.pidfile != NULL) {
+        ret = pidfile(opts.pidfile);
+        if (ret != 0) {
+            fprintf(stderr, "Cannot create pidfile %s: %s\n",
+                    opts.pidfile, strerror(ret));
+            goto done;
+        }
+    }
+
     echo(sock, &opts);
     close(sock);