2009-08-05 18:32:47 -04:00
|
|
|
#define _WIN32_WINNT 0x0400
|
2005-09-16 14:29:10 -04:00
|
|
|
#include <windows.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <direct.h>
|
|
|
|
extern "C" {
|
2008-02-10 20:00:41 -05:00
|
|
|
#include "thread/thread.h"
|
|
|
|
#include "avl/avl.h"
|
|
|
|
#include "log/log.h"
|
2005-09-16 14:29:10 -04:00
|
|
|
#include "global.h"
|
2008-02-10 20:00:41 -05:00
|
|
|
#include "httpp/httpp.h"
|
|
|
|
#include "net/sock.h"
|
2005-09-16 14:29:10 -04:00
|
|
|
#include "connection.h"
|
|
|
|
#include "refbuf.h"
|
|
|
|
#include "client.h"
|
|
|
|
#include "stats.h"
|
|
|
|
}
|
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
// Issues to be wary of. Careful of the runtime you use, I've had printf and similar routines
|
|
|
|
// crash because of this on apparently valid strings. some weird thing related to checking for
|
|
|
|
// multiple byte characters. DeleteService only marks a service for deletion, and the docs
|
|
|
|
// are unclear on the cases that lead to purging however a reboot should do it.
|
2005-09-16 14:29:10 -04:00
|
|
|
|
|
|
|
SERVICE_STATUS ServiceStatus;
|
|
|
|
SERVICE_STATUS_HANDLE hStatus;
|
|
|
|
|
|
|
|
void ServiceMain(int argc, char** argv);
|
|
|
|
void ControlHandler(DWORD request);
|
|
|
|
extern "C" int mainService(int argc, char **argv);
|
|
|
|
|
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
void installService (const char *path)
|
2005-09-16 14:29:10 -04:00
|
|
|
{
|
2008-05-11 21:21:06 -04:00
|
|
|
if (path) {
|
|
|
|
char fullPath[8096*2] = "\"";
|
|
|
|
int len = GetModuleFileName (NULL, fullPath+1, sizeof (fullPath)-1);
|
2005-09-16 14:29:10 -04:00
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
_snprintf(fullPath+len+1, sizeof (fullPath)-len, "\" \"%s\"", path);
|
|
|
|
|
|
|
|
SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
|
|
|
|
if (manager == NULL)
|
2008-02-10 20:00:41 -05:00
|
|
|
{
|
2009-08-05 18:32:47 -04:00
|
|
|
MessageBox (NULL, "OpenSCManager failed", NULL, MB_SERVICE_NOTIFICATION);
|
2008-02-10 20:00:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-16 14:29:10 -04:00
|
|
|
SC_HANDLE service = CreateService(
|
2008-05-11 21:21:06 -04:00
|
|
|
manager,
|
|
|
|
PACKAGE_STRING,
|
|
|
|
PACKAGE_STRING " Streaming Media Server",
|
2005-09-16 14:29:10 -04:00
|
|
|
GENERIC_READ | GENERIC_EXECUTE,
|
|
|
|
SERVICE_WIN32_OWN_PROCESS,
|
|
|
|
SERVICE_AUTO_START,
|
|
|
|
SERVICE_ERROR_IGNORE,
|
|
|
|
fullPath,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
);
|
2008-05-11 21:21:06 -04:00
|
|
|
if (service == NULL)
|
2008-02-10 20:00:41 -05:00
|
|
|
{
|
2009-08-05 18:32:47 -04:00
|
|
|
MessageBox (NULL, "CreateService failed", NULL, MB_SERVICE_NOTIFICATION);
|
2008-05-11 21:21:06 -04:00
|
|
|
CloseServiceHandle (manager);
|
2008-02-10 20:00:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-09-16 14:29:10 -04:00
|
|
|
printf("Service Installed\n");
|
2008-02-10 20:00:41 -05:00
|
|
|
CloseServiceHandle (service);
|
2008-05-11 21:21:06 -04:00
|
|
|
CloseServiceHandle (manager);
|
2005-09-16 14:29:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
void removeService()
|
|
|
|
{
|
2008-05-11 21:21:06 -04:00
|
|
|
SC_HANDLE manager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS );
|
|
|
|
if (manager == NULL)
|
2008-02-10 20:00:41 -05:00
|
|
|
{
|
2009-08-05 18:32:47 -04:00
|
|
|
MessageBox (NULL, "OpenSCManager failed", NULL, MB_SERVICE_NOTIFICATION);
|
2008-02-10 20:00:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
SC_HANDLE service = OpenService (manager, PACKAGE_STRING, DELETE);
|
2005-09-16 14:29:10 -04:00
|
|
|
if (service) {
|
|
|
|
DeleteService(service);
|
2008-05-11 21:21:06 -04:00
|
|
|
CloseServiceHandle (service);
|
2009-08-05 18:32:47 -04:00
|
|
|
printf ("Service deleted, may require reboot to complete removal\n");
|
2005-09-16 14:29:10 -04:00
|
|
|
}
|
2008-02-10 20:00:41 -05:00
|
|
|
else
|
|
|
|
printf("Service not found\n");
|
2008-05-11 21:21:06 -04:00
|
|
|
CloseServiceHandle (manager);
|
2009-08-05 18:32:47 -04:00
|
|
|
Sleep (1500);
|
2005-09-16 14:29:10 -04:00
|
|
|
}
|
|
|
|
void ControlHandler(DWORD request)
|
|
|
|
{
|
|
|
|
switch(request) {
|
|
|
|
case SERVICE_CONTROL_STOP:
|
2009-08-05 18:32:47 -04:00
|
|
|
if (ServiceStatus.dwCurrentState != SERVICE_STOP)
|
|
|
|
{
|
|
|
|
ServiceStatus.dwCurrentState = SERVICE_STOP_PENDING;
|
|
|
|
SetServiceStatus (hStatus, &ServiceStatus);
|
|
|
|
global.running = ICE_HALTING;
|
|
|
|
return;
|
|
|
|
}
|
2005-09-16 14:29:10 -04:00
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report current status
|
|
|
|
SetServiceStatus (hStatus, &ServiceStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ServiceMain(int argc, char** argv)
|
|
|
|
{
|
2009-08-05 18:32:47 -04:00
|
|
|
ServiceStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
|
2005-09-16 14:29:10 -04:00
|
|
|
ServiceStatus.dwWin32ExitCode = 0;
|
|
|
|
ServiceStatus.dwServiceSpecificExitCode = 0;
|
|
|
|
ServiceStatus.dwCheckPoint = 0;
|
|
|
|
ServiceStatus.dwWaitHint = 0;
|
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
hStatus = RegisterServiceCtrlHandler(PACKAGE_STRING, (LPHANDLER_FUNCTION)ControlHandler);
|
2005-09-16 14:29:10 -04:00
|
|
|
if (hStatus == (SERVICE_STATUS_HANDLE)0) {
|
|
|
|
// Registering Control Handler failed
|
2009-08-05 18:32:47 -04:00
|
|
|
MessageBox (NULL, "RegisterServiceCtrlHandler failed", NULL, MB_SERVICE_NOTIFICATION);
|
2005-09-16 14:29:10 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// We report the running status to SCM.
|
|
|
|
ServiceStatus.dwCurrentState = SERVICE_RUNNING;
|
2009-08-05 18:32:47 -04:00
|
|
|
ServiceStatus.dwControlsAccepted = SERVICE_ACCEPT_STOP;
|
2005-09-16 14:29:10 -04:00
|
|
|
SetServiceStatus (hStatus, &ServiceStatus);
|
|
|
|
|
|
|
|
/* Here we do the work */
|
|
|
|
|
|
|
|
int argc2 = 3;
|
2008-05-11 21:21:06 -04:00
|
|
|
char* argv2 [4];
|
2005-09-16 14:29:10 -04:00
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
argv2 [0] = argv[0];
|
|
|
|
argv2 [1] = "-c";
|
|
|
|
if (argc < 2)
|
|
|
|
argv2 [2] = "icecast.xml";
|
|
|
|
else
|
|
|
|
argv2 [2] = argv[1];
|
|
|
|
argv2[3] = NULL;
|
2005-09-16 14:29:10 -04:00
|
|
|
|
2009-08-05 18:32:47 -04:00
|
|
|
ServiceStatus.dwWin32ExitCode = mainService(argc2, (char **)argv2);
|
2005-09-16 14:29:10 -04:00
|
|
|
|
|
|
|
ServiceStatus.dwCurrentState = SERVICE_STOPPED;
|
|
|
|
SetServiceStatus(hStatus, &ServiceStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
int main(int argc, char **argv)
|
2005-09-16 14:29:10 -04:00
|
|
|
{
|
2008-05-11 21:21:06 -04:00
|
|
|
if (argc < 2)
|
|
|
|
{
|
|
|
|
printf ("Usage:\n %s remove\n %s install path\n", argv[0], argv[0]);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "install"))
|
|
|
|
{
|
|
|
|
if (argc > 2)
|
|
|
|
installService(argv[2]);
|
|
|
|
else
|
|
|
|
printf ("install requires a path arg as well\n");
|
|
|
|
Sleep (2000);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "remove") || !strcmp(argv[1], "uninstall"))
|
|
|
|
{
|
|
|
|
removeService();
|
|
|
|
return 0;
|
|
|
|
}
|
2005-09-16 14:29:10 -04:00
|
|
|
|
2008-05-11 21:21:06 -04:00
|
|
|
if (_chdir(argv[1]) < 0)
|
|
|
|
{
|
2009-08-05 18:32:47 -04:00
|
|
|
char buffer [256];
|
|
|
|
_snprintf (buffer, sizeof(buffer), "Unable to change to directory %s", argv[1]);
|
|
|
|
MessageBox (NULL, buffer, NULL, MB_SERVICE_NOTIFICATION);
|
2008-05-11 21:21:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
2005-09-16 14:29:10 -04:00
|
|
|
|
|
|
|
SERVICE_TABLE_ENTRY ServiceTable[2];
|
2008-05-11 21:21:06 -04:00
|
|
|
ServiceTable[0].lpServiceName = PACKAGE_STRING;
|
2005-09-16 14:29:10 -04:00
|
|
|
ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
|
|
|
|
|
|
|
|
ServiceTable[1].lpServiceName = NULL;
|
|
|
|
ServiceTable[1].lpServiceProc = NULL;
|
|
|
|
// Start the control dispatcher thread for our service
|
2009-08-05 18:32:47 -04:00
|
|
|
if (StartServiceCtrlDispatcher(ServiceTable) == 0)
|
|
|
|
MessageBox (NULL, "StartServiceCtrlDispatcher failed", NULL, MB_SERVICE_NOTIFICATION);
|
2008-05-11 21:21:06 -04:00
|
|
|
|
|
|
|
return 0;
|
2008-02-10 20:00:41 -05:00
|
|
|
}
|