Browse Source

Added append_to/remove_from_dependencies().

Iain Patterson 6 years ago
parent
commit
de47d8ddf9
2 changed files with 52 additions and 0 deletions
  1. 50 0
      service.cpp
  2. 2 0
      service.h

+ 50 - 0
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;

+ 2 - 0
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 *);