summaryrefslogtreecommitdiff
path: root/src/lib/process/process.h
diff options
context:
space:
mode:
authorAlexander Færøy <ahf@torproject.org>2018-11-22 04:25:11 +0100
committerNick Mathewson <nickm@torproject.org>2018-12-17 16:39:28 -0500
commit35509978dd4985901431abe895d1443e75afc00a (patch)
tree70d1b4ff8919b5da64607736adfaae9af08c92d5 /src/lib/process/process.h
parent2b41b857bdabffeafbbf748189b22cd6ee818394 (diff)
downloadtor-35509978dd4985901431abe895d1443e75afc00a.tar.gz
tor-35509978dd4985901431abe895d1443e75afc00a.zip
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
Diffstat (limited to 'src/lib/process/process.h')
-rw-r--r--src/lib/process/process.h127
1 files changed, 127 insertions, 0 deletions
diff --git a/src/lib/process/process.h b/src/lib/process/process.h
new file mode 100644
index 0000000000..cf20a5d80b
--- /dev/null
+++ b/src/lib/process/process.h
@@ -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). */