diff options
author | George Kadianakis <desnacked@gmail.com> | 2011-10-24 16:01:24 +0200 |
---|---|---|
committer | George Kadianakis <desnacked@gmail.com> | 2011-10-24 16:01:24 +0200 |
commit | 47a5b8009ba1cef0d563b52c28d6e1c68da2fbe9 (patch) | |
tree | 15234dc6b0694171cf09501dfb3c2ba165238fea /src/common/util.c | |
parent | 572aa4ec44223e581665026aa36b6e416558e115 (diff) | |
download | tor-47a5b8009ba1cef0d563b52c28d6e1c68da2fbe9.tar.gz tor-47a5b8009ba1cef0d563b52c28d6e1c68da2fbe9.zip |
Improve general code quality.
- Add a tor_process_get_pid() function that returns the PID of a
process_handle_t.
- Conform to make check-spaces.
- Add some more documentation.
- Improve some log messages.
Diffstat (limited to 'src/common/util.c')
-rw-r--r-- | src/common/util.c | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/src/common/util.c b/src/common/util.c index 6a0893b948..9df88eb49a 100644 --- a/src/common/util.c +++ b/src/common/util.c @@ -3165,11 +3165,12 @@ int tor_terminate_process(process_handle_t *process_handle) { #ifdef MS_WINDOWS - if (tor_get_exit_code(process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) { + if (tor_get_exit_code(*process_handle, 0, NULL) == PROCESS_EXIT_RUNNING) { 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, process_handle->pid.dwProcessId); + handle = OpenProcess(PROCESS_ALL_ACCESS, FALSE, + process_handle->pid.dwProcessId); if (!handle) return -1; @@ -3177,12 +3178,23 @@ tor_terminate_process(process_handle_t *process_handle) return -1; else return 0; - } else { /* process was not running */ - return -1; } #else /* Unix */ return kill(process_handle->pid, SIGTERM); #endif + + return -1; +} + +/** Return the Process ID of <b>process_handle</b>. */ +int +tor_process_get_pid(process_handle_t *process_handle) +{ +#ifdef MS_WINDOWS + return (int) process_handle->pid.dwProcessId; +#else + return (int) process_handle->pid; +#endif } #define CHILD_STATE_INIT 0 @@ -3360,7 +3372,7 @@ tor_spawn_background(const char *const filename, const char **argv, } retval = pipe(stderr_pipe); - if (-1 == retval) { /* if it fails here, it doesn't close the stdout_pipe */ + if (-1 == retval) { log_warn(LD_GENERAL, "Failed to set up pipe for stderr communication with child process: %s", strerror(errno)); @@ -3503,29 +3515,38 @@ tor_spawn_background(const char *const filename, const char **argv, #endif // MS_WINDOWS } +/** Destroy all resources allocated by the process handle in + * <b>process_handle</b>. + * If <b>also_terminate_process</b> is true, also terminate the + * process of the process handle. */ void tor_process_destroy(process_handle_t *process_handle, int also_terminate_process) { - if (also_terminate_process) + if (also_terminate_process) { if (tor_terminate_process(process_handle) < 0) { - log_notice(LD_GENERAL, "Failed to terminate process with PID " -#ifdef MS_WINDOWS - "%u\n", process_handle->pid.dwProcessId -#else - "%d\n", (int) process_handle->pid -#endif - ); + log_notice(LD_GENERAL, "Failed to terminate process with PID '%d'", + tor_process_get_pid(process_handle)); + } else { + log_info(LD_GENERAL, "Terminated process with PID '%d'", + tor_process_get_pid(process_handle)); } + } process_handle->status = PROCESS_STATUS_NOTRUNNING; #ifdef MS_WINDOWS - CloseHandle(process_handle->stdout_pipe); - CloseHandle(process_handle->stderr_pipe); + if (process_handle->stdout_pipe) + CloseHandle(process_handle->stdout_pipe); + + if (process_handle->stderr_pipe) + CloseHandle(process_handle->stderr_pipe); #else - fclose(process_handle->stdout_handle); - fclose(process_handle->stderr_handle); + if (process_handle->stdout_handle) + fclose(process_handle->stdout_handle); + + if (process_handle->stderr_handle) + fclose(process_handle->stderr_handle); #endif tor_free(process_handle); @@ -4025,14 +4046,10 @@ tor_check_port_forwarding(const char *filename, int dir_port, int or_port, time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL; return; } -#ifdef MS_WINDOWS - log_info(LD_GENERAL, - "Started port forwarding helper (%s)", filename); -#else + log_info(LD_GENERAL, - "Started port forwarding helper (%s) with pid %d", filename, - child_handle.pid); -#endif + "Started port forwarding helper (%s) with pid '%d'", + filename, tor_process_get_pid(&child_handle)); } /* If child is running, read from its stdout and stderr) */ |