diff options
author | Filipe Brandenburger <filbranden@gmail.com> | 2020-06-17 21:17:54 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2020-06-24 16:49:26 +0200 |
commit | 41d1f469cf10f5f3e9cb4f4853ace9b0cfe5beae (patch) | |
tree | 5a8bbe67cfafb46b99daf93f219699f93b968a95 | |
parent | feature to honor first shutdown request to completion (diff) | |
download | systemd-41d1f469cf10f5f3e9cb4f4853ace9b0cfe5beae.tar.xz systemd-41d1f469cf10f5f3e9cb4f4853ace9b0cfe5beae.zip |
log: introduce log_parse_environment_cli() and log_setup_cli()
Presently, CLI utilities such as systemctl will check whether they have a tty
attached or not to decide whether to parse /proc/cmdline or EFI variable
SystemdOptions looking for systemd.log_* entries.
But this check will be misleading if these tools are being launched by a
daemon, such as a monitoring daemon or automation service that runs in
background.
Make log handling of CLI tools uniform by never checking /proc/cmdline or EFI
variables to determine the logging level.
Furthermore, introduce a new log_setup_cli() shortcut to set up common options
used by most command-line utilities.
-rw-r--r-- | src/analyze/analyze.c | 4 | ||||
-rw-r--r-- | src/basic/log.c | 20 | ||||
-rw-r--r-- | src/basic/log.h | 4 | ||||
-rw-r--r-- | src/busctl/busctl.c | 4 | ||||
-rw-r--r-- | src/cgls/cgls.c | 4 | ||||
-rw-r--r-- | src/cgtop/cgtop.c | 4 | ||||
-rw-r--r-- | src/coredump/coredumpctl.c | 4 | ||||
-rw-r--r-- | src/delta/delta.c | 4 | ||||
-rw-r--r-- | src/detect-virt/detect-virt.c | 4 | ||||
-rw-r--r-- | src/escape/escape.c | 4 | ||||
-rw-r--r-- | src/home/homectl.c | 4 | ||||
-rw-r--r-- | src/hostname/hostnamectl.c | 4 | ||||
-rw-r--r-- | src/id128/id128.c | 4 | ||||
-rw-r--r-- | src/journal-remote/journal-remote-main.c | 2 | ||||
-rw-r--r-- | src/journal-remote/journal-upload.c | 2 | ||||
-rw-r--r-- | src/journal/cat.c | 4 | ||||
-rw-r--r-- | src/journal/journalctl.c | 4 | ||||
-rw-r--r-- | src/locale/localectl.c | 4 | ||||
-rw-r--r-- | src/login/loginctl.c | 4 | ||||
-rw-r--r-- | src/machine/machinectl.c | 4 | ||||
-rw-r--r-- | src/network/networkctl.c | 4 | ||||
-rw-r--r-- | src/portable/portablectl.c | 4 | ||||
-rw-r--r-- | src/resolve/resolvectl.c | 4 | ||||
-rw-r--r-- | src/systemctl/systemctl.c | 2 | ||||
-rw-r--r-- | src/test/test-chase-symlinks.c | 4 | ||||
-rw-r--r-- | src/timedate/timedatectl.c | 4 | ||||
-rw-r--r-- | src/userdb/userdbctl.c | 4 |
27 files changed, 45 insertions, 73 deletions
diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index faf50d2ac3..a8bd993cbe 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -2376,9 +2376,7 @@ static int run(int argc, char *argv[]) { setlocale(LC_ALL, ""); setlocale(LC_NUMERIC, "C"); /* we want to format/parse floats in C style */ - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/basic/log.c b/src/basic/log.c index 893181b23d..c6fe203808 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1146,16 +1146,20 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat } void log_parse_environment_realm(LogRealm realm) { - /* Do not call from library code. */ - - const char *e; - if (getpid_cached() == 1 || get_ctty_devnr(0, NULL) < 0) /* Only try to read the command line in daemons. We assume that anything that has a * controlling tty is user stuff. For PID1 we do a special check in case it hasn't * closed the console yet. */ (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); + log_parse_environment_cli_realm(realm); +} + +void log_parse_environment_cli_realm(LogRealm realm) { + /* Do not call from library code. */ + + const char *e; + e = getenv("SYSTEMD_LOG_TARGET"); if (e && log_set_target_from_string(e) < 0) log_warning("Failed to parse log target '%s'. Ignoring.", e); @@ -1430,3 +1434,11 @@ void log_setup_service(void) { log_parse_environment(); (void) log_open(); } + +void log_setup_cli(void) { + /* Sets up logging the way it is most appropriate for running a program as a CLI utility. */ + + log_show_color(true); + log_parse_environment_cli(); + (void) log_open(); +} diff --git a/src/basic/log.h b/src/basic/log.h index 2c1b00fb88..15807d3029 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -84,8 +84,11 @@ void log_close(void); void log_forget_fds(void); void log_parse_environment_realm(LogRealm realm); +void log_parse_environment_cli_realm(LogRealm realm); #define log_parse_environment() \ log_parse_environment_realm(LOG_REALM) +#define log_parse_environment_cli() \ + log_parse_environment_cli_realm(LOG_REALM) int log_dispatch_internal( int level, @@ -341,3 +344,4 @@ int log_syntax_invalid_utf8_internal( #define DEBUG_LOGGING _unlikely_(log_get_max_level() >= LOG_DEBUG) void log_setup_service(void); +void log_setup_cli(void); diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 8e000ee119..3ab90de63e 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -2598,9 +2598,7 @@ static int busctl_main(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index b55d7299ca..939a391e21 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -164,9 +164,7 @@ static void show_cg_info(const char *controller, const char *path) { static int run(int argc, char *argv[]) { int r, output_flags; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index de25aaae5d..e6c09d1b38 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -908,9 +908,7 @@ static int run(int argc, char *argv[]) { CGroupMask mask; int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index ed4d06e986..eceb7927af 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -1091,9 +1091,7 @@ static int run(int argc, char *argv[]) { int r, units_active; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/delta/delta.c b/src/delta/delta.c index 2d80d3a664..29e5120375 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -643,9 +643,7 @@ static int parse_argv(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r, k, n_found = 0; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/detect-virt/detect-virt.c b/src/detect-virt/detect-virt.c index 7fb80ca138..4f38de8e29 100644 --- a/src/detect-virt/detect-virt.c +++ b/src/detect-virt/detect-virt.c @@ -128,9 +128,7 @@ static int run(int argc, char *argv[]) { * to detect whether we are being run in a virtualized * environment or not */ - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/escape/escape.c b/src/escape/escape.c index 9066c30853..0c543a90f6 100644 --- a/src/escape/escape.c +++ b/src/escape/escape.c @@ -159,9 +159,7 @@ static int run(int argc, char *argv[]) { char **i; int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/home/homectl.c b/src/home/homectl.c index f7237d22d8..7a473b791b 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -3489,9 +3489,7 @@ static int run(int argc, char *argv[]) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 5596846ed2..e08ab15acd 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -436,9 +436,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/id128/id128.c b/src/id128/id128.c index 19435f80fe..13996573ab 100644 --- a/src/id128/id128.c +++ b/src/id128/id128.c @@ -249,9 +249,7 @@ static int id128_main(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/journal-remote/journal-remote-main.c b/src/journal-remote/journal-remote-main.c index 948b2d2fce..69a111afea 100644 --- a/src/journal-remote/journal-remote-main.c +++ b/src/journal-remote/journal-remote-main.c @@ -1110,7 +1110,7 @@ static int run(int argc, char **argv) { int r; log_show_color(true); - log_parse_environment(); + log_parse_environment_cli(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/journal-remote/journal-upload.c b/src/journal-remote/journal-upload.c index bd58fdbcdf..e169a27eb3 100644 --- a/src/journal-remote/journal-upload.c +++ b/src/journal-remote/journal-upload.c @@ -821,7 +821,7 @@ static int run(int argc, char **argv) { int r; log_show_color(true); - log_parse_environment(); + log_parse_environment_cli(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/journal/cat.c b/src/journal/cat.c index 500b674505..2faaa2e284 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -129,9 +129,7 @@ static int run(int argc, char *argv[]) { _cleanup_close_ int outfd = -1, errfd = -1, saved_stderr = -1; int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 859f4bbd44..6ba65a1071 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2115,9 +2115,7 @@ int main(int argc, char *argv[]) { int n_shown = 0, r, poll_fd = -1; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); /* Increase max number of open files if we can, we might needs this when browsing journal files, which might be * split up into many files. */ diff --git a/src/locale/localectl.c b/src/locale/localectl.c index b7548a0f7f..96fc09a570 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -502,9 +502,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 7805863d8d..9216224bfc 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -1460,9 +1460,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index d88b2651d2..d8f4526644 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -2882,9 +2882,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 8049912f43..8b7cf292fa 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -2818,9 +2818,7 @@ static void warn_networkd_missing(void) { static int run(int argc, char* argv[]) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/portable/portablectl.c b/src/portable/portablectl.c index 1bde30e90f..01614b6684 100644 --- a/src/portable/portablectl.c +++ b/src/portable/portablectl.c @@ -1119,9 +1119,7 @@ static int run(int argc, char *argv[]) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index 3cadac7639..a80b450bc0 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -3174,9 +3174,7 @@ static int run(int argc, char **argv) { int r; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); if (streq(program_invocation_short_name, "resolvconf")) r = resolvconf_parse_argv(argc, argv); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index ad7400bacc..681992b7e5 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -9207,7 +9207,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_parse_environment(); + log_parse_environment_cli(); log_open(); /* The journal merging logic potentially needs a lot of fds. */ diff --git a/src/test/test-chase-symlinks.c b/src/test/test-chase-symlinks.c index 0b7dd8764f..f7b3dd5e00 100644 --- a/src/test/test-chase-symlinks.c +++ b/src/test/test-chase-symlinks.c @@ -84,9 +84,7 @@ static int parse_argv(int argc, char *argv[]) { static int run(int argc, char **argv) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 3f34a91a7e..285eba802b 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -1052,9 +1052,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index 4b5eb34ca6..648eacd023 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -761,9 +761,7 @@ static int run(int argc, char *argv[]) { int r; - log_show_color(true); - log_parse_environment(); - log_open(); + log_setup_cli(); r = parse_argv(argc, argv); if (r <= 0) |