X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=57dc7a28f103ff3bc69312b982d53f14c40cd94c;hb=6a12d5bf24211cfee30c47354b32befc3abae5b8;hp=fc9b73c2bfefe7f8b360e02171808cd4a8e74ec2;hpb=5b9e64a9ae1fbf1254c9c246e5b123d3aa77a37a;p=nssm.git diff --git a/service.cpp b/service.cpp index fc9b73c..57dc7a2 100644 --- a/service.cpp +++ b/service.cpp @@ -36,6 +36,7 @@ SC_HANDLE open_service_manager() { void set_nssm_service_defaults(nssm_service_t *service) { if (! service) return; + service->type = SERVICE_WIN32_OWN_PROCESS; service->stdin_sharing = NSSM_STDIN_SHARING; service->stdin_disposition = NSSM_STDIN_DISPOSITION; service->stdin_flags = NSSM_STDIN_FLAGS; @@ -62,6 +63,11 @@ nssm_service_t *alloc_nssm_service() { /* Free memory for a service. */ void cleanup_nssm_service(nssm_service_t *service) { if (! service) return; + if (service->username) HeapFree(GetProcessHeap(), 0, service->username); + if (service->password) { + SecureZeroMemory(service->password, service->passwordlen); + HeapFree(GetProcessHeap(), 0, service->password); + } if (service->env) HeapFree(GetProcessHeap(), 0, service->env); if (service->env_extra) HeapFree(GetProcessHeap(), 0, service->env_extra); if (service->handle) CloseServiceHandle(service->handle); @@ -142,29 +148,58 @@ 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)); + + /* + The only two valid flags for service type are SERVICE_WIN32_OWN_PROCESS + and SERVICE_INTERACTIVE_PROCESS. + */ + service->type &= SERVICE_INTERACTIVE_PROCESS; + service->type |= SERVICE_WIN32_OWN_PROCESS; + + /* Startup type. */ + unsigned long startup; + switch (service->startup) { + case NSSM_STARTUP_MANUAL: startup = SERVICE_DEMAND_START; break; + case NSSM_STARTUP_DISABLED: startup = SERVICE_DISABLED; break; + default: startup = SERVICE_AUTO_START; } + /* Display name. */ + if (! service->displayname[0]) _sntprintf_s(service->displayname, _countof(service->displayname), _TRUNCATE, _T("%s"), service->name); + /* 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); + service->handle = CreateService(services, service->name, service->displayname, SC_MANAGER_ALL_ACCESS, service->type, startup, SERVICE_ERROR_NORMAL, command, 0, 0, 0, service->username, service->password); if (! service->handle) { print_message(stderr, NSSM_MESSAGE_CREATESERVICE_FAILED); CloseServiceHandle(services); return 5; } + if (service->description[0]) { + SERVICE_DESCRIPTION description; + ZeroMemory(&description, sizeof(description)); + description.lpDescription = service->description; + if (! ChangeServiceConfig2(service->handle, SERVICE_CONFIG_DESCRIPTION, &description)) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_DESCRIPTION_FAILED, service->name, error_string(GetLastError()), 0); + } + } + + if (service->startup == NSSM_STARTUP_DELAYED) { + SERVICE_DELAYED_AUTO_START_INFO delayed; + ZeroMemory(&delayed, sizeof(delayed)); + delayed.fDelayedAutostart = 1; + /* Delayed startup isn't supported until Vista. */ + if (! ChangeServiceConfig2(service->handle, SERVICE_CONFIG_DELAYED_AUTO_START_INFO, &delayed)) { + unsigned long error = GetLastError(); + /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */ + if (error != ERROR_INVALID_LEVEL) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_DELAYED_AUTO_START_INFO_FAILED, service->name, error_string(error), 0); + } + } + } + /* Now we need to put the parameters into the registry */ if (create_parameters(service)) { print_message(stderr, NSSM_MESSAGE_CREATE_PARAMETERS_FAILED); @@ -294,7 +329,7 @@ void set_service_recovery(nssm_service_t *service) { unsigned long error = GetLastError(); /* Pre-Vista we expect to fail with ERROR_INVALID_LEVEL */ if (error != ERROR_INVALID_LEVEL) { - log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CHANGESERVICECONFIG2_FAILED, service->name, error_string(error), 0); + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_SERVICE_CONFIG_FAILURE_ACTIONS_FAILED, service->name, error_string(error), 0); } } } @@ -461,11 +496,15 @@ int start_service(nssm_service_t *service) { flags |= CREATE_UNICODE_ENVIRONMENT; #endif if (! CreateProcess(0, cmd, 0, 0, inherit_handles, flags, service->env, service->dir, &si, &pi)) { + unsigned long exitcode = 3; unsigned long error = GetLastError(); - if (error == ERROR_INVALID_PARAMETER && service->env) log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service->name, service->exe, NSSM_REG_ENV, 0); + if (error == ERROR_INVALID_PARAMETER && service->env) { + log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED_INVALID_ENVIRONMENT, service->name, service->exe, NSSM_REG_ENV, 0); + if (test_environment(service->env)) exitcode = 4; + } else log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEPROCESS_FAILED, service->name, service->exe, error_string(error), 0); close_output_handles(&si); - return stop_service(service, 3, true, true); + return stop_service(service, exitcode, true, true); } service->process_handle = pi.hProcess; service->pid = pi.dwProcessId; @@ -514,7 +553,7 @@ int stop_service(nssm_service_t *service, unsigned long exitcode, bool graceful, } if (default_action && ! exitcode && ! graceful) { - log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service->name, service->exe, exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_REALLY] ,0); + log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_GRACEFUL_SUICIDE, service->name, service->exe, exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_UNCLEAN], exit_action_strings[NSSM_EXIT_REALLY], 0); graceful = true; } @@ -563,12 +602,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 +622,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.