From c15da9857987f25a03fc4e43fcda6ac858dfcd80 Mon Sep 17 00:00:00 2001 From: Michael Jerris Date: Sat, 19 Aug 2006 18:51:22 +0000 Subject: [PATCH] Add switch_core_init_and_modload to core, from switch.c. Add additional shutdown functionality to switch_core_destroy. move around more code in switch.c and fix freeswitch -stop on windows. git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2336 d0543943-73ff-0310-b7d9-9358b9ac24b2 --- src/include/switch_core.h | 8 ++ src/switch.c | 149 +++++++++----------------------------- src/switch_core.c | 80 ++++++++++++++++++++ 3 files changed, 124 insertions(+), 113 deletions(-) diff --git a/src/include/switch_core.h b/src/include/switch_core.h index b8df0906fb..b9b2b79023 100644 --- a/src/include/switch_core.h +++ b/src/include/switch_core.h @@ -116,6 +116,14 @@ struct switch_core_runtime; */ SWITCH_DECLARE(switch_status_t) switch_core_init(char *console, const char **err); +/*! + \brief Initilize the core and load modules + \param console optional FILE stream for output + \param err a pointer to set any errors to + \note to be called at application startup instead of switch_core_init. Includes module loading. +*/ +SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(char *console, const char **err); + /*! \brief Set/Get Session Limit \param new_limit new value (if > 0) diff --git a/src/switch.c b/src/switch.c index 7b1269c07f..985535cd8e 100644 --- a/src/switch.c +++ b/src/switch.c @@ -31,16 +31,6 @@ */ #include -#include -#ifdef HAVE_MLOCKALL -#include -#endif - -#ifdef CRASH_PROT -#define __CP "ENABLED" -#else -#define __CP "DISABLED" -#endif #define PIDFILE "freeswitch.pid" #define LOGFILE "freeswitch.log" @@ -52,29 +42,12 @@ static char *pfile = PIDFILE; #pragma warning (disable:167) #endif -static int handle_SIGPIPE(int sig) -{ - if(sig); - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig Pipe!\n"); - return 0; -} -#ifdef TRAP_BUS -static int handle_SIGBUS(int sig) -{ - switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig BUS!\n"); - return 0; -} + +#ifdef WIN32 +static HANDLE shutdown_event; #endif -/* no ctl-c mofo */ -static int handle_SIGINT(int sig) -{ - if (sig); - return 0; -} - - -static int handle_SIGHUP(int sig) + static int handle_SIGHUP(int sig) { if(sig); RUNNING = 0; @@ -90,44 +63,18 @@ static void set_high_priority() #endif } -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); - } - - 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; -} - 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; +#ifdef WIN32 + WaitForSingleObject(shutdown_event, INFINITE); +#else RUNNING = 1; while(RUNNING) { -#ifdef WIN32 - bg++; - if(bg == 100) { - if ((f = fopen(path, "r")) == 0) { - break; - } - fclose(f); - bg = 0; - } -#endif switch_yield(10000); } +#endif } else { /* wait for console input */ switch_console_loop(); @@ -136,8 +83,7 @@ static void freeswitch_runtime_loop(int bg) static int freeswitch_kill_background() { -#ifdef WIN32 -#else + FILE *f; char path[256] = ""; pid_t pid = 0; snprintf(path, sizeof(path), "%s%s%s", SWITCH_GLOBAL_dirs.log_dir, SWITCH_PATH_SEPARATOR, pfile); @@ -147,49 +93,23 @@ static int freeswitch_kill_background() } fscanf(f, "%d", &pid); if (pid > 0) { - fprintf(stderr, "Killing %d\n", (int) pid); + fprintf(stderr, "Killing: %d\n", (int) pid); +#ifdef WIN32 + snprintf(path, sizeof(path), "Global\\Freeswitch.%d", pid); + shutdown_event = OpenEvent(EVENT_MODIFY_STATE, FALSE, path); + if (!shutdown_event) { + /* we can't get the event, so we can't signal the process to shutdown */ + fprintf(stderr, "ERROR: Can't Shutdown: %d\n", (int) pid); + } else { + SetEvent(shutdown_event); + } + CloseHandle(shutdown_event); +#else kill(pid, SIGTERM); +#endif } 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); -#ifdef SIGPIPE - signal(SIGPIPE, (void *) handle_SIGPIPE); -#endif -#ifdef TRAP_BUS - signal(SIGBUS, (void *) handle_SIGBUS); -#endif - - 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); - return 255; - } - - if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) { - switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready"); - switch_event_fire(&event); - } - -#ifdef HAVE_MLOCKALL - 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; } @@ -200,6 +120,7 @@ int main(int argc, char *argv[]) const char *err = NULL; int bg = 0; FILE *f; + pid_t pid = 0; set_high_priority(); switch_core_set_globals(); @@ -212,6 +133,15 @@ int main(int argc, char *argv[]) bg++; } + 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", pid = getpid()); + fclose(f); + if (bg) { ppath = lfile; @@ -220,6 +150,8 @@ int main(int argc, char *argv[]) #ifdef WIN32 FreeConsole(); + snprintf(path, sizeof(path), "Global\\Freeswitch.%d", pid); + shutdown_event = CreateEvent(NULL, FALSE, FALSE, path); #else if ((pid = fork())) { fprintf(stderr, "%d Backgrounding.\n", (int)pid); @@ -228,21 +160,12 @@ int main(int argc, char *argv[]) #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 (freeswitch_init(ppath, &err) == 255) { + if (switch_core_init_and_modload(ppath, &err) != SWITCH_STATUS_SUCCESS) { fprintf(stderr, "Cannot Initilize [%s]\n", err); return 255; } freeswitch_runtime_loop(bg); - return freeswitch_shutdown(); + return switch_core_destroy(); } diff --git a/src/switch_core.c b/src/switch_core.c index ce844d1cc5..25ef90660f 100644 --- a/src/switch_core.c +++ b/src/switch_core.c @@ -32,8 +32,20 @@ #include #include +#include + +#ifdef HAVE_MLOCKALL +#include +#endif + //#define DEBUG_ALLOC +#ifdef CRASH_PROT +#define __CP "ENABLED" +#else +#define __CP "DISABLED" +#endif + #define SWITCH_EVENT_QUEUE_LEN 256 #define SWITCH_SQL_QUEUE_LEN 2000 @@ -3109,7 +3121,65 @@ SWITCH_DECLARE(switch_status_t) switch_core_init(char *console, const char **err runtime.initiated = switch_time_now(); return SWITCH_STATUS_SUCCESS; } +#ifdef SIGPIPE +static int handle_SIGPIPE(int sig) +{ + if(sig); + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig Pipe!\n"); + return 0; +} +#endif +#ifdef TRAP_BUS +static int handle_SIGBUS(int sig) +{ + switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Sig BUS!\n"); + return 0; +} +#endif +/* no ctl-c mofo */ +static int handle_SIGINT(int sig) +{ + if (sig); + return 0; +} +SWITCH_DECLARE(switch_status_t) switch_core_init_and_modload(char *console, const char **err) +{ + switch_event_t *event; + if (switch_core_init(console, err) != SWITCH_STATUS_SUCCESS) { + return SWITCH_STATUS_GENERR; + } + + /* set signal handlers */ + signal(SIGINT, (void *) handle_SIGINT); +#ifdef SIGPIPE + signal(SIGPIPE, (void *) handle_SIGPIPE); +#endif +#ifdef TRAP_BUS + signal(SIGBUS, (void *) handle_SIGBUS); +#endif + + 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); + return SWITCH_STATUS_GENERR; + } + + if (switch_event_create(&event, SWITCH_EVENT_STARTUP) == SWITCH_STATUS_SUCCESS) { + switch_event_add_header(event, SWITCH_STACK_BOTTOM, "Event-Info", "System Ready"); + switch_event_fire(&event); + } + +#ifdef HAVE_MLOCKALL + 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 SWITCH_STATUS_SUCCESS; + +} SWITCH_DECLARE(void) switch_core_measure_time(switch_time_t total_ms, switch_core_time_duration_t *duration) { @@ -3135,6 +3205,16 @@ SWITCH_DECLARE(switch_time_t) switch_core_uptime(void) SWITCH_DECLARE(switch_status_t) switch_core_destroy(void) { + 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); + } + + 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_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_CONSOLE, "Closing Event Engine.\n"); switch_event_shutdown();