From 29bb23de3168f77244e4159eb0870a3814efa27c Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Sun, 16 Jan 2022 16:15:41 +0100 Subject: [PATCH] lib: autodetect systemd/journald log on stdout systemd sets up environment variables to allow autodetecting and switching the log format to journald native. Make use of that for the stdout logging target. Signed-off-by: David Lamparter --- lib/log_vty.c | 98 +++++++++++++++++++++++++++++++++++++-------------- lib/systemd.h | 4 +++ 2 files changed, 75 insertions(+), 27 deletions(-) diff --git a/lib/log_vty.c b/lib/log_vty.c index d50e2788ef..682c9ea372 100644 --- a/lib/log_vty.c +++ b/lib/log_vty.c @@ -27,6 +27,7 @@ #include "lib/zlog_5424.h" #include "lib/lib_errors.h" #include "lib/printfrr.h" +#include "lib/systemd.h" #ifndef VTYSH_EXTRACT_PL #include "lib/log_vty_clippy.c" @@ -52,15 +53,34 @@ static struct zlog_cfg_file zt_file_cmdline = { static struct zlog_cfg_file zt_file = { .prio_min = ZLOG_DISABLED, }; -static struct zlog_cfg_file zt_stdout = { - .prio_min = ZLOG_DISABLED, -}; static struct zlog_cfg_filterfile zt_filterfile = { .parent = { .prio_min = ZLOG_DISABLED, }, }; +static struct zlog_cfg_file zt_stdout_file = { + .prio_min = ZLOG_DISABLED, +}; +static struct zlog_cfg_5424 zt_stdout_journald = { + .prio_min = ZLOG_DISABLED, + + .fmt = ZLOG_FMT_JOURNALD, + .dst = ZLOG_5424_DST_UNIX, + .filename = "/run/systemd/journal/socket", + + /* this can't be changed through config since this target substitutes + * in for the "plain" stdout target + */ + .facility = LOG_DAEMON, + .kw_version = false, + .kw_location = true, + .kw_uid = true, + .kw_ec = true, + .kw_args = true, +}; +static bool stdout_journald_in_use; + const char *zlog_progname; static const char *zlog_protoname; @@ -163,14 +183,18 @@ DEFUN_NOSH (show_logging, SHOW_STR "Show current logging configuration\n") { + int stdout_prio; + log_show_syslog(vty); + stdout_prio = stdout_journald_in_use ? zt_stdout_journald.prio_min + : zt_stdout_file.prio_min; + vty_out(vty, "Stdout logging: "); - if (zt_stdout.prio_min == ZLOG_DISABLED) + if (stdout_prio == ZLOG_DISABLED) vty_out(vty, "disabled"); else - vty_out(vty, "level %s", - zlog_priority[zt_stdout.prio_min]); + vty_out(vty, "level %s", zlog_priority[stdout_prio]); vty_out(vty, "\n"); vty_out(vty, "File logging: "); @@ -210,6 +234,21 @@ DEFUN_NOSH (show_logging, return CMD_SUCCESS; } +static void log_stdout_apply_level(void) +{ + int maxlvl; + + maxlvl = ZLOG_MAXLVL(log_config_stdout_lvl, log_cmdline_stdout_lvl); + + if (stdout_journald_in_use) { + zt_stdout_journald.prio_min = maxlvl; + zlog_5424_apply_meta(&zt_stdout_journald); + } else { + zt_stdout_file.prio_min = maxlvl; + zlog_file_set_other(&zt_stdout_file); + } +} + DEFPY (config_log_stdout, config_log_stdout_cmd, "log stdout [$levelarg]", @@ -227,9 +266,7 @@ DEFPY (config_log_stdout, level = log_default_lvl; log_config_stdout_lvl = level; - zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, - log_cmdline_stdout_lvl); - zlog_file_set_other(&zt_stdout); + log_stdout_apply_level(); return CMD_SUCCESS; } @@ -242,9 +279,7 @@ DEFUN (no_config_log_stdout, LOG_LEVEL_DESC) { log_config_stdout_lvl = ZLOG_DISABLED; - zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, - log_cmdline_stdout_lvl); - zlog_file_set_other(&zt_stdout); + log_stdout_apply_level(); return CMD_SUCCESS; } @@ -378,9 +413,7 @@ void command_setup_early_logging(const char *dest, const char *level) if (strcmp(type, "stdout") == 0) { log_cmdline_stdout_lvl = nlevel; - zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, - log_cmdline_stdout_lvl); - zlog_file_set_other(&zt_stdout); + log_stdout_apply_level(); return; } if (strcmp(type, "syslog") == 0) { @@ -414,9 +447,7 @@ DEFUN (clear_log_cmdline, log_cmdline_syslog_lvl)); log_cmdline_stdout_lvl = ZLOG_DISABLED; - zt_stdout.prio_min = ZLOG_MAXLVL(log_config_stdout_lvl, - log_cmdline_stdout_lvl); - zlog_file_set_other(&zt_stdout); + log_stdout_apply_level(); return CMD_SUCCESS; } @@ -524,8 +555,10 @@ DEFUN (config_log_record_priority, { zt_file.record_priority = true; zlog_file_set_other(&zt_file); - zt_stdout.record_priority = true; - zlog_file_set_other(&zt_stdout); + if (!stdout_journald_in_use) { + zt_stdout_file.record_priority = true; + zlog_file_set_other(&zt_stdout_file); + } zt_filterfile.parent.record_priority = true; zlog_file_set_other(&zt_filterfile.parent); return CMD_SUCCESS; @@ -540,8 +573,10 @@ DEFUN (no_config_log_record_priority, { zt_file.record_priority = false; zlog_file_set_other(&zt_file); - zt_stdout.record_priority = false; - zlog_file_set_other(&zt_stdout); + if (!stdout_journald_in_use) { + zt_stdout_file.record_priority = false; + zlog_file_set_other(&zt_stdout_file); + } zt_filterfile.parent.record_priority = false; zlog_file_set_other(&zt_filterfile.parent); return CMD_SUCCESS; @@ -557,8 +592,10 @@ DEFPY (config_log_timestamp_precision, { zt_file.ts_subsec = precision; zlog_file_set_other(&zt_file); - zt_stdout.ts_subsec = precision; - zlog_file_set_other(&zt_stdout); + if (!stdout_journald_in_use) { + zt_stdout_file.ts_subsec = precision; + zlog_file_set_other(&zt_stdout_file); + } zt_filterfile.parent.ts_subsec = precision; zlog_file_set_other(&zt_filterfile.parent); return CMD_SUCCESS; @@ -575,8 +612,10 @@ DEFUN (no_config_log_timestamp_precision, { zt_file.ts_subsec = 0; zlog_file_set_other(&zt_file); - zt_stdout.ts_subsec = 0; - zlog_file_set_other(&zt_stdout); + if (!stdout_journald_in_use) { + zt_stdout_file.ts_subsec = 0; + zlog_file_set_other(&zt_stdout_file); + } zt_filterfile.parent.ts_subsec = 0; zlog_file_set_other(&zt_filterfile.parent); return CMD_SUCCESS; @@ -822,7 +861,12 @@ static int log_vty_init(const char *progname, const char *protoname, zlog_filterfile_init(&zt_filterfile); - zlog_file_set_fd(&zt_stdout, STDOUT_FILENO); + if (sd_stdout_is_journal) { + stdout_journald_in_use = true; + zlog_5424_init(&zt_stdout_journald); + zlog_5424_apply_dst(&zt_stdout_journald); + } else + zlog_file_set_fd(&zt_stdout_file, STDOUT_FILENO); return 0; } diff --git a/lib/systemd.h b/lib/systemd.h index 1933f4f688..b9290930d7 100644 --- a/lib/systemd.h +++ b/lib/systemd.h @@ -23,6 +23,10 @@ extern "C" { #endif +/* fd 1/2 connected to journald? */ +extern bool sd_stdout_is_journal; +extern bool sd_stderr_is_journal; + /* * Wrapper functions to systemd calls. *