]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Add process_terminate().
authorAlexander Færøy <ahf@torproject.org>
Fri, 23 Nov 2018 02:44:59 +0000 (03:44 +0100)
committerNick Mathewson <nickm@torproject.org>
Mon, 17 Dec 2018 21:39:28 +0000 (16:39 -0500)
This patch adds support for process termination to the Process
subsystem.

See: https://bugs.torproject.org/28179

src/lib/process/process.c
src/lib/process/process.h
src/lib/process/process_unix.c
src/lib/process/process_unix.h
src/lib/process/process_win32.c
src/lib/process/process_win32.h

index ab19378a930b452e87181fec28fa0f0ec3285bdf..657b319f542263a067f01416931935644317091f 100644 (file)
@@ -255,6 +255,26 @@ process_exec(process_t *process)
   return status;
 }
 
+/** Terminate the given process. Returns true on success,
+ * otherwise false. */
+bool
+process_terminate(process_t *process)
+{
+  tor_assert(process);
+
+  /* Terminating a non-running process isn't going to work. */
+  if (process_get_status(process) != PROCESS_STATUS_RUNNING)
+    return false;
+
+  log_debug(LD_PROCESS, "Terminating process");
+
+#ifndef _WIN32
+  return process_unix_terminate(process);
+#else
+  return process_win32_terminate(process);
+#endif
+}
+
 /** Returns the unique process identifier for the given <b>process</b>. */
 process_pid_t
 process_get_pid(process_t *process)
index 7fd6cf53d081e04a758d8504a4850bce3a6ba1ea..c6b733a065c8bae52dbae3e01acc2badf70ba107 100644 (file)
@@ -66,6 +66,7 @@ void process_free_(process_t *process);
 #define process_free(s) FREE_AND_NULL(process_t, process_free_, (s))
 
 process_status_t process_exec(process_t *process);
+bool process_terminate(process_t *process);
 
 process_pid_t process_get_pid(process_t *process);
 
index 4f46bbd888e03efd370f84b6ebf909c7ce2e8e48..4a9aaa2edd4203dc2815b35fd40fa4e300045256 100644 (file)
@@ -356,6 +356,32 @@ process_unix_exec(process_t *process)
   return PROCESS_STATUS_RUNNING;
 }
 
+/** Terminate the given process. Returns true on success, otherwise false. */
+bool
+process_unix_terminate(process_t *process)
+{
+  tor_assert(process);
+
+  process_unix_t *unix_process = process_get_unix_process(process);
+
+  /* All running processes should have a waitpid. */
+  if (BUG(unix_process->waitpid == NULL))
+    return false;
+
+  /* Send a SIGTERM to our child process. */
+  int ret;
+
+  ret = kill(unix_process->pid, SIGTERM);
+
+  if (ret == -1) {
+    log_warn(LD_PROCESS, "Unable to terminate process: %s",
+             strerror(errno));
+    return false;
+  }
+
+  return ret == 0;
+}
+
 /** Returns the unique process identifier for the given <b>process</b>. */
 process_pid_t
 process_unix_get_pid(process_t *process)
index 0474746b2621ec4294bd1dc3c078bc894dc3ae6b..e17c59ea813e88180acc7acda670dc0f4c2c8413 100644 (file)
@@ -29,6 +29,7 @@ void process_unix_free_(process_unix_t *unix_process);
   FREE_AND_NULL(process_unix_t, process_unix_free_, (s))
 
 process_status_t process_unix_exec(struct process_t *process);
+bool process_unix_terminate(struct process_t *process);
 
 process_pid_t process_unix_get_pid(struct process_t *process);
 
index 7422493deb1d51231e8a29bda55427201486f1cf..7b18903c7025b84bd9835900fbe79338fd0b8633 100644 (file)
@@ -271,6 +271,28 @@ process_win32_exec(process_t *process)
   return PROCESS_STATUS_RUNNING;
 }
 
+/** Terminate the given process. Returns true on success, otherwise false. */
+bool
+process_win32_terminate(process_t *process)
+{
+  tor_assert(process);
+
+  process_win32_t *win32_process = process_get_win32_process(process);
+
+  /* Terminate our process. */
+  BOOL ret;
+
+  ret = TerminateProcess(win32_process->process_information.hProcess, 0);
+
+  if (! ret) {
+    log_warn(LD_PROCESS, "TerminateProcess() failed: %s",
+             format_win32_error(GetLastError()));
+    return false;
+  }
+
+  return true;
+}
+
 /** Returns the unique process identifier for the given <b>process</b>. */
 process_pid_t
 process_win32_get_pid(process_t *process)
index dbd264104cf78229f1a19f7a433546fd8dd13961..9a42e6c7135d7d3050b4409799781e9861cbd2b4 100644 (file)
@@ -33,6 +33,7 @@ void process_win32_init(void);
 void process_win32_deinit(void);
 
 process_status_t process_win32_exec(struct process_t *process);
+bool process_win32_terminate(struct process_t *process);
 
 process_pid_t process_win32_get_pid(struct process_t *process);