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. */
#endif
}
+#ifdef MS_WINDOWS
+HANDLE
+tor_process_get_stdout_pipe(process_handle_t *process_handle)
+{
+ return process_handle->stdout_pipe;
+}
+#else
+FILE *
+tor_process_get_stdout_pipe(process_handle_t *process_handle)
+{
+ return process_handle->stdout_handle;
+}
+#endif
+
+static process_handle_t *
+process_handle_new(void)
+{
+ process_handle_t *out = tor_malloc_zero(sizeof(process_handle_t));
+
+#ifndef MS_WINDOWS
+ out->stdout_pipe = -1;
+ out->stderr_pipe = -1;
+#endif
+
+ return out;
+}
+
+/*DOCDOC*/
#define CHILD_STATE_INIT 0
#define CHILD_STATE_PIPE 1
#define CHILD_STATE_MAXFD 2
#else
const char **envp,
#endif
- process_handle_t *process_handle)
+ process_handle_t **process_handle_out)
{
#ifdef MS_WINDOWS
HANDLE stdout_pipe_read = NULL;
HANDLE stdout_pipe_write = NULL;
HANDLE stderr_pipe_read = NULL;
HANDLE stderr_pipe_write = NULL;
+ process_handle_t *process_handle;
+ int status;
STARTUPINFO siStartInfo;
BOOL retval = FALSE;
(void)envp; // Unused on Windows
- /* process_handle must not be NULL */
- tor_assert(process_handle != NULL);
-
saAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
saAttr.bInheritHandle = TRUE;
/* TODO: should we set explicit security attributes? (#2046, comment 5) */
saAttr.lpSecurityDescriptor = NULL;
/* Assume failure to start process */
- memset(process_handle, 0, sizeof(process_handle_t));
- process_handle->status = PROCESS_STATUS_ERROR;
+ status = PROCESS_STATUS_ERROR;
/* Set up pipe for stdout */
if (!CreatePipe(&stdout_pipe_read, &stdout_pipe_write, &saAttr, 0)) {
log_warn(LD_GENERAL,
"Failed to create pipe for stdout communication with child process: %s",
format_win32_error(GetLastError()));
- return process_handle->status;
+ return status;
}
if (!SetHandleInformation(stdout_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
log_warn(LD_GENERAL,
"Failed to configure pipe for stdout communication with child "
"process: %s", format_win32_error(GetLastError()));
- return process_handle->status;
+ return status;
}
/* Set up pipe for stderr */
log_warn(LD_GENERAL,
"Failed to create pipe for stderr communication with child process: %s",
format_win32_error(GetLastError()));
- return process_handle->status;
+ return status;
}
if (!SetHandleInformation(stderr_pipe_read, HANDLE_FLAG_INHERIT, 0)) {
log_warn(LD_GENERAL,
"Failed to configure pipe for stderr communication with child "
"process: %s", format_win32_error(GetLastError()));
- return process_handle->status;
+ return status;
}
/* Create the child process */
*/
joined_argv = tor_join_win_cmdline(argv);
+ process_handle = process_handle_new();
+ process_handle->status = status;
+
ZeroMemory(&(process_handle->pid), sizeof(PROCESS_INFORMATION));
ZeroMemory(&siStartInfo, sizeof(STARTUPINFO));
siStartInfo.cb = sizeof(STARTUPINFO);
log_warn(LD_GENERAL,
"Failed to create child process %s: %s", filename?filename:argv[0],
format_win32_error(GetLastError()));
+ tor_free(process_handle);
} else {
/* TODO: Close hProcess and hThread in process_handle->pid? */
process_handle->stdout_pipe = stdout_pipe_read;
process_handle->stderr_pipe = stderr_pipe_read;
- process_handle->status = PROCESS_STATUS_RUNNING;
+ status = process_handle->status = PROCESS_STATUS_RUNNING;
}
/* TODO: Close pipes on exit */
-
- return process_handle->status;
+ *process_handle_out = process_handle;
+ return status;
#else // MS_WINDOWS
pid_t pid;
int stdout_pipe[2];
int stderr_pipe[2];
int fd, retval;
ssize_t nbytes;
+ process_handle_t *process_handle;
+ int status;
const char *error_message = SPAWN_ERROR_MESSAGE;
size_t error_message_length;
static int max_fd = -1;
- /* Assume failure to start */
- memset(process_handle, 0, sizeof(process_handle_t));
- process_handle->status = PROCESS_STATUS_ERROR;
+ status = PROCESS_STATUS_ERROR;
/* We do the strlen here because strlen() is not signal handler safe,
and we are not allowed to use unsafe functions between fork and exec */
log_warn(LD_GENERAL,
"Failed to set up pipe for stdout communication with child process: %s",
strerror(errno));
- return process_handle->status;
+ return status;
}
retval = pipe(stderr_pipe);
log_warn(LD_GENERAL,
"Failed to set up pipe for stderr communication with child process: %s",
strerror(errno));
- return process_handle->status;
+ return status;
}
child_state = CHILD_STATE_MAXFD;
_exit(255);
/* Never reached, but avoids compiler warning */
- return process_handle->status;
+ return status;
}
/* In parent */
close(stdout_pipe[1]);
close(stderr_pipe[0]);
close(stderr_pipe[1]);
- return process_handle->status;
+ return status;
}
+ process_handle = process_handle_new();
+ process_handle->status = status;
process_handle->pid = pid;
/* TODO: If the child process forked but failed to exec, waitpid it */
strerror(errno));
}
- process_handle->status = PROCESS_STATUS_RUNNING;
+ status = process_handle->status = PROCESS_STATUS_RUNNING;
/* Set stdout/stderr pipes to be non-blocking */
fcntl(process_handle->stdout_pipe, F_SETFL, O_NONBLOCK);
fcntl(process_handle->stderr_pipe, F_SETFL, O_NONBLOCK);
process_handle->stdout_handle = fdopen(process_handle->stdout_pipe, "r");
process_handle->stderr_handle = fdopen(process_handle->stderr_pipe, "r");
+ *process_handle_out = process_handle;
return process_handle->status;
#endif // MS_WINDOWS
}
tor_process_handle_destroy(process_handle_t *process_handle,
int also_terminate_process)
{
+ if (!process_handle)
+ return;
+
if (also_terminate_process) {
if (tor_terminate_process(process_handle) < 0) {
log_notice(LD_GENERAL, "Failed to terminate process with PID '%d'",
fclose(process_handle->stderr_handle);
#endif
+ memset(process_handle, 0x0f, sizeof(process_handle_t));
tor_free(process_handle);
}
* probably not work in Tor, because waitpid() is called in main.c to reap any
* terminated child processes.*/
int
-tor_get_exit_code(const process_handle_t process_handle,
+tor_get_exit_code(const process_handle_t *process_handle,
int block, int *exit_code)
{
#ifdef MS_WINDOWS
if (block) {
/* Wait for the process to exit */
- retval = WaitForSingleObject(process_handle.pid.hProcess, INFINITE);
+ retval = WaitForSingleObject(process_handle->pid.hProcess, INFINITE);
if (retval != WAIT_OBJECT_0) {
log_warn(LD_GENERAL, "WaitForSingleObject() failed (%d): %s",
(int)retval, format_win32_error(GetLastError()));
return PROCESS_EXIT_ERROR;
}
} else {
- retval = WaitForSingleObject(process_handle.pid.hProcess, 0);
+ retval = WaitForSingleObject(process_handle->pid.hProcess, 0);
if (WAIT_TIMEOUT == retval) {
/* Process has not exited */
return PROCESS_EXIT_RUNNING;
}
if (exit_code != NULL) {
- success = GetExitCodeProcess(process_handle.pid.hProcess,
+ success = GetExitCodeProcess(process_handle->pid.hProcess,
(PDWORD)exit_code);
if (!success) {
log_warn(LD_GENERAL, "GetExitCodeProcess() failed: %s",
int stat_loc;
int retval;
- retval = waitpid(process_handle.pid, &stat_loc, block?0:WNOHANG);
+ retval = waitpid(process_handle->pid, &stat_loc, block?0:WNOHANG);
if (!block && 0 == retval) {
/* Process has not exited */
return PROCESS_EXIT_RUNNING;
- } else if (retval != process_handle.pid) {
- log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s", process_handle.pid,
+ } else if (retval != process_handle->pid) {
+ log_warn(LD_GENERAL, "waitpid() failed for PID %d: %s", process_handle->pid,
strerror(errno));
return PROCESS_EXIT_ERROR;
}
if (!WIFEXITED(stat_loc)) {
log_warn(LD_GENERAL, "Process %d did not exit normally",
- process_handle.pid);
+ process_handle->pid);
return PROCESS_EXIT_ERROR;
}
if (NULL == retval) {
if (feof(h)) {
log_debug(LD_GENERAL, "fgets() reached end of file");
- fclose(h);
if (eof)
*eof = 1;
break;
} else {
log_warn(LD_GENERAL, "fgets() from handle failed: %s",
strerror(errno));
- fclose(h);
return -1;
}
}
r = get_string_from_pipe(stream, buf, sizeof(buf) - 1);
if (r == IO_STREAM_CLOSED) {
- fclose(stream);
return 1;
} else if (r == IO_STREAM_EAGAIN) {
return 0;
} else if (r == IO_STREAM_TERM) {
- fclose(stream);
return -1;
}
/* Static variables are initialized to zero, so child_handle.status=0
* which corresponds to it not running on startup */
- static process_handle_t child_handle;
+ static process_handle_t *child_handle=NULL;
static time_t time_to_run_helper = 0;
int stdout_status, stderr_status, retval;
argv[9] = NULL;
/* Start the child, if it is not already running */
- if (child_handle.status != PROCESS_STATUS_RUNNING &&
+ if ((!child_handle || child_handle->status != PROCESS_STATUS_RUNNING) &&
time_to_run_helper < now) {
+ int status;
+
/* Assume tor-fw-helper will succeed, start it later*/
time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_SUCCESS;
+ if (child_handle) {
+ tor_process_handle_destroy(child_handle, 1);
+ child_handle = NULL;
+ }
+
#ifdef MS_WINDOWS
/* Passing NULL as lpApplicationName makes Windows search for the .exe */
- tor_spawn_background(NULL, argv, NULL, &child_handle);
+ status = tor_spawn_background(NULL, argv, NULL, &child_handle);
#else
- tor_spawn_background(filename, argv, NULL, &child_handle);
+ status = tor_spawn_background(filename, argv, NULL, &child_handle);
#endif
- if (PROCESS_STATUS_ERROR == child_handle.status) {
+
+ if (PROCESS_STATUS_ERROR == status) {
log_warn(LD_GENERAL, "Failed to start port forwarding helper %s",
filename);
time_to_run_helper = now + TIME_TO_EXEC_FWHELPER_FAIL;
log_info(LD_GENERAL,
"Started port forwarding helper (%s) with pid '%d'",
- filename, tor_process_get_pid(&child_handle));
+ filename, tor_process_get_pid(child_handle));
}
/* If child is running, read from its stdout and stderr) */
- if (PROCESS_STATUS_RUNNING == child_handle.status) {
+ if (child_handle && PROCESS_STATUS_RUNNING == child_handle->status) {
/* Read from stdout/stderr and log result */
retval = 0;
#ifdef MS_WINDOWS
- stdout_status = log_from_handle(child_handle.stdout_pipe, LOG_INFO);
- stderr_status = log_from_handle(child_handle.stderr_pipe, LOG_WARN);
+ stdout_status = log_from_handle(child_handle->stdout_pipe, LOG_INFO);
+ stderr_status = log_from_handle(child_handle->stderr_pipe, LOG_WARN);
/* If we got this far (on Windows), the process started */
retval = 0;
#else
- stdout_status = log_from_pipe(child_handle.stdout_handle,
+ stdout_status = log_from_pipe(child_handle->stdout_handle,
LOG_INFO, filename, &retval);
- stderr_status = log_from_pipe(child_handle.stderr_handle,
+ stderr_status = log_from_pipe(child_handle->stderr_handle,
LOG_WARN, filename, &retval);
#endif
if (retval) {
/* There was a failure */
retval = -1;
#ifdef MS_WINDOWS
- else if (tor_get_exit_code(child_handle, 0, NULL) !=
+ else if (!child_handle || tor_get_exit_code(child_handle, 0, NULL) !=
PROCESS_EXIT_RUNNING) {
/* process has exited or there was an error */
/* TODO: Do something with the process return value */
if (0 != retval) {
if (1 == retval) {
log_info(LD_GENERAL, "Port forwarding helper terminated");
- child_handle.status = PROCESS_STATUS_NOTRUNNING;
+ child_handle->status = PROCESS_STATUS_NOTRUNNING;
} else {
log_warn(LD_GENERAL, "Failed to read from port forwarding helper");
- child_handle.status = PROCESS_STATUS_ERROR;
+ child_handle->status = PROCESS_STATUS_ERROR;
}
/* TODO: The child might not actually be finished (maybe it failed or
{
int retval, exit_code;
ssize_t pos;
- process_handle_t process_handle;
+ process_handle_t *process_handle=NULL;
char stdout_buf[100], stderr_buf[100];
+ int status;
/* Start the program */
#ifdef MS_WINDOWS
- tor_spawn_background(NULL, argv, NULL, &process_handle);
+ status = tor_spawn_background(NULL, argv, NULL, &process_handle);
#else
- tor_spawn_background(argv[0], argv, NULL, &process_handle);
+ status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
#endif
- tt_int_op(process_handle.status, ==, expected_status);
-
- /* If the process failed to start, don't bother continuing */
- if (process_handle.status == PROCESS_STATUS_ERROR)
+ tt_int_op(status, ==, expected_status);
+ if (status == PROCESS_STATUS_ERROR)
return;
- tt_int_op(process_handle.stdout_pipe, >, 0);
- tt_int_op(process_handle.stderr_pipe, >, 0);
+ tt_assert(process_handle != NULL);
+ tt_int_op(process_handle->status, ==, expected_status);
+
+ tt_int_op(process_handle->stdout_pipe, >, 0);
+ tt_int_op(process_handle->stderr_pipe, >, 0);
/* Check stdout */
- pos = tor_read_all_from_process_stdout(&process_handle, stdout_buf,
+ pos = tor_read_all_from_process_stdout(process_handle, stdout_buf,
sizeof(stdout_buf) - 1);
tt_assert(pos >= 0);
stdout_buf[pos] = '\0';
// TODO: Make test-child exit with something other than 0
/* Check stderr */
- pos = tor_read_all_from_process_stderr(&process_handle, stderr_buf,
+ pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
sizeof(stderr_buf) - 1);
tt_assert(pos >= 0);
stderr_buf[pos] = '\0';
tt_int_op(pos, ==, strlen(expected_err));
done:
- ;
+ if (process_handle)
+ tor_process_handle_destroy(process_handle, 1);
}
/** Check that we can launch a process and read the output */
int retval, exit_code;
ssize_t pos = -1;
- process_handle_t process_handle;
+ process_handle_t *process_handle=NULL;
+ int status;
char stdout_buf[100], stderr_buf[100];
#ifdef MS_WINDOWS
const char *argv[] = {"test-child.exe", "--test", NULL};
/* Start the program */
#ifdef MS_WINDOWS
- tor_spawn_background(NULL, argv, NULL, &process_handle);
+ status = tor_spawn_background(NULL, argv, NULL, &process_handle);
#else
- tor_spawn_background(argv[0], argv, NULL, &process_handle);
+ status = tor_spawn_background(argv[0], argv, NULL, &process_handle);
#endif
- tt_int_op(process_handle.status, ==, expected_status);
+ tt_int_op(status, ==, expected_status);
+ tt_assert(process_handle);
+ tt_int_op(process_handle->status, ==, expected_status);
/* Check stdout */
- for (expected_out_ctr =0; expected_out[expected_out_ctr] != NULL;) {
+ for (expected_out_ctr = 0; expected_out[expected_out_ctr] != NULL;) {
#ifdef MS_WINDOWS
- pos = tor_read_all_handle(process_handle.stdout_pipe, stdout_buf,
+ pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
sizeof(stdout_buf) - 1, NULL);
#else
/* Check that we didn't read the end of file last time */
tt_assert(!eof);
- pos = tor_read_all_handle(process_handle.stdout_handle, stdout_buf,
+ pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
sizeof(stdout_buf) - 1, NULL, &eof);
#endif
log_info(LD_GENERAL, "tor_read_all_handle() returned %d", (int)pos);
/* The process should have exited without writing more */
#ifdef MS_WINDOWS
- pos = tor_read_all_handle(process_handle.stdout_pipe, stdout_buf,
+ pos = tor_read_all_handle(process_handle->stdout_pipe, stdout_buf,
sizeof(stdout_buf) - 1,
- &process_handle);
+ process_handle);
tt_int_op(pos, ==, 0);
#else
if (!eof) {
/* We should have got all the data, but maybe not the EOF flag */
- pos = tor_read_all_handle(process_handle.stdout_handle, stdout_buf,
+ pos = tor_read_all_handle(process_handle->stdout_handle, stdout_buf,
sizeof(stdout_buf) - 1,
- &process_handle, &eof);
+ process_handle, &eof);
tt_int_op(pos, ==, 0);
tt_assert(eof);
}
// TODO: Make test-child exit with something other than 0
/* Check stderr */
- pos = tor_read_all_from_process_stderr(&process_handle, stderr_buf,
+ pos = tor_read_all_from_process_stderr(process_handle, stderr_buf,
sizeof(stderr_buf) - 1);
tt_assert(pos >= 0);
stderr_buf[pos] = '\0';
tt_int_op(pos, ==, strlen(expected_err));
done:
- ;
+ tor_process_handle_destroy(process_handle, 1);
}
/**