891 if (debug & D_STATE) {
892 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n",
893 pi->pi_name, (int)event, (int)old_state);
894 }
895 delay = solicit_event(pi, event, 0);
896 if (delay != TIMER_INFINITY) {
897 /* Make sure the global next event is updated */
898 timer_schedule(delay);
899 }
900
901 if (debug & D_STATE) {
902 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
903 pi->pi_name, (int)event, (int)old_state,
904 (int)pi->pi_sol_state);
905 }
906 }
907
908 static void
909 daemonize_ndpd(void)
910 {
911 FILE *pidfp;
912 mode_t pidmode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); /* 0644 */
913 struct itimerval it;
914 boolean_t timerval = _B_TRUE;
915
916 /*
917 * Need to get current timer settings so they can be restored
918 * after the fork(), as the it_value and it_interval values for
919 * the ITIMER_REAL timer are reset to 0 in the child process.
920 */
921 if (getitimer(ITIMER_REAL, &it) < 0) {
922 if (debug & D_TIMER)
923 logmsg(LOG_DEBUG,
924 "daemonize_ndpd: failed to get itimerval\n");
925 timerval = _B_FALSE;
926 }
927
928 /* Daemonize. */
929 switch (fork()) {
930 case 0:
931 /* Child */
932 break;
933 case -1:
934 logperror("fork");
935 exit(1);
936 default:
937 /* Parent */
938 _exit(0);
939 }
940
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 already_daemonized = _B_TRUE;
962
963 /*
964 * Restore timer values, if we were able to save them; if not,
965 * check and set the right value by calling run_timeouts().
966 */
967 if (timerval) {
968 if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
969 logperror("daemonize_ndpd: setitimer");
970 exit(2);
971 }
972 } else {
973 run_timeouts();
974 }
975 }
976
977 /*
978 * Check to see if the time is right to daemonize. The right time is when:
979 *
980 * 1. We haven't already daemonized.
1398 check_to_advertise(pi, START_FINAL_ADV);
1399
1400 /*
1401 * Remove all the configured addresses.
1402 * Remove the addrobj names created with ipmgmtd.
1403 * Release the dhcpv6 addresses if any.
1404 * Cleanup the phyints.
1405 */
1406 phyint_delete(pi);
1407 }
1408
1409 /*
1410 * Prevent fd leaks. Everything gets re-opened at start-up
1411 * time. 0, 1, and 2 are closed and re-opened as
1412 * /dev/null, so we'll leave those open.
1413 */
1414 closefrom(3);
1415
1416 logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
1417 (void) execv(argv0[0], argv0);
1418 (void) unlink(PATH_PID);
1419 _exit(0177);
1420 /* NOTREACHED */
1421 case SIGUSR1:
1422 logmsg(LOG_DEBUG, "Printing configuration:\n");
1423 phyint_print_all();
1424 break;
1425 case SIGINT:
1426 case SIGTERM:
1427 case SIGQUIT:
1428 for (pi = phyints; pi != NULL; pi = next_pi) {
1429 next_pi = pi->pi_next;
1430 if (pi->pi_AdvSendAdvertisements)
1431 check_to_advertise(pi, START_FINAL_ADV);
1432
1433 phyint_delete(pi);
1434 }
1435 (void) unlink(NDPD_SNMP_SOCKET);
1436 (void) unlink(PATH_PID);
1437 exit(0);
1438 /* NOTREACHED */
1439 case 255:
1440 /*
1441 * Special "signal" from loopback_ra_enqueue.
1442 * Handle any queued loopback router advertisements.
1443 */
1444 loopback_ra_dequeue();
1445 break;
1446 default:
1447 logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf);
1448 }
1449 }
1450
1451 /*
1452 * Create pipe for signal delivery and set up signal handlers.
1453 */
1454 static void
1455 setup_eventpipe(void)
1456 {
|
891 if (debug & D_STATE) {
892 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d\n",
893 pi->pi_name, (int)event, (int)old_state);
894 }
895 delay = solicit_event(pi, event, 0);
896 if (delay != TIMER_INFINITY) {
897 /* Make sure the global next event is updated */
898 timer_schedule(delay);
899 }
900
901 if (debug & D_STATE) {
902 logmsg(LOG_DEBUG, "check_to_solicit(%s, %d) state %d -> %d\n",
903 pi->pi_name, (int)event, (int)old_state,
904 (int)pi->pi_sol_state);
905 }
906 }
907
908 static void
909 daemonize_ndpd(void)
910 {
911 struct itimerval it;
912 boolean_t timerval = _B_TRUE;
913
914 /*
915 * Need to get current timer settings so they can be restored
916 * after the fork(), as the it_value and it_interval values for
917 * the ITIMER_REAL timer are reset to 0 in the child process.
918 */
919 if (getitimer(ITIMER_REAL, &it) < 0) {
920 if (debug & D_TIMER)
921 logmsg(LOG_DEBUG,
922 "daemonize_ndpd: failed to get itimerval\n");
923 timerval = _B_FALSE;
924 }
925
926 /* Daemonize. */
927 if (daemon(0, 0) == -1) {
928 logperror("fork");
929 exit(1);
930 }
931
932 already_daemonized = _B_TRUE;
933
934 /*
935 * Restore timer values, if we were able to save them; if not,
936 * check and set the right value by calling run_timeouts().
937 */
938 if (timerval) {
939 if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
940 logperror("daemonize_ndpd: setitimer");
941 exit(2);
942 }
943 } else {
944 run_timeouts();
945 }
946 }
947
948 /*
949 * Check to see if the time is right to daemonize. The right time is when:
950 *
951 * 1. We haven't already daemonized.
1369 check_to_advertise(pi, START_FINAL_ADV);
1370
1371 /*
1372 * Remove all the configured addresses.
1373 * Remove the addrobj names created with ipmgmtd.
1374 * Release the dhcpv6 addresses if any.
1375 * Cleanup the phyints.
1376 */
1377 phyint_delete(pi);
1378 }
1379
1380 /*
1381 * Prevent fd leaks. Everything gets re-opened at start-up
1382 * time. 0, 1, and 2 are closed and re-opened as
1383 * /dev/null, so we'll leave those open.
1384 */
1385 closefrom(3);
1386
1387 logmsg(LOG_ERR, "SIGHUP: restart and reread config file\n");
1388 (void) execv(argv0[0], argv0);
1389 _exit(0177);
1390 /* NOTREACHED */
1391 case SIGUSR1:
1392 logmsg(LOG_DEBUG, "Printing configuration:\n");
1393 phyint_print_all();
1394 break;
1395 case SIGINT:
1396 case SIGTERM:
1397 case SIGQUIT:
1398 for (pi = phyints; pi != NULL; pi = next_pi) {
1399 next_pi = pi->pi_next;
1400 if (pi->pi_AdvSendAdvertisements)
1401 check_to_advertise(pi, START_FINAL_ADV);
1402
1403 phyint_delete(pi);
1404 }
1405 (void) unlink(NDPD_SNMP_SOCKET);
1406 exit(0);
1407 /* NOTREACHED */
1408 case 255:
1409 /*
1410 * Special "signal" from loopback_ra_enqueue.
1411 * Handle any queued loopback router advertisements.
1412 */
1413 loopback_ra_dequeue();
1414 break;
1415 default:
1416 logmsg(LOG_ERR, "in_signal: unknown signal: %d\n", buf);
1417 }
1418 }
1419
1420 /*
1421 * Create pipe for signal delivery and set up signal handlers.
1422 */
1423 static void
1424 setup_eventpipe(void)
1425 {
|