From 3c4816555ecb08b8f1eadab75d8625cdc2f38075 Mon Sep 17 00:00:00 2001 From: Karl Heyes Date: Wed, 5 Aug 2009 22:32:47 +0000 Subject: [PATCH] win32 updates. cleanup of the start and finish of the gui and service svn path=/icecast/trunk/icecast/; revision=16440 --- src/compat.h | 1 + src/main.c | 58 ++++++++++---------- src/stats.c | 5 +- win32/Icecast2win.cpp | 12 +++++ win32/Icecast2win.h | 1 + win32/Icecast2winDlg.cpp | 3 -- win32/icecastService.cpp | 112 ++++++++------------------------------- 7 files changed, 68 insertions(+), 124 deletions(-) diff --git a/src/compat.h b/src/compat.h index b4c4fa79..2628c62d 100644 --- a/src/compat.h +++ b/src/compat.h @@ -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) diff --git a/src/main.c b/src/main.c index 4e044a72..99b8411d 100644 --- a/src/main.c +++ b/src/main.c @@ -17,6 +17,16 @@ #include #include +#include + +#ifdef WIN32 +#define _WIN32_WINNT 0x0400 +/* For getpid() */ +#include +#include +#define snprintf _snprintf +#define getpid _getpid +#endif #ifdef HAVE_UNISTD_H # include @@ -35,7 +45,6 @@ #include #include #include -#include #endif #include "cfgfile.h" @@ -56,14 +65,6 @@ #include -#ifdef _WIN32 -/* For getpid() */ -#include -#include -#define snprintf _snprintf -#define getpid _getpid -#endif - #undef CATMODULE #define CATMODULE "main" @@ -72,7 +73,9 @@ static char *pidfile = NULL; 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 @@ static void _stop_logging(void) log_close(playlistlog); } -static void _initialize_subsystems(void) +void initialize_subsystems(void) { log_initialize(); thread_initialize(); @@ -107,23 +110,16 @@ static void _initialize_subsystems(void) 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 @@ static void _shutdown_subsystems(void) /* 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 @@ int main(int argc, char **argv) */ 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 @@ int main(int argc, char **argv) _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 @@ int main(int argc, char **argv) /* 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 @@ int main(int argc, char **argv) { 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 @@ int main(int argc, char **argv) if (!_start_logging()) { _fatal_error("FATAL: Could not start logging"); - _shutdown_subsystems(); + shutdown_subsystems(); return 1; } @@ -512,9 +512,9 @@ int main(int argc, char **argv) _server_proc(); INFO0("Shutting down"); - - _shutdown_subsystems(); - +#if !defined(_WIN32) || defined(_CONSOLE) + shutdown_subsystems(); +#endif if (pidfile) { remove (pidfile); diff --git a/src/stats.c b/src/stats.c index d670b34a..4567e078 100644 --- a/src/stats.c +++ b/src/stats.c @@ -41,6 +41,7 @@ #include "logging.h" #ifdef _WIN32 +#define atoll _atoi64 #define vsnprintf _vsnprintf #define snprintf _snprintf #endif @@ -445,7 +446,7 @@ static void modify_node_event (stats_node_t *node, stats_event_t *event) } if (event->action != STATS_EVENT_SET) { - int value = 0; + int64_t value = 0; switch (event->action) { @@ -466,7 +467,7 @@ static void modify_node_event (stats_node_t *node, stats_event_t *event) break; } str = malloc (16); - snprintf (str, 16, "%d", value); + snprintf (str, 16, "%" PRId64, value); if (event->value == NULL) event->value = strdup (str); } diff --git a/win32/Icecast2win.cpp b/win32/Icecast2win.cpp index 2dd64d79..e02e2ec2 100644 --- a/win32/Icecast2win.cpp +++ b/win32/Icecast2win.cpp @@ -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 @@ BOOL CIcecast2winApp::InitInstance() // 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 @@ BOOL CIcecast2winApp::InitInstance() // application, rather than start the application's message pump. return FALSE; } + +int CIcecast2winApp::ExitInstance() +{ + shutdown_subsystems(); + return CWinApp::ExitInstance(); +} diff --git a/win32/Icecast2win.h b/win32/Icecast2win.h index 4f1788b3..c4f3e8fe 100644 --- a/win32/Icecast2win.h +++ b/win32/Icecast2win.h @@ -31,6 +31,7 @@ public: //{{AFX_VIRTUAL(CIcecast2winApp) public: virtual BOOL InitInstance(); + virtual int ExitInstance(); //}}AFX_VIRTUAL // Implementation diff --git a/win32/Icecast2winDlg.cpp b/win32/Icecast2winDlg.cpp index 39aea127..5ca94de8 100644 --- a/win32/Icecast2winDlg.cpp +++ b/win32/Icecast2winDlg.cpp @@ -339,9 +339,6 @@ BOOL CIcecast2winDlg::OnInitDialog() sprintf(version, "Icecast2 Version %s", ICECAST_VERSION); SetWindowText(version); - xslt_initialize(); - curl_global_init (CURL_GLOBAL_ALL); - if (m_Autostart) { OnStart(); } diff --git a/win32/icecastService.cpp b/win32/icecastService.cpp index 9dc58a17..20b27720 100644 --- a/win32/icecastService.cpp +++ b/win32/icecastService.cpp @@ -1,3 +1,4 @@ +#define _WIN32_WINNT 0x0400 #include #include #include @@ -24,14 +25,8 @@ SERVICE_STATUS_HANDLE hStatus; 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 @@ void installService (const char *path) 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 @@ void installService (const char *path) ); 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 @@ void removeService() 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 @@ void ServiceMain(int argc, char** argv) 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 @@ void ServiceMain(int argc, char** argv) 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 @@ int main(int argc, char **argv) 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 @@ int main(int argc, char **argv) } 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 @@ int main(int argc, char **argv) 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; }