From 9856f2792c301116cc4a3fcfba91b9672ee5db1f Mon Sep 17 00:00:00 2001 From: Vincent Bernat Date: Sat, 28 Nov 2015 14:49:45 +0100 Subject: [PATCH] log: rework the way -d work 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 | 3 +++ src/client/lldpcli.8.in | 2 +- src/client/lldpcli.c | 24 +++++++++++++++++------- src/daemon/lldpd.8.in | 11 ++++++----- src/daemon/lldpd.c | 34 +++++++++++++++++++++++----------- src/lib/errors.c | 2 +- src/log.c | 17 ++++++++++------- src/log.h | 2 +- 8 files changed, 62 insertions(+), 33 deletions(-) diff --git a/NEWS b/NEWS index 49ba7c15..f0fb9d30 100644 --- 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). diff --git a/src/client/lldpcli.8.in b/src/client/lldpcli.8.in index 3abc00b1..49bcceab 100644 --- a/src/client/lldpcli.8.in +++ b/src/client/lldpcli.8.in @@ -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 . diff --git a/src/client/lldpcli.c b/src/client/lldpcli.c index 38e73541..e38256a0 100644 --- a/src/client/lldpcli.c +++ b/src/client/lldpcli.c @@ -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 */ diff --git a/src/daemon/lldpd.8.in b/src/daemon/lldpd.8.in index 56c7d2e4..9599cc6a 100644 --- a/src/daemon/lldpd.8.in +++ b/src/daemon/lldpd.8.in @@ -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 diff --git a/src/daemon/lldpd.c b/src/daemon/lldpd.c index be94f4f0..22de4f08 100644 --- a/src/daemon/lldpd.c +++ b/src/daemon/lldpd.c @@ -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"); diff --git a/src/lib/errors.c b/src/lib/errors.c index 0bd31f32..44fb5835 100644 --- a/src/lib/errors.c +++ b/src/lib/errors.c @@ -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); } diff --git a/src/log.c b/src/log.c index d1106fa8..332ce999 100644 --- a/src/log.c +++ b/src/log.c @@ -28,7 +28,9 @@ #include /* 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); diff --git a/src/log.h b/src/log.h index 9f8707ed..09889d4f 100644 --- 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))); -- 2.39.5