/** * Set up log file and run in the backgroud */ void daemonize() { #ifdef WINDOWS if (!debug) { int fd; FILE *pidfh; if ((fd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) { perror("Can't open log file"); exit(1); } if (strcmp(pidfile, "")) { // Write out the pid file, before we redirect STDERR to the log. if ((pidfh = fopen(pidfile, "w")) == NULL) { perror("Can't open pid file for writing"); exit(1); } fprintf(pidfh, "%d\n", GetCurrentProcessId()); fclose(pidfh); } dup2(fd, 2); close(fd); applog = stderr; } if (!SetPriorityClass(GetCurrentProcess(), get_win_priority(priority))) { char errbuf[300]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, errbuf, sizeof(errbuf), NULL); log0(0, 0, "Error setting priority (%d): %s", GetLastError(), errbuf); } #else // WINDOWS if (!debug) { int pid, fd; FILE *pidfh; if ((fd = open(logfile, O_WRONLY | O_APPEND | O_CREAT, 0644)) == -1) { perror("Can't open log file"); exit(1); } if ((pid = fork()) == -1) { perror("Couldn't fork"); exit(1); } else if (pid > 0) { parent = 1; exit(0); } if (strcmp(pidfile, "")) { // Write out the pid file, before we redirect STDERR to the log. if ((pidfh = fopen(pidfile, "w")) == NULL) { perror("Can't open pid file for writing"); exit(1); } fprintf(pidfh, "%d\n", getpid()); fclose(pidfh); } setsid(); dup2(fd, 2); close(fd); for (fd = 0; fd < 30; fd++) { if ((fd != 2) && (fd != listener)) { close(fd); } } #ifdef VMS chdir("SYS$LOGIN"); #else chdir("/"); #endif umask(0); applog = stderr; } if (nice(priority) == -1) { syserror(0, 0, "Error setting priority"); } { struct sigaction act; sigfillset(&act.sa_mask); act.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART; act.sa_handler = gotsig; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); act.sa_handler = gotpipe; sigaction(SIGPIPE, &act, NULL); act.sa_handler = SIG_IGN; sigaction(SIGCHLD, &act, NULL); } #endif // WINDOWS }
/** * Set up log file and run in the background */ void daemonize(void) { showtime = 1; init_log_mux = 0; #ifdef WINDOWS init_log(debug); if (!debug) { FILE *pidfh; if (strcmp(pidfile, "")) { // Write out the pid file, before we redirect STDERR to the log. if ((pidfh = fopen(pidfile, "w")) == NULL) { syserror(0, 0, 0, "Can't open pid file for writing"); exit(ERR_PARAM); } fprintf(pidfh, "%d\n", GetCurrentProcessId()); fclose(pidfh); } } if (!SetPriorityClass(GetCurrentProcess(), get_win_priority(priority))) { char errbuf[300]; FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, GetLastError(), 0, errbuf, sizeof(errbuf), NULL); log0(0,0,0, "Error setting priority (%d): %s", GetLastError(), errbuf); } SetConsoleCtrlHandler(winsig, TRUE); #else // WINDOWS if (!debug) { int pid, fd; FILE *pidfh; if ((pid = fork()) == -1) { perror("Couldn't fork"); exit(ERR_ALLOC); } else if (pid > 0) { parent = 1; exit(ERR_NONE); } setsid(); for (fd = 0; fd < 30; fd++) { if ((fd != 2) && (fd != listener)) { close(fd); } } #ifdef VMS chdir("SYS$LOGIN"); #else chdir("/"); #endif umask(0); init_log(debug); if (strcmp(pidfile, "")) { // Write out the pid file, before we redirect STDERR to the log. if ((pidfh = fopen(pidfile, "w")) == NULL) { syserror(0, 0, 0, "Can't open pid file for writing"); exit(ERR_PARAM); } fprintf(pidfh, "%d\n", getpid()); fclose(pidfh); } } if (nice(priority) == -1) { syserror(0, 0, 0, "Error setting priority"); } { struct sigaction act; sigfillset(&act.sa_mask); act.sa_flags = SA_NOCLDSTOP | SA_NOCLDWAIT | SA_RESTART; act.sa_handler = gotsig; sigaction(SIGINT, &act, NULL); sigaction(SIGTERM, &act, NULL); act.sa_handler = SIG_IGN; sigaction(SIGPIPE, &act, NULL); sigaction(SIGCHLD, &act, NULL); } #endif // WINDOWS }