[Freeswitch-svn] [commit] r2340 - freeswitch/trunk/src

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


Author: mikej
Date: Sat Aug 19 23:36:14 2006
New Revision: 2340

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

Log:
fix windows line endings and fix potential memory leak from SWITCH_GLOBAL_dirs on win32.

Modified: freeswitch/trunk/src/switch.c
==============================================================================
--- freeswitch/trunk/src/switch.c	(original)
+++ freeswitch/trunk/src/switch.c	Sat Aug 19 23:36:14 2006
@@ -118,52 +118,52 @@
 }
 
 #ifdef WIN32
-SERVICE_STATUS_HANDLE hStatus;
-SERVICE_STATUS status;
-
-void WINAPI ServiceCtrlHandler( DWORD control )
-{
-    switch( control )
-    {
-    case SERVICE_CONTROL_SHUTDOWN:
-    case SERVICE_CONTROL_STOP:
-        // do shutdown stuff here
-		switch_core_destroy();
-        status.dwCurrentState = SERVICE_STOPPED;
-        status.dwWin32ExitCode = 0;
-        status.dwCheckPoint = 0;
-        status.dwWaitHint = 0;
-        break;
-    case SERVICE_CONTROL_INTERROGATE:
-        // just set the current state to whatever it is...
-        break;
-    }
-
-    SetServiceStatus( hStatus, &status );
-}
-
-void WINAPI service_main( DWORD numArgs, char **args )
-{
+SERVICE_STATUS_HANDLE hStatus;
+SERVICE_STATUS status;
+
+void WINAPI ServiceCtrlHandler( DWORD control )
+{
+    switch( control )
+    {
+    case SERVICE_CONTROL_SHUTDOWN:
+    case SERVICE_CONTROL_STOP:
+        // do shutdown stuff here
+		switch_core_destroy();
+        status.dwCurrentState = SERVICE_STOPPED;
+        status.dwWin32ExitCode = 0;
+        status.dwCheckPoint = 0;
+        status.dwWaitHint = 0;
+        break;
+    case SERVICE_CONTROL_INTERROGATE:
+        // just set the current state to whatever it is...
+        break;
+    }
+
+    SetServiceStatus( hStatus, &status );
+}
+
+void WINAPI service_main( DWORD numArgs, char **args )
+{
 	const char *err = NULL;
-    // we have to initialize the service-specific stuff
-    memset( &status, 0, sizeof(SERVICE_STATUS) );
-    status.dwServiceType = SERVICE_WIN32;
-    status.dwCurrentState = SERVICE_START_PENDING;
-    status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
-
-    hStatus = RegisterServiceCtrlHandler( SERVICENAME, &ServiceCtrlHandler );
-
-    SetServiceStatus( hStatus, &status );
+    // we have to initialize the service-specific stuff
+    memset( &status, 0, sizeof(SERVICE_STATUS) );
+    status.dwServiceType = SERVICE_WIN32;
+    status.dwCurrentState = SERVICE_START_PENDING;
+    status.dwControlsAccepted = SERVICE_ACCEPT_STOP;
+
+    hStatus = RegisterServiceCtrlHandler( SERVICENAME, &ServiceCtrlHandler );
+
+    SetServiceStatus( hStatus, &status );
 	set_high_priority();
 	if (switch_core_init_and_modload(lfile, &err) != SWITCH_STATUS_SUCCESS) {
-	    status.dwCurrentState = SERVICE_STOPPED;
+	    status.dwCurrentState = SERVICE_STOPPED;
 	} else {
 		status.dwCurrentState = SERVICE_RUNNING;
 	}
-
-    SetServiceStatus( hStatus, &status );
-}
-
+
+    SetServiceStatus( hStatus, &status );
+}
+
 #endif
 
 int main(int argc, char *argv[])
@@ -183,46 +183,46 @@
     };
 
 	if (argv[1] && !strcmp(argv[1], "-service")) {
-		if(StartServiceCtrlDispatcher( dispatchTable ) == 0 )
-		{
-			//Not loaded as a service
+		if(StartServiceCtrlDispatcher( dispatchTable ) == 0 )
+		{
+			//Not loaded as a service
 			fprintf(stderr, "Error Freeswitch loaded as a console app with -service option\n");
 			fprintf(stderr, "To install the service load freeswitch with -install\n");
-		}
-		exit(0);
+		}
+		exit(0);
 	}
 	if (argv[1] && !strcmp(argv[1], "-install")) {
 		char exePath[1024];
 	    char servicePath[1024];
 
-		SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
+		SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
 		GetModuleFileName( NULL, exePath, 1024 );
 		snprintf(servicePath, sizeof(servicePath), "%s -service", exePath);
-		CreateService(
-			handle,
-			SERVICENAME,
-			SERVICENAME,
-			GENERIC_READ | GENERIC_EXECUTE,
-			SERVICE_WIN32_OWN_PROCESS,
-			SERVICE_AUTO_START,
-			SERVICE_ERROR_IGNORE,
-			servicePath,
-			NULL,
-			NULL,
-			NULL,
-			NULL,
-			NULL
-		);
+		CreateService(
+			handle,
+			SERVICENAME,
+			SERVICENAME,
+			GENERIC_READ | GENERIC_EXECUTE,
+			SERVICE_WIN32_OWN_PROCESS,
+			SERVICE_AUTO_START,
+			SERVICE_ERROR_IGNORE,
+			servicePath,
+			NULL,
+			NULL,
+			NULL,
+			NULL,
+			NULL
+		);
 		exit(0);
 	}
 	if (argv[1] && !strcmp(argv[1], "-uninstall")) {
-		SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
-		SC_HANDLE service = OpenService( handle, SERVICENAME, DELETE );
-		if( service != NULL )
-		{
-			// remove the service!
-			DeleteService( service );
-		}
+		SC_HANDLE handle = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
+		SC_HANDLE service = OpenService( handle, SERVICENAME, DELETE );
+		if( service != NULL )
+		{
+			// remove the service!
+			DeleteService( service );
+		}
 		exit(0);
 	}
 #endif

Modified: freeswitch/trunk/src/switch_core.c
==============================================================================
--- freeswitch/trunk/src/switch_core.c	(original)
+++ freeswitch/trunk/src/switch_core.c	Sat Aug 19 23:36:14 2006
@@ -2961,27 +2961,27 @@
 	char exePath[1024];
 	char *lastbacklash;
 	GetModuleFileName( NULL, exePath, BUFSIZE );
-	lastbacklash = strrchr( exePath, '\\');
+	lastbacklash = strrchr( exePath, '\\');
 	exePath[(lastbacklash - exePath + 1)] = '\0';
-	if ((SWITCH_GLOBAL_dirs.base_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.base_dir && (SWITCH_GLOBAL_dirs.base_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.base_dir, BUFSIZE, "%s", exePath);
 	}
-	if ((SWITCH_GLOBAL_dirs.mod_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.mod_dir && (SWITCH_GLOBAL_dirs.mod_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.mod_dir, BUFSIZE, "%s\\mod", exePath);
 	}
-	if ((SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.conf_dir && (SWITCH_GLOBAL_dirs.conf_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.conf_dir, BUFSIZE, "%s\\conf", exePath);
 	}
-	if ((SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.log_dir && (SWITCH_GLOBAL_dirs.log_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.log_dir, BUFSIZE, "%s\\log", exePath);
 	}
-	if ((SWITCH_GLOBAL_dirs.db_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.db_dir && (SWITCH_GLOBAL_dirs.db_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.db_dir, BUFSIZE, "%s\\db", exePath);
 	}
-	if ((SWITCH_GLOBAL_dirs.script_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.script_dir && (SWITCH_GLOBAL_dirs.script_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.script_dir, BUFSIZE, "%s\\scripts", exePath);
 	}
-	if ((SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.htdocs_dir && (SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.htdocs_dir, BUFSIZE, "%s\\mod", exePath);
 	}
 #else
@@ -2998,7 +2998,7 @@
 #else
 #ifdef WIN32
 	GetTempPath(dwBufSize, lpPathBuffer);
-	if ((SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(BUFSIZE))) {
+	if (!SWITCH_GLOBAL_dirs.htdocs_dir && (SWITCH_GLOBAL_dirs.htdocs_dir = (char *) malloc(BUFSIZE))) {
 		snprintf(SWITCH_GLOBAL_dirs.htdocs_dir, BUFSIZE, "%s", lpPathBuffer);
 	}
 #else
@@ -3269,6 +3269,17 @@
 		apr_pool_destroy(runtime.memory_pool);
 		apr_terminate();
 	}
+
+#ifdef WIN32
+	free(SWITCH_GLOBAL_dirs.base_dir);
+	free(SWITCH_GLOBAL_dirs.mod_dir);
+	free(SWITCH_GLOBAL_dirs.conf_dir);
+	free(SWITCH_GLOBAL_dirs.log_dir);
+	free(SWITCH_GLOBAL_dirs.db_dir);
+	free(SWITCH_GLOBAL_dirs.script_dir);
+	free(SWITCH_GLOBAL_dirs.htdocs_dir);
+	free(SWITCH_GLOBAL_dirs.temp_dir);
+#endif
 
 	return SWITCH_STATUS_SUCCESS;
 }



More information about the Freeswitch-svn mailing list