]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Log state transitions for Pluggable Transports
authorAlexander Færøy <ahf@torproject.org>
Fri, 24 Sep 2021 12:43:24 +0000 (14:43 +0200)
committerDavid Goulet <dgoulet@torproject.org>
Thu, 25 May 2023 14:50:11 +0000 (10:50 -0400)
This patch makes Tor log state transitions within the PT layer at the
info log-level. This should make it easier to figure out if Tor ends up
in a strange state.

See: tpo/core/tor#33669

src/feature/client/transports.c
src/feature/client/transports.h

index 167beb96c6f997f6c1875b1fb234af7957a2984a..c5d7df479d8d2f12e54e04497b8302c4d8822c50 100644 (file)
@@ -541,7 +541,7 @@ proxy_prepare_for_restart(managed_proxy_t *mp)
   mp->proxy_supported = 0;
 
   /* flag it as an infant proxy so that it gets launched on next tick */
-  mp->conf_state = PT_PROTO_INFANT;
+  managed_proxy_set_state(mp, PT_PROTO_INFANT);
   unconfigured_proxies_n++;
 }
 
@@ -578,7 +578,7 @@ launch_managed_proxy(managed_proxy_t *mp)
   log_info(LD_CONFIG,
            "Managed proxy at '%s' has spawned with PID '%" PRIu64 "'.",
            mp->argv[0], process_get_pid(mp->process));
-  mp->conf_state = PT_PROTO_LAUNCHED;
+  managed_proxy_set_state(mp, PT_PROTO_LAUNCHED);
 
   return 0;
 }
@@ -648,7 +648,7 @@ configure_proxy(managed_proxy_t *mp)
   /* if we haven't launched the proxy yet, do it now */
   if (mp->conf_state == PT_PROTO_INFANT) {
     if (launch_managed_proxy(mp) < 0) { /* launch fail */
-      mp->conf_state = PT_PROTO_FAILED_LAUNCH;
+      managed_proxy_set_state(mp, PT_PROTO_FAILED_LAUNCH);
       handle_finished_proxy(mp);
     }
     return 0;
@@ -810,8 +810,12 @@ handle_finished_proxy(managed_proxy_t *mp)
       managed_proxy_destroy(mp, 1); /* annihilate it. */
       break;
     }
-    register_proxy(mp); /* register its transports */
-    mp->conf_state = PT_PROTO_COMPLETED; /* and mark it as completed. */
+
+    /* register its transports */
+    register_proxy(mp);
+
+    /* and mark it as completed. */
+    managed_proxy_set_state(mp, PT_PROTO_COMPLETED);
     break;
   case PT_PROTO_INFANT:
   case PT_PROTO_LAUNCHED:
@@ -881,7 +885,7 @@ handle_proxy_line(const char *line, managed_proxy_t *mp)
       goto err;
 
     tor_assert(mp->conf_protocol != 0);
-    mp->conf_state = PT_PROTO_ACCEPTING_METHODS;
+    managed_proxy_set_state(mp, PT_PROTO_ACCEPTING_METHODS);
     return;
   } else if (!strcmpstart(line, PROTO_CMETHODS_DONE)) {
     if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
@@ -889,7 +893,7 @@ handle_proxy_line(const char *line, managed_proxy_t *mp)
 
     handle_methods_done(mp);
 
-    mp->conf_state = PT_PROTO_CONFIGURED;
+    managed_proxy_set_state(mp, PT_PROTO_CONFIGURED);
     return;
   } else if (!strcmpstart(line, PROTO_SMETHODS_DONE)) {
     if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
@@ -897,7 +901,7 @@ handle_proxy_line(const char *line, managed_proxy_t *mp)
 
     handle_methods_done(mp);
 
-    mp->conf_state = PT_PROTO_CONFIGURED;
+    managed_proxy_set_state(mp, PT_PROTO_CONFIGURED);
     return;
   } else if (!strcmpstart(line, PROTO_CMETHOD_ERROR)) {
     if (mp->conf_state != PT_PROTO_ACCEPTING_METHODS)
@@ -960,7 +964,7 @@ handle_proxy_line(const char *line, managed_proxy_t *mp)
   return;
 
  err:
-  mp->conf_state = PT_PROTO_BROKEN;
+  managed_proxy_set_state(mp, PT_PROTO_BROKEN);
   log_warn(LD_CONFIG, "Managed proxy at '%s' failed the configuration protocol"
            " and will be destroyed.", mp->argv[0]);
 }
@@ -1521,7 +1525,7 @@ managed_proxy_create(const smartlist_t *with_transport_list,
                      char **proxy_argv, int is_server)
 {
   managed_proxy_t *mp = tor_malloc_zero(sizeof(managed_proxy_t));
-  mp->conf_state = PT_PROTO_INFANT;
+  managed_proxy_set_state(mp, PT_PROTO_INFANT);
   mp->is_server = is_server;
   mp->argv = proxy_argv;
   mp->transports = smartlist_new();
@@ -2015,3 +2019,45 @@ managed_proxy_outbound_address(const or_options_t *options, sa_family_t family)
   /* The user have not specified a preference for outgoing connections. */
   return NULL;
 }
+
+STATIC const char *
+managed_proxy_state_to_string(enum pt_proto_state state)
+{
+  switch (state) {
+  case PT_PROTO_INFANT:
+    return "Infant";
+  case PT_PROTO_LAUNCHED:
+    return "Launched";
+  case PT_PROTO_ACCEPTING_METHODS:
+    return "Accepting methods";
+  case PT_PROTO_CONFIGURED:
+    return "Configured";
+  case PT_PROTO_COMPLETED:
+    return "Completed";
+  case PT_PROTO_BROKEN:
+    return "Broken";
+  case PT_PROTO_FAILED_LAUNCH:
+    return "Failed to launch";
+  }
+
+  /* LCOV_EXCL_START */
+  tor_assert_unreached();
+  return NULL;
+  /* LCOV_EXCL_STOP */
+}
+
+/** Set the internal state of the given <b>mp</b> to the given <b>new_state</b>
+ * value. */
+STATIC void
+managed_proxy_set_state(managed_proxy_t *mp, enum pt_proto_state new_state)
+{
+  if (mp->conf_state == new_state)
+    return;
+
+  tor_log(LOG_INFO, LD_PT, "Managed proxy \"%s\" changed state: %s -> %s",
+          mp->argv[0],
+          managed_proxy_state_to_string(mp->conf_state),
+          managed_proxy_state_to_string(new_state));
+
+  mp->conf_state = new_state;
+}
index 3f08beadba229bf0d87a9cbb0930a9f52bedcf2d..535689537c16bbf279cac0d0c055f928bec8a621 100644 (file)
@@ -153,6 +153,8 @@ STATIC int managed_proxy_severity_parse(const char *);
 STATIC const tor_addr_t *managed_proxy_outbound_address(const or_options_t *,
                                                         sa_family_t);
 
+STATIC const char *managed_proxy_state_to_string(enum pt_proto_state);
+STATIC void managed_proxy_set_state(managed_proxy_t *, enum pt_proto_state);
 #endif /* defined(PT_PRIVATE) */
 
 #endif /* !defined(TOR_TRANSPORTS_H) */