diff options
author | Alexander Færøy <ahf@torproject.org> | 2018-11-22 05:22:24 +0100 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-12-17 16:39:28 -0500 |
commit | 89393a77e5db804784d4f08ef67fd2831799d65b (patch) | |
tree | bc6560f11c60d944b6b220ce0349469bf0c6ca4a /src | |
parent | bb784cf4f36256a8276ba60641d3ff766b9cd9df (diff) | |
download | tor-89393a77e5db804784d4f08ef67fd2831799d65b.tar.gz tor-89393a77e5db804784d4f08ef67fd2831799d65b.zip |
Add process_get_pid() to the Process subsystem.
This patch adds support for getting the unique process identifier from a
given process_t. This patch implements both support for both the Unix
and Microsoft Windows backend.
See: https://bugs.torproject.org/28179
Diffstat (limited to 'src')
-rw-r--r-- | src/lib/process/process.c | 13 | ||||
-rw-r--r-- | src/lib/process/process.h | 3 | ||||
-rw-r--r-- | src/lib/process/process_unix.c | 10 | ||||
-rw-r--r-- | src/lib/process/process_unix.h | 2 | ||||
-rw-r--r-- | src/lib/process/process_win32.c | 10 | ||||
-rw-r--r-- | src/lib/process/process_win32.h | 2 | ||||
-rw-r--r-- | src/test/test_process.c | 3 |
7 files changed, 43 insertions, 0 deletions
diff --git a/src/lib/process/process.c b/src/lib/process/process.c index d4237b2b1f..ab19378a93 100644 --- a/src/lib/process/process.c +++ b/src/lib/process/process.c @@ -255,6 +255,19 @@ process_exec(process_t *process) return status; } +/** Returns the unique process identifier for the given <b>process</b>. */ +process_pid_t +process_get_pid(process_t *process) +{ + tor_assert(process); + +#ifndef _WIN32 + return process_unix_get_pid(process); +#else + return process_win32_get_pid(process); +#endif +} + /** Set the callback function for output from the child process's standard out * handle. This function sets the callback function which is called every time * the child process have written output to its standard out file handle. diff --git a/src/lib/process/process.h b/src/lib/process/process.h index f759c71939..7fd6cf53d0 100644 --- a/src/lib/process/process.h +++ b/src/lib/process/process.h @@ -49,6 +49,7 @@ struct process_t; typedef struct process_t process_t; typedef uint64_t process_exit_code_t; +typedef uint64_t process_pid_t; typedef void (*process_read_callback_t)(process_t *, char *, @@ -66,6 +67,8 @@ void process_free_(process_t *process); process_status_t process_exec(process_t *process); +process_pid_t process_get_pid(process_t *process); + void process_set_stdout_read_callback(process_t *, process_read_callback_t); void process_set_stderr_read_callback(process_t *, diff --git a/src/lib/process/process_unix.c b/src/lib/process/process_unix.c index c3691f1851..fa03fdbbe4 100644 --- a/src/lib/process/process_unix.c +++ b/src/lib/process/process_unix.c @@ -356,6 +356,16 @@ process_unix_exec(process_t *process) return PROCESS_STATUS_RUNNING; } +/** Returns the unique process identifier for the given <b>process</b>. */ +process_pid_t +process_unix_get_pid(process_t *process) +{ + tor_assert(process); + + process_unix_t *unix_process = process_get_unix_process(process); + return (process_pid_t)unix_process->pid; +} + /** Write the given <b>buffer</b> as input to the given <b>process</b>'s * standard input. Returns the number of bytes written. */ int diff --git a/src/lib/process/process_unix.h b/src/lib/process/process_unix.h index 5fc23bcf07..0474746b26 100644 --- a/src/lib/process/process_unix.h +++ b/src/lib/process/process_unix.h @@ -30,6 +30,8 @@ void process_unix_free_(process_unix_t *unix_process); process_status_t process_unix_exec(struct process_t *process); +process_pid_t process_unix_get_pid(struct process_t *process); + int process_unix_write(struct process_t *process, buf_t *buffer); int process_unix_read_stdout(struct process_t *process, buf_t *buffer); int process_unix_read_stderr(struct process_t *process, buf_t *buffer); diff --git a/src/lib/process/process_win32.c b/src/lib/process/process_win32.c index a019e0b4f3..3e97f37801 100644 --- a/src/lib/process/process_win32.c +++ b/src/lib/process/process_win32.c @@ -271,6 +271,16 @@ process_win32_exec(process_t *process) return PROCESS_STATUS_RUNNING; } +/** Returns the unique process identifier for the given <b>process</b>. */ +process_pid_t +process_win32_get_pid(process_t *process) +{ + tor_assert(process); + + process_win32_t *win32_process = process_get_win32_process(process); + return (process_pid_t)win32_process->process_information.dwProcessId; +} + /** Schedule an async write of the data found in <b>buffer</b> for the given * process. This function runs an async write operation of the content of * buffer, if we are not already waiting for a pending I/O request. Returns the diff --git a/src/lib/process/process_win32.h b/src/lib/process/process_win32.h index 8c3b80d341..dbd264104c 100644 --- a/src/lib/process/process_win32.h +++ b/src/lib/process/process_win32.h @@ -34,6 +34,8 @@ void process_win32_deinit(void); process_status_t process_win32_exec(struct process_t *process); +process_pid_t process_win32_get_pid(struct process_t *process); + int process_win32_write(struct process_t *process, buf_t *buffer); int process_win32_read_stdout(struct process_t *process, buf_t *buffer); int process_win32_read_stderr(struct process_t *process, buf_t *buffer); diff --git a/src/test/test_process.c b/src/test/test_process.c index 85ee9691a4..4f86e786cb 100644 --- a/src/test/test_process.c +++ b/src/test/test_process.c @@ -160,6 +160,9 @@ test_default_values(void *arg) tt_assert(smartlist_contains(process_get_all_processes(), process)); + /* Default PID is 0. */ + tt_int_op(0, OP_EQ, process_get_pid(process)); + /* Our arguments should be empty. */ tt_int_op(0, OP_EQ, smartlist_len(process_get_arguments(process))); |