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>

Split Close
Expand all
Collapse all
          --- old/usr/src/cmd/cmd-inet/usr.lib/in.ndpd/main.c
          +++ new/usr/src/cmd/cmd-inet/usr.lib/in.ndpd/main.c
↓ open down ↓ 900 lines elided ↑ open up ↑
 901  901          if (debug & D_STATE) {
 902  902                  logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
 903  903                      pi->pi_name, (int)event, (int)old_state,
 904  904                      (int)pi->pi_sol_state);
 905  905          }
 906  906  }
 907  907  
 908  908  static void
 909  909  daemonize_ndpd(void)
 910  910  {
 911      -        FILE *pidfp;
 912      -        mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
 913  911          struct itimerval it;
 914  912          boolean_t timerval = _B_TRUE;
 915  913  
 916  914          /*
 917  915           * Need to get current timer settings so they can be restored
 918  916           * after the fork(), as the it_value and it_interval values for
 919  917           * the ITIMER_REAL timer are reset to 0 in the child process.
 920  918           */
 921  919          if (getitimer(ITIMER_REAL, &it) < 0) {
 922  920                  if (debug & D_TIMER)
 923  921                          logmsg(LOG_DEBUG,
 924  922                              "daemonize_ndpd: failed to get itimerval\n");
 925  923                  timerval = _B_FALSE;
 926  924          }
 927  925  
 928  926          /* Daemonize. */
 929      -        switch (fork()) {
 930      -        case 0:
 931      -                /* Child */
 932      -                break;
 933      -        case -1:
      927 +        if (daemon(0, 0) == -1) {
 934  928                  logperror("fork");
 935  929                  exit(1);
 936      -        default:
 937      -                /* Parent */
 938      -                _exit(0);
 939  930          }
 940  931  
 941      -        /* Store our process id, blow away any existing file if it exists. */
 942      -        if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
 943      -                (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
 944      -                    argv0[0], strerror(errno));
 945      -        } else {
 946      -                (void) fprintf(pidfp, "%ld\n", getpid());
 947      -                (void) fclose(pidfp);
 948      -                (void) chmod(PATH_PID, pidmode);
 949      -        }
 950      -
 951      -        (void) close(0);
 952      -        (void) close(1);
 953      -        (void) close(2);
 954      -
 955      -        (void) chdir("/");
 956      -        (void) open("/dev/null", O_RDWR);
 957      -        (void) dup2(0, 1);
 958      -        (void) dup2(0, 2);
 959      -        (void) setsid();
 960      -
 961  932          already_daemonized = _B_TRUE;
 962  933  
 963  934          /*
 964  935           * Restore timer values, if we were able to save them; if not,
 965  936           * check and set the right value by calling run_timeouts().
 966  937           */
 967  938          if (timerval) {
 968  939                  if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
 969  940                          logperror("daemonize_ndpd: setitimer");
 970  941                          exit(2);
↓ open down ↓ 437 lines elided ↑ open up ↑
1408 1379  
1409 1380                  /*
1410 1381                   * Prevent fd leaks.  Everything gets re-opened at start-up
1411 1382                   * time.  0, 1, and 2 are closed and re-opened as
1412 1383                   * /dev/null, so we'll leave those open.
1413 1384                   */
1414 1385                  closefrom(3);
1415 1386  
1416 1387                  logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
1417 1388                  (void) execv(argv0[0], argv0);
1418      -                (void) unlink(PATH_PID);
1419 1389                  _exit(0177);
1420 1390                  /* NOTREACHED */
1421 1391          case SIGUSR1:
1422 1392                  logmsg(LOG_DEBUG, "Printing configuration:\n");
1423 1393                  phyint_print_all();
1424 1394                  break;
1425 1395          case SIGINT:
1426 1396          case SIGTERM:
1427 1397          case SIGQUIT:
1428 1398                  for (pi = phyints; pi != NULL; pi = next_pi) {
1429 1399                          next_pi = pi->pi_next;
1430 1400                          if (pi->pi_AdvSendAdvertisements)
1431 1401                                  check_to_advertise(pi, START_FINAL_ADV);
1432 1402  
1433 1403                          phyint_delete(pi);
1434 1404                  }
1435 1405                  (void) unlink(NDPD_SNMP_SOCKET);
1436      -                (void) unlink(PATH_PID);
1437 1406                  exit(0);
1438 1407                  /* NOTREACHED */
1439 1408          case 255:
1440 1409                  /*
1441 1410                   * Special "signal" from loopback_ra_enqueue.
1442 1411                   * Handle any queued loopback router advertisements.
1443 1412                   */
1444 1413                  loopback_ra_dequeue();
1445 1414                  break;
1446 1415          default:
↓ open down ↓ 1171 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX