[tor-commits] [tor/master] tor_api: Extend tor_api code so it can pass extra arguments to main.

nickm at torproject.org nickm at torproject.org
Tue Sep 4 15:05:03 UTC 2018


commit 9a89450b6d1734fb8e015093353a5d0568b41b79
Author: Nick Mathewson <nickm at torproject.org>
Date:   Wed Aug 1 10:15:44 2018 -0400

    tor_api: Extend tor_api code so it can pass extra arguments to main.
    
    We need this so that the tor_api user can specify some arguments,
    while the tor_api implementation adds others.
    
    This implementation detail should not be visible to tor_api users.
---
 src/core/mainloop/main.c           | 29 +++++++++++++++++++++--------
 src/feature/api/tor_api.c          | 31 ++++++++++++++++++++++++++++++-
 src/feature/api/tor_api.h          |  1 -
 src/feature/api/tor_api_internal.h |  8 +++++++-
 src/tools/tor_runner.c             |  9 ++++++---
 5 files changed, 64 insertions(+), 14 deletions(-)

diff --git a/src/core/mainloop/main.c b/src/core/mainloop/main.c
index 048397a2d..4bb55c7ba 100644
--- a/src/core/mainloop/main.c
+++ b/src/core/mainloop/main.c
@@ -4204,9 +4204,6 @@ tor_run_main(const tor_main_configuration_t *tor_cfg)
 {
   int result = 0;
 
-  int argc = tor_cfg->argc;
-  char **argv = tor_cfg->argv;
-
 #ifdef _WIN32
 #ifndef HeapEnableTerminationOnCorruption
 #define HeapEnableTerminationOnCorruption 1
@@ -4242,25 +4239,39 @@ tor_run_main(const tor_main_configuration_t *tor_cfg)
   tor_compress_init();
   init_logging(0);
   monotime_init();
+
+  int argc = tor_cfg->argc + tor_cfg->argc_owned;
+  char **argv = tor_calloc(argc, sizeof(char*));
+  memcpy(argv, tor_cfg->argv, tor_cfg->argc*sizeof(char*));
+  if (tor_cfg->argc_owned)
+    memcpy(argv + tor_cfg->argc, tor_cfg->argv_owned,
+           tor_cfg->argc_owned*sizeof(char*));
+
 #ifdef NT_SERVICE
   {
      int done = 0;
      result = nt_service_parse_options(argc, argv, &done);
-     if (done) return result;
+     if (done) {
+       goto done;
+     }
   }
 #endif /* defined(NT_SERVICE) */
   {
     int init_rv = tor_init(argc, argv);
-    if (init_rv < 0)
-      return -1;
-    else if (init_rv > 0)
-      return 0;
+    if (init_rv < 0) {
+      result = -1;
+      goto done;
+    } else if (init_rv > 0) {
+      result = 0;
+      goto done;
+    }
   }
 
   if (get_options()->Sandbox && get_options()->command == CMD_RUN_TOR) {
     sandbox_cfg_t* cfg = sandbox_init_filter();
 
     if (sandbox_init(cfg)) {
+      tor_free(argv);
       log_err(LD_BUG,"Failed to create syscall sandbox filter");
       return -1;
     }
@@ -4308,5 +4319,7 @@ tor_run_main(const tor_main_configuration_t *tor_cfg)
     result = -1;
   }
   tor_cleanup();
+ done:
+  tor_free(argv);
   return result;
 }
diff --git a/src/feature/api/tor_api.c b/src/feature/api/tor_api.c
index 2a4458bf6..755d28aad 100644
--- a/src/feature/api/tor_api.c
+++ b/src/feature/api/tor_api.c
@@ -25,6 +25,28 @@
 
 #define raw_malloc malloc
 #define raw_free free
+#define raw_realloc realloc
+#define raw_strdup strdup
+
+/**
+ * Helper: Add a copy of <b>arg</b> to the owned arguments of <b>cfg</b>.
+ * Return 0 on success, -1 on failure.
+ */
+static int
+cfg_add_owned_arg(tor_main_configuration_t *cfg, const char *arg)
+{
+  /* We aren't using amortized realloc here, because libc should do it for us,
+   * and because this function is not critical-path. */
+  char **new_argv = raw_realloc(cfg->argv_owned,
+                                sizeof(char*) * (cfg->argc_owned+1));
+  if (new_argv == NULL)
+    return -1;
+  cfg->argv_owned = new_argv;
+  if (NULL == (cfg->argv_owned[cfg->argc_owned] = raw_strdup(arg)))
+    return -1;
+  ++cfg->argc_owned;
+  return 0;
+}
 
 tor_main_configuration_t *
 tor_main_configuration_new(void)
@@ -58,6 +80,14 @@ tor_main_configuration_free(tor_main_configuration_t *cfg)
 {
   if (cfg == NULL)
     return;
+  cfg_add_owned_arg(cfg, "bye");//XXXX
+  if (cfg->argv_owned) {
+    int i;
+    for (i = 0; i < cfg->argc_owned; ++i) {
+      raw_free(cfg->argv_owned[i]);
+    }
+    raw_free(cfg->argv_owned);
+  }
   raw_free(cfg);
 }
 
@@ -85,4 +115,3 @@ tor_main(int argc, char *argv[])
   tor_main_configuration_free(cfg);
   return rv;
 }
-
diff --git a/src/feature/api/tor_api.h b/src/feature/api/tor_api.h
index ead9493c1..5133e3cec 100644
--- a/src/feature/api/tor_api.h
+++ b/src/feature/api/tor_api.h
@@ -98,4 +98,3 @@ int tor_run_main(const tor_main_configuration_t *);
 int tor_main(int argc, char **argv);
 
 #endif /* !defined(TOR_API_H) */
-
diff --git a/src/feature/api/tor_api_internal.h b/src/feature/api/tor_api_internal.h
index 2c392a68d..30924b8b0 100644
--- a/src/feature/api/tor_api_internal.h
+++ b/src/feature/api/tor_api_internal.h
@@ -7,6 +7,8 @@
 #ifndef TOR_API_INTERNAL_H
 #define TOR_API_INTERNAL_H
 
+#include "lib/net/nettypes.h"
+
 /* The contents of this type are private; don't mess with them from outside
  * Tor. */
 struct tor_main_configuration_t {
@@ -14,7 +16,11 @@ struct tor_main_configuration_t {
   int argc;
   /** As in main(). This pointer is owned by the caller */
   char **argv;
+
+  /** As argc, but describes the number of elements in argv_owned */
+  int argc_owned;
+  /** As argv, but is owned by the tor_main_configuration_t object. */
+  char **argv_owned;
 };
 
 #endif /* !defined(TOR_API_INTERNAL_H) */
-
diff --git a/src/tools/tor_runner.c b/src/tools/tor_runner.c
index dd90af3df..4b21d7bf9 100644
--- a/src/tools/tor_runner.c
+++ b/src/tools/tor_runner.c
@@ -86,9 +86,13 @@ child(const tor_main_configuration_t *cfg)
 {
   /* XXXX Close unused file descriptors. */
 
-  char **args = real_calloc(cfg->argc+1, sizeof(char *));
+  char **args = real_calloc(cfg->argc + cfg->argc_owned+1, sizeof(char *));
   memcpy(args, cfg->argv, cfg->argc * sizeof(char *));
-  args[cfg->argc] = NULL;
+  if (cfg->argc_owned)
+    memcpy(args + cfg->argc, cfg->argv_owned,
+           cfg->argc_owned * sizeof(char *));
+
+  args[cfg->argc + cfg->argc_owned] = NULL;
 
   int rv = execv(BINDIR "/tor", args);
 
@@ -98,4 +102,3 @@ child(const tor_main_configuration_t *cfg)
     abort(); /* Unreachable */
   }
 }
-





More information about the tor-commits mailing list