From de47d8ddf95aeddbdcaf0d9ecf2d126aae9a5df7 Mon Sep 17 00:00:00 2001 From: Iain Patterson Date: Fri, 22 Jul 2016 17:51:19 +0100 Subject: [PATCH] Added append_to/remove_from_dependencies(). --- service.cpp | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ service.h | 2 ++ 2 files changed, 52 insertions(+) diff --git a/service.cpp b/service.cpp index 91435db..a94e698 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; diff --git a/service.h b/service.h index 1a1b263..7e8d0ab 100644 --- a/service.h +++ b/service.h @@ -138,6 +138,8 @@ void cleanup_nssm_service(nssm_service_t *); SC_HANDLE open_service_manager(unsigned long); SC_HANDLE open_service(SC_HANDLE, TCHAR *, unsigned long, TCHAR *, unsigned long); QUERY_SERVICE_CONFIG *query_service_config(const TCHAR *, SC_HANDLE); +int append_to_dependencies(TCHAR *, unsigned long, TCHAR *, TCHAR **, unsigned long *, int); +int remove_from_dependencies(TCHAR *, unsigned long, TCHAR *, TCHAR **, unsigned long *, int); int set_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR *); int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *, int); int get_service_dependencies(const TCHAR *, SC_HANDLE, TCHAR **, unsigned long *); -- 2.20.1