X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;ds=sidebyside;f=service.cpp;h=2d20ff30b5017fbbe1441c23d596af7572d64341;hb=800ca0c4f150de75c38c87a59d18878b0341f16b;hp=f18626ced304d34fbb3752ba0a010c9b7d74fbdc;hpb=7bc41a82f696699aface2103f277e7ebf1808b2f;p=nssm.git diff --git a/service.cpp b/service.cpp index f18626c..2d20ff3 100644 --- a/service.cpp +++ b/service.cpp @@ -1503,6 +1503,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; @@ -2075,7 +2078,7 @@ void CALLBACK end_service(void *arg, unsigned char why) { stop_service(service, exitcode, false, default_action); wait_for_hooks(service, false); free_imports(); - exit(exitcode); + nssm_exit(exitcode); } } @@ -2209,7 +2212,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 +2261,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); } @@ -2267,3 +2272,77 @@ int list_nssm_services() { HeapFree(GetProcessHeap(), 0, status); return 0; } + +int service_process_tree(int argc, TCHAR **argv) { + int errors = 0; + if (argc < 1) return usage(1); + + SC_HANDLE services = open_service_manager(SC_MANAGER_CONNECT); + if (! services) { + print_message(stderr, NSSM_MESSAGE_OPEN_SERVICE_MANAGER_FAILED); + return 1; + } + + /* + We need SeDebugPrivilege to read the process tree. + We ignore failure here so that an error will be printed later when we + try to open a process handle. + */ + HANDLE token = get_debug_token(); + + TCHAR canonical_name[SERVICE_NAME_LENGTH]; + SERVICE_STATUS_PROCESS service_status; + nssm_service_t *service; + kill_t k; + + int i; + for (i = 0; i < argc; i++) { + TCHAR *service_name = argv[i]; + SC_HANDLE service_handle = open_service(services, service_name, SERVICE_QUERY_STATUS, canonical_name, _countof(canonical_name)); + if (! service_handle) { + errors++; + continue; + } + + unsigned long size; + int ret = QueryServiceStatusEx(service_handle, SC_STATUS_PROCESS_INFO, (LPBYTE) &service_status, sizeof(service_status), &size); + long error = GetLastError(); + CloseServiceHandle(service_handle); + if (! ret) { + _ftprintf(stderr, _T("%s: %s\n"), canonical_name, error_string(error)); + errors++; + continue; + } + + ZeroMemory(&k, sizeof(k)); + k.pid = service_status.dwProcessId; + if (! k.pid) continue; + + k.process_handle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, k.pid); + if (! k.process_handle) { + _ftprintf(stderr, _T("%s: %lu: %s\n"), canonical_name, k.pid, error_string(GetLastError())); + continue; + } + + if (get_process_creation_time(k.process_handle, &k.creation_time)) continue; + /* Dummy exit time so we can check processes' parents. */ + GetSystemTimeAsFileTime(&k.exit_time); + + service = alloc_nssm_service(); + if (! service) { + errors++; + continue; + } + + _sntprintf_s(service->name, _countof(service->name), _TRUNCATE, _T("%s"), canonical_name); + k.name = service->name; + walk_process_tree(service, print_process, &k, k.pid); + + cleanup_nssm_service(service); + } + + CloseServiceHandle(services); + if (token != INVALID_HANDLE_VALUE) CloseHandle(token); + + return errors; +}