From 44613d4d868ed5e73a1132280880f0699af56733 Mon Sep 17 00:00:00 2001 From: Evan Hunt Date: Tue, 29 Apr 2014 17:17:03 -0700 Subject: [PATCH] [master] named -L option for default logfile 3832. [func] "named -L " causes named to send log messages to the specified file by default instead of to the system log. (Thanks to Tony Finch.) [RT #35845] --- CHANGES | 5 ++ README | 2 + bin/named/include/named/globals.h | 1 + bin/named/log.c | 54 +++++++++++++++---- bin/named/main.c | 5 +- bin/named/named.docbook | 11 ++++ bin/tests/system/logfileconfig/clean.sh | 1 + .../system/logfileconfig/ns1/named.plainconf | 45 ++++++++++++++++ bin/tests/system/logfileconfig/tests.sh | 24 +++++++++ 9 files changed, 136 insertions(+), 12 deletions(-) create mode 100644 bin/tests/system/logfileconfig/ns1/named.plainconf diff --git a/CHANGES b/CHANGES index 9248edd7a7..47bd4b4e11 100644 --- a/CHANGES +++ b/CHANGES @@ -1,3 +1,8 @@ +3832. [func] "named -L " causes named to send log + messages to the specified file by default instead + of to the system log. (Thanks to Tony Finch.) + [RT #35845] + 3831. [cleanup] Reduce logging noise when EDNS state changes occur. [RT #35843] diff --git a/README b/README index 499e3f5fe8..824a87642d 100644 --- a/README +++ b/README @@ -68,6 +68,8 @@ BIND 9.11.0 the serial number will be set to the current date in YYYYMMDDNN format. - "dnssec-signzone -N date" sets the serial number to YYYYMMDDNN. + - "named -L " causes named to send log messages to + the specified file by default instead of to the system log. BIND 9.10.0 diff --git a/bin/named/include/named/globals.h b/bin/named/include/named/globals.h index 8ed18d1a08..3c261d3b59 100644 --- a/bin/named/include/named/globals.h +++ b/bin/named/include/named/globals.h @@ -128,6 +128,7 @@ EXTERN const char * ns_g_chrootdir INIT(NULL); EXTERN isc_boolean_t ns_g_foreground INIT(ISC_FALSE); EXTERN isc_boolean_t ns_g_logstderr INIT(ISC_FALSE); EXTERN isc_boolean_t ns_g_nosyslog INIT(ISC_FALSE); +EXTERN const char * ns_g_logfile INIT(NULL); EXTERN const char * ns_g_defaultsessionkeyfile INIT(NS_LOCALSTATEDIR "/run/named/" diff --git a/bin/named/log.c b/bin/named/log.c index a34dea47ec..9f1b0fa215 100644 --- a/bin/named/log.c +++ b/bin/named/log.c @@ -138,6 +138,22 @@ ns_log_setdefaultchannels(isc_logconfig_t *lcfg) { goto cleanup; } + if (ns_g_logfile != NULL) { + destination.file.stream = NULL; + destination.file.name = ns_g_logfile; + destination.file.versions = ISC_LOG_ROLLNEVER; + destination.file.maximum_size = 0; + result = isc_log_createchannel(lcfg, "default_logfile", + ISC_LOG_TOFILE, + ISC_LOG_DYNAMIC, + &destination, + ISC_LOG_PRINTTIME| + ISC_LOG_PRINTCATEGORY| + ISC_LOG_PRINTLEVEL); + if (result != ISC_R_SUCCESS) + goto cleanup; + } + #if ISC_FACILITY != LOG_DAEMON destination.facility = ISC_FACILITY; result = isc_log_createchannel(lcfg, "default_syslog", @@ -161,9 +177,7 @@ ns_log_setdefaultchannels(isc_logconfig_t *lcfg) { isc_result_t ns_log_setsafechannels(isc_logconfig_t *lcfg) { isc_result_t result; -#if ISC_FACILITY != LOG_DAEMON isc_logdestination_t destination; -#endif if (! ns_g_logstderr) { result = isc_log_createchannel(lcfg, "default_debug", @@ -182,6 +196,22 @@ ns_log_setsafechannels(isc_logconfig_t *lcfg) { isc_log_setdebuglevel(ns_g_lctx, ns_g_debuglevel); } + if (ns_g_logfile != NULL) { + destination.file.stream = NULL; + destination.file.name = ns_g_logfile; + destination.file.versions = ISC_LOG_ROLLNEVER; + destination.file.maximum_size = 0; + result = isc_log_createchannel(lcfg, "default_logfile", + ISC_LOG_TOFILE, + ISC_LOG_DYNAMIC, + &destination, + ISC_LOG_PRINTTIME| + ISC_LOG_PRINTCATEGORY| + ISC_LOG_PRINTLEVEL); + if (result != ISC_R_SUCCESS) + goto cleanup; + } + #if ISC_FACILITY != LOG_DAEMON destination.facility = ISC_FACILITY; result = isc_log_createchannel(lcfg, "default_syslog", @@ -199,21 +229,23 @@ ns_log_setsafechannels(isc_logconfig_t *lcfg) { isc_result_t ns_log_setdefaultcategory(isc_logconfig_t *lcfg) { - isc_result_t result; - - if (! ns_g_logstderr && ! ns_g_nosyslog) { - result = isc_log_usechannel(lcfg, "default_syslog", - ISC_LOGCATEGORY_DEFAULT, NULL); - if (result != ISC_R_SUCCESS) - goto cleanup; - } + isc_result_t result = ISC_R_SUCCESS; result = isc_log_usechannel(lcfg, "default_debug", ISC_LOGCATEGORY_DEFAULT, NULL); if (result != ISC_R_SUCCESS) goto cleanup; - result = ISC_R_SUCCESS; + if (! ns_g_logstderr) { + if (ns_g_logfile != NULL) + result = isc_log_usechannel(lcfg, "default_logfile", + ISC_LOGCATEGORY_DEFAULT, + NULL); + else if (! ns_g_nosyslog) + result = isc_log_usechannel(lcfg, "default_syslog", + ISC_LOGCATEGORY_DEFAULT, + NULL); + } cleanup: return (result); diff --git a/bin/named/main.c b/bin/named/main.c index b2104adda7..a6856bbe28 100644 --- a/bin/named/main.c +++ b/bin/named/main.c @@ -421,7 +421,7 @@ parse_command_line(int argc, char *argv[]) { save_command_line(argc, argv); /* PLEASE keep options synchronized when main is hooked! */ -#define CMDLINE_FLAGS "46c:C:d:D:E:fFgi:lm:n:N:p:P:sS:t:T:U:u:vVx:" +#define CMDLINE_FLAGS "46c:C:d:D:E:fFgi:lL:m:n:N:p:P:sS:t:T:U:u:vVx:" isc_commandline_errprint = ISC_FALSE; while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) { switch (ch) { @@ -478,6 +478,9 @@ parse_command_line(int argc, char *argv[]) { case 'l': ns_g_lwresdonly = ISC_TRUE; break; + case 'L': + ns_g_logfile = isc_commandline_argument; + break; case 'm': set_flags(isc_commandline_argument, mem_debug_flags, &isc_mem_debugging); diff --git a/bin/named/named.docbook b/bin/named/named.docbook index 595b207fef..0440feaa5a 100644 --- a/bin/named/named.docbook +++ b/bin/named/named.docbook @@ -66,6 +66,7 @@ + @@ -200,6 +201,16 @@ + + -L logfile + + + Log to the file by default + instead of the system log. + + + + -m flag diff --git a/bin/tests/system/logfileconfig/clean.sh b/bin/tests/system/logfileconfig/clean.sh index 0ceadbac7b..a63f0f0130 100644 --- a/bin/tests/system/logfileconfig/clean.sh +++ b/bin/tests/system/logfileconfig/clean.sh @@ -24,3 +24,4 @@ rm -f ns1/named.memstats ns1/dig.out rm -f ns1/named_log ns1/named_pipe ns1/named_sym rm -f ns1/named.conf rm -rf ns1/named_dir +rm -f ns1/named_deflog diff --git a/bin/tests/system/logfileconfig/ns1/named.plainconf b/bin/tests/system/logfileconfig/ns1/named.plainconf new file mode 100644 index 0000000000..3c5f90082d --- /dev/null +++ b/bin/tests/system/logfileconfig/ns1/named.plainconf @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2014 Internet Systems Consortium, Inc. ("ISC") + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH + * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY + * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, + * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM + * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE + * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR + * PERFORMANCE OF THIS SOFTWARE. + */ + +options { + query-source address 10.53.0.1; + notify-source 10.53.0.1; + transfer-source 10.53.0.1; + port 5300; + pid-file "named.pid"; + listen-on port 5300 { + 10.53.0.1; + }; + listen-on-v6 { none; }; + recursion no; + notify yes; +}; + +controls { + inet 127.0.0.1 port 9593 allow { + 127.0.0.1/32; ::1/128; } + keys { "rndc-key"; }; +}; + +key "rndc-key" { + algorithm hmac-sha256; + secret "Am9vCg=="; +}; + +zone "." { + type master; + file "root.db"; +}; diff --git a/bin/tests/system/logfileconfig/tests.sh b/bin/tests/system/logfileconfig/tests.sh index 3c4f52241f..d00848a1dd 100644 --- a/bin/tests/system/logfileconfig/tests.sh +++ b/bin/tests/system/logfileconfig/tests.sh @@ -24,10 +24,12 @@ PLAINCONF="${THISDIR}/${CONFDIR}/named.plain" DIRCONF="${THISDIR}/${CONFDIR}/named.dirconf" PIPECONF="${THISDIR}/${CONFDIR}/named.pipeconf" SYMCONF="${THISDIR}/${CONFDIR}/named.symconf" +PLAINCONF="${THISDIR}/${CONFDIR}/named.plainconf" PLAINFILE="named_log" DIRFILE="named_dir" PIPEFILE="named_pipe" SYMFILE="named_sym" +DLFILE="named_deflog" PIDFILE="${THISDIR}/${CONFDIR}/named.pid" myRNDC="$RNDC -c ${THISDIR}/${CONFDIR}/rndc.conf" myNAMED="$NAMED -c ${THISDIR}/${CONFDIR}/named.conf -m record,size,mctx -T clienttest -T nosyslog -d 99 -U 4" @@ -227,5 +229,27 @@ else echo "I: skipping symlink test (unable to create symlink)" fi +status=0 + +# Now stop the server again and test the -L option +rm -f $DLFILE +$myRNDC stop +cp $PLAINCONF named.conf +$myNAMED -L $DLFILE > /dev/null 2>&1 +if [ $? -ne 0 ]; then + echo "I:failed to start $myNAMED" + echo "I:exit status: $status" + exit $status +fi + +sleep 1 +if [ -f "$DLFILE" ]; then + echo "I: testing default logfile using named -L succeeded" +else + echo "I:testing default logfile using named -L failed" + echo "I:exit status: 1" + exit 1 +fi + echo "I:exit status: $status" exit $status