X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=ea4b633ef290e35071cc30e6dc1b68801e3c4630;hb=135329c0790e6d33f658727be3e5439a167a7da8;hp=fc9b73c2bfefe7f8b360e02171808cd4a8e74ec2;hpb=5b9e64a9ae1fbf1254c9c246e5b123d3aa77a37a;p=nssm.git diff --git a/service.cpp b/service.cpp index fc9b73c..ea4b633 100644 --- a/service.cpp +++ b/service.cpp @@ -142,20 +142,8 @@ int install_service(nssm_service_t *service) { } /* Get path of this program */ - TCHAR path[MAX_PATH]; - GetModuleFileName(0, path, MAX_PATH); - - /* Construct command */ - TCHAR command[CMD_LENGTH]; - size_t pathlen = _tcslen(path); - if (pathlen + 1 >= VALUE_LENGTH) { - print_message(stderr, NSSM_MESSAGE_PATH_TOO_LONG, NSSM); - return 3; - } - if (_sntprintf_s(command, sizeof(command), _TRUNCATE, _T("\"%s\""), path) < 0) { - print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY_FOR_IMAGEPATH); - return 4; - } + TCHAR command[MAX_PATH]; + GetModuleFileName(0, command, _countof(command)); /* Create the service */ service->handle = CreateService(services, service->name, service->name, SC_MANAGER_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, command, 0, 0, 0, 0, 0); @@ -563,12 +551,14 @@ void CALLBACK end_service(void *arg, unsigned char why) { /* Check exit code */ unsigned long exitcode = 0; TCHAR code[16]; - GetExitCodeProcess(service->process_handle, &exitcode); - if (exitcode == STILL_ACTIVE || get_process_exit_time(service->process_handle, &service->exit_time)) GetSystemTimeAsFileTime(&service->exit_time); - CloseHandle(service->process_handle); + if (service->process_handle) { + GetExitCodeProcess(service->process_handle, &exitcode); + if (exitcode == STILL_ACTIVE || get_process_exit_time(service->process_handle, &service->exit_time)) GetSystemTimeAsFileTime(&service->exit_time); + CloseHandle(service->process_handle); + } + else GetSystemTimeAsFileTime(&service->exit_time); service->process_handle = 0; - service->pid = 0; /* Log that the service ended BEFORE logging about killing the process @@ -581,7 +571,8 @@ void CALLBACK end_service(void *arg, unsigned char why) { /* Clean up. */ if (exitcode == STILL_ACTIVE) exitcode = 0; - kill_process_tree(service, service->pid, exitcode, service->pid); + if (service->pid) kill_process_tree(service, service->pid, exitcode, service->pid); + service->pid = 0; /* The why argument is true if our wait timed out or false otherwise.