From 4f769f1aec5e462ca25a1d0a6754f87cfc651c25 Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Wed, 17 Sep 2008 02:09:58 +0000 Subject: [PATCH] (FSCORE-189) Improve daemonize code git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@9577 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/switch.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 56 insertions(+), 5 deletions(-) diff --git a/src/switch.c b/src/switch.c index f5adc2e3af..19f454195d 100644 --- a/src/switch.c +++ b/src/switch.c @@ -194,6 +194,58 @@ void WINAPI service_main(DWORD numArgs, char **args) SetServiceStatus(hStatus, &status); } +#else + +void daemonize(void) { + int fd; + pid_t pid; + + switch (fork()) { + case 0: + break; + case -1: + fprintf(stderr, "Error Backgrounding (fork)! %d - %s\n", errno, strerror(errno)); + exit(0); + break; + default: + exit(0); + } + + if (setsid() < 0) { + fprintf(stderr, "Error Backgrounding (setsid)! %d - %s\n", errno, strerror(errno)); + exit(0); + } + pid = fork(); + switch (pid) { + case 0: + break; + case -1: + fprintf(stderr, "Error Backgrounding (fork2)! %d - %s\n", errno, strerror(errno)); + exit(0); + break; + default: + fprintf(stderr, "%d Backgrounding.\n", (int) pid); + exit(0); + } + + /* redirect std* to null */ + fd = open("/dev/null", O_RDONLY); + if (fd != 0) { + dup2(fd, 0); + close(fd); + } + fd = open("/dev/null", O_WRONLY); + if (fd != 1) { + dup2(fd, 1); + close(fd); + } + fd = open("/dev/null", O_WRONLY); + if (fd != 2) { + dup2(fd, 2); + close(fd); + } +} + #endif /* the main application entry point */ @@ -501,9 +553,8 @@ int main(int argc, char *argv[]) #ifdef WIN32 FreeConsole(); #else - if (!nf && (pid = fork())) { - fprintf(stderr, "%d Backgrounding.\n", (int) pid); - exit(0); + if (!nf) { + daemonize(); } #endif } @@ -530,12 +581,12 @@ int main(int argc, char *argv[]) memset(pid_buffer, 0, sizeof(pid_buffer)); switch_snprintf(pid_path, sizeof(pid_path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); switch_snprintf(pid_buffer, sizeof(pid_buffer), "%d", pid); - pid_len = sizeof(pid_buffer); + pid_len = strlen(pid_buffer); apr_pool_create(&pool, NULL); if (switch_file_open(&fd, pid_path, - SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE, + SWITCH_FOPEN_WRITE | SWITCH_FOPEN_CREATE | SWITCH_FOPEN_TRUNCATE, SWITCH_FPROT_UREAD | SWITCH_FPROT_UWRITE, pool) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot open pid file %s.\n", pid_path);