X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=09fbe1f0073f1523fcf54523663194c18b838d5c;hb=df8b0890255d7d1eeade3ba7754ef37c61d2fbad;hp=30a75d3d4cccf06003cf175b23f01e561133a336;hpb=f113d025a47ea5a3dd0cb962b5913045ac811b71;p=nssm.git diff --git a/service.cpp b/service.cpp index 30a75d3..09fbe1f 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); @@ -74,17 +80,17 @@ void cleanup_nssm_service(nssm_service_t *service) { /* About to install the service */ int pre_install_service(int argc, TCHAR **argv) { + nssm_service_t *service = alloc_nssm_service(); + set_nssm_service_defaults(service); + if (argc) _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]); + /* Show the dialogue box if we didn't give the service name and path */ - if (argc < 2) return nssm_gui(IDD_INSTALL, argv[0]); + if (argc < 2) return nssm_gui(IDD_INSTALL, service); - nssm_service_t *service = alloc_nssm_service(); if (! service) { print_message(stderr, NSSM_EVENT_OUT_OF_MEMORY, _T("service"), _T("pre_install_service()")); return 1; } - - set_nssm_service_defaults(service); - _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]); _sntprintf_s(service->exe, _countof(service->exe), _TRUNCATE, _T("%s"), argv[1]); /* Arguments are optional */ @@ -116,11 +122,13 @@ int pre_install_service(int argc, TCHAR **argv) { /* About to remove the service */ int pre_remove_service(int argc, TCHAR **argv) { + nssm_service_t *service = alloc_nssm_service(); + set_nssm_service_defaults(service); + if (argc) _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]); + /* Show dialogue box if we didn't pass service name and "confirm" */ - if (argc < 2) return nssm_gui(IDD_REMOVE, argv[0]); + if (argc < 2) return nssm_gui(IDD_REMOVE, service); if (str_equiv(argv[1], _T("confirm"))) { - nssm_service_t *service = alloc_nssm_service(); - _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]); int ret = remove_service(service); cleanup_nssm_service(service); return ret; @@ -145,14 +153,55 @@ int install_service(nssm_service_t *service) { 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); @@ -282,7 +331,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); } } } @@ -506,7 +555,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; }