Commit 3c481655 authored by Karl Heyes's avatar Karl Heyes

win32 updates. cleanup of the start and finish of the gui and service

svn path=/icecast/trunk/icecast/; revision=16440
parent 1f92e2ab
...@@ -44,6 +44,7 @@ ...@@ -44,6 +44,7 @@
# define uint64_t unsigned __int64 # define uint64_t unsigned __int64
# define uint32_t unsigned int # define uint32_t unsigned int
# define PRIu64 "I64u" # define PRIu64 "I64u"
# define PRId64 "I64d"
#else #else
# define PATH_SEPARATOR "/" # define PATH_SEPARATOR "/"
# if defined(HAVE_INTTYPES_H) # if defined(HAVE_INTTYPES_H)
......
...@@ -17,6 +17,16 @@ ...@@ -17,6 +17,16 @@
#include <stdio.h> #include <stdio.h>
#include <string.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 #ifdef HAVE_UNISTD_H
# include <unistd.h> # include <unistd.h>
...@@ -35,7 +45,6 @@ ...@@ -35,7 +45,6 @@
#include <sys/types.h> #include <sys/types.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <errno.h>
#endif #endif
#include "cfgfile.h" #include "cfgfile.h"
...@@ -56,14 +65,6 @@ ...@@ -56,14 +65,6 @@
#include <libxml/xmlmemory.h> #include <libxml/xmlmemory.h>
#ifdef _WIN32
/* For getpid() */
#include <process.h>
#include <windows.h>
#define snprintf _snprintf
#define getpid _getpid
#endif
#undef CATMODULE #undef CATMODULE
#define CATMODULE "main" #define CATMODULE "main"
...@@ -72,7 +73,9 @@ static char *pidfile = NULL; ...@@ -72,7 +73,9 @@ static char *pidfile = NULL;
static void _fatal_error(char *perr) 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); MessageBox(NULL, perr, "Error", MB_OK);
#else #else
fprintf(stdout, "%s\n", perr); fprintf(stdout, "%s\n", perr);
...@@ -97,7 +100,7 @@ static void _stop_logging(void) ...@@ -97,7 +100,7 @@ static void _stop_logging(void)
log_close(playlistlog); log_close(playlistlog);
} }
static void _initialize_subsystems(void) void initialize_subsystems(void)
{ {
log_initialize(); log_initialize();
thread_initialize(); thread_initialize();
...@@ -107,23 +110,16 @@ static void _initialize_subsystems(void) ...@@ -107,23 +110,16 @@ static void _initialize_subsystems(void)
connection_initialize(); connection_initialize();
global_initialize(); global_initialize();
refbuf_initialize(); refbuf_initialize();
#if !defined(WIN32) || defined(WIN32_SERVICE)
/* win32 GUI needs to do the initialise before here */
xslt_initialize(); xslt_initialize();
#ifdef HAVE_CURL_GLOBAL_INIT #ifdef HAVE_CURL_GLOBAL_INIT
curl_global_init (CURL_GLOBAL_ALL); curl_global_init (CURL_GLOBAL_ALL);
#endif #endif
#endif
} }
static void _shutdown_subsystems(void) void shutdown_subsystems(void)
{ {
fserve_shutdown(); 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(); refbuf_shutdown();
slave_shutdown(); slave_shutdown();
auth_shutdown(); auth_shutdown();
...@@ -144,6 +140,7 @@ static void _shutdown_subsystems(void) ...@@ -144,6 +140,7 @@ static void _shutdown_subsystems(void)
/* Now that these are done, we can stop the loggers. */ /* Now that these are done, we can stop the loggers. */
_stop_logging(); _stop_logging();
log_shutdown(); log_shutdown();
xslt_shutdown();
} }
static int _parse_config_opts(int argc, char **argv, char *filename, int size) static int _parse_config_opts(int argc, char **argv, char *filename, int size)
...@@ -425,9 +422,10 @@ int main(int argc, char **argv) ...@@ -425,9 +422,10 @@ int main(int argc, char **argv)
*/ */
res = _parse_config_opts(argc, argv, filename, 512); res = _parse_config_opts(argc, argv, filename, 512);
if (res == 1) { if (res == 1) {
#if !defined(_WIN32) || defined(_CONSOLE)
/* startup all the modules */ /* startup all the modules */
_initialize_subsystems(); initialize_subsystems();
#endif
/* parse the config file */ /* parse the config file */
config_get_config(); config_get_config();
ret = config_initial_parse_file(filename); ret = config_initial_parse_file(filename);
...@@ -451,7 +449,9 @@ int main(int argc, char **argv) ...@@ -451,7 +449,9 @@ int main(int argc, char **argv)
_fatal_error("XML config parsing error"); _fatal_error("XML config parsing error");
break; break;
} }
_shutdown_subsystems(); #if !defined(_WIN32) || defined(_CONSOLE)
shutdown_subsystems();
#endif
return 1; return 1;
} }
} else if (res == -1) { } else if (res == -1) {
...@@ -465,7 +465,7 @@ int main(int argc, char **argv) ...@@ -465,7 +465,7 @@ int main(int argc, char **argv)
/* Bind socket, before we change userid */ /* Bind socket, before we change userid */
if(!_server_proc_init()) { if(!_server_proc_init()) {
_fatal_error("Server startup failed. Exiting"); _fatal_error("Server startup failed. Exiting");
_shutdown_subsystems(); shutdown_subsystems();
return 1; return 1;
} }
...@@ -481,7 +481,7 @@ int main(int argc, char **argv) ...@@ -481,7 +481,7 @@ int main(int argc, char **argv)
{ {
fprintf(stderr, "ERROR: You should not run icecast2 as root\n"); fprintf(stderr, "ERROR: You should not run icecast2 as root\n");
fprintf(stderr, "Use the changeowner directive in the config file\n"); fprintf(stderr, "Use the changeowner directive in the config file\n");
_shutdown_subsystems(); shutdown_subsystems();
return 1; return 1;
} }
#endif #endif
...@@ -491,7 +491,7 @@ int main(int argc, char **argv) ...@@ -491,7 +491,7 @@ int main(int argc, char **argv)
if (!_start_logging()) { if (!_start_logging()) {
_fatal_error("FATAL: Could not start logging"); _fatal_error("FATAL: Could not start logging");
_shutdown_subsystems(); shutdown_subsystems();
return 1; return 1;
} }
...@@ -512,9 +512,9 @@ int main(int argc, char **argv) ...@@ -512,9 +512,9 @@ int main(int argc, char **argv)
_server_proc(); _server_proc();
INFO0("Shutting down"); INFO0("Shutting down");
#if !defined(_WIN32) || defined(_CONSOLE)
_shutdown_subsystems(); shutdown_subsystems();
#endif
if (pidfile) if (pidfile)
{ {
remove (pidfile); remove (pidfile);
......
...@@ -41,6 +41,7 @@ ...@@ -41,6 +41,7 @@
#include "logging.h" #include "logging.h"
#ifdef _WIN32 #ifdef _WIN32
#define atoll _atoi64
#define vsnprintf _vsnprintf #define vsnprintf _vsnprintf
#define snprintf _snprintf #define snprintf _snprintf
#endif #endif
...@@ -445,7 +446,7 @@ static void modify_node_event (stats_node_t *node, stats_event_t *event) ...@@ -445,7 +446,7 @@ static void modify_node_event (stats_node_t *node, stats_event_t *event)
} }
if (event->action != STATS_EVENT_SET) if (event->action != STATS_EVENT_SET)
{ {
int value = 0; int64_t value = 0;
switch (event->action) switch (event->action)
{ {
...@@ -466,7 +467,7 @@ static void modify_node_event (stats_node_t *node, stats_event_t *event) ...@@ -466,7 +467,7 @@ static void modify_node_event (stats_node_t *node, stats_event_t *event)
break; break;
} }
str = malloc (16); str = malloc (16);
snprintf (str, 16, "%d", value); snprintf (str, 16, "%" PRId64, value);
if (event->value == NULL) if (event->value == NULL)
event->value = strdup (str); event->value = strdup (str);
} }
......
...@@ -5,6 +5,11 @@ ...@@ -5,6 +5,11 @@
#include "Icecast2win.h" #include "Icecast2win.h"
#include "Icecast2winDlg.h" #include "Icecast2winDlg.h"
extern "C" {
#include "xslt.h"
void initialize_subsystems(void);
void shutdown_subsystems(void);
}
#ifdef _DEBUG #ifdef _DEBUG
#define new DEBUG_NEW #define new DEBUG_NEW
#undef THIS_FILE #undef THIS_FILE
...@@ -49,6 +54,7 @@ BOOL CIcecast2winApp::InitInstance() ...@@ -49,6 +54,7 @@ BOOL CIcecast2winApp::InitInstance()
// of your final executable, you should remove from the following // of your final executable, you should remove from the following
// the specific initialization routines you do not need. // the specific initialization routines you do not need.
initialize_subsystems();
if (strlen(m_lpCmdLine) > 0) { if (strlen(m_lpCmdLine) > 0) {
strcpy(m_configFile, m_lpCmdLine); strcpy(m_configFile, m_lpCmdLine);
} }
...@@ -87,3 +93,9 @@ BOOL CIcecast2winApp::InitInstance() ...@@ -87,3 +93,9 @@ BOOL CIcecast2winApp::InitInstance()
// application, rather than start the application's message pump. // application, rather than start the application's message pump.
return FALSE; return FALSE;
} }
int CIcecast2winApp::ExitInstance()
{
shutdown_subsystems();
return CWinApp::ExitInstance();
}
...@@ -31,6 +31,7 @@ public: ...@@ -31,6 +31,7 @@ public:
//{{AFX_VIRTUAL(CIcecast2winApp) //{{AFX_VIRTUAL(CIcecast2winApp)
public: public:
virtual BOOL InitInstance(); virtual BOOL InitInstance();
virtual int ExitInstance();
//}}AFX_VIRTUAL //}}AFX_VIRTUAL
// Implementation // Implementation
......
...@@ -339,9 +339,6 @@ BOOL CIcecast2winDlg::OnInitDialog() ...@@ -339,9 +339,6 @@ BOOL CIcecast2winDlg::OnInitDialog()
sprintf(version, "Icecast2 Version %s", ICECAST_VERSION); sprintf(version, "Icecast2 Version %s", ICECAST_VERSION);
SetWindowText(version); SetWindowText(version);
xslt_initialize();
curl_global_init (CURL_GLOBAL_ALL);
if (m_Autostart) { if (m_Autostart) {
OnStart(); OnStart();
} }
......
#define _WIN32_WINNT 0x0400
#include <windows.h> #include <windows.h>
#include <stdio.h> #include <stdio.h>
#include <direct.h> #include <direct.h>
...@@ -24,14 +25,8 @@ SERVICE_STATUS_HANDLE hStatus; ...@@ -24,14 +25,8 @@ SERVICE_STATUS_HANDLE hStatus;
void ServiceMain(int argc, char** argv); void ServiceMain(int argc, char** argv);
void ControlHandler(DWORD request); void ControlHandler(DWORD request);
int InitService();
extern "C" int mainService(int argc, char **argv); extern "C" int mainService(int argc, char **argv);
int InitService()
{
int result = 0;
return(result);
}
void installService (const char *path) void installService (const char *path)
{ {
...@@ -44,21 +39,7 @@ void installService (const char *path) ...@@ -44,21 +39,7 @@ void installService (const char *path)
SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if (manager == NULL) if (manager == NULL)
{ {
LPVOID lpMsgBuf; MessageBox (NULL, "OpenSCManager failed", NULL, MB_SERVICE_NOTIFICATION);
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 );
return; return;
} }
...@@ -79,21 +60,7 @@ void installService (const char *path) ...@@ -79,21 +60,7 @@ void installService (const char *path)
); );
if (service == NULL) if (service == NULL)
{ {
LPVOID lpMsgBuf; MessageBox (NULL, "CreateService failed", NULL, MB_SERVICE_NOTIFICATION);
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 );
CloseServiceHandle (manager); CloseServiceHandle (manager);
return; return;
} }
...@@ -108,67 +75,44 @@ void removeService() ...@@ -108,67 +75,44 @@ void removeService()
SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS ); SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
if (manager == NULL) if (manager == NULL)
{ {
LPVOID lpMsgBuf; MessageBox (NULL, "OpenSCManager failed", NULL, MB_SERVICE_NOTIFICATION);
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 );
return; return;
} }
SC_HANDLE service = OpenService (manager, PACKAGE_STRING, DELETE); SC_HANDLE service = OpenService (manager, PACKAGE_STRING, DELETE);
if (service) { if (service) {
DeleteService(service); DeleteService(service);
printf("Service Removed\n");
CloseServiceHandle (service); CloseServiceHandle (service);
printf ("Service deleted, may require reboot to complete removal\n");
} }
else else
printf("Service not found\n"); printf("Service not found\n");
CloseServiceHandle (manager); CloseServiceHandle (manager);
Sleep (1500);
} }
void ControlHandler(DWORD request) void ControlHandler(DWORD request)
{ {
switch(request) { switch(request) {
case SERVICE_CONTROL_STOP: case SERVICE_CONTROL_STOP:
global.running = ICE_HALTING; if (ServiceStatus.dwCurrentState != SERVICE_STOP)
ServiceStatus.dwWin32ExitCode = 0; {
ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
SetServiceStatus (hStatus, &ServiceStatus); SetServiceStatus (hStatus, &ServiceStatus);
return; 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: default:
break; break;
} }
// Report current status // Report current status
SetServiceStatus (hStatus, &ServiceStatus); SetServiceStatus (hStatus, &ServiceStatus);
return;
} }
void ServiceMain(int argc, char** argv) void ServiceMain(int argc, char** argv)
{ {
int error; ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
ServiceStatus.dwServiceType = SERVICE_WIN32;
ServiceStatus.dwCurrentState = SERVICE_START_PENDING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN;
ServiceStatus.dwWin32ExitCode = 0; ServiceStatus.dwWin32ExitCode = 0;
ServiceStatus.dwServiceSpecificExitCode = 0; ServiceStatus.dwServiceSpecificExitCode = 0;
ServiceStatus.dwCheckPoint = 0; ServiceStatus.dwCheckPoint = 0;
...@@ -177,19 +121,12 @@ void ServiceMain(int argc, char** argv) ...@@ -177,19 +121,12 @@ void ServiceMain(int argc, char** argv)
hStatus = RegisterServiceCtrlHandler(PACKAGE_STRING, (LPHANDLER_FUNCTION)ControlHandler); hStatus = RegisterServiceCtrlHandler(PACKAGE_STRING, (LPHANDLER_FUNCTION)ControlHandler);
if (hStatus == (SERVICE_STATUS_HANDLE)0) { if (hStatus == (SERVICE_STATUS_HANDLE)0) {
// Registering Control Handler failed // Registering Control Handler failed
MessageBox (NULL, "RegisterServiceCtrlHandler failed", NULL, MB_SERVICE_NOTIFICATION);
return; 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. // We report the running status to SCM.
ServiceStatus.dwCurrentState = SERVICE_RUNNING; ServiceStatus.dwCurrentState = SERVICE_RUNNING;
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
SetServiceStatus (hStatus, &ServiceStatus); SetServiceStatus (hStatus, &ServiceStatus);
/* Here we do the work */ /* Here we do the work */
...@@ -205,12 +142,10 @@ void ServiceMain(int argc, char** argv) ...@@ -205,12 +142,10 @@ void ServiceMain(int argc, char** argv)
argv2 [2] = argv[1]; argv2 [2] = argv[1];
argv2[3] = NULL; argv2[3] = NULL;
int ret = mainService(argc2, (char **)argv2); ServiceStatus.dwWin32ExitCode = mainService(argc2, (char **)argv2);
ServiceStatus.dwCurrentState = SERVICE_STOPPED; ServiceStatus.dwCurrentState = SERVICE_STOPPED;
ServiceStatus.dwWin32ExitCode = -1;
SetServiceStatus(hStatus, &ServiceStatus); SetServiceStatus(hStatus, &ServiceStatus);
return;
} }
...@@ -224,10 +159,7 @@ int main(int argc, char **argv) ...@@ -224,10 +159,7 @@ int main(int argc, char **argv)
if (!strcmp(argv[1], "install")) if (!strcmp(argv[1], "install"))
{ {
if (argc > 2) if (argc > 2)
{
printf ("Installing service from %s\n", argv[2]);
installService(argv[2]); installService(argv[2]);
}
else else
printf ("install requires a path arg as well\n"); printf ("install requires a path arg as well\n");
Sleep (2000); Sleep (2000);
...@@ -235,16 +167,15 @@ int main(int argc, char **argv) ...@@ -235,16 +167,15 @@ int main(int argc, char **argv)
} }
if (!strcmp(argv[1], "remove") || !strcmp(argv[1], "uninstall")) if (!strcmp(argv[1], "remove") || !strcmp(argv[1], "uninstall"))
{ {
printf ("removing service\n");
removeService(); removeService();
Sleep (2000);
return 0; return 0;
} }
if (_chdir(argv[1]) < 0) if (_chdir(argv[1]) < 0)
{ {
printf ("unable to change to directory %s\n", argv[1]); char buffer [256];
Sleep (2000); _snprintf (buffer, sizeof(buffer), "Unable to change to directory %s", argv[1]);
MessageBox (NULL, buffer, NULL, MB_SERVICE_NOTIFICATION);
return 0; return 0;
} }
...@@ -255,7 +186,8 @@ int main(int argc, char **argv) ...@@ -255,7 +186,8 @@ int main(int argc, char **argv)
ServiceTable[1].lpServiceName = NULL; ServiceTable[1].lpServiceName = NULL;
ServiceTable[1].lpServiceProc = NULL; ServiceTable[1].lpServiceProc = NULL;
// Start the control dispatcher thread for our service // Start the control dispatcher thread for our service
StartServiceCtrlDispatcher(ServiceTable); if (StartServiceCtrlDispatcher(ServiceTable) == 0)
MessageBox (NULL, "StartServiceCtrlDispatcher failed", NULL, MB_SERVICE_NOTIFICATION);
return 0; return 0;
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment