]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
log: rework the way -d work
authorVincent Bernat <vincent@bernat.im>
Sat, 28 Nov 2015 13:49:45 +0000 (14:49 +0100)
committerVincent Bernat <vincent@bernat.im>
Sat, 28 Nov 2015 13:49:45 +0000 (14:49 +0100)
Now:
 - `-d` means to stay in foreground, log to syslog
 - `-dd` means to stay in foreground, log warnings to console
 - `-ddd` means to stay in foreground, log warnings and info to console
 - `-dddd` means to stay in foreground, log all to console

Fix #146

NEWS
src/client/lldpcli.8.in
src/client/lldpcli.c
src/daemon/lldpd.8.in
src/daemon/lldpd.c
src/lib/errors.c
src/log.c
src/log.h

diff --git a/NEWS b/NEWS
index 49ba7c15b36a6598efaaaa3665926e342a8ea947..f0fb9d30d156f0a2c5c398c010e90d712b193dc9 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,9 @@ lldpd (0.8.0)
       address TLV. Unless hardening has been disabled, this overflow
       cannot be used for arbitrary code execution.
   * Change:
+    + Running lldpd with "-d" will keep the process in foreground but
+      logs will still go to syslog. To log to the console, add at
+      least one "-d".
     + Fix minimal kernel version to 2.6.39. Add a runtime warning when
       this is not the case.
     + Remove old bridge code (the one using ioctl).
index 3abc00b1ec6d8d6ee0c03126857f1dae0d8c5cea..49bcceabf6f52ed1f7a971c8e2264d432f8a76d7 100644 (file)
@@ -48,7 +48,7 @@ interactive shell should provide completion and history support.
 The options are as follows:
 .Bl -tag -width Ds
 .It Fl d
-Enable more debugging information.
+Enable more debugging information. This flag can be repeated.
 .It Fl u Ar socket
 Specify the Unix-domain socket used for communication with
 .Xr lldpd 8 .
index 38e73541a374a9ee55b8e39959a351f1de061c7c..e38256a037e6e563d2591221e8da92f05e0c7c09 100644 (file)
@@ -429,7 +429,7 @@ input_append(const char *arg, struct inputs *inputs, int acceptdir)
 int
 main(int argc, char *argv[])
 {
-       int ch, debug = 1, rc = EXIT_FAILURE;
+       int ch, debug = 0, use_syslog = 0, rc = EXIT_FAILURE;
        const char *fmt = "plain";
        lldpctl_conn_t *conn = NULL;
        const char *options = is_lldpctl(argv[0])?"hdvf:u:":"hdsvf:c:u:";
@@ -446,8 +446,18 @@ main(int argc, char *argv[])
        optind = 1;
        while ((ch = getopt(argc, argv, options)) != -1) {
                switch (ch) {
-               case 'd': debug++; break;
-               case 's': debug--; break;
+               case 'd':
+                       if (use_syslog)
+                               use_syslog = 0;
+                       else
+                               debug++;
+                       break;
+               case 's':
+                       if (debug == 0)
+                               use_syslog = 1;
+                       else
+                               debug--;
+                       break;
                case 'h':
                        usage();
                        break;
@@ -463,8 +473,8 @@ main(int argc, char *argv[])
                        break;
                case 'c':
                        if (!gotinputs) {
-                               log_init(debug, __progname);
-                               lldpctl_log_level(debug);
+                               log_init(use_syslog, debug, __progname);
+                               lldpctl_log_level(debug + 1);
                                gotinputs = 1;
                        }
                        input_append(optarg, &inputs, 1);
@@ -475,8 +485,8 @@ main(int argc, char *argv[])
        }
 
        if (!gotinputs) {
-               log_init(debug, __progname);
-               lldpctl_log_level(debug);
+               log_init(use_syslog, debug, __progname);
+               lldpctl_log_level(debug + 1);
        }
 
        /* Disable SIGPIPE */
index 56c7d2e4be0a68c89f66464ae69d57dffcf03e04..9599cc6a9efe72674c7be91745c302bd00c3b19b 100644 (file)
@@ -60,11 +60,12 @@ The options are as follows:
 Do not daemonize.
 If this option is specified,
 .Nm
-will run in the foreground and log to
-.Em stderr .
-This option can be specified many times to increase verbosity. When
-specified three times, debug logs will be enabled. They can be
-filtered with
+will run in the foreground. When specified one more time,
+.Nm will log to
+.Em stderr
+instead of using syslog. Then, this option can be specified many times
+to increase verbosity. When specified four times, debug logs will be
+enabled. They can be filtered with
 .Fl D
 flag.
 .It Fl D Ar debug
index be94f4f0ffa546fd500f7c2abe82cf536c9400b0..22de4f08bc16cadd114c0680f6e65acc68756a05 100644 (file)
@@ -1229,15 +1229,22 @@ lldpd_exit(struct lldpd *cfg)
  * @return PID of running lldpcli or -1 if error.
  */
 static pid_t
-lldpd_configure(int debug, const char *path, const char *ctlname)
+lldpd_configure(int use_syslog, int debug, const char *path, const char *ctlname)
 {
        pid_t lldpcli = vfork();
        int devnull;
 
-       char sdebug[debug + 3];
-       memset(sdebug, 'd', debug + 3);
-       sdebug[debug + 2] = '\0';
-       sdebug[0] = '-'; sdebug[1] = 's';
+       char sdebug[debug + 4];
+       if (use_syslog)
+               strlcpy(sdebug, "-s", 3);
+       else {
+               /* debug = 0 -> -sd */
+               /* debug = 1 -> -sdd */
+               /* debug = 2 -> -sddd */
+               memset(sdebug, 'd', sizeof(sdebug));
+               sdebug[debug + 3] = '\0';
+               sdebug[0] = '-'; sdebug[1] = 's';
+       }
        log_debug("main", "invoke %s %s", path, sdebug);
 
        switch (lldpcli) {
@@ -1417,7 +1424,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
 {
        struct lldpd *cfg;
        struct lldpd_chassis *lchassis;
-       int ch, debug = 0;
+       int ch, debug = 0, use_syslog = 1, daemonize = 1;
 #ifdef USE_SNMP
        int snmp = 0;
        const char *agentx = NULL;      /* AgentX socket */
@@ -1478,7 +1485,12 @@ lldpd_main(int argc, char *argv[], char *envp[])
                        exit(0);
                        break;
                case 'd':
-                       debug++;
+                       if (daemonize)
+                               daemonize = 0;
+                       else if (use_syslog)
+                               use_syslog = 0;
+                       else
+                               debug++;
                        break;
                case 'D':
                        log_accept(optarg);
@@ -1598,7 +1610,7 @@ lldpd_main(int argc, char *argv[], char *envp[])
        }
        smart = filters[i].b;
 
-       log_init(debug, __progname);
+       log_init(use_syslog, debug, __progname);
        tzset();                /* Get timezone info before chroot */
 
        log_debug("main", "lldpd " PACKAGE_VERSION " starting...");
@@ -1658,14 +1670,14 @@ lldpd_main(int argc, char *argv[], char *envp[])
        /* Configuration with lldpcli */
        if (lldpcli) {
                log_debug("main", "invoking lldpcli for configuration");
-               if (lldpd_configure(debug, lldpcli, ctlname) == -1)
+               if (lldpd_configure(use_syslog, debug, lldpcli, ctlname) == -1)
                        fatal("main", "unable to spawn lldpcli");
        }
 
        /* Daemonization, unless started by upstart, systemd or launchd or debug */
 #ifndef HOST_OS_OSX
-       if (!lldpd_started_by_upstart() && !lldpd_started_by_systemd() &&
-           !debug) {
+       if (daemonize &&
+           !lldpd_started_by_upstart() && !lldpd_started_by_systemd()) {
                int pid;
                char *spid;
                log_debug("main", "daemonize");
index 0bd31f325c490bb3d4e8b527abd142608d1bb0b8..44fb583559078a216ba75941880efb343f8b4382 100644 (file)
@@ -57,5 +57,5 @@ lldpctl_log_callback(void (*cb)(int severity, const char *msg))
 void
 lldpctl_log_level(int level)
 {
-       log_level(level);
+       if (level >= 1) log_level(level-1);
 }
index d1106fa8d34fd219048dbacb4b08500098cdb7dc..332ce99947858b9faa499e3e13fac83c1071966e 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -28,7 +28,9 @@
 #include <time.h>
 
 /* By default, logging is done on stderr. */
-static int      debug = 1;
+static int      use_syslog = 0;
+/* Default debug level */
+static int      debug = 0;
 
 /* Logging can be modified by providing an appropriate log handler. */
 static void (*logh)(int severity, const char *msg) = NULL;
@@ -40,11 +42,12 @@ static void  logit(int, const char *, const char *, ...);
 static const char *tokens[MAX_DBG_TOKENS + 1] = {NULL};
 
 void
-log_init(int n_debug, const char *progname)
+log_init(int n_syslog, int n_debug, const char *progname)
 {
+       use_syslog = n_syslog;
        debug = n_debug;
 
-       if (!debug)
+       if (use_syslog)
                openlog(progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
 
        tzset();
@@ -53,7 +56,7 @@ log_init(int n_debug, const char *progname)
 void
 log_level(int n_debug)
 {
-       if (debug > 0 && n_debug >= 1)
+       if (n_debug >= 0)
                debug = n_debug;
 }
 
@@ -142,7 +145,7 @@ vlog(int pri, const char *token, const char *fmt, va_list ap)
                }
                /* Otherwise, fallback to output on stderr. */
        }
-       if (debug || logh) {
+       if (!use_syslog || logh) {
                char *nfmt;
                /* best effort in out of mem situations */
                if (asprintf(&nfmt, "%s %s%s%s]%s %s\n",
@@ -202,7 +205,7 @@ log_info(const char *token, const char *emsg, ...)
 {
        va_list  ap;
 
-       if (!debug || debug > 1 || logh) {
+       if (use_syslog || debug > 0 || logh) {
                va_start(ap, emsg);
                vlog(LOG_INFO, token, emsg, ap);
                va_end(ap);
@@ -228,7 +231,7 @@ log_debug(const char *token, const char *emsg, ...)
 {
        va_list  ap;
 
-       if ((debug > 2 && log_debug_accept_token(token)) || logh) {
+       if ((debug > 1 && log_debug_accept_token(token)) || logh) {
                va_start(ap, emsg);
                vlog(LOG_DEBUG, token, emsg, ap);
                va_end(ap);
index 9f8707ed8e25d5c279950cc89b2daddeedd659eb..09889d4f1882bc4222ceebe0a15a93bd2b4f2a86 100644 (file)
--- a/src/log.h
+++ b/src/log.h
@@ -19,7 +19,7 @@
 #define _LOG_H
 
 /* log.c */
-void             log_init(int, const char *);
+void             log_init(int, int, const char *);
 void             log_warn(const char *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
 void             log_warnx(const char *, const char *, ...) __attribute__ ((format (printf, 2, 3)));
 void             log_info(const char *, const char *, ...) __attribute__ ((format (printf, 2, 3)));