diff options
author | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-08-24 19:08:36 +0000 |
---|---|---|
committer | brain <brain@e03df62e-2008-0410-955e-edbf42e46eb7> | 2008-08-24 19:08:36 +0000 |
commit | a6cf47a2cd96c459bfc241c6dce8ca8454140484 (patch) | |
tree | 8330b5a6304d8a59fdf37328a6e74eb9745d0d65 /win/win32service.cpp | |
parent | 6a149a4718ead139535ae038c2d8f8847f1a594c (diff) |
work in progress
git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@10238 e03df62e-2008-0410-955e-edbf42e46eb7
Diffstat (limited to 'win/win32service.cpp')
-rw-r--r-- | win/win32service.cpp | 56 |
1 files changed, 41 insertions, 15 deletions
diff --git a/win/win32service.cpp b/win/win32service.cpp index 0fb94f3e4..5923c1458 100644 --- a/win/win32service.cpp +++ b/win/win32service.cpp @@ -40,6 +40,16 @@ struct Commandline /* A function pointer for dynamic linking tricks */ SETSERVDESC ChangeServiceConf; +bool IsAService(); +{ + USEROBJECTFLAGS uoflags; + HWINSTA winstation = GetProcessWindowStation(); + if (GetUserObjectInformation(winstation, UOI_FLAGS, &uoflags, sizeof(uoflags), NULL)) + return ((uoflags.dwFlags & WSF_VISIBLE) == 0); + else + return false; +} + /* Kills the service by setting an event which the other thread picks up and exits */ void KillService() { @@ -61,6 +71,22 @@ DWORD WINAPI WorkerThread(LPDWORD param) return 0; } +/* This is called when all startup is done */ +void SetServiceRunning() +{ + if (!IsAService()) + return; + + serviceCurrentStatus = SERVICE_RUNNING; + success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0); + if (!success) + { + terminateService(6, GetLastError()); + return; + } +} + + /** Starts the worker thread above */ void StartServiceThread() { @@ -110,10 +136,22 @@ BOOL UpdateSCMStatus (DWORD dwCurrentState, DWORD dwWin32ExitCode, DWORD dwServi /** This function is called by us when the service is being shut down or when it can't be started */ void terminateService (int code, int wincode) { - UpdateSCMStatus(SERVICE_STOPPED,wincode?wincode:ERROR_SERVICE_SPECIFIC_ERROR,(wincode)?0:code,0,0); + UpdateSCMStatus(SERVICE_STOPPED, wincode ? wincode : ERROR_SERVICE_SPECIFIC_ERROR, wincode ? 0 : code, 0, 0); return; } +/* In windows we hook this to exit() */ +void newexit(int status) +{ + if (!IsAService()) + exit(status); + + /* Are we running as a service? If so, trigger the service specific exit code */ + terminateService(status, 0); + KillService(); + exit(status); +} + /** This callback is called by windows when the state of the service has been changed */ VOID ServiceCtrlHandler (DWORD controlCode) { @@ -171,13 +209,6 @@ VOID ServiceMain(DWORD argc, LPTSTR *argv) } StartServiceThread(); - serviceCurrentStatus = SERVICE_RUNNING; - success = UpdateSCMStatus(SERVICE_RUNNING, NO_ERROR, 0, 0, 0); - if (!success) - { - terminateService(6, GetLastError()); - return; - } WaitForSingleObject (killServiceEvent, INFINITE); } @@ -323,13 +354,8 @@ int main(int argc, char** argv) /* Check if the process is running interactively. InspIRCd does not run interactively * as a service so if this is true, we just run the non-service inspircd. */ - USEROBJECTFLAGS uoflags; - HWINSTA winstation = GetProcessWindowStation(); - if (GetUserObjectInformation(winstation, UOI_FLAGS, &uoflags, sizeof(uoflags), NULL)) - { - if (uoflags.dwFlags == WSF_VISIBLE) - return smain(argc, argv); - } + if (!IsAService()) + return smain(argv, argc); /* If we get here, we know the service is installed so we can start it */ |