[Freeswitch-svn] [commit] r2336 - in freeswitch/trunk/src: . include

Freeswitch SVN mikej at freeswitch.org
Sat Aug 19 14:51:23 EDT 2006


Author: mikej
Date: Sat Aug 19 14:51:22 2006
New Revision: 2336

Modified:
   freeswitch/trunk/src/include/switch_core.h
   freeswitch/trunk/src/switch.c
   freeswitch/trunk/src/switch_core.c

Log:
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.

Modified: freeswitch/trunk/src/include/switch_core.h
==============================================================================
--- freeswitch/trunk/src/include/switch_core.h	(original)
+++ freeswitch/trunk/src/include/switch_core.h	Sat Aug 19 14:51:22 2006
@@ -117,6 +117,14 @@
 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)
   \return the current session limit

Modified: freeswitch/trunk/src/switch.c
==============================================================================
--- freeswitch/trunk/src/switch.c	(original)
+++ freeswitch/trunk/src/switch.c	Sat Aug 19 14:51:22 2006
@@ -31,17 +31,7 @@
  */
 
 #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"
 static int RUNNING = 0;
@@ -52,30 +42,13 @@
 #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)
+	static int handle_SIGHUP(int sig)
 {
-	if (sig);
-	return 0;
-}
-
-
-static int handle_SIGHUP(int sig)
-{
 	if(sig);
 	RUNNING = 0;
 	return 0;
@@ -90,44 +63,18 @@
 #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 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,52 +93,26 @@
 	}
 	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;
-}
-
 int main(int argc, char *argv[])
 {
 	char path[256] = "";
@@ -200,6 +120,7 @@
 	const char *err = NULL;
 	int bg = 0;
 	FILE *f;
+	pid_t pid = 0;
 
 	set_high_priority();
 	switch_core_set_globals();
@@ -212,6 +133,15 @@
 		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 @@
 
 #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 @@
 #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();
 }

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Sat Aug 19 14:51:22 2006
@@ -32,8 +32,20 @@
 
 #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,8 +3121,66 @@
 	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)
 {
     switch_time_t temp = total_ms / 1000;
@@ -3135,6 +3205,16 @@
 
 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();



More information about the Freeswitch-svn mailing list