[tor-commits] [tor/master] Make initialization for the "err" library into a subsystem.

nickm at torproject.org nickm at torproject.org
Fri Nov 9 20:01:54 UTC 2018


commit 175153a3290b3987faacac9d5390e87e1ad4a457
Author: Nick Mathewson <nickm at torproject.org>
Date:   Thu Nov 1 12:40:55 2018 -0400

    Make initialization for the "err" library into a subsystem.
---
 src/app/main/main.c           | 10 ----------
 src/app/main/subsystem_list.c |  4 +++-
 src/lib/err/.may_include      |  2 ++
 src/lib/err/include.am        |  8 +++++---
 src/lib/err/torerr.c          | 10 ++++++++++
 src/lib/err/torerr.h          |  1 +
 src/lib/err/torerr_sys.c      | 39 +++++++++++++++++++++++++++++++++++++++
 src/lib/err/torerr_sys.h      | 14 ++++++++++++++
 8 files changed, 74 insertions(+), 14 deletions(-)

diff --git a/src/app/main/main.c b/src/app/main/main.c
index 031f57009..e3d7610c8 100644
--- a/src/app/main/main.c
+++ b/src/app/main/main.c
@@ -69,7 +69,6 @@
 #include "lib/container/buffers.h"
 #include "lib/crypt_ops/crypto_rand.h"
 #include "lib/crypt_ops/crypto_s2k.h"
-#include "lib/err/backtrace.h"
 #include "lib/geoip/geoip.h"
 
 #include "lib/process/waitpid.h"
@@ -822,7 +821,6 @@ tor_free_all(int postfork)
   if (!postfork) {
     escaped(NULL);
     esc_router_info(NULL);
-    clean_up_backtrace_handler();
     logs_free_all(); /* free log strings. do this last so logs keep working. */
   }
 }
@@ -1419,14 +1417,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg)
 #endif /* !defined(_WIN64) */
 #endif /* defined(_WIN32) */
 
-  {
-    int bt_err = configure_backtrace_handler(get_version());
-    if (bt_err < 0) {
-      log_warn(LD_BUG, "Unable to install backtrace handler: %s",
-               strerror(-bt_err));
-    }
-  }
-
 #ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
   event_set_mem_functions(tor_malloc_, tor_realloc_, tor_free_);
 #endif
diff --git a/src/app/main/subsystem_list.c b/src/app/main/subsystem_list.c
index fc1249e1c..244dbadbd 100644
--- a/src/app/main/subsystem_list.c
+++ b/src/app/main/subsystem_list.c
@@ -8,13 +8,15 @@
 #include "lib/cc/compat_compiler.h"
 #include "lib/cc/torint.h"
 
+#include "lib/err/torerr_sys.h"
+
 #include <stddef.h>
 
 /**
  * Global list of the subsystems in Tor, in the order of their initialization.
  **/
 const subsys_fns_t *tor_subsystems[] = {
-   NULL // placeholder.
+  &sys_torerr,
 };
 
 const unsigned n_tor_subsystems = ARRAY_LENGTH(tor_subsystems);
diff --git a/src/lib/err/.may_include b/src/lib/err/.may_include
index 48cc0ef08..daa1b6e4c 100644
--- a/src/lib/err/.may_include
+++ b/src/lib/err/.may_include
@@ -1,3 +1,5 @@
 orconfig.h
 lib/cc/*.h
 lib/err/*.h
+lib/subsys/*.h
+lib/version/*.h
\ No newline at end of file
diff --git a/src/lib/err/include.am b/src/lib/err/include.am
index f2a409c51..43adcd269 100644
--- a/src/lib/err/include.am
+++ b/src/lib/err/include.am
@@ -6,8 +6,9 @@ noinst_LIBRARIES += src/lib/libtor-err-testing.a
 endif
 
 src_lib_libtor_err_a_SOURCES =			\
-	src/lib/err/backtrace.c				\
-	src/lib/err/torerr.c
+	src/lib/err/backtrace.c			\
+	src/lib/err/torerr.c			\
+	src/lib/err/torerr_sys.c
 
 src_lib_libtor_err_testing_a_SOURCES = \
 	$(src_lib_libtor_err_a_SOURCES)
@@ -16,4 +17,5 @@ src_lib_libtor_err_testing_a_CFLAGS = $(AM_CFLAGS) $(TEST_CFLAGS)
 
 noinst_HEADERS +=					\
 	src/lib/err/backtrace.h				\
-	src/lib/err/torerr.h
+	src/lib/err/torerr.h				\
+	src/lib/err/torerr_sys.h
diff --git a/src/lib/err/torerr.c b/src/lib/err/torerr.c
index f9e139f96..e9de86837 100644
--- a/src/lib/err/torerr.c
+++ b/src/lib/err/torerr.c
@@ -123,6 +123,16 @@ tor_log_set_sigsafe_err_fds(const int *fds, int n)
 }
 
 /**
+ * Reset the list of emergency error fds to its default.
+ */
+void
+tor_log_reset_sigsafe_err_fds(void)
+{
+  int fds[] = { STDERR_FILENO };
+  tor_log_set_sigsafe_err_fds(fds, 1);
+}
+
+/**
  * Set the granularity (in ms) to use when reporting fatal errors outside
  * the logging system.
  */
diff --git a/src/lib/err/torerr.h b/src/lib/err/torerr.h
index d4bba6916..b415ef73e 100644
--- a/src/lib/err/torerr.h
+++ b/src/lib/err/torerr.h
@@ -39,6 +39,7 @@ void tor_raw_assertion_failed_msg_(const char *file, int line,
 void tor_log_err_sigsafe(const char *m, ...);
 int tor_log_get_sigsafe_err_fds(const int **out);
 void tor_log_set_sigsafe_err_fds(const int *fds, int n);
+void tor_log_reset_sigsafe_err_fds(void);
 void tor_log_sigsafe_err_set_granularity(int ms);
 
 int format_hex_number_sigsafe(unsigned long x, char *buf, int max_len);
diff --git a/src/lib/err/torerr_sys.c b/src/lib/err/torerr_sys.c
new file mode 100644
index 000000000..54666f410
--- /dev/null
+++ b/src/lib/err/torerr_sys.c
@@ -0,0 +1,39 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file torerr_sys.c
+ * \brief Subsystem object for the error handling subsystem.
+ **/
+
+#include "orconfig.h"
+#include "lib/err/backtrace.h"
+#include "lib/err/torerr.h"
+#include "lib/err/torerr_sys.h"
+#include "lib/subsys/subsys.h"
+#include "lib/version/torversion.h"
+
+#include <stddef.h>
+
+static int
+torerr_subsys_init(void)
+{
+  configure_backtrace_handler(get_version());
+  tor_log_reset_sigsafe_err_fds();
+
+  return 0;
+}
+static void
+torerr_subsys_shutdown(void)
+{
+  tor_log_reset_sigsafe_err_fds();
+  clean_up_backtrace_handler();
+}
+
+const subsys_fns_t sys_torerr = {
+  .name = "err",
+  .level = -100,
+  .supported = true,
+  .initialize = torerr_subsys_init,
+  .shutdown = torerr_subsys_shutdown
+};
diff --git a/src/lib/err/torerr_sys.h b/src/lib/err/torerr_sys.h
new file mode 100644
index 000000000..b56270d53
--- /dev/null
+++ b/src/lib/err/torerr_sys.h
@@ -0,0 +1,14 @@
+/* Copyright (c) 2018, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * \file torerr_sys.h
+ * \brief Declare subsystem object for torerr.c
+ **/
+
+#ifndef TOR_TORERR_SYS_H
+#define TOR_TORERR_SYS_H
+
+extern const struct subsys_fns_t sys_torerr;
+
+#endif /* !defined(TOR_TORERR_SYS_H) */





More information about the tor-commits mailing list