From f7e175db57c3c0a0acc2ca424163213544f34633 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Thu, 20 Dec 2018 14:36:04 +0100 Subject: [PATCH 1/5] Forward declare smartlist_t in process.h This allows other libraries to include process.h without including the smartlist_t headers first. See: https://bugs.torproject.org/28847 --- src/lib/process/process.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/lib/process/process.h b/src/lib/process/process.h index 179db19aeb..956d34ab29 100644 --- a/src/lib/process/process.h +++ b/src/lib/process/process.h @@ -47,6 +47,8 @@ const char *process_protocol_to_string(process_protocol_t protocol); void tor_disable_spawning_background_processes(void); +struct smartlist_t; + struct process_t; typedef struct process_t process_t; @@ -61,7 +63,7 @@ typedef bool void process_init(void); void process_free_all(void); -const smartlist_t *process_get_all_processes(void); +const struct smartlist_t *process_get_all_processes(void); process_t *process_new(const char *command); void process_free_(process_t *process); @@ -82,10 +84,11 @@ void process_set_exit_callback(process_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); +const struct smartlist_t *process_get_arguments(const process_t *process); char **process_get_argv(const process_t *process); -void process_reset_environment(process_t *process, const smartlist_t *env); +void process_reset_environment(process_t *process, + const struct smartlist_t *env); void process_set_environment(process_t *process, const char *key, const char *value); From 01819faaba0b4817ce15d29be0944e23268e76c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Thu, 20 Dec 2018 14:36:59 +0100 Subject: [PATCH 2/5] Remove Process initializer/shutdown function from main.c. See: https://bugs.torproject.org/28847 --- src/app/main/main.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/app/main/main.c b/src/app/main/main.c index aa322046fe..d71e43ec30 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -74,7 +74,6 @@ #include "lib/net/resolve.h" #include "lib/process/waitpid.h" -#include "lib/process/process.h" #include "lib/meminfo/meminfo.h" #include "lib/osinfo/uname.h" @@ -560,9 +559,6 @@ tor_init(int argc, char *argv[]) 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(); @@ -790,7 +786,6 @@ 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(); From cf4b3dbd445799bfad21e84777eff91d3a409f21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Thu, 20 Dec 2018 15:07:05 +0100 Subject: [PATCH 3/5] Use the subsystem list to initialize and shutdown process module. This patch makes the process module use the subsystem list for initializing and shutting down. See: https://bugs.torproject.org/28847 --- changes/ticket28847 | 3 +++ src/app/main/subsystem_list.c | 2 ++ src/lib/process/include.am | 2 ++ src/lib/process/process_sys.c | 33 +++++++++++++++++++++++++++++++++ src/lib/process/process_sys.h | 14 ++++++++++++++ 5 files changed, 54 insertions(+) create mode 100644 changes/ticket28847 create mode 100644 src/lib/process/process_sys.c create mode 100644 src/lib/process/process_sys.h diff --git a/changes/ticket28847 b/changes/ticket28847 new file mode 100644 index 0000000000..63100c5813 --- /dev/null +++ b/changes/ticket28847 @@ -0,0 +1,3 @@ + o Minor features (process): + - Use the subsystem module to initialize and shut down the process module. + Closes ticket 28847. diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c index 8640329e92..122f7af215 100644 --- a/src/app/main/subsystem_list.c +++ b/src/app/main/subsystem_list.c @@ -18,6 +18,7 @@ #include "lib/time/time_sys.h" #include "lib/tls/tortls_sys.h" #include "lib/wallclock/wallclock_sys.h" +#include "lib/process/process_sys.h" #include @@ -32,6 +33,7 @@ const subsys_fns_t *tor_subsystems[] = { &sys_logging, /* -90 */ &sys_time, /* -90 */ &sys_network, /* -90 */ + &sys_process, /* -80 */ &sys_compress, /* -70 */ &sys_crypto, /* -60 */ &sys_tortls, /* -50 */ diff --git a/src/lib/process/include.am b/src/lib/process/include.am index a2d54b6238..83b67bf029 100644 --- a/src/lib/process/include.am +++ b/src/lib/process/include.am @@ -10,6 +10,7 @@ src_lib_libtor_process_a_SOURCES = \ src/lib/process/env.c \ src/lib/process/pidfile.c \ src/lib/process/process.c \ + src/lib/process/process_sys.c \ src/lib/process/process_unix.c \ src/lib/process/process_win32.c \ src/lib/process/restrict.c \ @@ -27,6 +28,7 @@ noinst_HEADERS += \ src/lib/process/env.h \ src/lib/process/pidfile.h \ src/lib/process/process.h \ + src/lib/process/process_sys.h \ src/lib/process/process_unix.h \ src/lib/process/process_win32.h \ src/lib/process/restrict.h \ diff --git a/src/lib/process/process_sys.c b/src/lib/process/process_sys.c new file mode 100644 index 0000000000..a880ff146e --- /dev/null +++ b/src/lib/process/process_sys.c @@ -0,0 +1,33 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file process_sys.c + * \brief Subsystem object for process setup. + **/ + +#include "orconfig.h" +#include "lib/subsys/subsys.h" +#include "lib/process/process_sys.h" +#include "lib/process/process.h" + +static int +subsys_process_initialize(void) +{ + process_init(); + return 0; +} + +static void +subsys_process_shutdown(void) +{ + process_free_all(); +} + +const subsys_fns_t sys_process = { + .name = "process", + .level = -80, + .supported = true, + .initialize = subsys_process_initialize, + .shutdown = subsys_process_shutdown +}; diff --git a/src/lib/process/process_sys.h b/src/lib/process/process_sys.h new file mode 100644 index 0000000000..b299334b6b --- /dev/null +++ b/src/lib/process/process_sys.h @@ -0,0 +1,14 @@ +/* Copyright (c) 2018, The Tor Project, Inc. */ +/* See LICENSE for licensing information */ + +/** + * \file process_sys.h + * \brief Declare subsystem object for the process module. + **/ + +#ifndef TOR_PROCESS_SYS_H +#define TOR_PROCESS_SYS_H + +extern const struct subsys_fns_t sys_process; + +#endif /* !defined(TOR_PROCESS_SYS_H) */ From 2322b56389626969b85da605240cbaafbb98bb33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Thu, 20 Dec 2018 15:16:14 +0100 Subject: [PATCH 4/5] Fix typo in time_sys.h. --- src/lib/time/time_sys.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/time/time_sys.h b/src/lib/time/time_sys.h index 0f1aebc268..5f5982a33b 100644 --- a/src/lib/time/time_sys.h +++ b/src/lib/time/time_sys.h @@ -2,7 +2,7 @@ /* See LICENSE for licensing information */ /** - * \file log_time.h + * \file time_sys.h * \brief Declare subsystem object for the time module. **/ From bc836d559dce5756f8ad2150d2351220ba7610f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexander=20F=C3=A6r=C3=B8y?= Date: Thu, 20 Dec 2018 17:03:50 +0100 Subject: [PATCH 5/5] Don't initialize the process module manually in tests. It's not longer needed for us to initialize the process module in tests. See: https://bugs.torproject.org/28847 --- src/test/test_process.c | 27 --------------------------- src/test/test_process_slow.c | 8 -------- src/test/test_pt.c | 9 --------- 3 files changed, 44 deletions(-) diff --git a/src/test/test_process.c b/src/test/test_process.c index 9b62862f04..27c9935e0d 100644 --- a/src/test/test_process.c +++ b/src/test/test_process.c @@ -142,8 +142,6 @@ 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. */ @@ -171,14 +169,12 @@ test_default_values(void *arg) done: process_free(process); - process_free_all(); } static void test_environment(void *arg) { (void)arg; - process_init(); process_t *process = process_new(""); process_environment_t *env = NULL; @@ -221,7 +217,6 @@ test_environment(void *arg) done: process_environment_free(env); process_free(process); - process_free_all(); } static void @@ -249,7 +244,6 @@ static void test_line_protocol_simple(void *arg) { (void)arg; - process_init(); process_data_t *process_data = process_data_new(); @@ -289,7 +283,6 @@ test_line_protocol_simple(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); UNMOCK(process_read_stdout); UNMOCK(process_read_stderr); @@ -299,7 +292,6 @@ static void test_line_protocol_multi(void *arg) { (void)arg; - process_init(); process_data_t *process_data = process_data_new(); @@ -349,7 +341,6 @@ test_line_protocol_multi(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); UNMOCK(process_read_stdout); UNMOCK(process_read_stderr); @@ -359,7 +350,6 @@ static void test_line_protocol_partial(void *arg) { (void)arg; - process_init(); process_data_t *process_data = process_data_new(); @@ -431,7 +421,6 @@ test_line_protocol_partial(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); UNMOCK(process_read_stdout); UNMOCK(process_read_stderr); @@ -441,7 +430,6 @@ static void test_raw_protocol_simple(void *arg) { (void)arg; - process_init(); process_data_t *process_data = process_data_new(); @@ -499,7 +487,6 @@ test_raw_protocol_simple(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); UNMOCK(process_read_stdout); UNMOCK(process_read_stderr); @@ -510,8 +497,6 @@ test_write_simple(void *arg) { (void)arg; - process_init(); - process_data_t *process_data = process_data_new(); process_t *process = process_new(""); @@ -530,7 +515,6 @@ test_write_simple(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); UNMOCK(process_write_stdin); } @@ -540,8 +524,6 @@ test_exit_simple(void *arg) { (void)arg; - process_init(); - process_data_t *process_data = process_data_new(); process_t *process = process_new(""); @@ -566,14 +548,12 @@ test_exit_simple(void *arg) 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; @@ -600,7 +580,6 @@ test_argv_simple(void *arg) done: tor_free(argv); process_free(process); - process_free_all(); } static void @@ -608,8 +587,6 @@ test_unix(void *arg) { (void)arg; #ifndef _WIN32 - process_init(); - process_t *process = process_new(""); /* On Unix all processes should have a Unix process handle. */ @@ -617,7 +594,6 @@ test_unix(void *arg) done: process_free(process); - process_free_all(); #endif } @@ -626,8 +602,6 @@ test_win32(void *arg) { (void)arg; #ifdef _WIN32 - process_init(); - process_t *process = process_new(""); char *joined_argv = NULL; @@ -675,7 +649,6 @@ test_win32(void *arg) done: tor_free(joined_argv); process_free(process); - process_free_all(); #endif } diff --git a/src/test/test_process_slow.c b/src/test/test_process_slow.c index a1f99bff0f..845662bde4 100644 --- a/src/test/test_process_slow.c +++ b/src/test/test_process_slow.c @@ -203,9 +203,6 @@ test_callbacks(void *arg) filename = TEST_PROCESS; #endif - /* Initialize Process subsystem. */ - process_init(); - /* Process callback data. */ process_data_t *process_data = process_data_new(); @@ -285,7 +282,6 @@ test_callbacks(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); } static void @@ -300,9 +296,6 @@ test_callbacks_terminate(void *arg) filename = TEST_PROCESS; #endif - /* Initialize Process subsystem. */ - process_init(); - /* Process callback data. */ process_data_t *process_data = process_data_new(); @@ -332,7 +325,6 @@ test_callbacks_terminate(void *arg) done: process_data_free(process_data); process_free(process); - process_free_all(); } struct testcase_t slow_process_tests[] = { diff --git a/src/test/test_pt.c b/src/test/test_pt.c index 8fcdd5c1e8..c8b9d18823 100644 --- a/src/test/test_pt.c +++ b/src/test/test_pt.c @@ -151,8 +151,6 @@ test_pt_get_transport_options(void *arg) config_line_t *cl = NULL; (void)arg; - process_init(); - execve_args = tor_malloc(sizeof(char*)*2); execve_args[0] = tor_strdup("cheeseshop"); execve_args[1] = NULL; @@ -192,7 +190,6 @@ test_pt_get_transport_options(void *arg) config_free_lines(cl); managed_proxy_destroy(mp, 0); smartlist_free(transport_list); - process_free_all(); } static void @@ -256,8 +253,6 @@ test_pt_get_extrainfo_string(void *arg) char *s = NULL; (void) arg; - process_init(); - argv1 = tor_malloc_zero(sizeof(char*)*3); argv1[0] = tor_strdup("ewige"); argv1[1] = tor_strdup("Blumenkraft"); @@ -291,7 +286,6 @@ test_pt_get_extrainfo_string(void *arg) smartlist_free(t1); smartlist_free(t2); tor_free(s); - process_free_all(); } static int @@ -347,8 +341,6 @@ test_pt_configure_proxy(void *arg) managed_proxy_t *mp = NULL; (void) arg; - process_init(); - dummy_state = tor_malloc_zero(sizeof(or_state_t)); MOCK(process_read_stdout, process_read_stdout_replacement); @@ -463,7 +455,6 @@ test_pt_configure_proxy(void *arg) tor_free(mp->argv[0]); tor_free(mp->argv); tor_free(mp); - process_free_all(); } /* Test the get_pt_proxy_uri() function. */