Print this page
3245 in.ndp daemon should not be session leader
Reviewed by: Sebastien Roy <sebastien.roy@delphix.com>
Reviewed by: Garrett D'Amore <garrett@damore.org>

@@ -906,12 +906,10 @@
 }
 
 static void
 daemonize_ndpd(void)
 {
-        FILE *pidfp;
-        mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
         struct itimerval it;
         boolean_t timerval = _B_TRUE;
 
         /*
          * Need to get current timer settings so they can be restored

@@ -924,42 +922,15 @@
                             "daemonize_ndpd: failed to get itimerval\n");
                 timerval = _B_FALSE;
         }
 
         /* Daemonize. */
-        switch (fork()) {
-        case 0:
-                /* Child */
-                break;
-        case -1:
+        if (daemon(0, 0) == -1) {
                 logperror("fork");
                 exit(1);
-        default:
-                /* Parent */
-                _exit(0);
         }
 
-        /* Store our process id, blow away any existing file if it exists. */
-        if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
-                (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
-                    argv0[0], strerror(errno));
-        } else {
-                (void) fprintf(pidfp, "%ld\n", getpid());
-                (void) fclose(pidfp);
-                (void) chmod(PATH_PID, pidmode);
-        }
-
-        (void) close(0);
-        (void) close(1);
-        (void) close(2);
-
-        (void) chdir("/");
-        (void) open("/dev/null", O_RDWR);
-        (void) dup2(0, 1);
-        (void) dup2(0, 2);
-        (void) setsid();
-
         already_daemonized = _B_TRUE;
 
         /*
          * Restore timer values, if we were able to save them; if not,
          * check and set the right value by calling run_timeouts().

@@ -1413,11 +1384,10 @@
                  */
                 closefrom(3);
 
                 logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
                 (void) execv(argv0[0], argv0);
-                (void) unlink(PATH_PID);
                 _exit(0177);
                 /* NOTREACHED */
         case SIGUSR1:
                 logmsg(LOG_DEBUG, "Printing configuration:\n");
                 phyint_print_all();

@@ -1431,11 +1401,10 @@
                                 check_to_advertise(pi, START_FINAL_ADV);
 
                         phyint_delete(pi);
                 }
                 (void) unlink(NDPD_SNMP_SOCKET);
-                (void) unlink(PATH_PID);
                 exit(0);
                 /* NOTREACHED */
         case 255:
                 /*
                  * Special "signal" from loopback_ra_enqueue.