Browse Source

Abstract kill_process_tree().

The function is now walk_process_tree() and instead of killing a process
it invokes the supplied callback function.  When killing a process we
pass a pointer to kill_process().

This will allow performing operations things other than killing
processes on the tree.
Iain Patterson 6 years ago
parent
commit
d06a30c2b9
2 changed files with 8 additions and 6 deletions
  1. 5 5
      process.cpp
  2. 3 1
      process.h

+ 5 - 5
process.cpp

@@ -275,7 +275,7 @@ int kill_console(kill_t *k) {
   return kill_console(NULL, k);
 }
 
-void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid) {
+void walk_process_tree(nssm_service_t *service, walk_function_t fn, kill_t *k, unsigned long ppid) {
   if (! k) return;
   /* Shouldn't happen unless the service failed to start. */
   if (! k->pid) return; /* XXX: needed? */
@@ -294,7 +294,7 @@ void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid)
     _sntprintf_s(ppid_string, _countof(ppid_string), _TRUNCATE, _T("%lu"), ppid);
     log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, k->name, 0);
     k->process_handle = process_handle; /* XXX: open directly? */
-    if (! kill_process(k)) {
+    if (! fn(service, k)) {
       /* Maybe it already died. */
       unsigned long ret;
       if (! GetExitCodeProcess(process_handle, &ret) || ret == STILL_ACTIVE) {
@@ -327,7 +327,7 @@ void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid)
   /* This is a child of the doomed process so kill it. */
   if (! check_parent(k, &pe, pid)) {
     k->pid = pe.th32ProcessID;
-    kill_process_tree(k, ppid);
+    walk_process_tree(service, fn, k, ppid);
   }
   k->pid = pid;
 
@@ -343,7 +343,7 @@ void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid)
 
     if (! check_parent(k, &pe, pid)) {
       k->pid = pe.th32ProcessID;
-      kill_process_tree(k, ppid);
+      walk_process_tree(service, fn, k, ppid);
     }
     k->pid = pid;
   }
@@ -352,5 +352,5 @@ void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid)
 }
 
 void kill_process_tree(kill_t *k, unsigned long ppid) {
-  return kill_process_tree(NULL, k, ppid);
+  return walk_process_tree(NULL, kill_process, k, ppid);
 }

+ 3 - 1
process.h

@@ -19,6 +19,8 @@ typedef struct {
   int signalled;
 } kill_t;
 
+typedef int (*walk_function_t)(nssm_service_t *, kill_t *);
+
 void service_kill_t(nssm_service_t *, kill_t *);
 int get_process_creation_time(HANDLE, FILETIME *);
 int get_process_exit_time(HANDLE, FILETIME *);
@@ -30,7 +32,7 @@ int kill_console(nssm_service_t *, kill_t *);
 int kill_console(kill_t *);
 int kill_process(nssm_service_t *, kill_t *);
 int kill_process(kill_t *);
-void kill_process_tree(nssm_service_t *, kill_t *, unsigned long);
+void walk_process_tree(nssm_service_t *, walk_function_t, kill_t *, unsigned long);
 void kill_process_tree(kill_t *, unsigned long);
 
 #endif