X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=d66b91606f5b5be28627b8cb9da954118e39d33f;hb=b3b29f0b27046a282e688c494def532252ad21c3;hp=849f79093573d4ffa383c8eb9acf9e75f058fed7;hpb=586ea54f696a562ed3837b6b55e3fa1bbe1b2b22;p=nssm.git diff --git a/service.cpp b/service.cpp index 849f790..d66b916 100644 --- a/service.cpp +++ b/service.cpp @@ -83,10 +83,11 @@ static inline int service_control_response(unsigned long control, unsigned long return 0; } -static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status) { +static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status, unsigned long cutoff) { int tries = 0; unsigned long checkpoint = 0; unsigned long waithint = 0; + unsigned long waited = 0; while (QueryServiceStatus(service_handle, service_status)) { int response = service_control_response(control, service_status->dwCurrentState); /* Alas we can't WaitForSingleObject() on an SC_HANDLE. */ @@ -96,13 +97,22 @@ static inline int await_service_control_response(unsigned long control, SC_HANDL checkpoint = service_status->dwCheckPoint; waithint = service_status->dwWaitHint; if (++tries > 10) tries = 10; - Sleep(50 * tries); + unsigned long wait = 50 * tries; + if (cutoff) { + if (waited > cutoff) return response; + waited += wait; + } + Sleep(wait); } else return response; } return -1; } +static inline int await_service_control_response(unsigned long control, SC_HANDLE service_handle, SERVICE_STATUS *service_status, unsigned long initial_status) { + return await_service_control_response(control, service_handle, service_status, initial_status, 0); +} + static inline void wait_for_hooks(nssm_service_t *service, bool notify) { SERVICE_STATUS_HANDLE status_handle; SERVICE_STATUS *status; @@ -1274,19 +1284,33 @@ int edit_service(nssm_service_t *service, bool editing) { TCHAR *username = 0; TCHAR *canon = 0; TCHAR *password = 0; + boolean virtual_account = false; if (service->usernamelen) { username = service->username; - if (canonicalise_username(username, &canon)) return 5; - if (service->passwordlen) password = service->password; + if (is_virtual_account(service->name, username)) { + virtual_account = true; + canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (service->usernamelen + 1) * sizeof(TCHAR)); + if (! canon) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("edit_service()")); + return 5; + } + memmove(canon, username, (service->usernamelen + 1) * sizeof(TCHAR)); + } + else { + if (canonicalise_username(username, &canon)) return 5; + if (service->passwordlen) password = service->password; + } } else if (editing) username = canon = NSSM_LOCALSYSTEM_ACCOUNT; - if (well_known_username(canon)) password = _T(""); - else { - if (grant_logon_as_service(canon)) { - if (canon != username) HeapFree(GetProcessHeap(), 0, canon); - print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username); - return 5; + if (! virtual_account) { + if (well_known_username(canon)) password = _T(""); + else { + if (grant_logon_as_service(canon)) { + if (canon != username) HeapFree(GetProcessHeap(), 0, canon); + print_message(stderr, NSSM_MESSAGE_GRANT_LOGON_AS_SERVICE_FAILED, username); + return 5; + } } } @@ -1393,7 +1417,16 @@ int control_service(unsigned long control, int argc, TCHAR **argv) { } if (ret) { - int response = await_service_control_response(control, service_handle, &service_status, initial_status); + unsigned long cutoff = 0; + + /* If we manage the service, respect the throttle time. */ + HKEY key = open_registry(service_name, 0, KEY_READ, false); + if (key) { + if (get_number(key, NSSM_REG_THROTTLE, &cutoff, false) != 1) cutoff = NSSM_RESET_THROTTLE_RESTART; + RegCloseKey(key); + } + + int response = await_service_control_response(control, service_handle, &service_status, initial_status, cutoff); CloseServiceHandle(service_handle); if (response) { @@ -1503,6 +1536,9 @@ void WINAPI service_main(unsigned long argc, TCHAR **argv) { nssm_service_t *service = alloc_nssm_service(); if (! service) return; + static volatile bool await_debugger = (argc > 1 && str_equiv(argv[1], _T("debug"))); + while (await_debugger) Sleep(1000); + if (_sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), argv[0]) < 0) { log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_OUT_OF_MEMORY, _T("service->name"), _T("service_main()"), 0); return; @@ -2074,8 +2110,7 @@ void CALLBACK end_service(void *arg, unsigned char why) { log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_EXIT_UNCLEAN, service->name, code, exit_action_strings[action], 0); stop_service(service, exitcode, false, default_action); wait_for_hooks(service, false); - free_imports(); - exit(exitcode); + nssm_exit(exitcode); } } @@ -2209,7 +2244,9 @@ awaited: return ret; } -int list_nssm_services() { +int list_nssm_services(int argc, TCHAR **argv) { + bool including_native = (argc > 0 && str_equiv(argv[0], _T("all"))); + /* Open service manager. */ SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT | SC_MANAGER_ENUMERATE_SERVICE); if (! services) { @@ -2256,7 +2293,7 @@ int list_nssm_services() { get_parameters(service, 0); /* We manage the service if we have an Application. */ - if (service->exe[0]) _tprintf(_T("%s\n"), service->name); + if (including_native || service->exe[0]) _tprintf(_T("%s\n"), service->name); cleanup_nssm_service(service); }