[tor-commits] [tor/master] Add process_terminate().
nickm at torproject.org
nickm at torproject.org
Tue Dec 18 18:36:43 UTC 2018
commit 4f611a1df70d2c5e4cb6261f75c1b82c9ed04598
Author: Alexander Færøy <ahf at torproject.org>
Date: Fri Nov 23 03:44:59 2018 +0100
Add process_terminate().
This patch adds support for process termination to the Process
subsystem.
See: https://bugs.torproject.org/28179
---
src/lib/process/process.c | 20 ++++++++++++++++++++
src/lib/process/process.h | 1 +
src/lib/process/process_unix.c | 26 ++++++++++++++++++++++++++
src/lib/process/process_unix.h | 1 +
src/lib/process/process_win32.c | 22 ++++++++++++++++++++++
src/lib/process/process_win32.h | 1 +
6 files changed, 71 insertions(+)
diff --git a/src/lib/process/process.c b/src/lib/process/process.c
index ab19378a9..657b319f5 100644
--- a/src/lib/process/process.c
+++ b/src/lib/process/process.c
@@ -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)
diff --git a/src/lib/process/process.h b/src/lib/process/process.h
index 7fd6cf53d..c6b733a06 100644
--- a/src/lib/process/process.h
+++ b/src/lib/process/process.h
@@ -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);
diff --git a/src/lib/process/process_unix.c b/src/lib/process/process_unix.c
index 4f46bbd88..4a9aaa2ed 100644
--- a/src/lib/process/process_unix.c
+++ b/src/lib/process/process_unix.c
@@ -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)
diff --git a/src/lib/process/process_unix.h b/src/lib/process/process_unix.h
index 0474746b2..e17c59ea8 100644
--- a/src/lib/process/process_unix.h
+++ b/src/lib/process/process_unix.h
@@ -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);
diff --git a/src/lib/process/process_win32.c b/src/lib/process/process_win32.c
index 7422493de..7b18903c7 100644
--- a/src/lib/process/process_win32.c
+++ b/src/lib/process/process_win32.c
@@ -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)
diff --git a/src/lib/process/process_win32.h b/src/lib/process/process_win32.h
index dbd264104..9a42e6c71 100644
--- a/src/lib/process/process_win32.h
+++ b/src/lib/process/process_win32.h
@@ -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);
More information about the tor-commits
mailing list