Tor  0.4.5.0-alpha-dev
process.h
Go to the documentation of this file.
1 /* Copyright (c) 2003-2004, Roger Dingledine
2  * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
3  * Copyright (c) 2007-2020, The Tor Project, Inc. */
4 /* See LICENSE for licensing information */
5 
6 /**
7  * \file process.h
8  * \brief Header for process.c
9  **/
10 
11 #ifndef TOR_PROCESS_H
12 #define TOR_PROCESS_H
13 
14 #include "orconfig.h"
15 #include "lib/malloc/malloc.h"
16 #include "lib/string/printf.h"
17 
18 #include <stdbool.h>
19 
20 /** Maximum number of bytes to write to a process' stdin. */
21 #define PROCESS_MAX_WRITE (1024)
22 
23 /** Maximum number of bytes to read from a process' stdout/stderr. */
24 #define PROCESS_MAX_READ (1024)
25 
26 typedef enum {
27  /** The process is not running. */
29 
30  /** The process is running. */
32 
33  /** The process is in an erroneous state. */
36 
37 const char *process_status_to_string(process_status_t status);
38 
39 typedef enum {
40  /** Pass complete newline-terminated lines to the
41  * callback (with the LF or CRLF removed). */
43 
44  /** Pass the raw response from read() to the callback. */
47 
49 
51 
52 struct smartlist_t;
53 
54 struct process_t;
55 typedef struct process_t process_t;
56 
57 typedef uint64_t process_exit_code_t;
58 typedef uint64_t process_pid_t;
59 
60 typedef void (*process_read_callback_t)(process_t *,
61  const char *,
62  size_t);
63 typedef bool
64 (*process_exit_callback_t)(process_t *, process_exit_code_t);
65 
66 void process_init(void);
67 void process_free_all(void);
68 const struct smartlist_t *process_get_all_processes(void);
69 
70 process_t *process_new(const char *command);
71 void process_free_(process_t *process);
72 #define process_free(s) FREE_AND_NULL(process_t, process_free_, (s))
73 
75 bool process_terminate(process_t *process);
76 
77 process_pid_t process_get_pid(process_t *process);
78 
80  process_read_callback_t);
82  process_read_callback_t);
84  process_exit_callback_t);
85 
86 const char *process_get_command(const process_t *process);
87 
88 void process_append_argument(process_t *process, const char *argument);
89 const struct smartlist_t *process_get_arguments(const process_t *process);
90 char **process_get_argv(const process_t *process);
91 
93  const struct smartlist_t *env);
95  const char *key,
96  const char *value);
97 
100 
101 void process_set_protocol(process_t *process, process_protocol_t protocol);
103 
104 void process_set_data(process_t *process, void *data);
105 void *process_get_data(const process_t *process);
106 
107 void process_set_status(process_t *process, process_status_t status);
109 
110 #ifndef _WIN32
111 struct process_unix_t;
112 struct process_unix_t *process_get_unix_process(const process_t *process);
113 #else
114 struct process_win32_t;
115 struct process_win32_t *process_get_win32_process(const process_t *process);
116 #endif /* !defined(_WIN32) */
117 
118 void process_write(process_t *process,
119  const uint8_t *data, size_t size);
120 void process_vprintf(process_t *process,
121  const char *format, va_list args) CHECK_PRINTF(2, 0);
122 void process_printf(process_t *process,
123  const char *format, ...) CHECK_PRINTF(2, 3);
124 
129  process_exit_code_t);
130 
131 #ifdef PROCESS_PRIVATE
132 struct buf_t;
133 MOCK_DECL(STATIC int, process_read_stdout, (process_t *, struct buf_t *));
134 MOCK_DECL(STATIC int, process_read_stderr, (process_t *, struct buf_t *));
135 MOCK_DECL(STATIC void, process_write_stdin, (process_t *, struct buf_t *));
136 
137 STATIC void process_read_data(process_t *process,
138  struct buf_t *buffer,
139  process_read_callback_t callback);
140 STATIC void process_read_buffer(process_t *process,
141  struct buf_t *buffer,
142  process_read_callback_t callback);
143 STATIC void process_read_lines(process_t *process,
144  struct buf_t *buffer,
145  process_read_callback_t callback);
146 #endif /* defined(PROCESS_PRIVATE) */
147 
148 #endif /* !defined(TOR_PROCESS_H) */
process_reset_environment
void process_reset_environment(process_t *process, const smartlist_t *env)
Definition: process.c:470
process_set_stdout_read_callback
void process_set_stdout_read_callback(process_t *, process_read_callback_t)
Definition: process.c:321
process_set_stderr_read_callback
void process_set_stderr_read_callback(process_t *, process_read_callback_t)
Definition: process.c:336
tor_disable_spawning_background_processes
void tor_disable_spawning_background_processes(void)
Definition: process.c:132
process_get_pid
process_pid_t process_get_pid(process_t *process)
Definition: process.c:303
process_read_data
STATIC void process_read_data(process_t *process, buf_t *buffer, process_read_callback_t callback)
Definition: process.c:695
process_protocol_t
process_protocol_t
Definition: process.h:39
process_read_stderr
STATIC int process_read_stderr(process_t *process, buf_t *buffer)
Definition: process.c:663
process_read_buffer
STATIC void process_read_buffer(process_t *process, buf_t *buffer, process_read_callback_t callback)
Definition: process.c:722
process_get_all_processes
const struct smartlist_t * process_get_all_processes(void)
Definition: process.c:166
process_notify_event_stderr
void process_notify_event_stderr(process_t *process)
Definition: process.c:594
PROCESS_PROTOCOL_LINE
@ PROCESS_PROTOCOL_LINE
Definition: process.h:42
process_read_stdout
STATIC int process_read_stdout(process_t *process, buf_t *buffer)
Definition: process.c:648
process_terminate
bool process_terminate(process_t *process)
Definition: process.c:284
PROCESS_PROTOCOL_RAW
@ PROCESS_PROTOCOL_RAW
Definition: process.h:45
process_exec
process_status_t process_exec(process_t *process)
Definition: process.c:253
process_get_environment
struct process_environment_t * process_get_environment(const process_t *)
Definition: process.c:501
process_set_environment
void process_set_environment(process_t *process, const char *key, const char *value)
Definition: process.c:487
process_write_stdin
STATIC void process_write_stdin(process_t *process, buf_t *buffer)
Definition: process.c:678
process_init
void process_init(void)
Definition: process.c:141
process_unix_t
Definition: process_unix.c:71
process_t
Definition: process.c:36
printf.h
Header for printf.c.
process_status_to_string
const char * process_status_to_string(process_status_t status)
Definition: process.c:92
process_status_t
process_status_t
Definition: process.h:26
process_notify_event_exit
void process_notify_event_exit(process_t *process, process_exit_code_t)
Definition: process.c:623
process_set_exit_callback
void process_set_exit_callback(process_t *, process_exit_callback_t)
Definition: process.c:347
process_get_argv
char ** process_get_argv(const process_t *process)
Definition: process.c:439
PROCESS_STATUS_NOT_RUNNING
@ PROCESS_STATUS_NOT_RUNNING
Definition: process.h:28
process_get_arguments
const struct smartlist_t * process_get_arguments(const process_t *process)
Definition: process.c:430
MOCK_DECL
#define MOCK_DECL(rv, funcname, arglist)
Definition: testsupport.h:127
process_notify_event_stdout
void process_notify_event_stdout(process_t *process)
Definition: process.c:577
malloc.h
Headers for util_malloc.c.
command
tor_cmdline_mode_t command
Definition: config.c:2445
process_notify_event_stdin
void process_notify_event_stdin(process_t *process)
Definition: process.c:610
process_get_data
void * process_get_data(const process_t *process)
Definition: process.c:395
process_vprintf
void process_vprintf(process_t *process, const char *format, va_list args)
Definition: process.c:543
process_free_
void process_free_(process_t *process)
Definition: process.c:217
process_get_status
process_status_t process_get_status(const process_t *process)
Definition: process.c:411
process_get_command
const char * process_get_command(const process_t *process)
Definition: process.c:356
process_set_data
void process_set_data(process_t *process, void *data)
Definition: process.c:385
PROCESS_STATUS_ERROR
@ PROCESS_STATUS_ERROR
Definition: process.h:34
process_write
void process_write(process_t *process, const uint8_t *data, size_t size)
Definition: process.c:530
process_protocol_to_string
const char * process_protocol_to_string(process_protocol_t protocol)
Definition: process.c:112
process_environment_t
Definition: env.h:21
PROCESS_STATUS_RUNNING
@ PROCESS_STATUS_RUNNING
Definition: process.h:31
process_append_argument
void process_append_argument(process_t *process, const char *argument)
Definition: process.c:419
process_get_protocol
process_protocol_t process_get_protocol(const process_t *process)
Definition: process.c:371
process_get_unix_process
struct process_unix_t * process_get_unix_process(const process_t *process)
Definition: process.c:510
process_set_status
void process_set_status(process_t *process, process_status_t status)
Definition: process.c:403
STATIC
#define STATIC
Definition: testsupport.h:32
process_free_all
void process_free_all(void)
Definition: process.c:153
process_new
process_t * process_new(const char *command)
Definition: process.c:180
smartlist_t
Definition: smartlist_core.h:26
process_printf
void process_printf(process_t *process, const char *format,...)
Definition: process.c:561
process_read_lines
STATIC void process_read_lines(process_t *process, buf_t *buffer, process_read_callback_t callback)
Definition: process.c:749