]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
Add switch_core_init_and_modload to core, from switch.c. Add additional shutdown...
authorMichael Jerris <mike@jerris.com>
Sat, 19 Aug 2006 18:51:22 +0000 (18:51 +0000)
committerMichael Jerris <mike@jerris.com>
Sat, 19 Aug 2006 18:51:22 +0000 (18:51 +0000)
git-svn-id: http://svn.freeswitch.org/svn/freeswitch/trunk@2336 d0543943-73ff-0310-b7d9-9358b9ac24b2

src/include/switch_core.h
src/switch.c
src/switch_core.c

index b8df0906fb3d40a4b07cdc502b4901190baff479..b9b2b79023faf6ad7067b5562f015c6addc9bec7 100644 (file)
@@ -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)
index 7b1269c07f764eb56af2b9909f22149deb55c39c..985535cd8e9fdaccfbf781034184f84b420584b3 100644 (file)
  */
 
 #include <switch.h>
-#include <switch_version.h>
-#ifdef HAVE_MLOCKALL
-#include <sys/mman.h>
-#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;
-}
-#endif
-
-/* no ctl-c mofo */
-static int handle_SIGINT(int sig)
-{
-       if (sig);
-       return 0;
-}
 
+#ifdef WIN32
+static HANDLE shutdown_event;
+#endif
 
-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);\r
+               if (!shutdown_event) {\r
+                       /* we can't get the event, so we can't signal the process to shutdown */\r
+                       fprintf(stderr, "ERROR: Can't Shutdown: %d\n", (int) pid);
+               } else {\r
+                       SetEvent(shutdown_event);\r
+               }\r
+               CloseHandle(shutdown_event);\r
+#else
                kill(pid, SIGTERM);
-       }
-
-       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));
+       fclose(f);
        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();
 }
index ce844d1cc57e0e5cb97bfb94aa61cc5bf45c19a8..25ef90660fac3a4dc3084480da390778487b2df4 100644 (file)
 
 #include <switch.h>
 #include <stdio.h>
+#include <switch_version.h>
+
+#ifdef HAVE_MLOCKALL
+#include <sys/mman.h>
+#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();