From 8ed6f81ba3b7be55d20a5d703c9aa7d4018b80ed Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 9 Sep 2020 02:28:36 +0900 Subject: [PATCH] core: make log_unit_error() or friends return void --- src/core/bpf-firewall.c | 10 +++++----- src/core/cgroup.c | 16 ++++++++-------- src/core/service.c | 16 ++++++++-------- src/core/swap.c | 4 ++-- src/core/transaction.c | 15 +++++++-------- src/core/unit.h | 26 ++++++++++++++------------ 6 files changed, 44 insertions(+), 43 deletions(-) diff --git a/src/core/bpf-firewall.c b/src/core/bpf-firewall.c index 5671d0c1952..5da0106a330 100644 --- a/src/core/bpf-firewall.c +++ b/src/core/bpf-firewall.c @@ -899,11 +899,11 @@ void emit_bpf_firewall_warning(Unit *u) { if (!warned) { bool quiet = bpf_firewall_unsupported_reason == -EPERM && detect_container(); - log_unit_full(u, quiet ? LOG_DEBUG : LOG_WARNING, bpf_firewall_unsupported_reason, - "unit configures an IP firewall, but %s.\n" - "(This warning is only shown for the first unit using IP firewalling.)", - getuid() != 0 ? "not running as root" : - "the local system does not support BPF/cgroup firewalling"); + log_unit_full_errno(u, quiet ? LOG_DEBUG : LOG_WARNING, bpf_firewall_unsupported_reason, + "unit configures an IP firewall, but %s.\n" + "(This warning is only shown for the first unit using IP firewalling.)", + getuid() != 0 ? "not running as root" : + "the local system does not support BPF/cgroup firewalling"); warned = true; } } diff --git a/src/core/cgroup.c b/src/core/cgroup.c index 6af08e78e6c..d6fca2593f1 100644 --- a/src/core/cgroup.c +++ b/src/core/cgroup.c @@ -78,8 +78,8 @@ static int set_attribute_and_warn(Unit *u, const char *controller, const char *a r = cg_set_attribute(controller, u->cgroup_path, attribute, value); if (r < 0) - log_unit_full(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to set '%s' attribute on '%s' to '%.*s': %m", - strna(attribute), isempty(u->cgroup_path) ? "/" : u->cgroup_path, (int) strcspn(value, NEWLINE), value); + log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r, "Failed to set '%s' attribute on '%s' to '%.*s': %m", + strna(attribute), isempty(u->cgroup_path) ? "/" : u->cgroup_path, (int) strcspn(value, NEWLINE), value); return r; } @@ -724,7 +724,7 @@ static usec_t cgroup_cpu_adjust_period_and_log(Unit *u, usec_t period, usec_t qu if (new_period != period) { char v[FORMAT_TIMESPAN_MAX]; - log_unit_full(u, u->warned_clamping_cpu_quota_period ? LOG_DEBUG : LOG_WARNING, 0, + log_unit_full(u, u->warned_clamping_cpu_quota_period ? LOG_DEBUG : LOG_WARNING, "Clamping CPU interval for cpu.max: period is now %s", format_timespan(v, sizeof(v), new_period, 1)); u->warned_clamping_cpu_quota_period = true; @@ -986,8 +986,8 @@ static int cgroup_apply_devices(Unit *u) { else r = cg_set_attribute("devices", path, "devices.allow", "a"); if (r < 0) - log_unit_full(u, IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r, - "Failed to reset devices.allow/devices.deny: %m"); + log_unit_full_errno(u, IN_SET(r, -ENOENT, -EROFS, -EINVAL, -EACCES, -EPERM) ? LOG_DEBUG : LOG_WARNING, r, + "Failed to reset devices.allow/devices.deny: %m"); } bool allow_list_static = policy == CGROUP_DEVICE_POLICY_CLOSED || @@ -1351,8 +1351,8 @@ static void cgroup_context_apply( else r = 0; if (r < 0) - log_unit_full(u, LOG_LEVEL_CGROUP_WRITE(r), r, - "Failed to write to tasks limit sysctls: %m"); + log_unit_full_errno(u, LOG_LEVEL_CGROUP_WRITE(r), r, + "Failed to write to tasks limit sysctls: %m"); } /* The attribute itself is not available on the host root cgroup, and in the container case we want to @@ -2435,7 +2435,7 @@ void unit_prune_cgroup(Unit *u) { * the containing slice is stopped. So even if we failed now, this unit shouldn't assume * that the cgroup is still realized the next time it is started. Do not return early * on error, continue cleanup. */ - log_unit_full(u, r == -EBUSY ? LOG_DEBUG : LOG_WARNING, r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path); + log_unit_full_errno(u, r == -EBUSY ? LOG_DEBUG : LOG_WARNING, r, "Failed to destroy cgroup %s, ignoring: %m", u->cgroup_path); if (is_root_slice) return; diff --git a/src/core/service.c b/src/core/service.c index 14c90c26df4..03493fd04b7 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -918,17 +918,17 @@ static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) { * good */ if (pid == getpid_cached() || pid == 1) { - log_unit_full(UNIT(s), prio, 0, "New main PID "PID_FMT" is the manager, refusing.", pid); + log_unit_full(UNIT(s), prio, "New main PID "PID_FMT" is the manager, refusing.", pid); return -EPERM; } if (pid == s->control_pid) { - log_unit_full(UNIT(s), prio, 0, "New main PID "PID_FMT" is the control process, refusing.", pid); + log_unit_full(UNIT(s), prio, "New main PID "PID_FMT" is the control process, refusing.", pid); return -EPERM; } if (!pid_is_alive(pid)) { - log_unit_full(UNIT(s), prio, 0, "New main PID "PID_FMT" does not exist or is a zombie.", pid); + log_unit_full(UNIT(s), prio, "New main PID "PID_FMT" does not exist or is a zombie.", pid); return -ESRCH; } @@ -958,16 +958,16 @@ static int service_load_pid_file(Service *s, bool may_warn) { r = chase_symlinks(s->pid_file, NULL, CHASE_SAFE, NULL, &fd); if (r == -ENOLINK) { - log_unit_full(UNIT(s), LOG_DEBUG, r, - "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file); + log_unit_debug_errno(UNIT(s), r, + "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file); questionable_pid_file = true; r = chase_symlinks(s->pid_file, NULL, 0, NULL, &fd); } if (r < 0) - return log_unit_full(UNIT(s), prio, fd, - "Can't open PID file %s (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); + return log_unit_full_errno(UNIT(s), prio, fd, + "Can't open PID file %s (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); /* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd * chase_symlinks() returned us into a proper fd first. */ @@ -980,7 +980,7 @@ static int service_load_pid_file(Service *s, bool may_warn) { r = parse_pid(k, &pid); if (r < 0) - return log_unit_full(UNIT(s), prio, r, "Failed to parse PID from file %s: %m", s->pid_file); + return log_unit_full_errno(UNIT(s), prio, r, "Failed to parse PID from file %s: %m", s->pid_file); if (s->main_pid_known && pid == s->main_pid) return 0; diff --git a/src/core/swap.c b/src/core/swap.c index e8bf0ad12d7..fa600a9797c 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -284,8 +284,8 @@ static int swap_load_devnode(Swap *s) { r = device_new_from_stat_rdev(&d, &st); if (r < 0) { - log_unit_full(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, - "Failed to allocate device for swap %s: %m", s->what); + log_unit_full_errno(UNIT(s), r == -ENOENT ? LOG_DEBUG : LOG_WARNING, r, + "Failed to allocate device for swap %s: %m", s->what); return 0; } diff --git a/src/core/transaction.c b/src/core/transaction.c index ab07c80f1dd..6cf305f4b50 100644 --- a/src/core/transaction.c +++ b/src/core/transaction.c @@ -998,10 +998,9 @@ int transaction_add_job_and_dependencies( SET_FOREACH(dep, following) { r = transaction_add_job_and_dependencies(tr, type, dep, ret, false, false, false, ignore_order, e); if (r < 0) { - log_unit_full(dep, - r == -ERFKILL ? LOG_INFO : LOG_WARNING, - r, "Cannot add dependency job, ignoring: %s", - bus_error_message(e, r)); + log_unit_full_errno(dep, r == -ERFKILL ? LOG_INFO : LOG_WARNING, r, + "Cannot add dependency job, ignoring: %s", + bus_error_message(e, r)); sd_bus_error_free(e); } } @@ -1035,10 +1034,10 @@ int transaction_add_job_and_dependencies( r = transaction_add_job_and_dependencies(tr, JOB_START, dep, ret, false, false, false, ignore_order, e); if (r < 0) { /* unit masked, job type not applicable and unit not found are not considered as errors. */ - log_unit_full(dep, - IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING, - r, "Cannot add dependency job, ignoring: %s", - bus_error_message(e, r)); + log_unit_full_errno(dep, + IN_SET(r, -ERFKILL, -EBADR, -ENOENT) ? LOG_DEBUG : LOG_WARNING, + r, "Cannot add dependency job, ignoring: %s", + bus_error_message(e, r)); sd_bus_error_free(e); } } diff --git a/src/core/unit.h b/src/core/unit.h index 5a75136e3d0..2205b7b1637 100644 --- a/src/core/unit.h +++ b/src/core/unit.h @@ -896,7 +896,7 @@ int unit_thaw_vtable_common(Unit *u); /* Macros which append UNIT= or USER_UNIT= to the message */ -#define log_unit_full(unit, level, error, ...) \ +#define log_unit_full_errno(unit, level, error, ...) \ ({ \ const Unit *_u = (unit); \ (log_get_max_level() < LOG_PRI(level)) ? -ERRNO_VALUE(error) : \ @@ -904,17 +904,19 @@ int unit_thaw_vtable_common(Unit *u); log_internal(level, error, PROJECT_FILE, __LINE__, __func__, ##__VA_ARGS__); \ }) -#define log_unit_debug(unit, ...) log_unit_full(unit, LOG_DEBUG, 0, ##__VA_ARGS__) -#define log_unit_info(unit, ...) log_unit_full(unit, LOG_INFO, 0, ##__VA_ARGS__) -#define log_unit_notice(unit, ...) log_unit_full(unit, LOG_NOTICE, 0, ##__VA_ARGS__) -#define log_unit_warning(unit, ...) log_unit_full(unit, LOG_WARNING, 0, ##__VA_ARGS__) -#define log_unit_error(unit, ...) log_unit_full(unit, LOG_ERR, 0, ##__VA_ARGS__) - -#define log_unit_debug_errno(unit, error, ...) log_unit_full(unit, LOG_DEBUG, error, ##__VA_ARGS__) -#define log_unit_info_errno(unit, error, ...) log_unit_full(unit, LOG_INFO, error, ##__VA_ARGS__) -#define log_unit_notice_errno(unit, error, ...) log_unit_full(unit, LOG_NOTICE, error, ##__VA_ARGS__) -#define log_unit_warning_errno(unit, error, ...) log_unit_full(unit, LOG_WARNING, error, ##__VA_ARGS__) -#define log_unit_error_errno(unit, error, ...) log_unit_full(unit, LOG_ERR, error, ##__VA_ARGS__) +#define log_unit_full(unit, level, ...) (void) log_unit_full_errno(unit, level, 0, __VA_ARGS__) + +#define log_unit_debug(unit, ...) log_unit_full_errno(unit, LOG_DEBUG, 0, __VA_ARGS__) +#define log_unit_info(unit, ...) log_unit_full(unit, LOG_INFO, __VA_ARGS__) +#define log_unit_notice(unit, ...) log_unit_full(unit, LOG_NOTICE, __VA_ARGS__) +#define log_unit_warning(unit, ...) log_unit_full(unit, LOG_WARNING, __VA_ARGS__) +#define log_unit_error(unit, ...) log_unit_full(unit, LOG_ERR, __VA_ARGS__) + +#define log_unit_debug_errno(unit, error, ...) log_unit_full_errno(unit, LOG_DEBUG, error, __VA_ARGS__) +#define log_unit_info_errno(unit, error, ...) log_unit_full_errno(unit, LOG_INFO, error, __VA_ARGS__) +#define log_unit_notice_errno(unit, error, ...) log_unit_full_errno(unit, LOG_NOTICE, error, __VA_ARGS__) +#define log_unit_warning_errno(unit, error, ...) log_unit_full_errno(unit, LOG_WARNING, error, __VA_ARGS__) +#define log_unit_error_errno(unit, error, ...) log_unit_full_errno(unit, LOG_ERR, error, __VA_ARGS__) #define LOG_UNIT_MESSAGE(unit, fmt, ...) "MESSAGE=%s: " fmt, (unit)->id, ##__VA_ARGS__ #define LOG_UNIT_ID(unit) (unit)->manager->unit_log_format_string, (unit)->id -- 2.47.3