diff options
author | Nick Mathewson <nickm@torproject.org> | 2018-08-01 10:15:44 -0400 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2018-08-01 11:01:52 -0400 |
commit | 9a89450b6d1734fb8e015093353a5d0568b41b79 (patch) | |
tree | 0d1ea9530dc51c449cb40af0dc59df757c73a7c6 /src/feature/api | |
parent | ff7229b32cb2f5a3c384200caac35e0626199087 (diff) | |
download | tor-9a89450b6d1734fb8e015093353a5d0568b41b79.tar.gz tor-9a89450b6d1734fb8e015093353a5d0568b41b79.zip |
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.
Diffstat (limited to 'src/feature/api')
-rw-r--r-- | src/feature/api/tor_api.c | 31 | ||||
-rw-r--r-- | src/feature/api/tor_api.h | 1 | ||||
-rw-r--r-- | src/feature/api/tor_api_internal.h | 8 |
3 files changed, 37 insertions, 3 deletions
diff --git a/src/feature/api/tor_api.c b/src/feature/api/tor_api.c index 2a4458bf69..755d28aad6 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 ead9493c1f..5133e3cec6 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 2c392a68de..30924b8b04 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) */ - |