Commit 16528aa0 authored by Nick Mathewson's avatar Nick Mathewson 🎨
Browse files

Add a config-file GETINFO entry; fix a minor memory leak on some SAVECONF calls.


svn:r4761
parent c031d146
......@@ -294,6 +294,8 @@ $Id$
"version" -- The version of the server's software, including the name
of the software. (example: "Tor 0.0.9.4")
"config-file" -- The location of Tor's configuration file ("torrc").
"desc/id/<OR identity>" or "desc/name/<OR nickname>" -- the latest server
descriptor for a given OR, NUL-terminated. If no such OR is known, the
corresponding value is an empty string.
......
......@@ -322,7 +322,7 @@ static config_format_t state_format = {
/** Command-line and config-file options. */
static or_options_t *global_options = NULL;
/** Name of most recently read torrc file. */
static char *config_fname = NULL;
static char *torrc_fname = NULL;
/** Persistant serialized state. */
static or_state_t *global_state = NULL;
/** DOCDOC */
......@@ -360,7 +360,7 @@ void
config_free_all(void)
{
config_free(&options_format, global_options);
tor_free(config_fname);
tor_free(torrc_fname);
addr_policy_free(reachable_addr_policy);
reachable_addr_policy = NULL;
}
......@@ -2026,16 +2026,16 @@ get_windows_conf_root(void)
#endif
/** Return the default location for our torrc file. */
static char *
static const char *
get_default_conf_file(void)
{
#ifdef MS_WINDOWS
char *path = tor_malloc(MAX_PATH);
static char path[MAX_PATH+1];
strlcpy(path, get_windows_conf_root(), MAX_PATH);
strlcat(path,"\\torrc",MAX_PATH);
return path;
#else
return tor_strdup(CONFDIR "/torrc");
return (CONFDIR "/torrc");
#endif
}
......@@ -2131,22 +2131,21 @@ options_init_from_torrc(int argc, char **argv)
if (using_default_torrc) {
/* didn't find one, try CONFDIR */
char *fn;
fn = get_default_conf_file();
if (fn && file_status(fn) == FN_FILE) {
fname = fn;
const char *dflt = get_default_conf_file();
char *fn = NULL;
if (dflt && file_status(dflt) == FN_FILE) {
fname = tor_strdup(dflt);
} else {
tor_free(fn);
#ifndef MS_WINDOWS
fn = expand_filename("~/.torrc");
if (fn && file_status(fn) == FN_FILE) {
fname = fn;
} else {
tor_free(fn);
fname = get_default_conf_file();
fname = tor_strdup(dflt);
}
#else
fname = get_default_conf_file();
fname = tor_strdup(dflt);
#endif
}
}
......@@ -2194,8 +2193,8 @@ options_init_from_torrc(int argc, char **argv)
log_fn(LOG_ERR,"Acting on config options left us in a broken state. Dying.");
exit(1);
}
tor_free(config_fname);
config_fname = fname;
tor_free(torrc_fname);
torrc_fname = fname;
return 0;
err:
tor_free(fname);
......@@ -2203,6 +2202,18 @@ options_init_from_torrc(int argc, char **argv)
return -1;
}
/** Return the location for our configuration file.
*/
const char *
get_torrc_fname(void)
{
if (torrc_fname)
return torrc_fname;
else
return get_default_conf_file();
}
/** Adjust the address map mased on the MapAddress elements in the
* configuration <b>options</b>
*/
......@@ -2818,15 +2829,13 @@ write_configuration_file(const char *fname, or_options_t *options)
int
options_save_current(void)
{
char *fn;
if (config_fname) {
if (torrc_fname) {
/* XXX This fails if we can't write to our configuration file.
* Arguably, we should try falling back to datadirectory or something.
* But just as arguably, we shouldn't. */
return write_configuration_file(config_fname, get_options());
return write_configuration_file(torrc_fname, get_options());
}
fn = get_default_conf_file();
return write_configuration_file(fn, get_options());
return write_configuration_file(get_default_conf_file(), get_options());
}
struct unit_table_t {
......
......@@ -1150,6 +1150,8 @@ handle_getinfo_helper(const char *question, char **answer)
*answer = NULL; /* unrecognized key by default */
if (!strcmp(question, "version")) {
*answer = tor_strdup(VERSION);
} else if (!strcmp(question, "config-file")) {
*answer = tor_strdup(get_torrc_fname());
} else if (!strcmpstart(question, "accounting/")) {
return accounting_getinfo_helper(question, answer);
} else if (!strcmpstart(question, "helper-nodes")) {
......
......@@ -1376,6 +1376,7 @@ config_line_t *option_get_assignment(or_options_t *options,
const char *key);
char *options_dump(or_options_t *options, int minimal);
int options_save_current(void);
const char *get_torrc_fname(void);
or_state_t *get_or_state(void);
int or_state_load(void);
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment