[xiph-commits] r16440 - in icecast/trunk/icecast: src win32

karl at svn.xiph.org karl at svn.xiph.org
Wed Aug 5 15:32:47 PDT 2009


Author: karl
Date: 2009-08-05 15:32:47 -0700 (Wed, 05 Aug 2009)
New Revision: 16440

Modified:
   icecast/trunk/icecast/src/compat.h
   icecast/trunk/icecast/src/main.c
   icecast/trunk/icecast/src/stats.c
   icecast/trunk/icecast/win32/Icecast2win.cpp
   icecast/trunk/icecast/win32/Icecast2win.h
   icecast/trunk/icecast/win32/Icecast2winDlg.cpp
   icecast/trunk/icecast/win32/icecastService.cpp
Log:
win32 updates. cleanup of the start and finish of the gui and service


Modified: icecast/trunk/icecast/src/compat.h
===================================================================
--- icecast/trunk/icecast/src/compat.h	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/src/compat.h	2009-08-05 22:32:47 UTC (rev 16440)
@@ -44,6 +44,7 @@
 #  define uint64_t unsigned __int64
 #  define uint32_t unsigned int
 #  define PRIu64  "I64u"
+#  define PRId64  "I64d"
 #else
 #  define PATH_SEPARATOR "/"
 #  if defined(HAVE_INTTYPES_H)

Modified: icecast/trunk/icecast/src/main.c
===================================================================
--- icecast/trunk/icecast/src/main.c	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/src/main.c	2009-08-05 22:32:47 UTC (rev 16440)
@@ -17,7 +17,17 @@
 
 #include <stdio.h>
 #include <string.h>
+#include <errno.h>
 
+#ifdef WIN32
+#define _WIN32_WINNT 0x0400
+/* For getpid() */
+#include <process.h>
+#include <windows.h>
+#define snprintf _snprintf
+#define getpid _getpid
+#endif
+
 #ifdef HAVE_UNISTD_H
 # include <unistd.h>
 #endif
@@ -35,7 +45,6 @@
 #include <sys/types.h>
 #include <grp.h>
 #include <pwd.h>
-#include <errno.h>
 #endif
 
 #include "cfgfile.h"
@@ -56,14 +65,6 @@
 
 #include <libxml/xmlmemory.h>
 
-#ifdef _WIN32
-/* For getpid() */
-#include <process.h>
-#include <windows.h>
-#define snprintf _snprintf
-#define getpid _getpid
-#endif
-
 #undef CATMODULE
 #define CATMODULE "main"
 
@@ -72,7 +73,9 @@
 
 static void _fatal_error(char *perr)
 {
-#ifdef WIN32
+#ifdef WIN32_SERVICE
+    MessageBox(NULL, perr, "Error", MB_SERVICE_NOTIFICATION);
+#elif defined(WIN32)
     MessageBox(NULL, perr, "Error", MB_OK);
 #else
     fprintf(stdout, "%s\n", perr);
@@ -97,7 +100,7 @@
     log_close(playlistlog);
 }
 
-static void _initialize_subsystems(void)
+void initialize_subsystems(void)
 {
     log_initialize();
     thread_initialize();
@@ -107,23 +110,16 @@
     connection_initialize();
     global_initialize();
     refbuf_initialize();
-#if !defined(WIN32) || defined(WIN32_SERVICE)
-    /* win32 GUI needs to do the initialise before here */
+
     xslt_initialize();
 #ifdef HAVE_CURL_GLOBAL_INIT
     curl_global_init (CURL_GLOBAL_ALL);
 #endif
-#endif
 }
 
-static void _shutdown_subsystems(void)
+void shutdown_subsystems(void)
 {
     fserve_shutdown();
-#if !defined(WIN32) || defined(WIN32_SERVICE)
-    /* If we do the following cleanup on the win32 GUI then the app will crash when libxml2 is
-     * initialised again as the process doesn't terminate */
-    xslt_shutdown();
-#endif
     refbuf_shutdown();
     slave_shutdown();
     auth_shutdown();
@@ -144,6 +140,7 @@
     /* Now that these are done, we can stop the loggers. */
     _stop_logging();
     log_shutdown();
+    xslt_shutdown();
 }
 
 static int _parse_config_opts(int argc, char **argv, char *filename, int size)
@@ -425,9 +422,10 @@
     */
     res = _parse_config_opts(argc, argv, filename, 512);
     if (res == 1) {
+#if !defined(_WIN32) || defined(_CONSOLE)
         /* startup all the modules */
-        _initialize_subsystems();
-
+        initialize_subsystems();
+#endif
         /* parse the config file */
         config_get_config();
         ret = config_initial_parse_file(filename);
@@ -451,7 +449,9 @@
                 _fatal_error("XML config parsing error");
                 break;
             }
-            _shutdown_subsystems();
+#if !defined(_WIN32) || defined(_CONSOLE)
+            shutdown_subsystems();
+#endif
             return 1;
         }
     } else if (res == -1) {
@@ -465,7 +465,7 @@
     /* Bind socket, before we change userid */
     if(!_server_proc_init()) {
         _fatal_error("Server startup failed. Exiting");
-        _shutdown_subsystems();
+        shutdown_subsystems();
         return 1;
     }
 
@@ -481,7 +481,7 @@
     {
         fprintf(stderr, "ERROR: You should not run icecast2 as root\n");
         fprintf(stderr, "Use the changeowner directive in the config file\n");
-        _shutdown_subsystems();
+        shutdown_subsystems();
         return 1;
     }
 #endif
@@ -491,7 +491,7 @@
 
     if (!_start_logging()) {
         _fatal_error("FATAL: Could not start logging");
-        _shutdown_subsystems();
+        shutdown_subsystems();
         return 1;
     }
 
@@ -512,9 +512,9 @@
     _server_proc();
 
     INFO0("Shutting down");
-
-    _shutdown_subsystems();
-
+#if !defined(_WIN32) || defined(_CONSOLE)
+    shutdown_subsystems();
+#endif
     if (pidfile)
     {
         remove (pidfile);

Modified: icecast/trunk/icecast/src/stats.c
===================================================================
--- icecast/trunk/icecast/src/stats.c	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/src/stats.c	2009-08-05 22:32:47 UTC (rev 16440)
@@ -41,6 +41,7 @@
 #include "logging.h"
 
 #ifdef _WIN32
+#define atoll _atoi64
 #define vsnprintf _vsnprintf
 #define snprintf _snprintf
 #endif
@@ -445,7 +446,7 @@
     }
     if (event->action != STATS_EVENT_SET)
     {
-        int value = 0;
+        int64_t value = 0;
 
         switch (event->action)
         {
@@ -466,7 +467,7 @@
                 break;
         }
         str = malloc (16);
-        snprintf (str, 16, "%d", value);
+        snprintf (str, 16, "%" PRId64, value);
         if (event->value == NULL)
             event->value = strdup (str);
     }

Modified: icecast/trunk/icecast/win32/Icecast2win.cpp
===================================================================
--- icecast/trunk/icecast/win32/Icecast2win.cpp	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/win32/Icecast2win.cpp	2009-08-05 22:32:47 UTC (rev 16440)
@@ -5,6 +5,11 @@
 #include "Icecast2win.h"
 #include "Icecast2winDlg.h"
 
+extern "C" {
+#include "xslt.h"
+void initialize_subsystems(void);
+void shutdown_subsystems(void);
+}
 #ifdef _DEBUG
 #define new DEBUG_NEW
 #undef THIS_FILE
@@ -49,6 +54,7 @@
 	//  of your final executable, you should remove from the following
 	//  the specific initialization routines you do not need.
 
+    initialize_subsystems();
 	if (strlen(m_lpCmdLine) > 0) {
 		strcpy(m_configFile, m_lpCmdLine);
 	}
@@ -87,3 +93,9 @@
 	//  application, rather than start the application's message pump.
 	return FALSE;
 }
+
+int CIcecast2winApp::ExitInstance()
+{
+    shutdown_subsystems();
+    return CWinApp::ExitInstance();
+}

Modified: icecast/trunk/icecast/win32/Icecast2win.h
===================================================================
--- icecast/trunk/icecast/win32/Icecast2win.h	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/win32/Icecast2win.h	2009-08-05 22:32:47 UTC (rev 16440)
@@ -31,6 +31,7 @@
 	//{{AFX_VIRTUAL(CIcecast2winApp)
 	public:
 	virtual BOOL InitInstance();
+    virtual int ExitInstance();
 	//}}AFX_VIRTUAL
 
 // Implementation

Modified: icecast/trunk/icecast/win32/Icecast2winDlg.cpp
===================================================================
--- icecast/trunk/icecast/win32/Icecast2winDlg.cpp	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/win32/Icecast2winDlg.cpp	2009-08-05 22:32:47 UTC (rev 16440)
@@ -339,9 +339,6 @@
 	sprintf(version, "Icecast2 Version %s", ICECAST_VERSION);
 	SetWindowText(version);
 
-    xslt_initialize();
-    curl_global_init (CURL_GLOBAL_ALL);
-
 	if (m_Autostart) {
 		OnStart();
 	}

Modified: icecast/trunk/icecast/win32/icecastService.cpp
===================================================================
--- icecast/trunk/icecast/win32/icecastService.cpp	2009-08-05 22:24:04 UTC (rev 16439)
+++ icecast/trunk/icecast/win32/icecastService.cpp	2009-08-05 22:32:47 UTC (rev 16440)
@@ -1,3 +1,4 @@
+#define _WIN32_WINNT 0x0400
 #include <windows.h>
 #include <stdio.h>
 #include <direct.h>
@@ -24,14 +25,8 @@
  
 void  ServiceMain(int argc, char** argv); 
 void  ControlHandler(DWORD request); 
-int InitService();
 extern "C" int mainService(int argc, char **argv);
 
-int InitService() 
-{ 
-   int result = 0;
-   return(result); 
-}
 
 void installService (const char *path)
 {
@@ -44,21 +39,7 @@
         SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
         if (manager == NULL)
 		{
-			LPVOID lpMsgBuf;
-			FormatMessage( 
-					FORMAT_MESSAGE_ALLOCATE_BUFFER | 
-					FORMAT_MESSAGE_FROM_SYSTEM | 
-					FORMAT_MESSAGE_IGNORE_INSERTS,
-					NULL,
-					GetLastError(),
-					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
-					(LPTSTR) &lpMsgBuf,
-					0,
-					NULL 
-					);
-
-			printf ("OpenSCManager: %s\n", (LPCTSTR)lpMsgBuf);
-			LocalFree( lpMsgBuf );
+            MessageBox (NULL, "OpenSCManager failed", NULL, MB_SERVICE_NOTIFICATION);
 			return;
 		}
 
@@ -79,21 +60,7 @@
 		);
 		if (service == NULL)
 		{
-			LPVOID lpMsgBuf;
-			FormatMessage( 
-					FORMAT_MESSAGE_ALLOCATE_BUFFER | 
-					FORMAT_MESSAGE_FROM_SYSTEM | 
-					FORMAT_MESSAGE_IGNORE_INSERTS,
-					NULL,
-					GetLastError(),
-					MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
-					(LPTSTR) &lpMsgBuf,
-					0,
-					NULL 
-					);
-
-			printf ("CreateService: %s\n", (LPCTSTR)lpMsgBuf);
-			LocalFree( lpMsgBuf );
+            MessageBox (NULL, "CreateService failed", NULL, MB_SERVICE_NOTIFICATION);
 			CloseServiceHandle (manager);
 			return;
 		}
@@ -108,67 +75,44 @@
 	SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
 	if (manager == NULL)
 	{
-		LPVOID lpMsgBuf;
-		FormatMessage( 
-				FORMAT_MESSAGE_ALLOCATE_BUFFER | 
-				FORMAT_MESSAGE_FROM_SYSTEM | 
-				FORMAT_MESSAGE_IGNORE_INSERTS,
-				NULL,
-				GetLastError(),
-				MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
-				(LPTSTR) &lpMsgBuf,
-				0,
-				NULL 
-				);
-
-		printf ("OpenSCManager: %s\n", (LPCTSTR)lpMsgBuf);
-		LocalFree( lpMsgBuf );
+        MessageBox (NULL, "OpenSCManager failed", NULL, MB_SERVICE_NOTIFICATION);
 		return;
 	}
 
 	SC_HANDLE service = OpenService (manager, PACKAGE_STRING, DELETE);
 	if (service) {
 		DeleteService(service);
-		printf("Service Removed\n");
         CloseServiceHandle (service);
+        printf ("Service deleted, may require reboot to complete removal\n");
 	}
 	else
 		printf("Service not found\n");
     CloseServiceHandle (manager);
+    Sleep (1500);
 }
 void ControlHandler(DWORD request) 
 { 
    switch(request) { 
       case SERVICE_CONTROL_STOP: 
-		global.running = ICE_HALTING;
-		ServiceStatus.dwWin32ExitCode = 0; 
-		ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
-		SetServiceStatus (hStatus, &ServiceStatus);
-		return; 
+          if (ServiceStatus.dwCurrentState != SERVICE_STOP)
+          {
+              ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING; 
+              SetServiceStatus (hStatus, &ServiceStatus);
+              global.running = ICE_HALTING;
+              return; 
+          }
  
-      case SERVICE_CONTROL_SHUTDOWN: 
-		global.running = ICE_HALTING;
-		ServiceStatus.dwWin32ExitCode = 0; 
-		ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
-		SetServiceStatus (hStatus, &ServiceStatus);
-		return; 
       default:
 		break;
     } 
  
     // Report current status
     SetServiceStatus (hStatus, &ServiceStatus);
- 
-    return; 
 }
 
 void ServiceMain(int argc, char** argv) 
 { 
-   int error; 
- 
-   ServiceStatus.dwServiceType = SERVICE_WIN32; 
-   ServiceStatus.dwCurrentState = SERVICE_START_PENDING; 
-   ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
+   ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
    ServiceStatus.dwWin32ExitCode = 0; 
    ServiceStatus.dwServiceSpecificExitCode = 0; 
    ServiceStatus.dwCheckPoint = 0; 
@@ -177,19 +121,12 @@
    hStatus = RegisterServiceCtrlHandler(PACKAGE_STRING, (LPHANDLER_FUNCTION)ControlHandler); 
    if (hStatus == (SERVICE_STATUS_HANDLE)0) { 
       // Registering Control Handler failed
+      MessageBox (NULL, "RegisterServiceCtrlHandler failed", NULL, MB_SERVICE_NOTIFICATION);
       return; 
    }  
-   // Initialize Service 
-   error = InitService(); 
-   if (error) {
-      // Initialization failed
-      ServiceStatus.dwCurrentState = SERVICE_STOPPED; 
-      ServiceStatus.dwWin32ExitCode = -1; 
-      SetServiceStatus(hStatus, &ServiceStatus); 
-      return; 
-   } 
    // We report the running status to SCM. 
    ServiceStatus.dwCurrentState = SERVICE_RUNNING; 
+   ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
    SetServiceStatus (hStatus, &ServiceStatus);
  
    /* Here we do the work */
@@ -205,12 +142,10 @@
         argv2 [2] = argv[1];
     argv2[3] = NULL;
 
-	int ret = mainService(argc2, (char **)argv2);
+	ServiceStatus.dwWin32ExitCode = mainService(argc2, (char **)argv2);
 
 	ServiceStatus.dwCurrentState = SERVICE_STOPPED;
-	ServiceStatus.dwWin32ExitCode = -1;
 	SetServiceStatus(hStatus, &ServiceStatus);
-	return; 
 }
 
 
@@ -224,10 +159,7 @@
     if (!strcmp(argv[1], "install"))
     {
         if (argc > 2)
-        {
-            printf ("Installing service from %s\n", argv[2]);
             installService(argv[2]);
-        }
         else
             printf ("install requires a path arg as well\n");
         Sleep (2000);
@@ -235,16 +167,15 @@
     }
     if (!strcmp(argv[1], "remove") || !strcmp(argv[1], "uninstall"))
     {
-        printf ("removing service\n");
         removeService();
-        Sleep (2000);
         return 0;
     }
 
     if (_chdir(argv[1]) < 0)
     {
-        printf ("unable to change to directory %s\n", argv[1]);
-        Sleep (2000);
+        char buffer [256];
+        _snprintf (buffer, sizeof(buffer), "Unable to change to directory %s", argv[1]);
+        MessageBox (NULL, buffer, NULL, MB_SERVICE_NOTIFICATION);
         return 0;
     }
 
@@ -255,7 +186,8 @@
 	ServiceTable[1].lpServiceName = NULL;
 	ServiceTable[1].lpServiceProc = NULL;
 	// Start the control dispatcher thread for our service
-	StartServiceCtrlDispatcher(ServiceTable);  
+    if (StartServiceCtrlDispatcher(ServiceTable) == 0)
+        MessageBox (NULL, "StartServiceCtrlDispatcher failed", NULL, MB_SERVICE_NOTIFICATION);
 
     return 0;
 }



More information about the commits mailing list