Add new Process subsystem.

This patch adds a new Process subsystem for running external programs in
the background of Tor. The design is focused around a new type named
`process_t` which have an API that allows the developer to easily write
code that interacts with the given child process. These interactions
includes:

- Easy API for writing output to the child process's standard input
  handle.
- Receive callbacks whenever the child has output on either its standard
  output or standard error handles.
- Receive callback when the child process terminates.

We also support two different "protocols" for handling output from the
child process. The default protocol is the "line" protocol where the
process output callbacks will be invoked only when there is complete
lines (either "\r\n" or "\n" terminated). We also support the "raw"
protocol where the read callbacks will get whatever the operating system
delivered to us in a single read operation.

This patch does not include any operating system backends, but the Unix
and Windows backends will be included in separate commits.

See: https://bugs.torproject.org/28179
This commit is contained in:
Alexander Færøy 2018-11-22 04:25:11 +01:00 committed by Nick Mathewson
parent 2b41b857bd
commit 35509978dd
9 changed files with 1343 additions and 2 deletions

View File

@ -73,6 +73,7 @@
#include "lib/geoip/geoip.h"
#include "lib/process/waitpid.h"
#include "lib/process/process.h"
#include "lib/meminfo/meminfo.h"
#include "lib/osinfo/uname.h"
@ -557,6 +558,10 @@ tor_init(int argc, char *argv[])
rend_cache_init();
addressmap_init(); /* Init the client dns cache. Do it always, since it's
* cheap. */
/* Initialize Process subsystem. */
process_init();
/* Initialize the HS subsystem. */
hs_init();
@ -784,6 +789,7 @@ tor_free_all(int postfork)
circuitmux_ewma_free_all();
accounting_free_all();
protover_summary_cache_free_all();
process_free_all();
if (!postfork) {
config_free_all();

View File

@ -4,8 +4,9 @@ lib/cc/*.h
lib/container/*.h
lib/ctime/*.h
lib/err/*.h
lib/intmath/*.h
lib/evloop/*.h
lib/fs/*.h
lib/intmath/*.h
lib/log/*.h
lib/malloc/*.h
lib/net/*.h
@ -15,4 +16,4 @@ lib/subsys/*.h
lib/testsupport/*.h
lib/thread/*.h
ht.h
ht.h

View File

@ -9,6 +9,7 @@ src_lib_libtor_process_a_SOURCES = \
src/lib/process/daemon.c \
src/lib/process/env.c \
src/lib/process/pidfile.c \
src/lib/process/process.c \
src/lib/process/restrict.c \
src/lib/process/setuid.c \
src/lib/process/subprocess.c \
@ -24,6 +25,7 @@ noinst_HEADERS += \
src/lib/process/daemon.h \
src/lib/process/env.h \
src/lib/process/pidfile.h \
src/lib/process/process.h \
src/lib/process/restrict.h \
src/lib/process/setuid.h \
src/lib/process/subprocess.h \

644
src/lib/process/process.c Normal file
View File

@ -0,0 +1,644 @@
/* Copyright (c) 2003, Roger Dingledine
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file process.c
* \brief Module for working with other processes.
**/
#define PROCESS_PRIVATE
#include "lib/container/buffers.h"
#include "lib/net/buffers_net.h"
#include "lib/container/smartlist.h"
#include "lib/log/log.h"
#include "lib/log/util_bug.h"
#include "lib/process/process.h"
#include "lib/process/env.h"
#ifdef HAVE_STDDEF_H
#include <stddef.h>
#endif
/** A list of all <b>process_t</b> instances currently allocated. */
static smartlist_t *processes;
/** Structure to represent a child process. */
struct process_t {
/** Process status. */
process_status_t status;
/** Which protocol is the process using? */
process_protocol_t protocol;
/** Which function to call when we have data ready from stdout? */
process_read_callback_t stdout_read_callback;
/** Which function to call when we have data ready from stderr? */
process_read_callback_t stderr_read_callback;
/** Which function call when our process terminated? */
process_exit_callback_t exit_callback;
/** Our exit code when the process have terminated. */
process_exit_code_t exit_code;
/** Name of the command we want to execute (for example: /bin/ls). */
char *command;
/** The arguments used for the new process. */
smartlist_t *arguments;
/** The environment used for the new process. */
smartlist_t *environment;
/** Buffer to store data from stdout when it is read. */
buf_t *stdout_buffer;
/** Buffer to store data from stderr when it is read. */
buf_t *stderr_buffer;
/** Buffer to store data to stdin before it is written. */
buf_t *stdin_buffer;
/** Do we need to store some custom data with the process? */
void *data;
};
/** Convert a given process status in <b>status</b> to its string
* representation. */
const char *
process_status_to_string(process_status_t status)
{
switch (status) {
case PROCESS_STATUS_NOT_RUNNING:
return "not running";
case PROCESS_STATUS_RUNNING:
return "running";
case PROCESS_STATUS_ERROR:
return "error";
}
/* LCOV_EXCL_START */
tor_assert_unreached();
return NULL;
/* LCOV_EXCL_STOP */
}
/** Convert a given process protocol in <b>protocol</b> to its string
* representation. */
const char *
process_protocol_to_string(process_protocol_t protocol)
{
switch (protocol) {
case PROCESS_PROTOCOL_LINE:
return "Line";
case PROCESS_PROTOCOL_RAW:
return "Raw";
}
/* LCOV_EXCL_START */
tor_assert_unreached();
return NULL;
/* LCOV_EXCL_STOP */
}
/** Initialize the Process subsystem. This function initializes the Process
* subsystem's global state. For cleaning up, <b>process_free_all()</b> should
* be called. */
void
process_init(void)
{
processes = smartlist_new();
}
/** Free up all resources that is handled by the Process subsystem. Note that
* this call does not terminate already running processes. */
void
process_free_all(void)
{
SMARTLIST_FOREACH(processes, process_t *, x, process_free(x));
smartlist_free(processes);
}
/** Get a list of all processes. This function returns a smartlist of
* <b>process_t</b> containing all the currently allocated processes. */
const smartlist_t *
process_get_all_processes(void)
{
return processes;
}
/** Allocate and initialize a new process. This function returns a newly
* allocated and initialized process data, which can be used to configure and
* later run a subprocess of Tor. Use the various <b>process_set_*()</b>
* methods to configure it and run the process using <b>process_exec()</b>. Use
* <b>command</b> to specify the path to the command to run. You can either
* specify an absolute path to the command or relative where Tor will use the
* underlying operating system's functionality for finding the command to run.
* */
process_t *
process_new(const char *command)
{
tor_assert(command);
process_t *process;
process = tor_malloc_zero(sizeof(process_t));
/* Set our command. */
process->command = tor_strdup(command);
/* By default we are not running. */
process->status = PROCESS_STATUS_NOT_RUNNING;
/* Prepare process environment. */
process->arguments = smartlist_new();
process->environment = smartlist_new();
/* Prepare the buffers. */
process->stdout_buffer = buf_new();
process->stderr_buffer = buf_new();
process->stdin_buffer = buf_new();
smartlist_add(processes, process);
return process;
}
/** Deallocate the given process in <b>process</b>. */
void
process_free_(process_t *process)
{
if (! process)
return;
/* Cleanup parameters. */
tor_free(process->command);
/* Cleanup arguments and environment. */
SMARTLIST_FOREACH(process->arguments, char *, x, tor_free(x));
smartlist_free(process->arguments);
SMARTLIST_FOREACH(process->environment, char *, x, tor_free(x));
smartlist_free(process->environment);
/* Cleanup the buffers. */
buf_free(process->stdout_buffer);
buf_free(process->stderr_buffer);
buf_free(process->stdin_buffer);
smartlist_remove(processes, process);
tor_free(process);
}
/** Execute the given process. This function executes the given process as a
* subprocess of Tor. Returns <b>PROCESS_STATUS_RUNNING</b> upon success. */
process_status_t
process_exec(process_t *process)
{
tor_assert(process);
process_status_t status = PROCESS_STATUS_NOT_RUNNING;
log_info(LD_PROCESS, "Starting new process: %s", process->command);
/* Update our state. */
process_set_status(process, status);
if (status != PROCESS_STATUS_RUNNING) {
log_warn(LD_PROCESS, "Failed to start process: %s",
process_get_command(process));
}
return status;
}
/** 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.
*
* Use <b>process_set_protocol(process, PROCESS_PROTOCOL_LINE)</b> if you want
* the callback to only contain complete "\n" or "\r\n" terminated lines. */
void
process_set_stdout_read_callback(process_t *process,
process_read_callback_t callback)
{
tor_assert(process);
process->stdout_read_callback = callback;
}
/** Set the callback function for output from the child process's standard
* error handle. This function sets the callback function which is called
* every time the child process have written output to its standard error file
* handle.
*
* Use <b>process_set_protocol(process, PROCESS_PROTOCOL_LINE)</b> if you want
* the callback to only contain complete "\n" or "\r\n" terminated lines. */
void
process_set_stderr_read_callback(process_t *process,
process_read_callback_t callback)
{
tor_assert(process);
process->stderr_read_callback = callback;
}
/** Set the callback function for process exit notification. The
* <b>callback</b> function will be called every time your child process have
* terminated. */
void
process_set_exit_callback(process_t *process,
process_exit_callback_t callback)
{
tor_assert(process);
process->exit_callback = callback;
}
/** Get the current command of the given process. */
const char *
process_get_command(const process_t *process)
{
tor_assert(process);
return process->command;
}
void
process_set_protocol(process_t *process, process_protocol_t protocol)
{
tor_assert(process);
process->protocol = protocol;
}
/** Get the currently used protocol of the given process. */
process_protocol_t
process_get_protocol(const process_t *process)
{
tor_assert(process);
return process->protocol;
}
/** Set opague pointer to data. This function allows you to store a pointer to
* your own data in the given process. Use <b>process_get_data()</b> in the
* various callback functions to retrieve the data again.
*
* Note that the given process does NOT take ownership of the data and you are
* responsible for freeing up any resources allocated by the given data.
* */
void
process_set_data(process_t *process, void *data)
{
tor_assert(process);
process->data = data;
}
/** Get the opaque pointer to callback data from the given process. This
* function allows you get the data you stored with <b>process_set_data()</b>
* in the different callback functions. */
void *
process_get_data(const process_t *process)
{
tor_assert(process);
return process->data;
}
/** Set the status of a given process. */
void
process_set_status(process_t *process, process_status_t status)
{
tor_assert(process);
process->status = status;
}
/** Get the status of the given process. */
process_status_t
process_get_status(const process_t *process)
{
tor_assert(process);
return process->status;
}
/** Append an argument to the list of arguments in the given process. */
void
process_append_argument(process_t *process, const char *argument)
{
tor_assert(process);
tor_assert(argument);
smartlist_add(process->arguments, tor_strdup(argument));
}
/** Returns a list of arguments (excluding the command itself) from the
* given process. */
const smartlist_t *
process_get_arguments(const process_t *process)
{
tor_assert(process);
return process->arguments;
}
/** Returns a newly allocated Unix style argument vector. Use <b>tor_free()</b>
* to deallocate it after use. */
char **
process_get_argv(const process_t *process)
{
tor_assert(process);
/** Generate a Unix style process argument vector from our process's
* arguments smartlist_t. */
char **argv = NULL;
char *filename = process->command;
const smartlist_t *arguments = process->arguments;
const size_t size = smartlist_len(arguments);
/* Make space for the process filename as argv[0] and a trailing NULL. */
argv = tor_malloc_zero(sizeof(char *) * (size + 2));
/* Set our filename as first argument. */
argv[0] = filename;
/* Put in the rest of the values from arguments. */
SMARTLIST_FOREACH_BEGIN(arguments, char *, arg_val) {
tor_assert(arg_val != NULL);
argv[arg_val_sl_idx + 1] = arg_val;
} SMARTLIST_FOREACH_END(arg_val);
return argv;
}
/** Set the given <b>key</b>/<b>value</b> pair as environment variable in the
* given process. */
void
process_set_environment(process_t *process,
const char *key,
const char *value)
{
tor_assert(process);
tor_assert(key);
tor_assert(value);
smartlist_add_asprintf(process->environment, "%s=%s", key, value);
}
/** Returns a newly allocated <b>process_environment_t</b> containing the
* environment variables for the given process. */
process_environment_t *
process_get_environment(const process_t *process)
{
tor_assert(process);
return process_environment_make(process->environment);
}
/** Write <b>size</b> bytes of <b>data</b> to the given process's standard
* input. */
void
process_write(process_t *process,
const uint8_t *data, size_t size)
{
tor_assert(process);
tor_assert(data);
buf_add(process->stdin_buffer, (char *)data, size);
process_write_stdin(process, process->stdin_buffer);
}
/** As tor_vsnprintf(), but write the data to the given process's standard
* input. */
void
process_vprintf(process_t *process,
const char *format, va_list args)
{
tor_assert(process);
tor_assert(format);
int size;
char *data;
size = tor_vasprintf(&data, format, args);
process_write(process, (uint8_t *)data, size);
tor_free(data);
}
/** As tor_snprintf(), but write the data to the given process's standard
* input. */
void
process_printf(process_t *process,
const char *format, ...)
{
tor_assert(process);
tor_assert(format);
va_list ap;
va_start(ap, format);
process_vprintf(process, format, ap);
va_end(ap);
}
/** This function is called by the Process backend when a given process have
* data that is ready to be read from the child process's standard output
* handle. */
void
process_notify_event_stdout(process_t *process)
{
tor_assert(process);
int ret;
ret = process_read_stdout(process, process->stdout_buffer);
if (ret > 0)
process_read_data(process,
process->stdout_buffer,
process->stdout_read_callback);
}
/** This function is called by the Process backend when a given process have
* data that is ready to be read from the child process's standard error
* handle. */
void
process_notify_event_stderr(process_t *process)
{
tor_assert(process);
int ret;
ret = process_read_stderr(process, process->stderr_buffer);
if (ret > 0)
process_read_data(process,
process->stderr_buffer,
process->stderr_read_callback);
}
/** This function is called by the Process backend when a given process is
* allowed to begin writing data to the standard input of the child process. */
void
process_notify_event_stdin(process_t *process)
{
tor_assert(process);
process_write_stdin(process, process->stdin_buffer);
}
/** This function is called by the Process backend when a given process have
* terminated. The exit status code is passed in <b>exit_code</b>. We mark the
* process as no longer running and calls the <b>exit_callback</b> with
* information about the process termination. */
void
process_notify_event_exit(process_t *process, process_exit_code_t exit_code)
{
tor_assert(process);
log_debug(LD_PROCESS,
"Process terminated with exit code: %"PRIu64, exit_code);
/* Update our state. */
process_set_status(process, PROCESS_STATUS_NOT_RUNNING);
process->exit_code = exit_code;
/* Call our exit callback, if it exists. */
if (process->exit_callback) {
process->exit_callback(process, exit_code);
}
}
/** This function is called whenever the Process backend have notified us that
* there is data to be read from its standard out handle. Returns the number of
* bytes that have been put into the given buffer. */
MOCK_IMPL(STATIC int, process_read_stdout, (process_t *process, buf_t *buffer))
{
tor_assert(process);
tor_assert(buffer);
return 0;
}
/** This function is called whenever the Process backend have notified us that
* there is data to be read from its standard error handle. Returns the number
* of bytes that have been put into the given buffer. */
MOCK_IMPL(STATIC int, process_read_stderr, (process_t *process, buf_t *buffer))
{
tor_assert(process);
tor_assert(buffer);
return 0;
}
/** This function calls the backend function for the given process whenever
* there is data to be written to the backends' file handles. */
MOCK_IMPL(STATIC void, process_write_stdin,
(process_t *process, buf_t *buffer))
{
tor_assert(process);
tor_assert(buffer);
}
/** This function calls the protocol handlers based on the value of
* <b>process_get_protocol(process)</b>. Currently we call
* <b>process_read_buffer()</b> for <b>PROCESS_PROTOCOL_RAW</b> and
* <b>process_read_lines()</b> for <b>PROCESS_PROTOCOL_LINE</b>. */
STATIC void
process_read_data(process_t *process,
buf_t *buffer,
process_read_callback_t callback)
{
tor_assert(process);
tor_assert(buffer);
switch (process_get_protocol(process)) {
case PROCESS_PROTOCOL_RAW:
process_read_buffer(process, buffer, callback);
break;
case PROCESS_PROTOCOL_LINE:
process_read_lines(process, buffer, callback);
break;
default:
/* LCOV_EXCL_START */
tor_assert_unreached();
return;
/* LCOV_EXCL_STOP */
}
}
/** This function takes the content of the given <b>buffer</b> and passes it to
* the given <b>callback</b> function, but ensures that an additional zero byte
* is added to the end of the data such that the given callback implementation
* can threat the content as a ASCIIZ string. */
STATIC void
process_read_buffer(process_t *process,
buf_t *buffer,
process_read_callback_t callback)
{
tor_assert(process);
tor_assert(buffer);
const size_t size = buf_datalen(buffer);
/* We allocate an extra byte for the zero byte in the end. */
char *data = tor_malloc_zero(size + 1);
buf_get_bytes(buffer, data, size);
log_debug(LD_PROCESS, "Read data from process");
if (callback)
callback(process, data, size);
tor_free(data);
}
/** This function tries to extract complete lines from the given <b>buffer</b>
* and calls the given <b>callback</b> function whenever it has a complete
* line. Before calling <b>callback</b> we remove the trailing "\n" or "\r\n"
* from the line. If we are unable to extract a complete line we leave the data
* in the buffer for next call. */
STATIC void
process_read_lines(process_t *process,
buf_t *buffer,
process_read_callback_t callback)
{
tor_assert(process);
tor_assert(buffer);
const size_t size = buf_datalen(buffer) + 1;
size_t line_size = 0;
char *data = tor_malloc_zero(size);
int ret;
while (true) {
line_size = size;
ret = buf_get_line(buffer, data, &line_size);
/* A complete line should always be smaller than the size of our
* buffer. */
tor_assert(ret != -1);
/* Remove \n from the end of the line. */
if (data[line_size - 1] == '\n') {
data[line_size - 1] = '\0';
--line_size;
}
/* Remove \r from the end of the line. */
if (data[line_size - 1] == '\r') {
data[line_size - 1] = '\0';
--line_size;
}
if (ret == 1) {
log_debug(LD_PROCESS, "Read line from process: \"%s\"", data);
if (callback)
callback(process, data, line_size);
/* We have read a whole line, let's see if there is more lines to read.
* */
continue;
}
/* No complete line for us to read. We are done for now. */
tor_assert_nonfatal(ret == 0);
break;
}
tor_free(data);
}

127
src/lib/process/process.h Normal file
View File

@ -0,0 +1,127 @@
/* Copyright (c) 2003-2004, Roger Dingledine
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
* Copyright (c) 2007-2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file process.h
* \brief Header for process.c
**/
#ifndef TOR_PROCESS_H
#define TOR_PROCESS_H
#include "orconfig.h"
#include "lib/malloc/malloc.h"
#include "lib/string/printf.h"
/** Maximum number of bytes to write to a process' stdin. */
#define PROCESS_MAX_WRITE (1024)
/** Maximum number of bytes to read from a process' stdout/stderr. */
#define PROCESS_MAX_READ (1024)
typedef enum {
/** The process is not running. */
PROCESS_STATUS_NOT_RUNNING,
/** The process is running. */
PROCESS_STATUS_RUNNING,
/** The process is in an erroneous state. */
PROCESS_STATUS_ERROR
} process_status_t;
const char *process_status_to_string(process_status_t status);
typedef enum {
/** Pass complete \n-terminated lines to the
* callback (with the \n or \r\n removed). */
PROCESS_PROTOCOL_LINE,
/** Pass the raw response from read() to the callback. */
PROCESS_PROTOCOL_RAW
} process_protocol_t;
const char *process_protocol_to_string(process_protocol_t protocol);
struct process_t;
typedef struct process_t process_t;
typedef uint64_t process_exit_code_t;
typedef void (*process_read_callback_t)(process_t *,
char *,
size_t);
typedef void (*process_exit_callback_t)(process_t *,
process_exit_code_t);
void process_init(void);
void process_free_all(void);
const smartlist_t *process_get_all_processes(void);
process_t *process_new(const char *command);
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);
void process_set_stdout_read_callback(process_t *,
process_read_callback_t);
void process_set_stderr_read_callback(process_t *,
process_read_callback_t);
void process_set_exit_callback(process_t *,
process_exit_callback_t);
const char *process_get_command(const process_t *process);
void process_append_argument(process_t *process, const char *argument);
const smartlist_t *process_get_arguments(const process_t *process);
char **process_get_argv(const process_t *process);
void process_set_environment(process_t *process,
const char *key,
const char *value);
struct process_environment_t;
struct process_environment_t *process_get_environment(const process_t *);
void process_set_protocol(process_t *process, process_protocol_t protocol);
process_protocol_t process_get_protocol(const process_t *process);
void process_set_data(process_t *process, void *data);
void *process_get_data(const process_t *process);
void process_set_status(process_t *process, process_status_t status);
process_status_t process_get_status(const process_t *process);
void process_write(process_t *process,
const uint8_t *data, size_t size);
void process_vprintf(process_t *process,
const char *format, va_list args) CHECK_PRINTF(2, 0);
void process_printf(process_t *process,
const char *format, ...) CHECK_PRINTF(2, 3);
void process_notify_event_stdout(process_t *process);
void process_notify_event_stderr(process_t *process);
void process_notify_event_stdin(process_t *process);
void process_notify_event_exit(process_t *process,
process_exit_code_t);
#ifdef PROCESS_PRIVATE
MOCK_DECL(STATIC int, process_read_stdout, (process_t *, buf_t *));
MOCK_DECL(STATIC int, process_read_stderr, (process_t *, buf_t *));
MOCK_DECL(STATIC void, process_write_stdin, (process_t *, buf_t *));
STATIC void process_read_data(process_t *process,
buf_t *buffer,
process_read_callback_t callback);
STATIC void process_read_buffer(process_t *process,
buf_t *buffer,
process_read_callback_t callback);
STATIC void process_read_lines(process_t *process,
buf_t *buffer,
process_read_callback_t callback);
#endif /* defined(PROCESS_PRIVATE). */
#endif /* defined(TOR_PROCESS_H). */

View File

@ -153,6 +153,7 @@ src_test_test_SOURCES += \
src/test/test_pem.c \
src/test/test_periodic_event.c \
src/test/test_policy.c \
src/test/test_process.c \
src/test/test_procmon.c \
src/test/test_proto_http.c \
src/test/test_proto_misc.c \

View File

@ -898,6 +898,7 @@ struct testgroup_t testgroups[] = {
{ "periodic-event/" , periodic_event_tests },
{ "policy/" , policy_tests },
{ "procmon/", procmon_tests },
{ "process/", process_tests },
{ "proto/http/", proto_http_tests },
{ "proto/misc/", proto_misc_tests },
{ "protover/", protover_tests },

View File

@ -241,6 +241,7 @@ extern struct testcase_t pem_tests[];
extern struct testcase_t periodic_event_tests[];
extern struct testcase_t policy_tests[];
extern struct testcase_t procmon_tests[];
extern struct testcase_t process_tests[];
extern struct testcase_t proto_http_tests[];
extern struct testcase_t proto_misc_tests[];
extern struct testcase_t protover_tests[];

558
src/test/test_process.c Normal file
View File

@ -0,0 +1,558 @@
/* Copyright (c) 2018, The Tor Project, Inc. */
/* See LICENSE for licensing information */
/**
* \file test_process.c
* \brief Test cases for the Process API.
*/
#include "orconfig.h"
#include "core/or/or.h"
#include "test/test.h"
#define PROCESS_PRIVATE
#include "lib/process/process.h"
static const char *stdout_read_buffer;
static const char *stderr_read_buffer;
struct process_data_t {
smartlist_t *stdout_data;
smartlist_t *stderr_data;
smartlist_t *stdin_data;
process_exit_code_t exit_code;
};
typedef struct process_data_t process_data_t;
static process_data_t *
process_data_new(void)
{
process_data_t *process_data = tor_malloc_zero(sizeof(process_data_t));
process_data->stdout_data = smartlist_new();
process_data->stderr_data = smartlist_new();
process_data->stdin_data = smartlist_new();
return process_data;
}
static void
process_data_free(process_data_t *process_data)
{
if (process_data == NULL)
return;
SMARTLIST_FOREACH(process_data->stdout_data, char *, x, tor_free(x));
SMARTLIST_FOREACH(process_data->stderr_data, char *, x, tor_free(x));
SMARTLIST_FOREACH(process_data->stdin_data, char *, x, tor_free(x));
smartlist_free(process_data->stdout_data);
smartlist_free(process_data->stderr_data);
smartlist_free(process_data->stdin_data);
tor_free(process_data);
}
static int
process_mocked_read_stdout(process_t *process, buf_t *buffer)
{
(void)process;
if (stdout_read_buffer != NULL) {
buf_add_string(buffer, stdout_read_buffer);
stdout_read_buffer = NULL;
}
return (int)buf_datalen(buffer);
}
static int
process_mocked_read_stderr(process_t *process, buf_t *buffer)
{
(void)process;
if (stderr_read_buffer != NULL) {
buf_add_string(buffer, stderr_read_buffer);
stderr_read_buffer = NULL;
}
return (int)buf_datalen(buffer);
}
static void
process_mocked_write_stdin(process_t *process, buf_t *buffer)
{
const size_t size = buf_datalen(buffer);
if (size == 0)
return;
char *data = tor_malloc_zero(size + 1);
process_data_t *process_data = process_get_data(process);
buf_get_bytes(buffer, data, size);
smartlist_add(process_data->stdin_data, data);
}
static void
process_stdout_callback(process_t *process, char *data, size_t size)
{
tt_ptr_op(process, OP_NE, NULL);
tt_ptr_op(data, OP_NE, NULL);
tt_int_op(strlen(data), OP_EQ, size);
process_data_t *process_data = process_get_data(process);
smartlist_add(process_data->stdout_data, tor_strdup(data));
done:
return;
}
static void
process_stderr_callback(process_t *process, char *data, size_t size)
{
tt_ptr_op(process, OP_NE, NULL);
tt_ptr_op(data, OP_NE, NULL);
tt_int_op(strlen(data), OP_EQ, size);
process_data_t *process_data = process_get_data(process);
smartlist_add(process_data->stderr_data, tor_strdup(data));
done:
return;
}
static void
process_exit_callback(process_t *process, process_exit_code_t exit_code)
{
tt_ptr_op(process, OP_NE, NULL);
process_data_t *process_data = process_get_data(process);
process_data->exit_code = exit_code;
done:
return;
}
static void
test_default_values(void *arg)
{
(void)arg;
process_init();
process_t *process = process_new("/path/to/nothing");
/* We are not running by default. */
tt_int_op(PROCESS_STATUS_NOT_RUNNING, OP_EQ, process_get_status(process));
/* We use the line protocol by default. */
tt_int_op(PROCESS_PROTOCOL_LINE, OP_EQ, process_get_protocol(process));
/* We don't set any custom data by default. */
tt_ptr_op(NULL, OP_EQ, process_get_data(process));
/* Our command was given to the process_t's constructor in process_new(). */
tt_str_op("/path/to/nothing", OP_EQ, process_get_command(process));
/* Make sure we are listed in the list of proccesses. */
tt_assert(smartlist_contains(process_get_all_processes(),
process));
/* Our arguments should be empty. */
tt_int_op(0, OP_EQ,
smartlist_len(process_get_arguments(process)));
done:
process_free(process);
process_free_all();
}
static void
test_stringified_types(void *arg)
{
(void)arg;
/* process_protocol_t values. */
tt_str_op("Raw", OP_EQ, process_protocol_to_string(PROCESS_PROTOCOL_RAW));
tt_str_op("Line", OP_EQ, process_protocol_to_string(PROCESS_PROTOCOL_LINE));
/* process_status_t values. */
tt_str_op("not running", OP_EQ,
process_status_to_string(PROCESS_STATUS_NOT_RUNNING));
tt_str_op("running", OP_EQ,
process_status_to_string(PROCESS_STATUS_RUNNING));
tt_str_op("error", OP_EQ,
process_status_to_string(PROCESS_STATUS_ERROR));
done:
return;
}
static void
test_line_protocol_simple(void *arg)
{
(void)arg;
process_init();
process_data_t *process_data = process_data_new();
process_t *process = process_new("");
process_set_data(process, process_data);
process_set_stdout_read_callback(process, process_stdout_callback);
process_set_stderr_read_callback(process, process_stderr_callback);
MOCK(process_read_stdout, process_mocked_read_stdout);
MOCK(process_read_stderr, process_mocked_read_stderr);
/* Make sure we are running with the line protocol. */
tt_int_op(PROCESS_PROTOCOL_LINE, OP_EQ, process_get_protocol(process));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = "Hello stdout\n";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = "Hello stderr\r\n";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Data should be ready. */
tt_int_op(1, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(1, OP_EQ, smartlist_len(process_data->stderr_data));
/* Check if the data is correct. */
tt_str_op(smartlist_get(process_data->stdout_data, 0), OP_EQ,
"Hello stdout");
tt_str_op(smartlist_get(process_data->stderr_data, 0), OP_EQ,
"Hello stderr");
done:
process_data_free(process_data);
process_free(process);
process_free_all();
UNMOCK(process_read_stdout);
UNMOCK(process_read_stderr);
}
static void
test_line_protocol_multi(void *arg)
{
(void)arg;
process_init();
process_data_t *process_data = process_data_new();
process_t *process = process_new("");
process_set_data(process, process_data);
process_set_stdout_read_callback(process, process_stdout_callback);
process_set_stderr_read_callback(process, process_stderr_callback);
MOCK(process_read_stdout, process_mocked_read_stdout);
MOCK(process_read_stderr, process_mocked_read_stderr);
/* Make sure we are running with the line protocol. */
tt_int_op(PROCESS_PROTOCOL_LINE, OP_EQ, process_get_protocol(process));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = "Hello stdout\r\nOnion Onion Onion\nA B C D\r\n\r\n";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = "Hello stderr\nFoo bar baz\nOnion Onion Onion\n";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Data should be ready. */
tt_int_op(4, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(3, OP_EQ, smartlist_len(process_data->stderr_data));
/* Check if the data is correct. */
tt_str_op(smartlist_get(process_data->stdout_data, 0), OP_EQ,
"Hello stdout");
tt_str_op(smartlist_get(process_data->stdout_data, 1), OP_EQ,
"Onion Onion Onion");
tt_str_op(smartlist_get(process_data->stdout_data, 2), OP_EQ,
"A B C D");
tt_str_op(smartlist_get(process_data->stdout_data, 3), OP_EQ,
"");
tt_str_op(smartlist_get(process_data->stderr_data, 0), OP_EQ,
"Hello stderr");
tt_str_op(smartlist_get(process_data->stderr_data, 1), OP_EQ,
"Foo bar baz");
tt_str_op(smartlist_get(process_data->stderr_data, 2), OP_EQ,
"Onion Onion Onion");
done:
process_data_free(process_data);
process_free(process);
process_free_all();
UNMOCK(process_read_stdout);
UNMOCK(process_read_stderr);
}
static void
test_line_protocol_partial(void *arg)
{
(void)arg;
process_init();
process_data_t *process_data = process_data_new();
process_t *process = process_new("");
process_set_data(process, process_data);
process_set_stdout_read_callback(process, process_stdout_callback);
process_set_stderr_read_callback(process, process_stderr_callback);
MOCK(process_read_stdout, process_mocked_read_stdout);
MOCK(process_read_stderr, process_mocked_read_stderr);
/* Make sure we are running with the line protocol. */
tt_int_op(PROCESS_PROTOCOL_LINE, OP_EQ, process_get_protocol(process));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = "Hello stdout this is a partial line ...";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = "Hello stderr this is a partial line ...";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Data should NOT be ready. */
tt_int_op(0, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = " the end\nAnother partial string goes here ...";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = " the end\nAnother partial string goes here ...";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Some data should be ready. */
tt_int_op(1, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(1, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = " the end\nFoo bar baz\n";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = " the end\nFoo bar baz\n";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Some data should be ready. */
tt_int_op(3, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(3, OP_EQ, smartlist_len(process_data->stderr_data));
/* Check if the data is correct. */
tt_str_op(smartlist_get(process_data->stdout_data, 0), OP_EQ,
"Hello stdout this is a partial line ... the end");
tt_str_op(smartlist_get(process_data->stdout_data, 1), OP_EQ,
"Another partial string goes here ... the end");
tt_str_op(smartlist_get(process_data->stdout_data, 2), OP_EQ,
"Foo bar baz");
tt_str_op(smartlist_get(process_data->stderr_data, 0), OP_EQ,
"Hello stderr this is a partial line ... the end");
tt_str_op(smartlist_get(process_data->stderr_data, 1), OP_EQ,
"Another partial string goes here ... the end");
tt_str_op(smartlist_get(process_data->stderr_data, 2), OP_EQ,
"Foo bar baz");
done:
process_data_free(process_data);
process_free(process);
process_free_all();
UNMOCK(process_read_stdout);
UNMOCK(process_read_stderr);
}
static void
test_raw_protocol_simple(void *arg)
{
(void)arg;
process_init();
process_data_t *process_data = process_data_new();
process_t *process = process_new("");
process_set_data(process, process_data);
process_set_protocol(process, PROCESS_PROTOCOL_RAW);
process_set_stdout_read_callback(process, process_stdout_callback);
process_set_stderr_read_callback(process, process_stderr_callback);
MOCK(process_read_stdout, process_mocked_read_stdout);
MOCK(process_read_stderr, process_mocked_read_stderr);
/* Make sure we are running with the raw protocol. */
tt_int_op(PROCESS_PROTOCOL_RAW, OP_EQ, process_get_protocol(process));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(0, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = "Hello stdout\n";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = "Hello stderr\n";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Data should be ready. */
tt_int_op(1, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(1, OP_EQ, smartlist_len(process_data->stderr_data));
stdout_read_buffer = "Hello, again, stdout\nThis contains multiple lines";
process_notify_event_stdout(process);
tt_ptr_op(NULL, OP_EQ, stdout_read_buffer);
stderr_read_buffer = "Hello, again, stderr\nThis contains multiple lines";
process_notify_event_stderr(process);
tt_ptr_op(NULL, OP_EQ, stderr_read_buffer);
/* Data should be ready. */
tt_int_op(2, OP_EQ, smartlist_len(process_data->stdout_data));
tt_int_op(2, OP_EQ, smartlist_len(process_data->stderr_data));
/* Check if the data is correct. */
tt_str_op(smartlist_get(process_data->stdout_data, 0), OP_EQ,
"Hello stdout\n");
tt_str_op(smartlist_get(process_data->stdout_data, 1), OP_EQ,
"Hello, again, stdout\nThis contains multiple lines");
tt_str_op(smartlist_get(process_data->stderr_data, 0), OP_EQ,
"Hello stderr\n");
tt_str_op(smartlist_get(process_data->stderr_data, 1), OP_EQ,
"Hello, again, stderr\nThis contains multiple lines");
done:
process_data_free(process_data);
process_free(process);
process_free_all();
UNMOCK(process_read_stdout);
UNMOCK(process_read_stderr);
}
static void
test_write_simple(void *arg)
{
(void)arg;
process_init();
process_data_t *process_data = process_data_new();
process_t *process = process_new("");
process_set_data(process, process_data);
MOCK(process_write_stdin, process_mocked_write_stdin);
process_write(process, (uint8_t *)"Hello world\n", 12);
process_notify_event_stdin(process);
tt_int_op(1, OP_EQ, smartlist_len(process_data->stdin_data));
process_printf(process, "Hello %s !\n", "moon");
process_notify_event_stdin(process);
tt_int_op(2, OP_EQ, smartlist_len(process_data->stdin_data));
done:
process_data_free(process_data);
process_free(process);
process_free_all();
UNMOCK(process_write_stdin);
}
static void
test_exit_simple(void *arg)
{
(void)arg;
process_init();
process_data_t *process_data = process_data_new();
process_t *process = process_new("");
process_set_data(process, process_data);
process_set_exit_callback(process, process_exit_callback);
/* Our default is 0. */
tt_int_op(0, OP_EQ, process_data->exit_code);
/* Fake that we are a running process. */
process_set_status(process, PROCESS_STATUS_RUNNING);
tt_int_op(process_get_status(process), OP_EQ, PROCESS_STATUS_RUNNING);
/* Fake an exit. */
process_notify_event_exit(process, 1337);
/* Check if our state changed and if our callback fired. */
tt_int_op(process_get_status(process), OP_EQ, PROCESS_STATUS_NOT_RUNNING);
tt_int_op(1337, OP_EQ, process_data->exit_code);
done:
process_set_data(process, process_data);
process_data_free(process_data);
process_free(process);
process_free_all();
}
static void
test_argv_simple(void *arg)
{
(void)arg;
process_init();
process_t *process = process_new("/bin/cat");
char **argv = NULL;
/* Setup some arguments. */
process_append_argument(process, "foo");
process_append_argument(process, "bar");
process_append_argument(process, "baz");
/* Check the number of elements. */
tt_int_op(3, OP_EQ,
smartlist_len(process_get_arguments(process)));
/* Let's try to convert it into a Unix style char **argv. */
argv = process_get_argv(process);
/* Check our values. */
tt_str_op(argv[0], OP_EQ, "/bin/cat");
tt_str_op(argv[1], OP_EQ, "foo");
tt_str_op(argv[2], OP_EQ, "bar");
tt_str_op(argv[3], OP_EQ, "baz");
tt_ptr_op(argv[4], OP_EQ, NULL);
done:
tor_free(argv);
process_free(process);
process_free_all();
}
struct testcase_t process_tests[] = {
{ "default_values", test_default_values, TT_FORK, NULL, NULL },
{ "stringified_types", test_stringified_types, TT_FORK, NULL, NULL },
{ "line_protocol_simple", test_line_protocol_simple, TT_FORK, NULL, NULL },
{ "line_protocol_multi", test_line_protocol_multi, TT_FORK, NULL, NULL },
{ "line_protocol_partial", test_line_protocol_partial, TT_FORK, NULL, NULL },
{ "raw_protocol_simple", test_raw_protocol_simple, TT_FORK, NULL, NULL },
{ "write_simple", test_write_simple, TT_FORK, NULL, NULL },
{ "exit_simple", test_exit_simple, TT_FORK, NULL, NULL },
{ "argv_simple", test_argv_simple, TT_FORK, NULL, NULL },
END_OF_TESTCASES
};