From: Michael Jerris Date: Fri, 18 Aug 2006 21:57:47 +0000 (+0000) Subject: factor out some functions, some of this will probably move to the freeswitch lib... X-Git-Tag: v1.0-beta1~2357 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9ac299089db3f3da72182f928822739fdcb05759;p=thirdparty%2Ffreeswitch.git factor out some functions, some of this will probably move to the freeswitch lib still. git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2334 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- diff --git a/src/switch.c b/src/switch.c index 6b540385df..7b1269c07f 100644 --- a/src/switch.c +++ b/src/switch.c @@ -36,7 +36,21 @@ #include #endif +#ifdef CRASH_PROT +#define __CP "ENABLED" +#else +#define __CP "DISABLED" +#endif + +#define PIDFILE "freeswitch.pid" +#define LOGFILE "freeswitch.log" static int RUNNING = 0; +static char *lfile = LOGFILE; +static char *pfile = PIDFILE; + +#ifdef __ICC +#pragma warning (disable:167) +#endif static int handle_SIGPIPE(int sig) { @@ -67,75 +81,87 @@ static int handle_SIGHUP(int sig) return 0; } -int main(int argc, char *argv[]) +static void set_high_priority() { - char *lfile = "freeswitch.log"; - char *pfile = "freeswitch.pid"; - char path[256] = ""; - char *ppath = NULL; - const char *err = NULL; - switch_event_t *event; - int bg = 0; - FILE *f; #ifdef WIN32 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); #else - int pid; nice(-20); #endif +} -#ifndef WIN32 - if (argv[1] && !strcmp(argv[1], "-stop")) { - pid_t pid = 0; - switch_core_set_globals(); - snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); - if ((f = fopen(path, "r")) == 0) { - fprintf(stderr, "Cannot open pid file %s.\n", path); - return 255; - } - fscanf(f, "%d", &pid); - if (pid > 0) { - fprintf(stderr, "Killing %d\n", (int) pid); - kill(pid, SIGTERM); - } - - fclose(f); - return 0; - } -#endif - - if (argv[1] && !strcmp(argv[1], "-nc")) { - bg++; +static int freeswitch_shutdown() +{ + switch_event_t *event; + if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down"); + switch_event_fire(&event); } - if (bg) { - //snprintf(path, sizeof(path), "%s%c%s", SWITCH_GLOBAL_dirs.log_dir, sep, lfile); - ppath = lfile; + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n"); + switch_core_session_hupall(); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n"); + switch_loadable_module_shutdown(); + switch_core_destroy(); + return 0; +} - signal(SIGHUP, (void *) handle_SIGHUP); - signal(SIGTERM, (void *) handle_SIGHUP); +static void freeswitch_runtime_loop(int bg) +{ + FILE *f; + char path[256] = ""; + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); + if (bg) { + bg = 0; + RUNNING = 1; + while(RUNNING) { #ifdef WIN32 - FreeConsole(); -#else - if ((pid = fork())) { - fprintf(stderr, "%d Backgrounding.\n", (int)pid); - exit(0); + bg++; + if(bg == 100) { + if ((f = fopen(path, "r")) == 0) { + break; + } + fclose(f); + bg = 0; } #endif - + switch_yield(10000); + } + } else { + /* wait for console input */ + switch_console_loop(); } +} - - if (switch_core_init(ppath, &err) != SWITCH_STATUS_SUCCESS) { - fprintf(stderr, "Cannot Initilize [%s]\n", err); +static int freeswitch_kill_background() +{ +#ifdef WIN32 +#else + char path[256] = ""; + pid_t pid = 0; + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); + if ((f = fopen(path, "r")) == 0) { + fprintf(stderr, "Cannot open pid file %s.\n", path); return 255; } + fscanf(f, "%d", &pid); + if (pid > 0) { + fprintf(stderr, "Killing %d\n", (int) pid); + kill(pid, SIGTERM); + } - -#ifdef __ICC -#pragma warning (disable:167) + fclose(f); #endif + return 0; +} + +static int freeswitch_init(char *path, const char **err) +{ + switch_event_t *event; + if (switch_core_init(path, err) != SWITCH_STATUS_SUCCESS) { + return 255; + } /* set signal handlers */ signal(SIGINT, (void *) handle_SIGINT); @@ -145,32 +171,13 @@ int main(int argc, char *argv[]) #ifdef TRAP_BUS signal(SIGBUS, (void *) handle_SIGBUS); #endif - - - - - - - snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); - if ((f = fopen(path, "w")) == 0) { - fprintf(stderr, "Cannot open pid file %s.\n", path); - return 255; - } - - fprintf(f, "%d", getpid()); - fclose(f); - if (!err) { - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n"); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Loading Modules.\n"); - if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) { - err = "Cannot load modules"; - } - } - - if (err) { + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Bringing up environment.\n"); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Loading Modules.\n"); + if (switch_loadable_module_init() != SWITCH_STATUS_SUCCESS) { + *err = "Cannot load modules"; switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Error: %s", err); - exit(-1); + return 255; } if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) { @@ -182,46 +189,60 @@ int main(int argc, char *argv[]) mlockall(MCL_CURRENT|MCL_FUTURE); #endif + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "freeswitch Version %s Started. Crash Protection [%s] Max Sessions[%u]\n\n", SWITCH_VERSION_FULL, __CP, switch_core_session_limit(0)); + return 0; +} -#ifdef CRASH_PROT -#define __CP "ENABLED" -#else -#define __CP "DISABLED" -#endif +int main(int argc, char *argv[]) +{ + char path[256] = ""; + char *ppath = NULL; + const char *err = NULL; + int bg = 0; + FILE *f; - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "freeswitch Version %s Started. Crash Protection [%s] Max Sessions[%u]\n\n", SWITCH_VERSION_FULL, __CP, switch_core_session_limit(0)); - snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); + set_high_priority(); + switch_core_set_globals(); + + if (argv[1] && !strcmp(argv[1], "-stop")) { + return freeswitch_kill_background(); + } + + if (argv[1] && !strcmp(argv[1], "-nc")) { + bg++; + } if (bg) { - bg = 0; - RUNNING = 1; - while(RUNNING) { + ppath = lfile; + + signal(SIGHUP, (void *) handle_SIGHUP); + signal(SIGTERM, (void *) handle_SIGHUP); + #ifdef WIN32 - bg++; - if(bg == 100) { - if ((f = fopen(path, "r")) == 0) { - break; - } - fclose(f); - bg = 0; + FreeConsole(); +#else + if ((pid = fork())) { + fprintf(stderr, "%d Backgrounding.\n", (int)pid); + exit(0); } #endif - switch_yield(10000); - } - } else { - /* wait for console input */ - switch_console_loop(); } - - if (switch_event_create(&event, SWITCH_EVENT_SHUTDOWN) == SWITCH_STATUS_SUCCESS) { - switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Shutting Down"); - switch_event_fire(&event); + + snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); + if ((f = fopen(path, "w")) == 0) { + fprintf(stderr, "Cannot open pid file %s.\n", path); + return 255; } - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "End existing sessions\n"); - switch_core_session_hupall(); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Clean up modules.\n"); - switch_loadable_module_shutdown(); - switch_core_destroy(); - return 0; + fprintf(f, "%d", getpid()); + fclose(f); + + if (freeswitch_init(ppath, &err) == 255) { + fprintf(stderr, "Cannot Initilize [%s]\n", err); + return 255; + } + + freeswitch_runtime_loop(bg); + + return freeswitch_shutdown(); }