X-Git-Url: http://git.iain.cx/?a=blobdiff_plain;f=service.cpp;h=8561616387bcb24b2e099475d4a1718cbb06668c;hb=3c9d8f85d38b4c97b83e2baca63211a4008b5e0d;hp=79c2f566d954c63d5dea7e3e4e4a4518d9e5e30e;hpb=264c99d57e1efbbf1ffabd76903b9168c57f0cfc;p=nssm.git diff --git a/service.cpp b/service.cpp index 79c2f56..8561616 100644 --- a/service.cpp +++ b/service.cpp @@ -429,6 +429,56 @@ QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *service_name, SC_HANDLE return qsc; } +/* WILL NOT allocate a new string if the identifier is already present. */ +int prepend_service_group_identifier(TCHAR *group, TCHAR **canon) { + if (! group || ! group[0] || group[0] == SC_GROUP_IDENTIFIER) { + *canon = group; + return 0; + } + + size_t len = _tcslen(group) + 1; + *canon = (TCHAR *) HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(TCHAR)); + if (! *canon) { + print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("canon"), _T("prepend_service_group_identifier()")); + return 1; + } + + TCHAR *s = *canon; + *s++ = SC_GROUP_IDENTIFIER; + memmove(s, group, len * sizeof(TCHAR)); + (*canon)[len] = _T('\0'); + + return 0; +} + +int append_to_dependencies(TCHAR *dependencies, unsigned long dependencieslen, TCHAR *string, TCHAR **newdependencies, unsigned long *newlen, int type) { + *newlen = 0; + + TCHAR *canon = 0; + if (type == DEPENDENCY_GROUPS) { + if (prepend_service_group_identifier(string, &canon)) return 1; + } + else canon = string; + int ret = append_to_double_null(dependencies, dependencieslen, newdependencies, newlen, canon, 0, false); + if (canon && canon != string) HeapFree(GetProcessHeap(), 0, canon); + + return ret; +} + +int remove_from_dependencies(TCHAR *dependencies, unsigned long dependencieslen, TCHAR *string, TCHAR **newdependencies, unsigned long *newlen, int type) { + *newlen = 0; + + TCHAR *canon = 0; + if (type == DEPENDENCY_GROUPS) { + if (prepend_service_group_identifier(string, &canon)) return 1; + } + else canon = string; + int ret = remove_from_double_null(dependencies, dependencieslen, newdependencies, newlen, canon, 0, false); + if (canon && canon != string) HeapFree(GetProcessHeap(), 0, canon); + + return ret; +} + int set_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle, TCHAR *buffer) { TCHAR *dependencies = _T(""); unsigned long num_dependencies = 0; @@ -562,8 +612,10 @@ int get_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle QUERY_SERVICE_CONFIG *qsc = query_service_config(service_name, service_handle); if (! qsc) return 3; - if (! qsc->lpDependencies) return 0; - if (! qsc->lpDependencies[0]) return 0; + if (! qsc->lpDependencies || ! qsc->lpDependencies[0]) { + HeapFree(GetProcessHeap(), 0, qsc); + return 0; + } /* lpDependencies is doubly NULL terminated. */ while (qsc->lpDependencies[*bufsize]) { @@ -577,6 +629,7 @@ int get_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle if (! *buffer) { *bufsize = 0; print_message(stderr, NSSM_MESSAGE_OUT_OF_MEMORY, _T("lpDependencies"), _T("get_service_dependencies()")); + HeapFree(GetProcessHeap(), 0, qsc); return 4; } @@ -601,6 +654,12 @@ int get_service_dependencies(const TCHAR *service_name, SC_HANDLE service_handle HeapFree(GetProcessHeap(), 0, qsc); + if (! *buffer[0]) { + HeapFree(GetProcessHeap(), 0, *buffer); + *buffer = 0; + *bufsize = 0; + } + return 0; } @@ -824,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; @@ -847,6 +906,7 @@ int pre_edit_service(int argc, TCHAR **argv) { mandatory = 3; mode = MODE_RESETTING; } + else if (str_equiv(verb, _T("dump"))) mode = MODE_DUMPING; if (argc < mandatory) return usage(1); const TCHAR *parameter = 0; @@ -921,7 +981,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); @@ -931,7 +991,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); @@ -940,7 +1000,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); @@ -960,7 +1020,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; @@ -968,7 +1028,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; @@ -982,7 +1042,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. */ @@ -991,6 +1051,31 @@ int pre_edit_service(int argc, TCHAR **argv) { return 0; } + HKEY key; + value_t value; + int ret; + + if (mode == MODE_DUMPING) { + if (service->native) key = 0; + else { + key = open_registry(service->name, KEY_READ); + if (! key) return 4; + } + + 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); @@ -998,10 +1083,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); @@ -1024,7 +1105,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; }