]> git.ipfire.org Git - thirdparty/unbound.git/commitdiff
syslog feature.
authorWouter Wijngaards <wouter@nlnetlabs.nl>
Wed, 26 Sep 2007 10:19:28 +0000 (10:19 +0000)
committerWouter Wijngaards <wouter@nlnetlabs.nl>
Wed, 26 Sep 2007 10:19:28 +0000 (10:19 +0000)
git-svn-id: file:///svn/unbound/trunk@644 be551aaa-1e26-0410-a405-d3ace91eadb9

16 files changed:
Makefile.in
configure.ac
daemon/unbound.c
doc/Changelog
doc/example.conf
doc/unbound.conf.5
testcode/lock_verify.c
testcode/signit.c
testcode/testbound.c
testcode/unitmain.c
util/config_file.c
util/config_file.h
util/configlexer.lex
util/configparser.y
util/log.c
util/log.h

index 64ed245c805370a4731785e51311af3609c58b51..f43493fb28d01f986776c146226e484070bea5d0 100644 (file)
@@ -46,7 +46,7 @@ endif
 BUILD=build/
 
 LINT=splint
-LINTFLAGS=+quiet -weak -warnposix -unrecog -Din_addr_t=uint32_t -Du_int=unsigned -Du_char=uint8_t -preproc -Drlimit=rlimit64
+LINTFLAGS=+quiet -weak -warnposix -unrecog -Din_addr_t=uint32_t -Du_int=unsigned -Du_char=uint8_t -preproc -Drlimit=rlimit64 +posixlib
 # compat with openssl linux edition.
 LINTFLAGS+="-DBN_ULONG=unsigned long" -Dkrb5_int32=int "-Dkrb5_ui_4=unsigned int" -DPQ_64BIT=uint64_t
 
index 2b57840b8f2b1d326a902c857aa1ce34c06439c2..04f8fb12b8752f1afa6b840af5e1f8be7656cae1 100644 (file)
@@ -269,7 +269,7 @@ AC_PROG_LIBTOOL
 
 # Checks for header files.
 AC_HEADER_STDC
-AC_CHECK_HEADERS([stdarg.h stdbool.h netinet/in.h sys/param.h sys/socket.h sys/uio.h arpa/inet.h],,, [AC_INCLUDES_DEFAULT])
+AC_CHECK_HEADERS([stdarg.h stdbool.h netinet/in.h sys/param.h sys/socket.h sys/uio.h arpa/inet.h syslog.h],,, [AC_INCLUDES_DEFAULT])
 
 # check for types
 AC_CHECK_TYPE(int8_t, char)
@@ -538,7 +538,7 @@ AC_ARG_WITH(ldns,
 
 AC_CHECK_LIB(ldns, ldns_rr_new,, [AC_MSG_ERROR([Can't find ldns library])])
 
-AC_DEFINE_UNQUOTED([MAXSYSLOGMSGLEN], [512], [Define to the maximum message length to pass to syslog.])
+AC_DEFINE_UNQUOTED([MAXSYSLOGMSGLEN], [5120], [Define to the maximum message length to pass to syslog.])
 
 AH_BOTTOM([
 #include <stdio.h>
index 601863cbfbd9768c7700e097d3581c9e7c44376e..b3f31f903ec2e1600d8c49732025a43bf57a3717 100644 (file)
@@ -284,7 +284,7 @@ do_chroot(struct daemon* daemon, struct config_file* cfg, int debug_mode)
        }
 
        /* init logfile just before fork */
-       log_init(cfg->logfile);
+       log_init(cfg->logfile, cfg->use_syslog);
        if(!debug_mode && cfg->do_daemonize) {
                detach(cfg);
        }
@@ -329,7 +329,7 @@ run_daemon(const char* cfgfile, int cmdline_verbose, int debug_mode)
                if(!done_chroot) { 
                        do_chroot(daemon, cfg, debug_mode); 
                        done_chroot = 1; 
-               }
+               } else log_init(cfg->logfile, cfg->use_syslog);
                /* work */
                daemon_fork(daemon);
 
@@ -366,7 +366,7 @@ main(int argc, char* argv[])
        /* take debug snapshot of heap */
        unbound_start_brk = sbrk(0);
 
-       log_init(NULL);
+       log_init(NULL, 0);
        /* parse the options */
        while( (c=getopt(argc, argv, "c:dhv")) != -1) {
                switch(c) {
@@ -396,6 +396,6 @@ main(int argc, char* argv[])
        }
 
        run_daemon(cfgfile, cmdline_verbose, debug_mode);
-       log_init(NULL); /* close logfile */
+       log_init(NULL, 0); /* close logfile */
        return 0;
 }
index 86a31dd30d600ea6aef72e58e5eb4937cbd4f9fc..5b063c4de1a22c0da72fdcb99d2968d060bc633d 100644 (file)
@@ -1,3 +1,7 @@
+26 September 2007: Wouter
+       - SIGHUP will reopen the log file.
+       - Option to log to syslog.
+
 25 September 2007: Wouter
        - tests for NSEC3. Fixup bitmap checks for NSEC3.
        - positive ANY response needs to check if wildcard expansion, and
index b721c286af55901b6d80ce7747e060d71716bcc3..bbe06ba83b33884abd9c401b504c28af5b51e878 100644 (file)
@@ -112,8 +112,13 @@ server:
        # the working directory.
        # directory: "/etc/unbound"
 
-       # the log file, "" means log to stderr.
+       # the log file, "" means log to stderr. 
+       # Use of this option sets use-syslog to "no".
        # 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.
+       # use-syslog: yes 
 
        # the pid file.
        # pidfile: "unbound.pid"
index 71c5300a8a8bc6a7859fd04ed2c1ad7c3438fd87..631ee40ed424169a8f90d6a4a833cec87afec2e1 100644 (file)
@@ -39,9 +39,9 @@ example.conf file with all the options.
 # unbound.conf(5) config file for unbound(8).
 server:
        directory: "/etc/unbound"
-       username: unbound       # make sure it can write to pidfile, logfile.
+       username: unbound       # make sure it can write to pidfile.
        chroot: "/etc/unbound"
-       logfile: "/etc/unbound/unbound.log"
+       # logfile: "/etc/unbound/unbound.log"  #uncomment to use logfile.
        pidfile: "/etc/unbound/unbound.pid"
        # verbosity: 1          # uncomment and increase to get more logging.
 .fi
@@ -144,6 +144,13 @@ Sets the working directory for the program.
 If "" is given, logging goes to stderr, or nowhere once daemonized.
 The logfile is appended to, in the following format: 
 [seconds since 1970] unbound[pid:tid]: type: message. 
+If this option is given, the use-syslog is option is set to "no".
+.It \fBuse-syslog:\fR <yes or no>
+Sets unbound to send log messages to the syslogd, using 
+.Xr syslog 3 .
+The log facility LOG_DAEMON is used, with identity "unbound".
+The logfile setting is overridden when use-syslog is turned on.
+The default is to log to syslog.
 .It \fBpidfile:\fR <filename>
 The process id is written to the file. Default is "unbound.pid". So,
 kill -HUP `cat /etc/unbound/unbound.pid` will trigger a reload,
index b1801aaff18ecf1bf3b302fb200a83c3cbf0e188..f997b349be6bf732c9ff11209aa01ee66198ed01 100644 (file)
@@ -403,7 +403,7 @@ main(int argc, char* argv[])
                usage();
                return 1;
        }
-       log_init(NULL);
+       log_init(NULL, 0);
        log_ident_set("lock-verify");
        /* init */
        all_locks = rbtree_create(order_lock_cmp);
index b9cee166bbaab1a88bdc6f95c563a6b5c8b8c4ec..717b4e2de676ccaa06228eee443d10acb773d2e4 100644 (file)
@@ -236,7 +236,7 @@ process_nsec3(int argc, char* argv[])
 /** main program */
 int main(int argc, char* argv[])
 {
-       log_init(NULL);
+       log_init(NULL, 0);
        if(argc != 6) {
                usage();
        }
index ebb0136edb50310f06168bbdb4afdb905b84c4ed..f20e89c8061cc0b99f66da8df69cebb422761458 100644 (file)
@@ -135,6 +135,7 @@ setup_config(FILE* in, char* configfile, int* lineno,
        if(!cfg) fatal_exit("could not open %s: %s", 
                        configfile, strerror(errno));
        line[MAX_LINE_LEN-1] = 0;
+       fprintf(cfg, "server:   use-syslog: no\n");
        while(fgets(line, MAX_LINE_LEN-1, in)) {
                parse = line;
                (*lineno)++;
@@ -204,7 +205,7 @@ main(int argc, char* argv[])
        char* init_optarg = optarg;
        struct replay_scenario* scen = NULL;
 
-       log_init(NULL);
+       log_init(NULL, 0);
        log_info("Start of %s testbound program.", PACKAGE_STRING);
        /* determine commandline options for the daemon */
        cfgfile[0] = 0;
index 5b2b766f782dc2fdbb09eb65643839626421fa64..1e2e9e9710d88597c39e1f0ec923928e7743e881 100644 (file)
@@ -205,7 +205,7 @@ infra_test()
 int 
 main(int argc, char* argv[])
 {
-       log_init(NULL);
+       log_init(NULL, 0);
        if(argc != 1) {
                printf("usage: %s\n", argv[0]);
                printf("\tperforms unit tests.\n");
index 14396dab5d7eea31d3ba936f4efb016c7d047151..a85a86483d9765d3bf20002baa758d600f0d7d7f 100644 (file)
@@ -75,6 +75,7 @@ config_create()
        cfg->do_ip6 = 1;
        cfg->do_udp = 1;
        cfg->do_tcp = 1;
+       cfg->use_syslog = 1;
        cfg->outgoing_base_port = cfg->port + 2000;
        cfg->outgoing_num_ports = 16;
        cfg->outgoing_num_tcp = 10;
index a997d6c4939976cd95c41bfc2b81af02f2943bbd..8f08d67cf90eaa1f15681bf56f17b826c3315489 100644 (file)
@@ -131,6 +131,9 @@ struct config_file {
        /** pidfile to write pid to. */
        char* pidfile;
 
+       /** should log messages be sent to syslogd */
+       int use_syslog;
+
        /** do not report identity (id.server, hostname.bind) */
        int hide_identity;
        /** do not report version (version.server, version.bind) */
index 124079505063d7382e25af198033a7db9e9e25b2..5e537fd996fa951e69bd7bfc48da4e4795243183 100644 (file)
@@ -154,6 +154,7 @@ val-permissive-mode{COLON}  { YDOUT; return VAR_VAL_PERMISSIVE_MODE;}
 key-cache-size{COLON}  { YDOUT; return VAR_KEY_CACHE_SIZE;}
 key-cache-slabs{COLON} { YDOUT; return VAR_KEY_CACHE_SLABS;}
 val-nsec3-keysize-iterations{COLON}    { YDOUT; return VAR_VAL_NSEC3_KEYSIZE_ITERATIONS;}
+use-syslog{COLON}      { YDOUT; return VAR_USE_SYSLOG;}
 {NEWLINE}              { LEXOUT(("NL\n")); cfg_parser->line++;}
 
        /* Quoted strings. Strip leading and ending quotes */
index 69beb445b06e64def46e47f56576d433990caf76..56ebe4d4f8df8870e3bbe41051605fd50c1a7ab9 100644 (file)
@@ -84,7 +84,7 @@ extern struct config_parser_state* cfg_parser;
 %token VAR_BOGUS_TTL VAR_VAL_CLEAN_ADDITIONAL VAR_VAL_PERMISSIVE_MODE
 %token VAR_INCOMING_NUM_TCP VAR_MSG_BUFFER_SIZE VAR_KEY_CACHE_SIZE
 %token VAR_KEY_CACHE_SLABS VAR_TRUSTED_KEYS_FILE 
-%token VAR_VAL_NSEC3_KEYSIZE_ITERATIONS
+%token VAR_VAL_NSEC3_KEYSIZE_ITERATIONS VAR_USE_SYSLOG
 
 %%
 toplevelvars: /* empty */ | toplevelvars toplevelvar ;
@@ -122,7 +122,8 @@ content_server: server_num_threads | server_verbosity | server_port |
        server_val_clean_additional | server_val_permissive_mode |
        server_incoming_num_tcp | server_msg_buffer_size | 
        server_key_cache_size | server_key_cache_slabs | 
-       server_trusted_keys_file | server_val_nsec3_keysize_iterations
+       server_trusted_keys_file | server_val_nsec3_keysize_iterations |
+       server_use_syslog
        ;
 stubstart: VAR_STUB_ZONE
        {
@@ -268,6 +269,20 @@ server_do_tcp: VAR_DO_TCP STRING
                free($2);
        }
        ;
+server_use_syslog: VAR_USE_SYSLOG STRING
+       {
+               OUTYY(("P(server_use_syslog:%s)\n", $2));
+               if(strcmp($2, "yes") != 0 && strcmp($2, "no") != 0)
+                       yyerror("expected yes or no.");
+               else cfg_parser->cfg->use_syslog = (strcmp($2, "yes")==0);
+#ifndef HAVE_SYSLOG_H
+               if(strcmp($2, "yes") == 0)
+                       yyerror("no syslog services are available. "
+                               "(reconfigure and compile to add)");
+#endif
+               free($2);
+       }
+       ;
 server_chroot: VAR_CHROOT STRING
        {
                OUTYY(("P(server_chroot:%s)\n", $2));
@@ -294,6 +309,7 @@ server_logfile: VAR_LOGFILE STRING
                OUTYY(("P(server_logfile:%s)\n", $2));
                free(cfg_parser->cfg->logfile);
                cfg_parser->cfg->logfile = $2;
+               cfg_parser->cfg->use_syslog = 0;
        }
        ;
 server_pidfile: VAR_PIDFILE STRING
index 9ee324cc0b5907f680a9508d0a3335599ca6b7df..26912bc013eba485c5b67d07bc7565d18342c599 100644 (file)
 #ifdef HAVE_TIME_H
 #include <time.h>
 #endif
+#ifdef HAVE_SYSLOG_H
+#  include <syslog.h>
+#else
+/**define LOG_ constants */
+#  define LOG_CRIT 2
+#  define LOG_ERR 3
+#  define LOG_WARNING 4
+#  define LOG_NOTICE 5
+#  define LOG_INFO 6
+#  define LOG_DEBUG 7
+#endif
 
+/* default verbosity */
 enum verbosity_value verbosity = 4;
 /** the file logged to. */
 static FILE* logfile = 0;
@@ -53,19 +65,36 @@ static int key_created = 0;
 static ub_thread_key_t logkey;
 /** the identity of this executable/process */
 static const char* ident="unbound";
+#ifdef HAVE_SYSLOG_H
+/** are we using syslog(3) to log to */
+static int log_to_syslog = 0;
+#endif /* HAVE_SYSLOG_H */
 
 void
-log_init(const char* filename)
+log_init(const char* filename, int use_syslog)
 {
        FILE *f;
        if(!key_created) {
                key_created = 1;
                ub_thread_key_create(&logkey, NULL);
        }
-
+       if(logfile || log_to_syslog)
+               verbose(VERB_DETAIL, "switching log to %s", 
+               use_syslog?"syslog":(filename&&filename[0]?filename:"stderr"));
+       if(logfile && logfile != stderr)
+               fclose(logfile);
+#ifdef HAVE_SYSLOG_H
+       if(log_to_syslog) {
+               closelog();
+               log_to_syslog = 0;
+       }
+       if(use_syslog) {
+               openlog(ident, 0, LOG_DAEMON);
+               log_to_syslog = 1;
+               return;
+       }
+#endif /* HAVE_SYSLOG_H */
        if(!filename || !filename[0]) {
-               if(logfile && logfile != stderr)
-                       fclose(logfile);
                logfile = stderr;
                return;
        }
@@ -76,9 +105,6 @@ log_init(const char* filename)
                        strerror(errno));
                return;
        }
-       verbose(VERB_DETAIL, "switching to logfile %s", filename);
-       if(logfile && logfile != stderr)
-               fclose(logfile);
        logfile = f;
 }
 
@@ -93,14 +119,22 @@ void log_ident_set(const char* id)
 }
 
 void
-log_vmsg(const char* type, const char *format, va_list args)
+log_vmsg(int pri, const char* type,
+       const char *format, va_list args)
 {
-       char message[MAXSYSLOGMSGLEN * 10];
+       char message[MAXSYSLOGMSGLEN];
        unsigned int* tid = (unsigned int*)ub_thread_key_get(logkey);
+       (void)pri;
        vsnprintf(message, sizeof(message), format, args);
-       fprintf(logfile, "[%d] %s[%d:%x] %s: %s\n",
-               (int)time(NULL), ident, (int)getpid(), 
-               tid?*tid:0, type, message);
+#ifdef HAVE_SYSLOG_H
+       if(log_to_syslog) {
+               syslog(pri, "[%d:%x] %s: %s", 
+                       (int)getpid(), tid?*tid:0, type, message);
+               return;
+       }
+#endif /* HAVE_SYSLOG_H */
+       fprintf(logfile, "[%d] %s[%d:%x] %s: %s\n", (int)time(NULL), 
+               ident, (int)getpid(), tid?*tid:0, type, message);
        fflush(logfile);
 }
 
@@ -113,7 +147,7 @@ log_info(const char *format, ...)
 {
         va_list args;
        va_start(args, format);
-       log_vmsg("info", format, args);
+       log_vmsg(LOG_INFO, "info", format, args);
        va_end(args);
 }
 
@@ -126,7 +160,7 @@ log_err(const char *format, ...)
 {
         va_list args;
        va_start(args, format);
-       log_vmsg("error", format, args);
+       log_vmsg(LOG_ERR, "error", format, args);
        va_end(args);
 }
 
@@ -139,7 +173,7 @@ log_warn(const char *format, ...)
 {
         va_list args;
        va_start(args, format);
-       log_vmsg("warning", format, args);
+       log_vmsg(LOG_WARNING, "warning", format, args);
        va_end(args);
 }
 
@@ -152,7 +186,7 @@ fatal_exit(const char *format, ...)
 {
         va_list args;
        va_start(args, format);
-       log_vmsg("fatal error", format, args);
+       log_vmsg(LOG_CRIT, "fatal error", format, args);
        va_end(args);
        exit(1);
 }
@@ -167,8 +201,13 @@ verbose(enum verbosity_value level, const char* format, ...)
 {
         va_list args;
        va_start(args, format);
-       if(verbosity >= level)
-               log_vmsg("note", format, args);
+       if(verbosity >= level) {
+               if(level == VERB_OPS)
+                       log_vmsg(LOG_NOTICE, "notice", format, args);
+               else if(level == VERB_DETAIL)
+                       log_vmsg(LOG_INFO, "info", format, args);
+               else    log_vmsg(LOG_DEBUG, "debug", format, args);
+       }
        va_end(args);
 }
 
index dd126a5e07ff7ba87661ffbcf2ab02c090292015..12dc6bfe5663b6cfe5701bc9c9e78d2c5d4c26fa 100644 (file)
@@ -77,8 +77,9 @@ void verbose(enum verbosity_value level,
 /**
  * call this to initialize logging services.
  * @param filename: if NULL stderr is used.
+ * @param use_syslog: set to true to ignore filename and use syslog(3).
  */
-void log_init(const char* filename);
+void log_init(const char* filename, int use_syslog);
 
 /**
  * Init a thread (will print this number for the thread log entries).
@@ -133,11 +134,12 @@ void fatal_exit(const char* format, ...) ATTR_FORMAT(printf, 1, 2);
 
 /**
  * va_list argument version of log_info.
+ * @param pri: priority type, for example 5 (INFO).
  * @param type: string to designate type of message (info, error).
  * @param format: the printf style format to print. no newline.
  * @param args: arguments for format string.
  */
-void log_vmsg(const char* type, const char* format, va_list args);
+void log_vmsg(int pri, const char* type, const char* format, va_list args);
 
 /** always assert for now. */
 #define UNBOUND_ASSERT 1