Maintained by: NLnet Labs

[PATCH] log-identity: new option to control logging name.

robbat2 at gentoo.org
Thu Nov 3 01:10:57 CET 2016


From: "Robin H. Johnson" <robbat2 at gentoo.org>

Add an option to control the log identity of unbound instances.

This is primarily useful on systems that run more than one instance of
unbound, with different configurations, so that the logs can be easily
distinguished against.

Signed-off-by: Robin H. Johnson <robbat2 at gentoo.org>
---
 daemon/unbound.c     | 29 ++++++++++++++++++++++-------
 doc/example.conf.in  |  6 +++++-
 util/config_file.c   |  3 +++
 util/config_file.h   |  2 ++
 util/configlexer.lex |  1 +
 util/configparser.y  | 11 ++++++++++-
 6 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/daemon/unbound.c b/daemon/unbound.c
index 88fbb6f..0a0cf80 100644
--- a/daemon/unbound.c
+++ b/daemon/unbound.c
@@ -244,10 +244,21 @@ checkrlimits(struct config_file* cfg)
 #endif /* S_SPLINT_S */
 }
 
+/** set default logfile identity based on value from argv[0] at startup **/
+static void
+log_ident_set_fromdefault(struct config_file* cfg,
+	const char *log_default_identity)
+{
+	if(strncmp(cfg->log_identity, "", strlen(cfg->log_identity)) == 0)
+		log_ident_set(log_default_identity);
+	else
+		log_ident_set(cfg->log_identity);
+}
+
 /** set verbosity, check rlimits, cache settings */
 static void
 apply_settings(struct daemon* daemon, struct config_file* cfg, 
-	int cmdline_verbose, int debug_mode)
+	int cmdline_verbose, int debug_mode, const char* log_default_identity)
 {
 	/* apply if they have changed */
 	verbosity = cmdline_verbose + cfg->verbosity;
@@ -258,6 +269,7 @@ apply_settings(struct daemon* daemon, struct config_file* cfg,
 	}
 	daemon_apply_cfg(daemon, cfg);
 	checkrlimits(cfg);
+	log_ident_set_fromdefault(cfg, log_default_identity);
 }
 
 #ifdef HAVE_KILL
@@ -587,9 +599,10 @@ perform_setup(struct daemon* daemon, struct config_file* cfg, int debug_mode,
  * @param cmdline_verbose: verbosity resulting from commandline -v.
  *    These increase verbosity as specified in the config file.
  * @param debug_mode: if set, do not daemonize.
+ * @param log_default_identity: Default identity to report in logs
  */
 static void 
-run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode)
+run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode, const char* log_default_identity)
 {
 	struct config_file* cfg = NULL;
 	struct daemon* daemon = NULL;
@@ -611,7 +624,7 @@ run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode)
 					cfgfile);
 			log_warn("Continuing with default config settings");
 		}
-		apply_settings(daemon, cfg, cmdline_verbose, debug_mode);
+		apply_settings(daemon, cfg, cmdline_verbose, debug_mode, log_default_identity);
 		if(!done_setup)
 			config_lookup_uid(cfg);
 	
@@ -619,7 +632,7 @@ run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode)
 		if(!daemon_open_shared_ports(daemon))
 			fatal_exit("could not open ports");
 		if(!done_setup) { 
-			perform_setup(daemon, cfg, debug_mode, &cfgfile); 
+			perform_setup(daemon, cfg, debug_mode, &cfgfile);
 			done_setup = 1; 
 		} else {
 			/* reopen log after HUP to facilitate log rotation */
@@ -666,6 +679,7 @@ main(int argc, char* argv[])
 	int c;
 	const char* cfgfile = CONFIGFILE;
 	const char* winopt = NULL;
+	const char* log_ident_default;
 	int cmdline_verbose = 0;
 	int debug_mode = 0;
 #ifdef UB_ON_WINDOWS
@@ -678,7 +692,8 @@ main(int argc, char* argv[])
 #endif
 
 	log_init(NULL, 0, NULL);
-	log_ident_set(strrchr(argv[0],'/')?strrchr(argv[0],'/')+1:argv[0]);
+	log_ident_default = strrchr(argv[0],'/')?strrchr(argv[0],'/')+1:argv[0];
+	log_ident_set(log_ident_default);
 	/* parse the options */
 	while( (c=getopt(argc, argv, "c:dhvw:")) != -1) {
 		switch(c) {
@@ -711,7 +726,7 @@ main(int argc, char* argv[])
 	if(winopt) {
 #ifdef UB_ON_WINDOWS
 		wsvc_command_option(winopt, cfgfile, cmdline_verbose, 
-			cmdline_cfg);
+			cmdline_cfg, log_ident_default);
 #else
 		fatal_exit("option not supported");
 #endif
@@ -722,7 +737,7 @@ main(int argc, char* argv[])
 		return 1;
 	}
 
-	run_daemon(cfgfile, cmdline_verbose, debug_mode);
+	run_daemon(cfgfile, cmdline_verbose, debug_mode, log_ident_default);
 	log_init(NULL, 0, NULL); /* close logfile */
 	return 0;
 }
diff --git a/doc/example.conf.in b/doc/example.conf.in
index 9c097aa..2e47701 100644
--- a/doc/example.conf.in
+++ b/doc/example.conf.in
@@ -275,9 +275,13 @@ server:
 	# logfile: ""
 
 	# Log to syslog(3) if yes. The log facility LOG_DAEMON is used to
-	# log to, with identity "unbound". If yes, it overrides the logfile.
+	# log to. If yes, it overrides the logfile.
 	# use-syslog: yes
 
+	# Log identity to report. if empty, defaults to the name of argv[0]
+	# (usually "unbound").
+	# log-identity: ""
+
 	# print UTC timestamp in ascii to logfile, default is epoch in seconds.
 	# log-time-ascii: no
 
diff --git a/util/config_file.c b/util/config_file.c
index 7c668ba..e798700 100644
--- a/util/config_file.c
+++ b/util/config_file.c
@@ -105,6 +105,7 @@ config_create(void)
 	cfg->ssl_port = 853;
 	cfg->ssl_upstream = 0;
 	cfg->use_syslog = 1;
+	cfg->log_identity = strdup(""); /* changed later with argv[0] */
 	cfg->log_time_ascii = 0;
 	cfg->log_queries = 0;
 #ifndef USE_WINSOCK
@@ -370,6 +371,7 @@ int config_set_option(struct config_file* cfg, const char* opt,
 	  log_set_time_asc(cfg->log_time_ascii); }
 	else S_SIZET_NONZERO("max-udp-size:", max_udp_size)
 	else S_YNO("use-syslog:", use_syslog)
+	else S_STR("log-identity:", log_identity)
 	else S_YNO("extended-statistics:", stat_extended)
 	else S_YNO("statistics-cumulative:", stat_cumulative)
 	else S_YNO("do-ip4:", do_ip4)
@@ -680,6 +682,7 @@ config_get_option(struct config_file* cfg, const char* opt,
 	else O_YNO(opt, "statistics-cumulative", stat_cumulative)
 	else O_YNO(opt, "extended-statistics", stat_extended)
 	else O_YNO(opt, "use-syslog", use_syslog)
+	else O_STR(opt, "log-identity", log_identity)
 	else O_YNO(opt, "log-time-ascii", log_time_ascii)
 	else O_DEC(opt, "num-threads", num_threads)
 	else O_IFC(opt, "interface", num_ifs, ifs)
diff --git a/util/config_file.h b/util/config_file.h
index 4499ac3..bf518d1 100644
--- a/util/config_file.h
+++ b/util/config_file.h
@@ -229,6 +229,8 @@ struct config_file {
 	int log_time_ascii;
 	/** log queries with one line per query */
 	int log_queries;
+	/** log identity to report */
+	char* log_identity;
 
 	/** do not report identity (id.server, hostname.bind) */
 	int hide_identity;
diff --git a/util/configlexer.lex b/util/configlexer.lex
index 35b7ecb..074ccf5 100644
--- a/util/configlexer.lex
+++ b/util/configlexer.lex
@@ -328,6 +328,7 @@ del-holddown{COLON}		{ YDVAR(1, VAR_DEL_HOLDDOWN) }
 keep-missing{COLON}		{ YDVAR(1, VAR_KEEP_MISSING) }
 permit-small-holddown{COLON}	{ YDVAR(1, VAR_PERMIT_SMALL_HOLDDOWN) }
 use-syslog{COLON}		{ YDVAR(1, VAR_USE_SYSLOG) }
+log-identity{COLON}		{ YDVAR(1, VAR_LOG_IDENTITY) }
 log-time-ascii{COLON}		{ YDVAR(1, VAR_LOG_TIME_ASCII) }
 log-queries{COLON}		{ YDVAR(1, VAR_LOG_QUERIES) }
 local-zone{COLON}		{ YDVAR(2, VAR_LOCAL_ZONE) }
diff --git a/util/configparser.y b/util/configparser.y
index 6ac12b5..2a3df28 100644
--- a/util/configparser.y
+++ b/util/configparser.y
@@ -130,6 +130,7 @@ extern struct config_parser_state* cfg_parser;
 %token VAR_LOCAL_ZONE_OVERRIDE VAR_ACCESS_CONTROL_TAG_ACTION
 %token VAR_ACCESS_CONTROL_TAG_DATA VAR_VIEW VAR_ACCESS_CONTROL_VIEW
 %token VAR_VIEW_FIRST VAR_SERVE_EXPIRED VAR_FAKE_DSA
+%token VAR_LOG_IDENTITY
 
 %%
 toplevelvars: /* empty */ | toplevelvars toplevelvar ;
@@ -204,7 +205,7 @@ content_server: server_num_threads | server_verbosity | server_port |
 	server_local_zone_override | server_access_control_tag_action |
 	server_access_control_tag_data | server_access_control_view |
 	server_qname_minimisation_strict | server_serve_expired |
-	server_fake_dsa
+	server_fake_dsa | server_log_identity
 	;
 stubstart: VAR_STUB_ZONE
 	{
@@ -1909,6 +1910,14 @@ server_disable_dnssec_lame_check: VAR_DISABLE_DNSSEC_LAME_CHECK STRING_ARG
 			(strcmp($2, "yes")==0);
 		free($2);
 	}
+	;
+server_log_identity: VAR_LOG_IDENTITY STRING_ARG
+	{
+		OUTYY(("P(server_log_identity:%s)\n", $2));
+		free(cfg_parser->cfg->log_identity);
+		cfg_parser->cfg->log_identity = $2;
+	}
+	;
 %%
 
 /* parse helper routines could be here */
-- 
2.9.0