X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=f18626ced304d34fbb3752ba0a010c9b7d74fbdc;hb=7bc41a82f696699aface2103f277e7ebf1808b2f;hp=a94e6984d41e0b8a79a003ae2898cf1b87b9bf2b;hpb=de47d8ddf95aeddbdcaf0d9ecf2d126aae9a5df7;p=nssm.git diff --git a/service.cpp b/service.cpp index a94e698..f18626c 100644 --- a/service.cpp +++ b/service.cpp @@ -883,7 +883,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (argc < 2) return usage(1); /* Are we editing on the command line? */ - enum { MODE_EDITING, MODE_GETTING, MODE_SETTING, MODE_RESETTING } mode = MODE_EDITING; + enum { MODE_EDITING, MODE_GETTING, MODE_SETTING, MODE_RESETTING, MODE_DUMPING } mode = MODE_EDITING; const TCHAR *verb = argv[0]; const TCHAR *service_name = argv[1]; bool getting = false; @@ -906,6 +906,11 @@ int pre_edit_service(int argc, TCHAR **argv) { mandatory = 3; mode = MODE_RESETTING; } + else if (str_equiv(verb, _T("dump"))) { + mandatory = 1; + remainder = 2; + mode = MODE_DUMPING; + } if (argc < mandatory) return usage(1); const TCHAR *parameter = 0; @@ -980,7 +985,7 @@ int pre_edit_service(int argc, TCHAR **argv) { service->type = qsc->dwServiceType; if (! (service->type & SERVICE_WIN32_OWN_PROCESS)) { - if (mode != MODE_GETTING) { + if (mode != MODE_GETTING && mode != MODE_DUMPING) { HeapFree(GetProcessHeap(), 0, qsc); CloseServiceHandle(service->handle); CloseServiceHandle(services); @@ -990,7 +995,7 @@ int pre_edit_service(int argc, TCHAR **argv) { } if (get_service_startup(service->name, service->handle, qsc, &service->startup)) { - if (mode != MODE_GETTING) { + if (mode != MODE_GETTING && mode != MODE_DUMPING) { HeapFree(GetProcessHeap(), 0, qsc); CloseServiceHandle(service->handle); CloseServiceHandle(services); @@ -999,7 +1004,7 @@ int pre_edit_service(int argc, TCHAR **argv) { } if (get_service_username(service->name, qsc, &service->username, &service->usernamelen)) { - if (mode != MODE_GETTING) { + if (mode != MODE_GETTING && mode != MODE_DUMPING) { HeapFree(GetProcessHeap(), 0, qsc); CloseServiceHandle(service->handle); CloseServiceHandle(services); @@ -1019,7 +1024,7 @@ int pre_edit_service(int argc, TCHAR **argv) { /* Get extended system details. */ if (get_service_description(service->name, service->handle, _countof(service->description), service->description)) { - if (mode != MODE_GETTING) { + if (mode != MODE_GETTING && mode != MODE_DUMPING) { CloseServiceHandle(service->handle); CloseServiceHandle(services); return 6; @@ -1027,7 +1032,7 @@ int pre_edit_service(int argc, TCHAR **argv) { } if (get_service_dependencies(service->name, service->handle, &service->dependencies, &service->dependencieslen)) { - if (mode != MODE_GETTING) { + if (mode != MODE_GETTING && mode != MODE_DUMPING) { CloseServiceHandle(service->handle); CloseServiceHandle(services); return 7; @@ -1041,7 +1046,7 @@ int pre_edit_service(int argc, TCHAR **argv) { if (! service->exe[0]) { service->native = true; - if (mode != MODE_GETTING) print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image); + if (mode != MODE_GETTING && mode != MODE_DUMPING) print_message(stderr, NSSM_MESSAGE_INVALID_SERVICE, service->name, NSSM, service->image); } /* Editing with the GUI. */ @@ -1050,6 +1055,41 @@ int pre_edit_service(int argc, TCHAR **argv) { return 0; } + HKEY key; + value_t value; + int ret; + + if (mode == MODE_DUMPING) { + TCHAR *service_name = service->name; + if (argc > remainder) service_name = argv[remainder]; + if (service->native) key = 0; + else { + key = open_registry(service->name, KEY_READ); + if (! key) return 4; + } + + TCHAR quoted_service_name[SERVICE_NAME_LENGTH * 2]; + TCHAR quoted_exe[EXE_LENGTH * 2]; + TCHAR quoted_nssm[EXE_LENGTH * 2]; + if (quote(service_name, quoted_service_name, _countof(quoted_service_name))) return 5; + if (quote(service->exe, quoted_exe, _countof(quoted_exe))) return 6; + if (quote(nssm_exe(), quoted_nssm, _countof(quoted_nssm))) return 6; + _tprintf(_T("%s install %s %s\n"), quoted_nssm, quoted_service_name, quoted_exe); + + ret = 0; + for (i = 0; settings[i].name; i++) { + setting = &settings[i]; + if (! setting->native && service->native) continue; + if (dump_setting(service_name, key, service->handle, setting)) ret++; + } + + if (! service->native) RegCloseKey(key); + CloseServiceHandle(service->handle); + + if (ret) return 1; + return 0; + } + /* Trying to manage App* parameters for a non-NSSM service. */ if (! setting->native && service->native) { CloseServiceHandle(service->handle); @@ -1057,10 +1097,6 @@ int pre_edit_service(int argc, TCHAR **argv) { return 1; } - HKEY key; - value_t value; - int ret; - if (mode == MODE_GETTING) { if (! service->native) { key = open_registry(service->name, KEY_READ); @@ -1083,7 +1119,7 @@ int pre_edit_service(int argc, TCHAR **argv) { break; case REG_DWORD: - _tprintf(_T("%u\n"), value.numeric); + _tprintf(_T("%lu\n"), value.numeric); break; }