1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2024-12-24 21:34:08 +03:00

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.
This commit is contained in:
Filipe Brandenburger 2020-06-17 12:17:54 -07:00 committed by Lennart Poettering
parent a1ba8c5b71
commit 41d1f469cf
27 changed files with 45 additions and 73 deletions

View File

@ -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)

View File

@ -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();
}

View File

@ -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);

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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);

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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. */

View File

@ -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)

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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)

View File

@ -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);

View File

@ -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. */

View File

@ -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)

View File

@ -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)

View File

@ -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)