[tor-commits] [tor/master] Introduce tor_terminate_process() function.
nickm at torproject.org
nickm at torproject.org
Fri Oct 7 20:03:17 UTC 2011
commit 782810a8bf1fbc3feaca851a011a8124891d39fc
Author: George Kadianakis <desnacked at gmail.com>
Date: Sun Sep 11 20:26:01 2011 +0200
Introduce tor_terminate_process() function.
---
src/common/util.c | 26 ++++++++++++++++++++++++++
src/common/util.h | 1 +
2 files changed, 27 insertions(+), 0 deletions(-)
diff --git a/src/common/util.c b/src/common/util.c
index 0632c67..63172c3 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -28,6 +28,7 @@
#include <direct.h>
#include <process.h>
#include <tchar.h>
+#include <Winbase.h>
#else
#include <dirent.h>
#include <pwd.h>
@@ -43,6 +44,7 @@
#include <stdio.h>
#include <string.h>
#include <assert.h>
+#include <signal.h>
#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
@@ -2930,6 +2932,30 @@ format_helper_exit_status(unsigned char child_state, int saved_errno,
/* Maximum number of file descriptors, if we cannot get it via sysconf() */
#define DEFAULT_MAX_FD 256
+/** Terminate process running at PID <b>pid</b>.
+ * Code borrowed from Python's os.kill. */
+int
+tor_terminate_process(pid_t pid)
+{
+#ifdef MS_WINDOWS
+ DWORD pid_win = pid;
+ DWORD err;
+ HANDLE handle;
+ /* If the signal is outside of what GenerateConsoleCtrlEvent can use,
+ attempt to open and terminate the process. */
+ handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, pid);
+ if (handle == NULL)
+ return -1;
+
+ if (TerminateProcess(handle, sig) == 0)
+ return -1;
+ else
+ return 0;
+#else /* *nix */
+ return kill(pid, SIGTERM);
+#endif
+}
+
#define CHILD_STATE_INIT 0
#define CHILD_STATE_PIPE 1
#define CHILD_STATE_MAXFD 2
diff --git a/src/common/util.h b/src/common/util.h
index 8bf4f7b..7e889b1 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -350,6 +350,7 @@ void write_pidfile(char *filename);
void tor_check_port_forwarding(const char *filename,
int dir_port, int or_port, time_t now);
+int tor_terminate_process(pid_t pid);
int tor_spawn_background(const char *const filename, int *stdout_read,
int *stderr_read, const char **argv,
const char **envp);
More information about the tor-commits
mailing list