diff --git a/docs/man8/tinyproxy.txt.in b/docs/man8/tinyproxy.txt.in index e259dcb..13dafce 100644 --- a/docs/man8/tinyproxy.txt.in +++ b/docs/man8/tinyproxy.txt.in @@ -40,9 +40,15 @@ OPTIONS *-h*:: Display a short help screen of command line arguments and exit. -*-l*:: +*-L*:: Display the licensing agreement. +*-l *:: + Set log file path. Overrides configuration file *LogFile* directive. + +*-p *:: + Set pidfile path. Overrides configuration file *PidFile* directive. + *-v*:: Display version information and exit. diff --git a/src/conf.c b/src/conf.c index c003627..e01ea4d 100644 --- a/src/conf.c +++ b/src/conf.c @@ -542,7 +542,7 @@ static void initialize_with_defaults (struct config_s *conf, * Load the configuration. */ int reload_config_file (const char *config_fname, struct config_s *conf, - struct config_s *defaults) + struct config_s *defaults, struct config_s *overrides) { int ret; @@ -582,6 +582,34 @@ int reload_config_file (const char *config_fname, struct config_s *conf, conf->idletimeout = MAX_IDLE_TIME; } + /* Copy overridable values. */ + if (overrides->logf_name != NULL) { + if (conf->logf_name != NULL) { + safefree (conf->logf_name); + } + conf->logf_name = safestrdup (overrides->logf_name); + if (!conf->logf_name) { + fprintf (stderr, + "%s: Could not allocate memory.\n", + PACKAGE); + ret = -1; + } + conf->syslog = 0; + } + + if (overrides->pidpath != NULL) { + if (conf->pidpath != NULL) { + safefree (conf->pidpath); + } + conf->pidpath = safestrdup (overrides->pidpath); + if (!conf->pidpath) { + fprintf (stderr, + "%s: Could not allocate memory.\n", + PACKAGE); + ret = -1; + } + } + done: return ret; } diff --git a/src/conf.h b/src/conf.h index 0fb4226..8c989c5 100644 --- a/src/conf.h +++ b/src/conf.h @@ -113,7 +113,7 @@ struct config_s { }; extern int reload_config_file (const char *config_fname, struct config_s *conf, - struct config_s *defaults); + struct config_s *defaults, struct config_s *overrides); int config_compile_regex (void); diff --git a/src/main.c b/src/main.c index ae2a3a8..d38b5e5 100644 --- a/src/main.c +++ b/src/main.c @@ -50,6 +50,7 @@ */ struct config_s config; struct config_s config_defaults; +struct config_s config_overrides; unsigned int received_sighup = FALSE; /* boolean */ /* @@ -149,8 +150,10 @@ display_usage (void) "Options are:\n" " -d Do not daemonize (run in foreground).\n" " -c FILE Use an alternate configuration file.\n" + " -l FILE Set log file path (overrides config file)\n" + " -p FILE Set pid file path (overrides config file)\n" " -h Display this usage information.\n" - " -l Display the license.\n" + " -L Display the license.\n" " -v Display version information.\n"); /* Display the modes compiled into tinyproxy */ @@ -220,17 +223,17 @@ get_id (char *str) * This function parses command line arguments. **/ static void -process_cmdline (int argc, char **argv, struct config_s *conf) +process_cmdline (int argc, char **argv, struct config_s *conf, struct config_s *overrides) { int opt; - while ((opt = getopt (argc, argv, "c:vldh")) != EOF) { + while ((opt = getopt (argc, argv, "c:l:p:vLdh")) != EOF) { switch (opt) { case 'v': display_version (); exit (EX_OK); - case 'l': + case 'L': display_license (); exit (EX_OK); @@ -251,6 +254,32 @@ process_cmdline (int argc, char **argv, struct config_s *conf) } break; + case 'l': + if (overrides->logf_name != NULL) { + safefree (overrides->logf_name); + } + overrides->logf_name = safestrdup (optarg); + if (!overrides->logf_name) { + fprintf (stderr, + "%s: Could not allocate memory.\n", + argv[0]); + exit (EX_SOFTWARE); + } + break; + + case 'p': + if (overrides->pidpath != NULL) { + safefree (overrides->pidpath); + } + overrides->pidpath = safestrdup (optarg); + if (!overrides->pidpath) { + fprintf (stderr, + "%s: Could not allocate memory.\n", + argv[0]); + exit (EX_SOFTWARE); + } + break; + case 'h': display_usage (); exit (EX_OK); @@ -370,7 +399,7 @@ int reload_config (void) shutdown_logging (); ret = reload_config_file (config_defaults.config_file, &config, - &config_defaults); + &config_defaults, &config_overrides); if (ret != 0) { goto done; } @@ -396,11 +425,13 @@ main (int argc, char **argv) } initialize_config_defaults (&config_defaults); - process_cmdline (argc, argv, &config_defaults); + memset (&config_overrides, 0, sizeof(config_overrides)); + process_cmdline (argc, argv, &config_defaults, &config_overrides); if (reload_config_file (config_defaults.config_file, &config, - &config_defaults)) { + &config_defaults, + &config_overrides)) { exit (EX_SOFTWARE); }