From d7a22160f54dfa90702cfd6d6f83a0a75909edcb Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 17 Jan 2020 08:39:49 -0500 Subject: [PATCH 1/3] Revert "Restore feature where nt-services detect non-"run_tor" modes." This reverts commit 5c240db0bf7751d74ba438a1ca4ef0d051a53df7. --- src/app/main/main.c | 7 ---- src/app/main/ntmain.c | 52 ++++++++++++++++++++---------- src/feature/api/tor_api_internal.h | 5 --- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/src/app/main/main.c b/src/app/main/main.c index b533406eaa..dad9777265 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1274,13 +1274,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg) #endif } - if (tor_cfg->run_tor_only && get_options()->command != CMD_RUN_TOR) { - log_err(LD_CONFIG, "Unsupported command when running as an NT service."); - result = -1; - tor_cleanup(); - goto done; - } - switch (get_options()->command) { case CMD_RUN_TOR: nt_service_set_state(SERVICE_RUNNING); diff --git a/src/app/main/ntmain.c b/src/app/main/ntmain.c index 96da1690f2..ced158e686 100644 --- a/src/app/main/ntmain.c +++ b/src/app/main/ntmain.c @@ -29,8 +29,6 @@ #include "lib/evloop/compat_libevent.h" #include "lib/fs/winlib.h" #include "lib/log/win32err.h" -#include "feature/api/tor_api.h" -#include "feature/api/tor_api_internal.h" #include #define GENSRV_SERVICENAME "tor" @@ -265,6 +263,7 @@ nt_service_control(DWORD request) static void nt_service_body(int argc, char **argv) { + int r; (void) argc; /* unused */ (void) argv; /* unused */ nt_service_loadlibrary(); @@ -284,20 +283,24 @@ nt_service_body(int argc, char **argv) return; } - tor_main_configuration_t *cfg = tor_main_configuration_new(); - cfg->run_tor_only = 1; - if (tor_main_configuration_set_command_line(cfg, backup_argc, - backup_argv) < 0) + r = tor_init(backup_argc, backup_argv); + if (r) { + /* Failed to start the Tor service */ + r = NT_SERVICE_ERROR_TORINIT_FAILED; + service_status.dwCurrentState = SERVICE_STOPPED; + service_status.dwWin32ExitCode = r; + service_status.dwServiceSpecificExitCode = r; + service_fns.SetServiceStatus_fn(hStatus, &service_status); return; + } /* Set the service's status to SERVICE_RUNNING and start the main * event loop */ service_status.dwCurrentState = SERVICE_RUNNING; service_fns.SetServiceStatus_fn(hStatus, &service_status); - - tor_run_main(cfg); - - tor_main_configuration_free(cfg); + set_main_thread(); + run_tor_main_loop(); + tor_cleanup(); } /** Main service entry point. Starts the service control dispatcher and waits @@ -320,14 +323,29 @@ nt_service_main(void) printf("Service error %d : %s\n", (int) result, errmsg); tor_free(errmsg); if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { - tor_main_configuration_t *cfg = tor_main_configuration_new(); - cfg->run_tor_only = 1; - if (tor_main_configuration_set_command_line(cfg, backup_argc, - backup_argv) < 0) + if (tor_init(backup_argc, backup_argv)) return; - - tor_run_main(cfg); - tor_main_configuration_free(cfg); + switch (get_options()->command) { + case CMD_RUN_TOR: + run_tor_main_loop(); + break; + case CMD_LIST_FINGERPRINT: + case CMD_HASH_PASSWORD: + case CMD_VERIFY_CONFIG: + case CMD_DUMP_CONFIG: + case CMD_KEYGEN: + case CMD_KEY_EXPIRATION: + log_err(LD_CONFIG, "Unsupported command (--list-fingerprint, " + "--hash-password, --keygen, --dump-config, --verify-config, " + "or --key-expiration) in NT service."); + break; + case CMD_RUN_UNITTESTS: + case CMD_IMMEDIATE: + default: + log_err(LD_CONFIG, "Illegal command number %d: internal error.", + get_options()->command); + } + tor_cleanup(); } } } diff --git a/src/feature/api/tor_api_internal.h b/src/feature/api/tor_api_internal.h index ef06cd7e6f..d52b2caf44 100644 --- a/src/feature/api/tor_api_internal.h +++ b/src/feature/api/tor_api_internal.h @@ -29,11 +29,6 @@ struct tor_main_configuration_t { /** Socket that Tor will use as an owning control socket. Owned. */ tor_socket_t owning_controller_socket; - - /** Disable commands other than "run tor". Not for use from outside Tor - * itself; if you need to use this for embedding, please contact the tor - * developers. */ - int run_tor_only; }; #endif /* !defined(TOR_API_INTERNAL_H) */ From eef021e12d5ad2fbc86d7bf058eacc40ae6f1757 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Fri, 17 Jan 2020 08:39:56 -0500 Subject: [PATCH 2/3] Revert "Use tor_api.h entry points in ntmain.c." This reverts commit dca2629480f143ed8deb71b73b15c7c5ff890571. --- changes/bug32883 | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 changes/bug32883 diff --git a/changes/bug32883 b/changes/bug32883 deleted file mode 100644 index 6a15e0221b..0000000000 --- a/changes/bug32883 +++ /dev/null @@ -1,5 +0,0 @@ - o Code simplification and refactoring (windows services): - - The windows service logic now uses the tor_api.h entry points, to - avoid needless code duplication, and to prevent bugs related to - the different entry points getting out of sync. Closes ticket - 32883. From c8b6392b4e2edbaa188ea71594cb2f985149bd88 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Mon, 6 Jan 2020 08:45:29 -0500 Subject: [PATCH 3/3] Initialize publish/subscribe code when running as an NT service. Fixes bug 32778; bugfix on 0.4.1.1-alpha. --- changes/bug32778 | 3 +++ src/app/main/main.c | 4 ++-- src/app/main/main.h | 3 +++ src/app/main/ntmain.c | 7 +++++++ 4 files changed, 15 insertions(+), 2 deletions(-) create mode 100644 changes/bug32778 diff --git a/changes/bug32778 b/changes/bug32778 new file mode 100644 index 0000000000..ccb6104692 --- /dev/null +++ b/changes/bug32778 @@ -0,0 +1,3 @@ + o Minor bugfixes (windows service): + - Initialize publish/subscribe system when running as a windows service. + Fixes bug 32778; bugfix on 0.4.1.1-alpha. diff --git a/src/app/main/main.c b/src/app/main/main.c index dad9777265..689bc526ab 100644 --- a/src/app/main/main.c +++ b/src/app/main/main.c @@ -1191,7 +1191,7 @@ run_tor_main_loop(void) } /** Install the publish/subscribe relationships for all the subsystems. */ -static void +void pubsub_install(void) { pubsub_builder_t *builder = pubsub_builder_new(); @@ -1203,7 +1203,7 @@ pubsub_install(void) /** Connect the mainloop to its publish/subscribe message delivery events if * appropriate, and configure the global channels appropriately. */ -static void +void pubsub_connect(void) { if (get_options()->command == CMD_RUN_TOR) { diff --git a/src/app/main/main.h b/src/app/main/main.h index 209d419f0a..e6ed978c61 100644 --- a/src/app/main/main.h +++ b/src/app/main/main.h @@ -25,4 +25,7 @@ int tor_init(int argc, char **argv); int run_tor_main_loop(void); +void pubsub_install(void); +void pubsub_connect(void); + #endif /* !defined(TOR_MAIN_H) */ diff --git a/src/app/main/ntmain.c b/src/app/main/ntmain.c index ced158e686..4941199759 100644 --- a/src/app/main/ntmain.c +++ b/src/app/main/ntmain.c @@ -283,7 +283,9 @@ nt_service_body(int argc, char **argv) return; } + pubsub_install(); r = tor_init(backup_argc, backup_argv); + if (r) { /* Failed to start the Tor service */ r = NT_SERVICE_ERROR_TORINIT_FAILED; @@ -294,6 +296,8 @@ nt_service_body(int argc, char **argv) return; } + pubsub_connect(); + /* Set the service's status to SERVICE_RUNNING and start the main * event loop */ service_status.dwCurrentState = SERVICE_RUNNING; @@ -322,9 +326,12 @@ nt_service_main(void) errmsg = format_win32_error(result); printf("Service error %d : %s\n", (int) result, errmsg); tor_free(errmsg); + + pubsub_install(); if (result == ERROR_FAILED_SERVICE_CONTROLLER_CONNECT) { if (tor_init(backup_argc, backup_argv)) return; + pubsub_connect(); switch (get_options()->command) { case CMD_RUN_TOR: run_tor_main_loop();