X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=nssm.cpp;h=0ce86640c74346122a8345577448fee54695f71f;hb=636b16d3702049145143511c6461ff20bf1f245d;hp=dec474574467b9beb4138f1f8c26f8711624820e;hpb=f2ec1c0c55a6b3e8ca02b3d66b78c87fe0ac1f47;p=nssm.git diff --git a/nssm.cpp b/nssm.cpp index dec4745..0ce8664 100644 --- a/nssm.cpp +++ b/nssm.cpp @@ -2,6 +2,7 @@ extern unsigned long tls_index; extern bool is_admin; +extern imports_t imports; /* String function */ int str_equiv(const char *a, const char *b) { @@ -58,15 +59,32 @@ int main(int argc, char **argv) { /* Register messages */ if (is_admin) create_messages(); - /* Start service magic */ - SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } }; - if (! StartServiceCtrlDispatcher(table)) { - unsigned long error = GetLastError(); - /* User probably ran nssm with no argument */ - if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1)); - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0); - exit(100); + /* + Optimisation for Windows 2000: + When we're run from the command line the StartServiceCtrlDispatcher() call + will time out after a few seconds on Windows 2000. On newer versions the + call returns instantly. Check for stdin first and only try to call the + function if there's no input stream found. Although it's possible that + we're running with input redirected it's much more likely that we're + actually running as a service. + This will save time when running with no arguments from a command prompt. + */ + if (_fileno(stdin) < 0) { + /* Set up function pointers. */ + if (get_imports()) exit(111); + + /* Start service magic */ + SERVICE_TABLE_ENTRY table[] = { { NSSM, service_main }, { 0, 0 } }; + if (! StartServiceCtrlDispatcher(table)) { + unsigned long error = GetLastError(); + /* User probably ran nssm with no argument */ + if (error == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) exit(usage(1)); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_DISPATCHER_FAILED, error_string(error), 0); + free_imports(); + exit(100); + } } + else exit(usage(1)); /* And nothing more to do */ exit(0);