diff --git a/src/core/killall.c b/src/core/killall.c index 0710919640..81dc1a7bb6 100644 --- a/src/core/killall.c +++ b/src/core/killall.c @@ -78,27 +78,30 @@ static bool ignore_proc(pid_t pid, bool warn_rootfs) { } static void log_children_no_yet_killed(Set *pids) { - void *p; - Iterator i; _cleanup_free_ char *lst_child = NULL; + Iterator i; + void *p; SET_FOREACH(p, pids, i) { _cleanup_free_ char *s = NULL; - if (get_process_comm(PTR_TO_PID(p), &s) == 0) { - if (!strextend(&lst_child, ", ", s, NULL)) - break; + if (get_process_comm(PTR_TO_PID(p), &s) < 0) + (void) asprintf(&s, PID_FMT, PTR_TO_PID(p)); + + if (!strextend(&lst_child, ", ", s, NULL)) { + log_oom(); + return; } } - if (!isempty(lst_child)) - log_notice("Waiting for process:%s", lst_child + 1); + if (isempty(lst_child)) + return; + + log_warning("Waiting for process: %s", lst_child + 2); } static int wait_for_children(Set *pids, sigset_t *mask, usec_t timeout) { - usec_t until; - usec_t date_log_child; - usec_t n; + usec_t until, date_log_child, n; assert(mask); diff --git a/src/core/main.c b/src/core/main.c index 75c488795e..5bc3298fe0 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -1072,27 +1072,17 @@ static void bump_file_max_and_nr_open(void) { * hard) the only ones that really matter. */ #if BUMP_PROC_SYS_FS_FILE_MAX || BUMP_PROC_SYS_FS_NR_OPEN - _cleanup_free_ char *t = NULL; int r; #endif #if BUMP_PROC_SYS_FS_FILE_MAX /* The maximum the kernel allows for this since 5.2 is LONG_MAX, use that. (Previously thing where * different but the operation would fail silently.) */ - if (asprintf(&t, "%li\n", LONG_MAX) < 0) { - log_oom(); - return; - } - - r = sysctl_write("fs/file-max", t); + r = sysctl_writef("fs/file-max", "%li\n", LONG_MAX); if (r < 0) log_full_errno(IN_SET(r, -EROFS, -EPERM, -EACCES) ? LOG_DEBUG : LOG_WARNING, r, "Failed to bump fs.file-max, ignoring: %m"); #endif -#if BUMP_PROC_SYS_FS_FILE_MAX && BUMP_PROC_SYS_FS_NR_OPEN - t = mfree(t); -#endif - #if BUMP_PROC_SYS_FS_NR_OPEN int v = INT_MAX; @@ -1122,13 +1112,7 @@ static void bump_file_max_and_nr_open(void) { break; } - if (asprintf(&t, "%i\n", v) < 0) { - log_oom(); - return; - } - - r = sysctl_write("fs/nr_open", t); - t = mfree(t); + r = sysctl_writef("fs/nr_open", "%i\n", v); if (r == -EINVAL) { log_debug("Couldn't write fs.nr_open as %i, halving it.", v); v /= 2; diff --git a/src/shared/sysctl-util.c b/src/shared/sysctl-util.c index 9be4055b37..93bdcf11bf 100644 --- a/src/shared/sysctl-util.c +++ b/src/shared/sysctl-util.c @@ -60,6 +60,21 @@ int sysctl_write(const char *property, const char *value) { return 0; } +int sysctl_writef(const char *property, const char *format, ...) { + _cleanup_free_ char *v = NULL; + va_list ap; + int r; + + va_start(ap, format); + r = vasprintf(&v, format, ap); + va_end(ap); + + if (r < 0) + return -ENOMEM; + + return sysctl_write(property, v); +} + int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value) { const char *p; diff --git a/src/shared/sysctl-util.h b/src/shared/sysctl-util.h index d50c6e4381..cb30a97752 100644 --- a/src/shared/sysctl-util.h +++ b/src/shared/sysctl-util.h @@ -11,6 +11,7 @@ char *sysctl_normalize(char *s); int sysctl_read(const char *property, char **value); int sysctl_write(const char *property, const char *value); +int sysctl_writef(const char *propery, const char *format, ...) _printf_(2, 3); int sysctl_read_ip_property(int af, const char *ifname, const char *property, char **ret); int sysctl_write_ip_property(int af, const char *ifname, const char *property, const char *value); diff --git a/src/shutdown/shutdown.c b/src/shutdown/shutdown.c index 9623fe9ea2..a574a07041 100644 --- a/src/shutdown/shutdown.c +++ b/src/shutdown/shutdown.c @@ -258,7 +258,7 @@ static void sync_with_progress(void) { static int read_current_sysctl_printk_log_level(void) { _cleanup_free_ char *sysctl_printk_vals = NULL, *sysctl_printk_curr = NULL; - unsigned current_lvl = 0; + int current_lvl; const char *p; int r; @@ -268,38 +268,38 @@ static int read_current_sysctl_printk_log_level(void) { p = sysctl_printk_vals; r = extract_first_word(&p, &sysctl_printk_curr, NULL, 0); - if (r > 0) - r = safe_atou(sysctl_printk_curr, ¤t_lvl); - else if (r == 0) - r = -EINVAL; - if (r < 0) - return log_debug_errno(r, "Unexpected sysctl kernel.printk content: %s", sysctl_printk_vals); + return log_debug_errno(r, "Failed to split out kernel printk priority: %m"); + if (r == 0) + return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Short read while reading kernel.printk sysctl"); + + r = safe_atoi(sysctl_printk_curr, ¤t_lvl); + if (r < 0) + return log_debug_errno(r, "Failed to parse kernel.printk sysctl: %s", sysctl_printk_vals); return current_lvl; } static void bump_sysctl_printk_log_level(int min_level) { + int current_lvl, r; + /* Set the logging level to be able to see messages with log level smaller or equal to min_level */ - int current_lvl = read_current_sysctl_printk_log_level(); - if (current_lvl >= 0 && current_lvl <= min_level) { - char buf[DECIMAL_STR_MAX(int)]; - xsprintf(buf, "%d", min_level + 1); - int r = sysctl_write("kernel/printk", buf); - if (r < 0) - log_debug_errno(r, "Failed to bump kernel.printk to %s: %m", buf); - } + current_lvl = read_current_sysctl_printk_log_level(); + if (current_lvl < 0 || current_lvl >= min_level + 1) + return; + + r = sysctl_writef("kernel/printk", "%i", min_level + 1); + if (r < 0) + log_debug_errno(r, "Failed to bump kernel.printk to %i: %m", min_level + 1); } int main(int argc, char *argv[]) { - bool need_umount, need_swapoff, need_loop_detach, need_dm_detach; - bool in_container, use_watchdog = false, can_initrd; + bool need_umount, need_swapoff, need_loop_detach, need_dm_detach, in_container, use_watchdog = false, can_initrd; _cleanup_free_ char *cgroup = NULL; - char *arguments[3]; + char *arguments[3], *watchdog_device; int cmd, r, umount_log_level = LOG_INFO; static const char* const dirs[] = {SYSTEM_SHUTDOWN_PATH, NULL}; - char *watchdog_device; /* The log target defaults to console, but the original systemd process will pass its log target in through a * command line argument, which will override this default. Also, ensure we'll never log to the journal or @@ -342,15 +342,17 @@ int main(int argc, char *argv[]) { (void) cg_get_root_path(&cgroup); in_container = detect_container() > 0; - /* If the logging messages are going to KMSG, and if we are not running from a container, - * then try to update the sysctl kernel.printk current value in order to see "info" messages; - * This current log level is not updated if already big enough. + /* If the logging messages are going to KMSG, and if we are not running from a container, then try to + * update the sysctl kernel.printk current value in order to see "info" messages; This current log + * level is not updated if already big enough. */ - if (!in_container && IN_SET(log_get_target(), LOG_TARGET_AUTO, - LOG_TARGET_JOURNAL_OR_KMSG, - LOG_TARGET_SYSLOG_OR_KMSG, - LOG_TARGET_KMSG)) - bump_sysctl_printk_log_level(LOG_INFO); + if (!in_container && + IN_SET(log_get_target(), + LOG_TARGET_AUTO, + LOG_TARGET_JOURNAL_OR_KMSG, + LOG_TARGET_SYSLOG_OR_KMSG, + LOG_TARGET_KMSG)) + bump_sysctl_printk_log_level(LOG_WARNING); use_watchdog = getenv("WATCHDOG_USEC"); watchdog_device = getenv("WATCHDOG_DEVICE");