From 74189020eb2ec05336d93bdeeffbda1724ba1e07 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 17 Jan 2021 14:52:22 +0900 Subject: [PATCH 1/8] log: make tools also read the kernel command line when run as a service This effectively reverts 41d1f469cf10f5f3e9cb4f4853ace9b0cfe5beae. Before this, e.g., `networkctl reload` invoked by `systemctl reload systemd-networkd.service` does not produce debugging logs even if systemd.log-level=debug is set. This fixes the issue. --- src/basic/log.c | 14 +++++--------- src/basic/log.h | 1 - src/journal-remote/journal-remote-main.c | 2 +- src/journal-remote/journal-upload.c | 2 +- src/systemctl/systemctl.c | 2 +- 5 files changed, 8 insertions(+), 13 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index 59181910eb..4163f2e8d9 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1158,20 +1158,16 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat } void log_parse_environment(void) { + const char *e; + + /* Do not call from library code. */ + 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(); -} - -void log_parse_environment_cli(void) { - /* 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); @@ -1474,7 +1470,7 @@ void log_setup_cli(void) { /* Sets up logging the way it is most appropriate for running a program as a CLI utility. */ log_set_target(LOG_TARGET_AUTO); - log_parse_environment_cli(); + log_parse_environment(); (void) log_open(); if (log_on_console() && show_color < 0) log_show_color(true); diff --git a/src/basic/log.h b/src/basic/log.h index 9b106c57d8..9421fc8b49 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -69,7 +69,6 @@ void log_close(void); void log_forget_fds(void); void log_parse_environment(void); -void log_parse_environment_cli(void); int log_dispatch_internal( int level, diff --git a/src/journal-remote/journal-remote-main.c b/src/journal-remote/journal-remote-main.c index 685ce5a16b..972c13566f 100644 --- a/src/journal-remote/journal-remote-main.c +++ b/src/journal-remote/journal-remote-main.c @@ -1121,7 +1121,7 @@ static int run(int argc, char **argv) { int r; log_show_color(true); - log_parse_environment_cli(); + log_parse_environment(); /* 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 2d84bdcae5..455a6c942e 100644 --- a/src/journal-remote/journal-upload.c +++ b/src/journal-remote/journal-upload.c @@ -820,7 +820,7 @@ static int run(int argc, char **argv) { int r; log_show_color(true); - log_parse_environment_cli(); + log_parse_environment(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/systemctl/systemctl.c b/src/systemctl/systemctl.c index c53ed41cc3..e8e3d2438a 100644 --- a/src/systemctl/systemctl.c +++ b/src/systemctl/systemctl.c @@ -1085,7 +1085,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_parse_environment_cli(); + log_parse_environment(); log_open(); /* The journal merging logic potentially needs a lot of fds. */ From d2acb93dc52b23a9e5d36dda48c1988aa6e9adf1 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 17 Jan 2021 15:12:28 +0900 Subject: [PATCH 2/8] tree-wide: enable colorized logging for daemons when run in console It may be useful when debugging daemons. --- src/analyze/analyze.c | 2 +- src/backlight/backlight.c | 2 +- src/basic/log.c | 14 +------------- src/basic/log.h | 3 +-- src/binfmt/binfmt.c | 2 +- src/busctl/busctl.c | 2 +- src/cgls/cgls.c | 2 +- src/cgroups-agent/cgroups-agent.c | 2 +- src/cgtop/cgtop.c | 2 +- src/coredump/coredump.c | 2 +- src/coredump/coredumpctl.c | 2 +- src/cryptsetup/cryptsetup.c | 2 +- src/delta/delta.c | 2 +- src/detect-virt/detect-virt.c | 2 +- src/escape/escape.c | 2 +- src/firstboot/firstboot.c | 2 +- src/fsck/fsck.c | 2 +- src/hibernate-resume/hibernate-resume.c | 2 +- src/home/homectl.c | 2 +- src/home/homed.c | 2 +- src/home/homework.c | 2 +- src/hostname/hostnamectl.c | 2 +- src/hostname/hostnamed.c | 2 +- src/id128/id128.c | 2 +- src/import/importd.c | 2 +- src/initctl/initctl.c | 2 +- src/journal-remote/journal-gatewayd.c | 2 +- src/journal/cat.c | 2 +- src/journal/journalctl.c | 2 +- src/locale/localectl.c | 2 +- src/locale/localed.c | 2 +- src/login/loginctl.c | 2 +- src/login/logind.c | 2 +- src/machine/machinectl.c | 2 +- src/machine/machined.c | 2 +- src/modules-load/modules-load.c | 2 +- src/network/networkctl.c | 2 +- src/network/networkd.c | 2 +- src/network/wait-online/wait-online.c | 2 +- src/oom/oomd.c | 2 +- src/partition/growfs.c | 2 +- src/partition/makefs.c | 2 +- src/portable/portablectl.c | 2 +- src/portable/portabled.c | 2 +- src/pstore/pstore.c | 2 +- src/quotacheck/quotacheck.c | 2 +- src/random-seed/random-seed.c | 2 +- src/remount-fs/remount-fs.c | 2 +- src/reply-password/reply-password.c | 2 +- src/resolve/resolvectl.c | 2 +- src/resolve/resolved.c | 2 +- src/rfkill/rfkill.c | 2 +- src/sleep/sleep.c | 2 +- src/sulogin-shell/sulogin-shell.c | 2 +- src/sysctl/sysctl.c | 2 +- src/sysext/sysext.c | 2 +- src/sysusers/sysusers.c | 2 +- src/test/test-chase-symlinks.c | 2 +- src/timedate/timedatectl.c | 2 +- src/timedate/timedated.c | 2 +- src/timesync/timesyncd.c | 2 +- src/tmpfiles/tmpfiles.c | 2 +- .../tty-ask-password-agent.c | 2 +- src/update-done/update-done.c | 2 +- src/update-utmp/update-utmp.c | 2 +- src/user-sessions/user-sessions.c | 2 +- src/userdb/userdbctl.c | 2 +- src/userdb/userdbd.c | 2 +- src/userdb/userwork.c | 2 +- src/vconsole/vconsole-setup.c | 2 +- src/veritysetup/veritysetup.c | 2 +- src/volatile-root/volatile-root.c | 2 +- 72 files changed, 72 insertions(+), 85 deletions(-) diff --git a/src/analyze/analyze.c b/src/analyze/analyze.c index 31718afebd..3b953ac472 100644 --- a/src/analyze/analyze.c +++ b/src/analyze/analyze.c @@ -2446,7 +2446,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_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/backlight/backlight.c b/src/backlight/backlight.c index 717f0475ca..f3d53d6462 100644 --- a/src/backlight/backlight.c +++ b/src/backlight/backlight.c @@ -380,7 +380,7 @@ static int run(int argc, char *argv[]) { unsigned max_brightness, brightness; int r; - log_setup_service(); + log_setup(); if (strv_contains(strv_skip(argv, 1), "--help")) return help(); diff --git a/src/basic/log.c b/src/basic/log.c index 4163f2e8d9..f2cd1c3041 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1456,19 +1456,7 @@ int log_dup_console(void) { return 0; } -void log_setup_service(void) { - /* Sets up logging the way it is most appropriate for running a program as a service. Note that using this - * doesn't make the binary unsuitable for invocation on the command line, as log output will still go to the - * terminal if invoked interactively. */ - - log_set_target(LOG_TARGET_AUTO); - 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. */ - +void log_setup(void) { log_set_target(LOG_TARGET_AUTO); log_parse_environment(); (void) log_open(); diff --git a/src/basic/log.h b/src/basic/log.h index 9421fc8b49..60e67270db 100644 --- a/src/basic/log.h +++ b/src/basic/log.h @@ -300,5 +300,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); +void log_setup(void); diff --git a/src/binfmt/binfmt.c b/src/binfmt/binfmt.c index ecb9d90a78..f6b72e0bae 100644 --- a/src/binfmt/binfmt.c +++ b/src/binfmt/binfmt.c @@ -190,7 +190,7 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/busctl/busctl.c b/src/busctl/busctl.c index 02648957db..7774aeac7c 100644 --- a/src/busctl/busctl.c +++ b/src/busctl/busctl.c @@ -2565,7 +2565,7 @@ static int busctl_main(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/cgls/cgls.c b/src/cgls/cgls.c index 30fd743d11..6868049c18 100644 --- a/src/cgls/cgls.c +++ b/src/cgls/cgls.c @@ -163,7 +163,7 @@ static void show_cg_info(const char *controller, const char *path) { static int run(int argc, char *argv[]) { int r, output_flags; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/cgroups-agent/cgroups-agent.c b/src/cgroups-agent/cgroups-agent.c index eeb4ba80f5..071cba3099 100644 --- a/src/cgroups-agent/cgroups-agent.c +++ b/src/cgroups-agent/cgroups-agent.c @@ -22,7 +22,7 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - log_setup_service(); + log_setup(); fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC, 0); if (fd < 0) { diff --git a/src/cgtop/cgtop.c b/src/cgtop/cgtop.c index f63a7173e6..d0fa69ff88 100644 --- a/src/cgtop/cgtop.c +++ b/src/cgtop/cgtop.c @@ -915,7 +915,7 @@ static int run(int argc, char *argv[]) { CGroupMask mask; int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c index d546e1b8f6..bc8d34952b 100644 --- a/src/coredump/coredump.c +++ b/src/coredump/coredump.c @@ -861,7 +861,7 @@ static int process_socket(int fd) { assert(fd >= 0); - log_setup_service(); + log_setup(); log_debug("Processing coredump received on stdin..."); diff --git a/src/coredump/coredumpctl.c b/src/coredump/coredumpctl.c index 20bd97c1fc..55283a93ae 100644 --- a/src/coredump/coredumpctl.c +++ b/src/coredump/coredumpctl.c @@ -1108,7 +1108,7 @@ static int run(int argc, char *argv[]) { int r, units_active; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/cryptsetup/cryptsetup.c b/src/cryptsetup/cryptsetup.c index 9cef31398f..2df7633dac 100644 --- a/src/cryptsetup/cryptsetup.c +++ b/src/cryptsetup/cryptsetup.c @@ -1395,7 +1395,7 @@ static int run(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires at least two arguments."); - log_setup_service(); + log_setup(); cryptsetup_enable_logging(cd); diff --git a/src/delta/delta.c b/src/delta/delta.c index a50cb36ee3..21be1b8f54 100644 --- a/src/delta/delta.c +++ b/src/delta/delta.c @@ -643,7 +643,7 @@ static int parse_argv(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r, k, n_found = 0; - log_setup_cli(); + log_setup(); 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 be0db85d7c..bfdede6ce7 100644 --- a/src/detect-virt/detect-virt.c +++ b/src/detect-virt/detect-virt.c @@ -127,7 +127,7 @@ static int run(int argc, char *argv[]) { * to detect whether we are being run in a virtualized * environment or not */ - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/escape/escape.c b/src/escape/escape.c index 24c53bf14b..a2fdce790e 100644 --- a/src/escape/escape.c +++ b/src/escape/escape.c @@ -158,7 +158,7 @@ static int run(int argc, char *argv[]) { char **i; int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/firstboot/firstboot.c b/src/firstboot/firstboot.c index 6982cf2eed..904612ab4a 100644 --- a/src/firstboot/firstboot.c +++ b/src/firstboot/firstboot.c @@ -1271,7 +1271,7 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/fsck/fsck.c b/src/fsck/fsck.c index 04752fe9dc..cd012f0f3a 100644 --- a/src/fsck/fsck.c +++ b/src/fsck/fsck.c @@ -256,7 +256,7 @@ static int run(int argc, char *argv[]) { int r, exit_status; pid_t pid; - log_setup_service(); + log_setup(); if (argc > 2) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), diff --git a/src/hibernate-resume/hibernate-resume.c b/src/hibernate-resume/hibernate-resume.c index d8f91f4e66..58e35e403e 100644 --- a/src/hibernate-resume/hibernate-resume.c +++ b/src/hibernate-resume/hibernate-resume.c @@ -20,7 +20,7 @@ int main(int argc, char *argv[]) { return EXIT_FAILURE; } - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/home/homectl.c b/src/home/homectl.c index 0b41489897..176f041114 100644 --- a/src/home/homectl.c +++ b/src/home/homectl.c @@ -3346,7 +3346,7 @@ static int run(int argc, char *argv[]) { int r; - log_setup_cli(); + log_setup(); r = redirect_bus_mgr(); if (r < 0) diff --git a/src/home/homed.c b/src/home/homed.c index e4d64bdfda..807d25e273 100644 --- a/src/home/homed.c +++ b/src/home/homed.c @@ -17,7 +17,7 @@ static int run(int argc, char *argv[]) { _cleanup_(notify_on_cleanup) const char *notify_stop = NULL; int r; - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-homed.service", "A service to create, remove, change or inspect home areas.", diff --git a/src/home/homework.c b/src/home/homework.c index 35f3206bb0..bb5a774f81 100644 --- a/src/home/homework.c +++ b/src/home/homework.c @@ -1633,7 +1633,7 @@ static int run(int argc, char *argv[]) { start = now(CLOCK_MONOTONIC); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/hostname/hostnamectl.c b/src/hostname/hostnamectl.c index 4482608b28..62da90b121 100644 --- a/src/hostname/hostnamectl.c +++ b/src/hostname/hostnamectl.c @@ -443,7 +443,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/hostname/hostnamed.c b/src/hostname/hostnamed.c index 35c2c37f92..0e4dd02b49 100644 --- a/src/hostname/hostnamed.c +++ b/src/hostname/hostnamed.c @@ -1064,7 +1064,7 @@ static int run(int argc, char *argv[]) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; int r; - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-hostnamed.service", "Manage the system hostname and related metadata.", diff --git a/src/id128/id128.c b/src/id128/id128.c index 70ea4d0a69..435a56d4f3 100644 --- a/src/id128/id128.c +++ b/src/id128/id128.c @@ -249,7 +249,7 @@ static int id128_main(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/import/importd.c b/src/import/importd.c index 15914cc885..345fc7c2cb 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -1361,7 +1361,7 @@ static int run(int argc, char *argv[]) { _cleanup_(manager_unrefp) Manager *m = NULL; int r; - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-importd.service", "VM and container image import and export service.", diff --git a/src/initctl/initctl.c b/src/initctl/initctl.c index e0b78334f5..d818f452f7 100644 --- a/src/initctl/initctl.c +++ b/src/initctl/initctl.c @@ -317,7 +317,7 @@ static int run(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program does not take arguments."); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/journal-remote/journal-gatewayd.c b/src/journal-remote/journal-gatewayd.c index 17216c8e20..4ece66b51b 100644 --- a/src/journal-remote/journal-gatewayd.c +++ b/src/journal-remote/journal-gatewayd.c @@ -995,7 +995,7 @@ static int run(int argc, char *argv[]) { MHD_USE_THREAD_PER_CONNECTION; int r, n; - log_setup_service(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/journal/cat.c b/src/journal/cat.c index 223af8da1e..6599e64296 100644 --- a/src/journal/cat.c +++ b/src/journal/cat.c @@ -129,7 +129,7 @@ static int run(int argc, char *argv[]) { _cleanup_close_ int outfd = -1, errfd = -1, saved_stderr = -1; int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index dee0a0835b..94fc3472be 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -2145,7 +2145,7 @@ int main(int argc, char *argv[]) { int n_shown = 0, r, poll_fd = -1; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); /* 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 692a259275..c267db9edd 100644 --- a/src/locale/localectl.c +++ b/src/locale/localectl.c @@ -506,7 +506,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/locale/localed.c b/src/locale/localed.c index 12073bd6f3..97d6592cb8 100644 --- a/src/locale/localed.c +++ b/src/locale/localed.c @@ -830,7 +830,7 @@ static int run(int argc, char *argv[]) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; int r; - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-localed.service", "Manage system locale settings and key mappings.", diff --git a/src/login/loginctl.c b/src/login/loginctl.c index 995bcbff76..f61f9eca04 100644 --- a/src/login/loginctl.c +++ b/src/login/loginctl.c @@ -1464,7 +1464,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/login/logind.c b/src/login/logind.c index 3ddc7a074a..3dae695554 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -1160,7 +1160,7 @@ static int run(int argc, char *argv[]) { int r; log_set_facility(LOG_AUTH); - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-logind.service", "Manager for user logins and devices and privileged operations.", diff --git a/src/machine/machinectl.c b/src/machine/machinectl.c index e254e25559..1771abc077 100644 --- a/src/machine/machinectl.c +++ b/src/machine/machinectl.c @@ -2876,7 +2876,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); /* The journal merging logic potentially needs a lot of fds. */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/machine/machined.c b/src/machine/machined.c index 6a5bf391e6..e9e6a6270a 100644 --- a/src/machine/machined.c +++ b/src/machine/machined.c @@ -324,7 +324,7 @@ static int run(int argc, char *argv[]) { int r; log_set_facility(LOG_AUTH); - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-machined.service", "Manage registrations of local VMs and containers.", diff --git a/src/modules-load/modules-load.c b/src/modules-load/modules-load.c index ba690f52ca..9cfd292011 100644 --- a/src/modules-load/modules-load.c +++ b/src/modules-load/modules-load.c @@ -165,7 +165,7 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/network/networkctl.c b/src/network/networkctl.c index 3b9a7d6485..4381f63a2d 100644 --- a/src/network/networkctl.c +++ b/src/network/networkctl.c @@ -2822,7 +2822,7 @@ static void warn_networkd_missing(void) { static int run(int argc, char* argv[]) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/network/networkd.c b/src/network/networkd.c index ac2bc90d97..24b3cb57af 100644 --- a/src/network/networkd.c +++ b/src/network/networkd.c @@ -22,7 +22,7 @@ static int run(int argc, char *argv[]) { _cleanup_(notify_on_cleanup) const char *notify_message = NULL; int r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/network/wait-online/wait-online.c b/src/network/wait-online/wait-online.c index 2de3e79c3c..ca0116e7f3 100644 --- a/src/network/wait-online/wait-online.c +++ b/src/network/wait-online/wait-online.c @@ -184,7 +184,7 @@ static int run(int argc, char *argv[]) { _cleanup_(notify_on_cleanup) const char *notify_message = NULL; int r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/oom/oomd.c b/src/oom/oomd.c index 3f3ef944ee..28e059fbde 100644 --- a/src/oom/oomd.c +++ b/src/oom/oomd.c @@ -122,7 +122,7 @@ static int run(int argc, char *argv[]) { unsigned long long s = 0; int r; - log_setup_service(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/partition/growfs.c b/src/partition/growfs.c index 367644e3c9..15c56d0584 100644 --- a/src/partition/growfs.c +++ b/src/partition/growfs.c @@ -201,7 +201,7 @@ static int run(int argc, char *argv[]) { dev_t devno; int r; - log_setup_service(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/partition/makefs.c b/src/partition/makefs.c index fd924d2231..7c94fbfedb 100644 --- a/src/partition/makefs.c +++ b/src/partition/makefs.c @@ -24,7 +24,7 @@ static int run(int argc, char *argv[]) { struct stat st; int r; - log_setup_service(); + log_setup(); if (argc != 3) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), diff --git a/src/portable/portablectl.c b/src/portable/portablectl.c index 0b329134de..e9c4fae59b 100644 --- a/src/portable/portablectl.c +++ b/src/portable/portablectl.c @@ -1113,7 +1113,7 @@ static int run(int argc, char *argv[]) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/portable/portabled.c b/src/portable/portabled.c index f008f84e53..8a17f09365 100644 --- a/src/portable/portabled.c +++ b/src/portable/portabled.c @@ -135,7 +135,7 @@ static int run(int argc, char *argv[]) { _cleanup_(manager_unrefp) Manager *m = NULL; int r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/pstore/pstore.c b/src/pstore/pstore.c index db8a71fbab..c642a3252c 100644 --- a/src/pstore/pstore.c +++ b/src/pstore/pstore.c @@ -367,7 +367,7 @@ static int run(int argc, char *argv[]) { _cleanup_(pstore_entries_reset) PStoreList list = {}; int r; - log_setup_service(); + log_setup(); if (argc == 3) { arg_sourcedir = argv[1]; diff --git a/src/quotacheck/quotacheck.c b/src/quotacheck/quotacheck.c index d617b0bf3d..575965c470 100644 --- a/src/quotacheck/quotacheck.c +++ b/src/quotacheck/quotacheck.c @@ -58,7 +58,7 @@ static void test_files(void) { static int run(int argc, char *argv[]) { int r; - log_setup_service(); + log_setup(); if (argc > 1) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), diff --git a/src/random-seed/random-seed.c b/src/random-seed/random-seed.c index 8f8766cdd7..4caf967807 100644 --- a/src/random-seed/random-seed.c +++ b/src/random-seed/random-seed.c @@ -110,7 +110,7 @@ static int run(int argc, char *argv[]) { ssize_t k; int r; - log_setup_service(); + log_setup(); if (argc != 2) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), diff --git a/src/remount-fs/remount-fs.c b/src/remount-fs/remount-fs.c index 9668a49dc4..d747d60d5d 100644 --- a/src/remount-fs/remount-fs.c +++ b/src/remount-fs/remount-fs.c @@ -77,7 +77,7 @@ static int run(int argc, char *argv[]) { struct mntent* me; int r; - log_setup_service(); + log_setup(); if (argc > 1) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), diff --git a/src/reply-password/reply-password.c b/src/reply-password/reply-password.c index a73334e2b8..a9aa4b832a 100644 --- a/src/reply-password/reply-password.c +++ b/src/reply-password/reply-password.c @@ -39,7 +39,7 @@ static int run(int argc, char *argv[]) { size_t length = 0; int r; - log_setup_service(); + log_setup(); if (argc != 3) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Wrong number of arguments."); diff --git a/src/resolve/resolvectl.c b/src/resolve/resolvectl.c index b1779a6418..318528b419 100644 --- a/src/resolve/resolvectl.c +++ b/src/resolve/resolvectl.c @@ -3296,7 +3296,7 @@ static int run(int argc, char **argv) { int r; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); if (streq(program_invocation_short_name, "resolvconf")) r = resolvconf_parse_argv(argc, argv); diff --git a/src/resolve/resolved.c b/src/resolve/resolved.c index 4c42b6591d..aabaa266e9 100644 --- a/src/resolve/resolved.c +++ b/src/resolve/resolved.c @@ -26,7 +26,7 @@ static int run(int argc, char *argv[]) { _cleanup_(notify_on_cleanup) const char *notify_stop = NULL; int r; - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-resolved.service", "Provide name resolution with caching using DNS, mDNS, LLMNR.", diff --git a/src/rfkill/rfkill.c b/src/rfkill/rfkill.c index c38716842b..91dc72f425 100644 --- a/src/rfkill/rfkill.c +++ b/src/rfkill/rfkill.c @@ -276,7 +276,7 @@ static int run(int argc, char *argv[]) { if (argc > 1) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires no arguments."); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/sleep/sleep.c b/src/sleep/sleep.c index 297cc1fec6..262d4cea66 100644 --- a/src/sleep/sleep.c +++ b/src/sleep/sleep.c @@ -368,7 +368,7 @@ static int run(int argc, char *argv[]) { _cleanup_(free_sleep_configp) SleepConfig *sleep_config = NULL; int r; - log_setup_service(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/sulogin-shell/sulogin-shell.c b/src/sulogin-shell/sulogin-shell.c index b0d71ff580..0207e85b7e 100644 --- a/src/sulogin-shell/sulogin-shell.c +++ b/src/sulogin-shell/sulogin-shell.c @@ -100,7 +100,7 @@ int main(int argc, char *argv[]) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; int r; - log_setup_service(); + log_setup(); print_mode(argc > 1 ? argv[1] : ""); diff --git a/src/sysctl/sysctl.c b/src/sysctl/sysctl.c index 6d20ee1a84..1b433f7045 100644 --- a/src/sysctl/sysctl.c +++ b/src/sysctl/sysctl.c @@ -386,7 +386,7 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c index 601fd63a14..bfb9631bc8 100644 --- a/src/sysext/sysext.c +++ b/src/sysext/sysext.c @@ -1027,7 +1027,7 @@ static int sysext_main(int argc, char *argv[]) { static int run(int argc, char *argv[]) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/sysusers/sysusers.c b/src/sysusers/sysusers.c index 25a1871bd4..969896c0b5 100644 --- a/src/sysusers/sysusers.c +++ b/src/sysusers/sysusers.c @@ -1928,7 +1928,7 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - log_setup_service(); + log_setup(); if (arg_cat_config) return cat_config(); diff --git a/src/test/test-chase-symlinks.c b/src/test/test-chase-symlinks.c index d9b9b62dc5..892f8a073f 100644 --- a/src/test/test-chase-symlinks.c +++ b/src/test/test-chase-symlinks.c @@ -84,7 +84,7 @@ static int parse_argv(int argc, char *argv[]) { static int run(int argc, char **argv) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 7088a771a6..fb08f9ad26 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -1051,7 +1051,7 @@ static int run(int argc, char *argv[]) { int r; setlocale(LC_ALL, ""); - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/timedate/timedated.c b/src/timedate/timedated.c index 8149facb34..563f470f54 100644 --- a/src/timedate/timedated.c +++ b/src/timedate/timedated.c @@ -1109,7 +1109,7 @@ static int run(int argc, char *argv[]) { _cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; int r; - log_setup_service(); + log_setup(); r = service_parse_argv("systemd-timedated.service", "Manage the system clock and timezone and NTP enablement.", diff --git a/src/timesync/timesyncd.c b/src/timesync/timesyncd.c index 1f59bf14e1..e6a2b06687 100644 --- a/src/timesync/timesyncd.c +++ b/src/timesync/timesyncd.c @@ -97,7 +97,7 @@ static int run(int argc, char *argv[]) { int r; log_set_facility(LOG_CRON); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index b033def9df..8ad886236d 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -3363,7 +3363,7 @@ static int run(int argc, char *argv[]) { if (r <= 0) return r; - log_setup_service(); + log_setup(); /* Descending down file system trees might take a lot of fds */ (void) rlimit_nofile_bump(HIGH_RLIMIT_NOFILE); diff --git a/src/tty-ask-password-agent/tty-ask-password-agent.c b/src/tty-ask-password-agent/tty-ask-password-agent.c index 5695327024..ee66a3c312 100644 --- a/src/tty-ask-password-agent/tty-ask-password-agent.c +++ b/src/tty-ask-password-agent/tty-ask-password-agent.c @@ -684,7 +684,7 @@ static int ask_on_consoles(char *argv[]) { static int run(int argc, char *argv[]) { int r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/update-done/update-done.c b/src/update-done/update-done.c index 212892527e..6518830717 100644 --- a/src/update-done/update-done.c +++ b/src/update-done/update-done.c @@ -41,7 +41,7 @@ int main(int argc, char *argv[]) { struct stat st; int r, q = 0; - log_setup_service(); + log_setup(); if (stat("/usr", &st) < 0) { log_error_errno(errno, "Failed to stat /usr: %m"); diff --git a/src/update-utmp/update-utmp.c b/src/update-utmp/update-utmp.c index 59c49f2a80..1ec9066d34 100644 --- a/src/update-utmp/update-utmp.c +++ b/src/update-utmp/update-utmp.c @@ -224,7 +224,7 @@ static int run(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires one argument."); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/user-sessions/user-sessions.c b/src/user-sessions/user-sessions.c index d1b8176714..6b7493fd88 100644 --- a/src/user-sessions/user-sessions.c +++ b/src/user-sessions/user-sessions.c @@ -21,7 +21,7 @@ static int run(int argc, char *argv[]) { return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires one argument."); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/userdb/userdbctl.c b/src/userdb/userdbctl.c index ecaead39d7..5dc1cf8140 100644 --- a/src/userdb/userdbctl.c +++ b/src/userdb/userdbctl.c @@ -763,7 +763,7 @@ static int run(int argc, char *argv[]) { int r; - log_setup_cli(); + log_setup(); r = parse_argv(argc, argv); if (r <= 0) diff --git a/src/userdb/userdbd.c b/src/userdb/userdbd.c index 6f2c807843..6b28dd0a08 100644 --- a/src/userdb/userdbd.c +++ b/src/userdb/userdbd.c @@ -24,7 +24,7 @@ static int run(int argc, char *argv[]) { _cleanup_(notify_on_cleanup) const char *notify_stop = NULL; int r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/userdb/userwork.c b/src/userdb/userwork.c index d525a6e05a..c62a21974a 100644 --- a/src/userdb/userwork.c +++ b/src/userdb/userwork.c @@ -662,7 +662,7 @@ static int run(int argc, char *argv[]) { unsigned n_iterations = 0; int m, listen_fd, r; - log_setup_service(); + log_setup(); m = sd_listen_fds(false); if (m < 0) diff --git a/src/vconsole/vconsole-setup.c b/src/vconsole/vconsole-setup.c index b28e2853e1..45915ef853 100644 --- a/src/vconsole/vconsole-setup.c +++ b/src/vconsole/vconsole-setup.c @@ -430,7 +430,7 @@ int main(int argc, char **argv) { unsigned idx = 0; int r; - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/veritysetup/veritysetup.c b/src/veritysetup/veritysetup.c index 9ecfbc5217..2b54065437 100644 --- a/src/veritysetup/veritysetup.c +++ b/src/veritysetup/veritysetup.c @@ -136,7 +136,7 @@ static int run(int argc, char *argv[]) { if (argc < 3) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "This program requires at least two arguments."); - log_setup_service(); + log_setup(); umask(0022); diff --git a/src/volatile-root/volatile-root.c b/src/volatile-root/volatile-root.c index ee3532cc02..242ef81ecf 100644 --- a/src/volatile-root/volatile-root.c +++ b/src/volatile-root/volatile-root.c @@ -119,7 +119,7 @@ static int run(int argc, char *argv[]) { dev_t devt; int r; - log_setup_service(); + log_setup(); if (argc > 3) return log_error_errno(SYNTHETIC_ERRNO(EINVAL), From dc4e2940e87f4f0969476b45a1d25322b5210643 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 15:56:42 +0900 Subject: [PATCH 3/8] core: set $SYSTEMD_EXEC_PID= environment variable for executed commands It may be useful to detect a command is directly executed by systemd manager, or indirectly as a child of another process. --- man/systemd.exec.xml | 11 +++++++++++ src/core/execute.c | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/man/systemd.exec.xml b/man/systemd.exec.xml index 203fc0355d..5b5590a233 100644 --- a/man/systemd.exec.xml +++ b/man/systemd.exec.xml @@ -3043,6 +3043,17 @@ StandardInputData=SWNrIHNpdHplIGRhIHVuJyBlc3NlIEtsb3BzLAp1ZmYgZWVtYWwga2xvcHAncy + + $SYSTEMD_EXEC_PID + + The PID of the unit process (e.g. process invoked by + ExecStart=). The child process can use this information to determine + whether the process is directly invoked by the service manager or indirectly as a child of + another process by comparing this value with the current PID (as similar to the scheme used in + sd_listen_fds3 + with $LISTEN_PID and $LISTEN_FDS). + + $TERM diff --git a/src/core/execute.c b/src/core/execute.c index b7d78f2197..29fe9f05b1 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -1794,7 +1794,7 @@ static int build_environment( assert(p); assert(ret); -#define N_ENV_VARS 16 +#define N_ENV_VARS 17 our_env = new0(char*, N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX); if (!our_env) return -ENOMEM; @@ -1950,6 +1950,11 @@ static int build_environment( our_env[n_env++] = x; } + if (asprintf(&x, "SYSTEMD_EXEC_PID=" PID_FMT, getpid_cached()) < 0) + return -ENOMEM; + + our_env[n_env++] = x; + our_env[n_env++] = NULL; assert(n_env <= N_ENV_VARS + _EXEC_DIRECTORY_TYPE_MAX); #undef N_ENV_VARS From 2824374f952ef23d82403ee33b8604cac907903f Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 31 Jan 2021 23:32:05 +0900 Subject: [PATCH 4/8] test: use test_setup_logging() in test-env-util --- src/test/test-env-util.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c index f77b1cdbcb..755aa069de 100644 --- a/src/test/test-env-util.c +++ b/src/test/test-env-util.c @@ -7,6 +7,7 @@ #include "serialize.h" #include "string-util.h" #include "strv.h" +#include "tests.h" #include "util.h" static void test_strv_env_delete(void) { @@ -335,6 +336,8 @@ static void test_env_assignment_is_valid(void) { } int main(int argc, char *argv[]) { + test_setup_logging(LOG_DEBUG); + test_strv_env_delete(); test_strv_env_get(); test_strv_env_unset(); From cfd1c6e26280f27454a0759853befee08382bf7d Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 31 Jan 2021 23:33:06 +0900 Subject: [PATCH 5/8] env-util: introduce env_update_systemd_exec_pid() Will be used in later commits. --- src/basic/env-util.c | 23 +++++++++++++++++++++++ src/basic/env-util.h | 2 ++ src/test/test-env-util.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+) diff --git a/src/basic/env-util.c b/src/basic/env-util.c index 96c024afd7..a75186015e 100644 --- a/src/basic/env-util.c +++ b/src/basic/env-util.c @@ -12,6 +12,8 @@ #include "extract-word.h" #include "macro.h" #include "parse-util.h" +#include "process-util.h" +#include "stdio-util.h" #include "string-util.h" #include "strv.h" #include "utf8.h" @@ -749,3 +751,24 @@ int set_unset_env(const char *name, const char *value, bool overwrite) { return -errno; return 0; } + +int setenv_systemd_exec_pid(bool update_only) { + char str[DECIMAL_STR_MAX(pid_t)]; + const char *e; + + /* Update $SYSTEMD_EXEC_PID=pid except when '*' is set for the variable. */ + + e = secure_getenv("SYSTEMD_EXEC_PID"); + if (!e && update_only) + return 0; + + if (streq_ptr(e, "*")) + return 0; + + xsprintf(str, PID_FMT, getpid_cached()); + + if (setenv("SYSTEMD_EXEC_PID", str, 1) < 0) + return -errno; + + return 1; +} diff --git a/src/basic/env-util.h b/src/basic/env-util.h index 6684b3350f..5d8afec028 100644 --- a/src/basic/env-util.h +++ b/src/basic/env-util.h @@ -55,3 +55,5 @@ int getenv_bool_secure(const char *p); /* Like setenv, but calls unsetenv if value == NULL. */ int set_unset_env(const char *name, const char *value, bool overwrite); + +int setenv_systemd_exec_pid(bool update_only); diff --git a/src/test/test-env-util.c b/src/test/test-env-util.c index 755aa069de..ce215fdd36 100644 --- a/src/test/test-env-util.c +++ b/src/test/test-env-util.c @@ -4,6 +4,8 @@ #include "fd-util.h" #include "fileio.h" #include "fs-util.h" +#include "parse-util.h" +#include "process-util.h" #include "serialize.h" #include "string-util.h" #include "strv.h" @@ -335,6 +337,41 @@ static void test_env_assignment_is_valid(void) { assert_se(!env_assignment_is_valid("głąb=printf \"\x1b]0;\x07\"")); } +static void test_setenv_systemd_exec_pid(void) { + _cleanup_free_ char *saved = NULL; + const char *e; + pid_t p; + + log_info("/* %s */", __func__); + + e = getenv("SYSTEMD_EXEC_PID"); + if (e) + assert_se(saved = strdup(e)); + + assert_se(unsetenv("SYSTEMD_EXEC_PID") >= 0); + assert_se(setenv_systemd_exec_pid(true) == 0); + assert_se(!getenv("SYSTEMD_EXEC_PID")); + + assert_se(setenv("SYSTEMD_EXEC_PID", "*", 1) >= 0); + assert_se(setenv_systemd_exec_pid(true) == 0); + assert_se(e = getenv("SYSTEMD_EXEC_PID")); + assert_se(streq(e, "*")); + + assert_se(setenv("SYSTEMD_EXEC_PID", "123abc", 1) >= 0); + assert_se(setenv_systemd_exec_pid(true) == 1); + assert_se(e = getenv("SYSTEMD_EXEC_PID")); + assert_se(parse_pid(e, &p) >= 0); + assert_se(p == getpid_cached()); + + assert_se(unsetenv("SYSTEMD_EXEC_PID") >= 0); + assert_se(setenv_systemd_exec_pid(false) == 1); + assert_se(e = getenv("SYSTEMD_EXEC_PID")); + assert_se(parse_pid(e, &p) >= 0); + assert_se(p == getpid_cached()); + + assert_se(set_unset_env("SYSTEMD_EXEC_PID", saved, 1) >= 0); +} + int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); @@ -353,6 +390,7 @@ int main(int argc, char *argv[]) { test_env_name_is_valid(); test_env_value_is_valid(); test_env_assignment_is_valid(); + test_setenv_systemd_exec_pid(); return 0; } From 43f565c6770f940a978a5325fea8b0e6d8ee4c99 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 1 Feb 2021 00:51:33 +0900 Subject: [PATCH 6/8] core: also set $SYSTEMD_EXEC_PID= for generators --- src/core/manager.c | 6 ++++-- src/shared/exec-util.c | 10 ++++++++-- src/shared/exec-util.h | 7 ++++--- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/core/manager.c b/src/core/manager.c index a1d6f7cc10..73a850e5da 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -4105,7 +4105,8 @@ static int manager_run_environment_generators(Manager *m) { RUN_WITH_UMASK(0022) r = execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, gather_environment, - args, NULL, m->transient_environment, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS); + args, NULL, m->transient_environment, + EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID); return r; } @@ -4140,7 +4141,8 @@ static int manager_run_generators(Manager *m) { RUN_WITH_UMASK(0022) (void) execute_directories((const char* const*) paths, DEFAULT_TIMEOUT_USEC, NULL, NULL, - (char**) argv, m->transient_environment, EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS); + (char**) argv, m->transient_environment, + EXEC_DIR_PARALLEL | EXEC_DIR_IGNORE_ERRORS | EXEC_DIR_SET_SYSTEMD_EXEC_PID); r = 0; diff --git a/src/shared/exec-util.c b/src/shared/exec-util.c index bdea60ca02..6355ce8ce4 100644 --- a/src/shared/exec-util.c +++ b/src/shared/exec-util.c @@ -34,7 +34,7 @@ /* Put this test here for a lack of better place */ assert_cc(EAGAIN == EWOULDBLOCK); -static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) { +static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid, bool set_systemd_exec_pid) { pid_t _pid; int r; @@ -57,6 +57,12 @@ static int do_spawn(const char *path, char *argv[], int stdout_fd, pid_t *pid) { (void) rlimit_nofile_safe(); + if (set_systemd_exec_pid) { + r = setenv_systemd_exec_pid(false); + if (r < 0) + log_warning_errno(r, "Failed to set $SYSTEMD_EXEC_PID, ignoring: %m"); + } + if (!argv) { _argv[0] = (char*) path; _argv[1] = NULL; @@ -132,7 +138,7 @@ static int do_execute( return log_error_errno(fd, "Failed to open serialization file: %m"); } - r = do_spawn(t, argv, fd, &pid); + r = do_spawn(t, argv, fd, &pid, FLAGS_SET(flags, EXEC_DIR_SET_SYSTEMD_EXEC_PID)); if (r <= 0) continue; diff --git a/src/shared/exec-util.h b/src/shared/exec-util.h index df6214e4c3..5bd4a3785f 100644 --- a/src/shared/exec-util.h +++ b/src/shared/exec-util.h @@ -15,9 +15,10 @@ enum { }; typedef enum { - EXEC_DIR_NONE = 0, /* No execdir flags */ - EXEC_DIR_PARALLEL = 1 << 0, /* Execute scripts in parallel, if possible */ - EXEC_DIR_IGNORE_ERRORS = 1 << 1, /* Ignore non-zero exit status of scripts */ + EXEC_DIR_NONE = 0, /* No execdir flags */ + EXEC_DIR_PARALLEL = 1 << 0, /* Execute scripts in parallel, if possible */ + EXEC_DIR_IGNORE_ERRORS = 1 << 1, /* Ignore non-zero exit status of scripts */ + EXEC_DIR_SET_SYSTEMD_EXEC_PID = 1 << 2, /* Set $SYSTEMD_EXEC_PID environment variable */ } ExecDirFlags; typedef enum ExecCommandFlags { From d754890843db3c2784b4299fc9b9b6968e2ad533 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 1 Feb 2021 00:52:59 +0900 Subject: [PATCH 7/8] import,home: update $SYSTEMD_EXEC_PID= if it is set The subsequent execv() or execl() always calls tools provided by systemd. So, it is safe to update the variable. --- src/home/homed-home.c | 4 ++++ src/import/importd.c | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/src/home/homed-home.c b/src/home/homed-home.c index 38b7e20ca6..2ea0f7c81d 100644 --- a/src/home/homed-home.c +++ b/src/home/homed-home.c @@ -1038,6 +1038,10 @@ static int home_start_work(Home *h, const char *verb, UserRecord *hr, UserRecord _exit(EXIT_FAILURE); } + r = setenv_systemd_exec_pid(true); + if (r < 0) + log_warning_errno(r, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m"); + r = rearrange_stdio(stdin_fd, stdout_fd, STDERR_FILENO); if (r < 0) { log_error_errno(r, "Failed to rearrange stdin/stdout/stderr: %m"); diff --git a/src/import/importd.c b/src/import/importd.c index 345fc7c2cb..65fe3701c6 100644 --- a/src/import/importd.c +++ b/src/import/importd.c @@ -11,6 +11,7 @@ #include "bus-log-control-api.h" #include "bus-polkit.h" #include "def.h" +#include "env-util.h" #include "fd-util.h" #include "float.h" #include "hostname-util.h" @@ -403,6 +404,10 @@ static int transfer_start(Transfer *t) { _exit(EXIT_FAILURE); } + r = setenv_systemd_exec_pid(true); + if (r < 0) + log_warning_errno(r, "Failed to update $SYSTEMD_EXEC_PID, ignoring: %m"); + switch (t->type) { case TRANSFER_IMPORT_TAR: From 85cf96e3f567cd51f79d671bbf3559550fdd67b7 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Tue, 26 Jan 2021 16:12:41 +0900 Subject: [PATCH 8/8] log: skip reading the kernel command line if the process is invoked by a script CLI tools may be used in a script. E.g., a script for monitoring a service may use `systemctl`. Previously, if the kernel command line has e.g. systemd.log-level=debug, then systemctl in the script produces debugging logs when the script is invoked by a .service unit, but does not when the script is running in a terminal. Then, https://github.com/systemd/systemd/pull/18281#discussion_r561697482, > I expect users to be (negatively) surprised. In the previous commit, $SYSTEMD_EXEC_PID= is introduced. Then, we can now detect whether a command is directly invoked by systemd or through a script. Let's skip reading the kernel command line when a command is invoked through a script. --- src/basic/log.c | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/src/basic/log.c b/src/basic/log.c index f2cd1c3041..6a86fab398 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -1157,15 +1157,37 @@ static int parse_proc_cmdline_item(const char *key, const char *value, void *dat return 0; } +static bool should_parse_proc_cmdline(void) { + const char *e; + pid_t p; + + /* PID1 always reads the kernel command line. */ + if (getpid_cached() == 1) + return true; + + /* If the process is directly executed by PID1 (e.g. ExecStart= or generator), systemd-importd, + * or systemd-homed, then $SYSTEMD_EXEC_PID= is set, and read the command line. */ + e = getenv("SYSTEMD_EXEC_PID"); + if (!e) + return false; + + if (streq(e, "*")) + /* For testing. */ + return true; + + if (parse_pid(e, &p) < 0) + /* We know that systemd sets the variable correctly. Something else must have set it. */ + log_debug("Failed to parse \"$SYSTEMD_EXEC_PID=%s\". Ignoring.", e); + + return getpid_cached() == p; +} + void log_parse_environment(void) { const char *e; /* Do not call from library code. */ - 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. */ + if (should_parse_proc_cmdline()) (void) proc_cmdline_parse(parse_proc_cmdline_item, NULL, PROC_CMDLINE_STRIP_RD_PREFIX); e = getenv("SYSTEMD_LOG_TARGET");