Skip to content
Snippets Groups Projects
Commit 9a89450b authored by Nick Mathewson's avatar Nick Mathewson :game_die:
Browse files

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.
parent ff7229b3
No related branches found
No related tags found
No related merge requests found
......@@ -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;
}
......@@ -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;
}
......@@ -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) */
......@@ -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) */
......@@ -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 */
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment