Abstract kill_process_tree().
authorIain Patterson <me@iain.cx>
Mon, 25 Jul 2016 13:52:19 +0000 (14:52 +0100)
committerIain Patterson <me@iain.cx>
Thu, 28 Jul 2016 15:44:33 +0000 (16:44 +0100)
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.

process.cpp
process.h

index 22e5a12..fa8de6b 100644 (file)
@@ -275,7 +275,7 @@ int kill_console(kill_t *k) {
   return kill_console(NULL, k);\r
 }\r
 \r
-void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid) {\r
+void walk_process_tree(nssm_service_t *service, walk_function_t fn, kill_t *k, unsigned long ppid) {\r
   if (! k) return;\r
   /* Shouldn't happen unless the service failed to start. */\r
   if (! k->pid) return; /* XXX: needed? */\r
@@ -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);\r
     log_event(EVENTLOG_INFORMATION_TYPE, NSSM_EVENT_KILL_PROCESS_TREE, pid_string, ppid_string, k->name, 0);\r
     k->process_handle = process_handle; /* XXX: open directly? */\r
-    if (! kill_process(k)) {\r
+    if (! fn(service, k)) {\r
       /* Maybe it already died. */\r
       unsigned long ret;\r
       if (! GetExitCodeProcess(process_handle, &ret) || ret == STILL_ACTIVE) {\r
@@ -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. */\r
   if (! check_parent(k, &pe, pid)) {\r
     k->pid = pe.th32ProcessID;\r
-    kill_process_tree(k, ppid);\r
+    walk_process_tree(service, fn, k, ppid);\r
   }\r
   k->pid = pid;\r
 \r
@@ -343,7 +343,7 @@ void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid)
 \r
     if (! check_parent(k, &pe, pid)) {\r
       k->pid = pe.th32ProcessID;\r
-      kill_process_tree(k, ppid);\r
+      walk_process_tree(service, fn, k, ppid);\r
     }\r
     k->pid = pid;\r
   }\r
@@ -352,5 +352,5 @@ void kill_process_tree(nssm_service_t * service, kill_t *k, unsigned long ppid)
 }\r
 \r
 void kill_process_tree(kill_t *k, unsigned long ppid) {\r
-  return kill_process_tree(NULL, k, ppid);\r
+  return walk_process_tree(NULL, kill_process, k, ppid);\r
 }\r
index 1bfc273..fe36f2f 100644 (file)
--- a/process.h
+++ b/process.h
@@ -19,6 +19,8 @@ typedef struct {
   int signalled;\r
 } kill_t;\r
 \r
+typedef int (*walk_function_t)(nssm_service_t *, kill_t *);\r
+\r
 void service_kill_t(nssm_service_t *, kill_t *);\r
 int get_process_creation_time(HANDLE, FILETIME *);\r
 int get_process_exit_time(HANDLE, FILETIME *);\r
@@ -30,7 +32,7 @@ int kill_console(nssm_service_t *, kill_t *);
 int kill_console(kill_t *);\r
 int kill_process(nssm_service_t *, kill_t *);\r
 int kill_process(kill_t *);\r
-void kill_process_tree(nssm_service_t *, kill_t *, unsigned long);\r
+void walk_process_tree(nssm_service_t *, walk_function_t, kill_t *, unsigned long);\r
 void kill_process_tree(kill_t *, unsigned long);\r
 \r
 #endif\r