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.
|
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 /* Open pid file, blow away any existing file if it exists. */
929 if ((pidfp = fopen(PATH_PID, "w")) == NULL) {
930 (void) fprintf(stderr, "%s: unable to open " PATH_PID ": %s\n",
931 argv0[0], strerror(errno));
932 }
933
934 /* Daemonize. */
935 if (daemon(0, 0) == -1) {
936 logperror("fork");
937 exit(1);
938 }
939
940 /* Store our process id */
941 if (pidfp != NULL) {
942 (void) fprintf(pidfp, "%ld\n", getpid());
943 (void) fclose(pidfp);
944 (void) chmod(PATH_PID, pidmode);
945 }
946
947 already_daemonized = _B_TRUE;
948
949 /*
950 * Restore timer values, if we were able to save them; if not,
951 * check and set the right value by calling run_timeouts().
952 */
953 if (timerval) {
954 if (setitimer(ITIMER_REAL, &it, NULL) < 0) {
955 logperror("daemonize_ndpd: setitimer");
956 exit(2);
957 }
958 } else {
959 run_timeouts();
960 }
961 }
962
963 /*
964 * Check to see if the time is right to daemonize. The right time is when:
965 *
966 * 1. We haven't already daemonized.
|