diff --git a/coccinelle/errno.cocci b/coccinelle/errno.cocci index c928266481..4e594e7826 100644 --- a/coccinelle/errno.cocci +++ b/coccinelle/errno.cocci @@ -25,6 +25,13 @@ expression e; - return r; @@ identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$"; +local idexpression r; +@@ ++ return + log_LEVEL_errno(r, ...); +- return r; +@@ +identifier log_LEVEL_errno =~ "^log_(debug|info|notice|warning|error|emergency)_errno$"; expression e; @@ + return @@ -39,3 +46,57 @@ local idexpression r; - log_LEVEL_errno(e, args); - r = e; + r = log_LEVEL_errno(e, args); +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|netdev|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +local idexpression r; +expression e; +expression u; +@@ +- r = -e; ++ r = + log_UNIT_LEVEL_errno(u, e, ...); +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|netdev|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +local idexpression r; +expression e; +expression u; +@@ ++ r = + log_UNIT_LEVEL_errno(u, e, ...); +- r = -e; +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|netdev|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +local idexpression r; +expression e; +expression u; +@@ +- r = ++ return + log_UNIT_LEVEL_errno(u, e, ...); +- return r; +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|netdev|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +local idexpression r; +expression u; +@@ ++ return + log_UNIT_LEVEL_errno(u, r, ...); +- return r; +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|netdev|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +expression e; +expression u; +@@ ++ return + log_UNIT_LEVEL_errno(u, e, ...); +- return -e; +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|netdev|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +expression list args; +expression e; +expression u; +local idexpression r; +@@ +- log_UNIT_LEVEL_errno(u, e, args); +- r = e; ++ r = log_UNIT_LEVEL_errno(u, e, args); diff --git a/coccinelle/synthetic-errno.cocci b/coccinelle/synthetic-errno.cocci index 650c37e08b..dcae069f74 100644 --- a/coccinelle/synthetic-errno.cocci +++ b/coccinelle/synthetic-errno.cocci @@ -15,9 +15,15 @@ log_debug("Found no default boot entry :("); expression e; expression list args; @@ +( +/* Ignore specific cases in src/import/{export,import,pull}.c where we want to return positive value on success. */ +log_info("Exiting."); +return -r; +| - log_info(args); - return -e; + return log_info_errno(SYNTHETIC_ERRNO(e), args); +) @@ expression e; expression list args; @@ -46,3 +52,211 @@ expression list args; @@ - log_LEVEL_errno(ERRNO, args); + log_LEVEL_errno(SYNTHETIC_ERRNO(ERRNO), args); +@@ +identifier log_UNIT_LEVEL_errno =~ "^log_(unit|link|device|token)_(debug|info|notice|warning|error|emergency)_errno$"; +identifier ERRNO =~ "^E[A-Z]+$"; +expression u; +expression list args; +@@ +- log_UNIT_LEVEL_errno(u, ERRNO, args); ++ log_UNIT_LEVEL_errno(u, SYNTHETIC_ERRNO(ERRNO), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_unit_debug(u, args); +- return -e; ++ return log_unit_debug_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_unit_info(u, args); +- return -e; ++ return log_unit_info_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_unit_notice(u, args); +- return -e; ++ return log_unit_notice_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_unit_error(u, args); +- return -e; ++ return log_unit_error_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_unit_emergency(u, args); +- return -e; ++ return log_unit_emergency_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_link_debug(u, args); +- return -e; ++ return log_link_debug_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_link_info(u, args); +- return -e; ++ return log_link_info_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_link_notice(u, args); +- return -e; ++ return log_link_notice_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_link_error(u, args); +- return -e; ++ return log_link_error_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_link_emergency(u, args); +- return -e; ++ return log_link_emergency_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_netdev_debug(u, args); +- return -e; ++ return log_netdev_debug_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_netdev_info(u, args); +- return -e; ++ return log_netdev_info_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_netdev_notice(u, args); +- return -e; ++ return log_netdev_notice_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_netdev_error(u, args); +- return -e; ++ return log_netdev_error_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_netdev_emergency(u, args); +- return -e; ++ return log_netdev_emergency_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_device_debug(u, args); +- return -e; ++ return log_device_debug_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_device_info(u, args); +- return -e; ++ return log_device_info_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_device_notice(u, args); +- return -e; ++ return log_device_notice_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_device_error(u, args); +- return -e; ++ return log_device_error_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_device_emergency(u, args); +- return -e; ++ return log_device_emergency_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_token_debug(u, args); +- return -e; ++ return log_token_debug_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_token_info(u, args); +- return -e; ++ return log_token_info_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_token_notice(u, args); +- return -e; ++ return log_token_notice_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_token_error(u, args); +- return -e; ++ return log_token_error_errno(u, SYNTHETIC_ERRNO(e), args); +@@ +expression e; +expression u; +expression list args; +@@ +- log_token_emergency(u, args); +- return -e; ++ return log_token_emergency_errno(u, SYNTHETIC_ERRNO(e), args); diff --git a/src/core/automount.c b/src/core/automount.c index a84cddbdbd..5076b351f5 100644 --- a/src/core/automount.c +++ b/src/core/automount.c @@ -174,19 +174,15 @@ static int automount_verify(Automount *a) { assert(a); assert(UNIT(a)->load_state == UNIT_LOADED); - if (path_equal(a->where, "/")) { - log_unit_error(UNIT(a), "Cannot have an automount unit for the root directory. Refusing."); - return -ENOEXEC; - } + if (path_equal(a->where, "/")) + return log_unit_error_errno(UNIT(a), SYNTHETIC_ERRNO(ENOEXEC), "Cannot have an automount unit for the root directory. Refusing."); r = unit_name_from_path(a->where, ".automount", &e); if (r < 0) return log_unit_error_errno(UNIT(a), r, "Failed to generate unit name from path: %m"); - if (!unit_has_name(UNIT(a), e)) { - log_unit_error(UNIT(a), "Where= setting doesn't match unit name. Refusing."); - return -ENOEXEC; - } + if (!unit_has_name(UNIT(a), e)) + return log_unit_error_errno(UNIT(a), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing."); return 0; } @@ -811,10 +807,8 @@ static int automount_start(Unit *u) { assert(a); assert(IN_SET(a->state, AUTOMOUNT_DEAD, AUTOMOUNT_FAILED)); - if (path_is_mount_point(a->where, NULL, 0) > 0) { - log_unit_error(u, "Path %s is already a mount point, refusing start.", a->where); - return -EEXIST; - } + if (path_is_mount_point(a->where, NULL, 0) > 0) + return log_unit_error_errno(u, SYNTHETIC_ERRNO(EEXIST), "Path %s is already a mount point, refusing start.", a->where); r = unit_test_trigger_loaded(u); if (r < 0) diff --git a/src/core/bpf-firewall.c b/src/core/bpf-firewall.c index 31c2dcf37d..44aa1bfb1e 100644 --- a/src/core/bpf-firewall.c +++ b/src/core/bpf-firewall.c @@ -686,14 +686,10 @@ int bpf_firewall_install(Unit *u) { supported = bpf_firewall_supported(); if (supported < 0) return supported; - if (supported == BPF_FIREWALL_UNSUPPORTED) { - log_unit_debug(u, "BPF firewalling not supported on this manager, proceeding without."); - return -EOPNOTSUPP; - } - if (supported != BPF_FIREWALL_SUPPORTED_WITH_MULTI && u->type == UNIT_SLICE) { - log_unit_debug(u, "BPF_F_ALLOW_MULTI is not supported on this manager, not doing BPF firewall on slice units."); - return -EOPNOTSUPP; - } + if (supported == BPF_FIREWALL_UNSUPPORTED) + return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EOPNOTSUPP), "BPF firewalling not supported on this manager, proceeding without."); + if (supported != BPF_FIREWALL_SUPPORTED_WITH_MULTI && u->type == UNIT_SLICE) + return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EOPNOTSUPP), "BPF_F_ALLOW_MULTI is not supported on this manager, not doing BPF firewall on slice units."); if (supported != BPF_FIREWALL_SUPPORTED_WITH_MULTI && (!set_isempty(u->ip_bpf_custom_ingress) || !set_isempty(u->ip_bpf_custom_egress))) return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EOPNOTSUPP), "BPF_F_ALLOW_MULTI not supported on this manager, cannot attach custom BPF programs."); diff --git a/src/core/device.c b/src/core/device.c index 9a1d88270d..6440c59e26 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -515,11 +515,10 @@ static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool if (DEVICE(u)->state == DEVICE_PLUGGED && DEVICE(u)->sysfs && sysfs && - !path_equal(DEVICE(u)->sysfs, sysfs)) { - log_unit_debug(u, "Device %s appeared twice with different sysfs paths %s and %s, ignoring the latter.", - e, DEVICE(u)->sysfs, sysfs); - return -EEXIST; - } + !path_equal(DEVICE(u)->sysfs, sysfs)) + return log_unit_debug_errno(u, SYNTHETIC_ERRNO(EEXIST), + "Device %s appeared twice with different sysfs paths %s and %s, ignoring the latter.", + e, DEVICE(u)->sysfs, sysfs); delete = false; diff --git a/src/core/execute.c b/src/core/execute.c index c992b8d5d3..f1f3744191 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -3769,23 +3769,20 @@ static int exec_child( r = dynamic_creds_realize(dcreds, suggested_paths, &uid, &gid); if (r < 0) { *exit_status = EXIT_USER; - if (r == -EILSEQ) { - log_unit_error(unit, "Failed to update dynamic user credentials: User or group with specified name already exists."); - return -EOPNOTSUPP; - } + if (r == -EILSEQ) + return log_unit_error_errno(unit, SYNTHETIC_ERRNO(EOPNOTSUPP), + "Failed to update dynamic user credentials: User or group with specified name already exists."); return log_unit_error_errno(unit, r, "Failed to update dynamic user credentials: %m"); } if (!uid_is_valid(uid)) { *exit_status = EXIT_USER; - log_unit_error(unit, "UID validation failed for \""UID_FMT"\"", uid); - return -ESRCH; + return log_unit_error_errno(unit, SYNTHETIC_ERRNO(ESRCH), "UID validation failed for \""UID_FMT"\"", uid); } if (!gid_is_valid(gid)) { *exit_status = EXIT_USER; - log_unit_error(unit, "GID validation failed for \""GID_FMT"\"", gid); - return -ESRCH; + return log_unit_error_errno(unit, SYNTHETIC_ERRNO(ESRCH), "GID validation failed for \""GID_FMT"\"", gid); } if (dcreds->user) @@ -4628,15 +4625,11 @@ int exec_spawn(Unit *unit, context->std_output == EXEC_OUTPUT_SOCKET || context->std_error == EXEC_OUTPUT_SOCKET) { - if (params->n_socket_fds > 1) { - log_unit_error(unit, "Got more than one socket."); - return -EINVAL; - } + if (params->n_socket_fds > 1) + return log_unit_error_errno(unit, SYNTHETIC_ERRNO(EINVAL), "Got more than one socket."); - if (params->n_socket_fds == 0) { - log_unit_error(unit, "Got no socket."); - return -EINVAL; - } + if (params->n_socket_fds == 0) + return log_unit_error_errno(unit, SYNTHETIC_ERRNO(EINVAL), "Got no socket."); socket_fd = params->fds[0]; } else { diff --git a/src/core/mount.c b/src/core/mount.c index 8e83de0ba8..e672796694 100644 --- a/src/core/mount.c +++ b/src/core/mount.c @@ -549,25 +549,19 @@ static int mount_verify(Mount *m) { if (r < 0) return log_unit_error_errno(UNIT(m), r, "Failed to generate unit name from mount path: %m"); - if (!unit_has_name(UNIT(m), e)) { - log_unit_error(UNIT(m), "Where= setting doesn't match unit name. Refusing."); - return -ENOEXEC; - } + if (!unit_has_name(UNIT(m), e)) + return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Where= setting doesn't match unit name. Refusing."); - if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) { - log_unit_error(UNIT(m), "Cannot create mount unit for API file system %s. Refusing.", m->where); - return -ENOEXEC; - } + if (mount_point_is_api(m->where) || mount_point_ignore(m->where)) + return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Cannot create mount unit for API file system %s. Refusing.", m->where); p = get_mount_parameters_fragment(m); if (p && !p->what && !UNIT(m)->perpetual) return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "What= setting is missing. Refusing."); - if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) { - log_unit_error(UNIT(m), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing."); - return -ENOEXEC; - } + if (m->exec_context.pam_name && m->kill_context.kill_mode != KILL_CONTROL_GROUP) + return log_unit_error_errno(UNIT(m), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to control-group'. Refusing."); return 0; } diff --git a/src/core/path.c b/src/core/path.c index ca3a91df27..b954ee1f47 100644 --- a/src/core/path.c +++ b/src/core/path.c @@ -283,10 +283,8 @@ static int path_verify(Path *p) { assert(p); assert(UNIT(p)->load_state == UNIT_LOADED); - if (!p->specs) { - log_unit_error(UNIT(p), "Path unit lacks path setting. Refusing."); - return -ENOEXEC; - } + if (!p->specs) + return log_unit_error_errno(UNIT(p), SYNTHETIC_ERRNO(ENOEXEC), "Path unit lacks path setting. Refusing."); return 0; } diff --git a/src/core/scope.c b/src/core/scope.c index a372f8d726..654702ca3b 100644 --- a/src/core/scope.c +++ b/src/core/scope.c @@ -131,10 +131,8 @@ static int scope_verify(Scope *s) { if (set_isempty(UNIT(s)->pids) && !MANAGER_IS_RELOADING(UNIT(s)->manager) && - !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) { - log_unit_error(UNIT(s), "Scope has no PIDs. Refusing."); - return -ENOENT; - } + !unit_has_name(UNIT(s), SPECIAL_INIT_SCOPE)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOENT), "Scope has no PIDs. Refusing."); return 0; } @@ -498,11 +496,7 @@ static int scope_deserialize_item(Unit *u, const char *key, const char *value, F if (parse_pid(value, &pid) < 0) log_unit_debug(u, "Failed to parse pids value: %s", value); else { - r = set_ensure_allocated(&u->pids, NULL); - if (r < 0) - return r; - - r = set_put(u->pids, PID_TO_PTR(pid)); + r = set_ensure_put(&u->pids, NULL, PID_TO_PTR(pid)); if (r < 0) return r; } diff --git a/src/core/service.c b/src/core/service.c index d7bdeb7cca..b1a4d0bf18 100644 --- a/src/core/service.c +++ b/src/core/service.c @@ -548,51 +548,35 @@ static int service_verify(Service *s) { assert(s); assert(UNIT(s)->load_state == UNIT_LOADED); - if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] - && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) { + if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] && + UNIT(s)->success_action == EMERGENCY_ACTION_NONE) /* FailureAction= only makes sense if one of the start or stop commands is specified. * SuccessAction= will be executed unconditionally if no commands are specified. Hence, * either a command or SuccessAction= are required. */ - log_unit_error(UNIT(s), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing."); - return -ENOEXEC; - } + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing."); - if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) { - log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); - return -ENOEXEC; - } + if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); - if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START] && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) { - log_unit_error(UNIT(s), "Service has no ExecStart= and no SuccessAction= settings and does not have RemainAfterExit=yes set. Refusing."); - return -ENOEXEC; - } + if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START] && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= and no SuccessAction= settings and does not have RemainAfterExit=yes set. Refusing."); - if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) { - log_unit_error(UNIT(s), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); - return -ENOEXEC; - } + if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); - if (s->type == SERVICE_ONESHOT - && !IN_SET(s->restart, SERVICE_RESTART_NO, SERVICE_RESTART_ON_FAILURE, SERVICE_RESTART_ON_ABNORMAL, SERVICE_RESTART_ON_WATCHDOG, SERVICE_RESTART_ON_ABORT)) { - log_unit_error(UNIT(s), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); - return -ENOEXEC; - } + if (s->type == SERVICE_ONESHOT && + !IN_SET(s->restart, SERVICE_RESTART_NO, SERVICE_RESTART_ON_FAILURE, SERVICE_RESTART_ON_ABNORMAL, SERVICE_RESTART_ON_WATCHDOG, SERVICE_RESTART_ON_ABORT)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); - if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) { - log_unit_error(UNIT(s), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing."); - return -ENOEXEC; - } + if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing."); - if (s->type == SERVICE_DBUS && !s->bus_name) { - log_unit_error(UNIT(s), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); - return -ENOEXEC; - } + if (s->type == SERVICE_DBUS && !s->bus_name) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); - if (s->exec_context.pam_name && !IN_SET(s->kill_context.kill_mode, KILL_CONTROL_GROUP, KILL_MIXED)) { - log_unit_error(UNIT(s), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); - return -ENOEXEC; - } + if (s->exec_context.pam_name && !IN_SET(s->kill_context.kill_mode, KILL_CONTROL_GROUP, KILL_MIXED)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); if (s->usb_function_descriptors && !s->usb_function_strings) log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); @@ -914,20 +898,14 @@ static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) { * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is * good */ - if (pid == getpid_cached() || pid == 1) { - log_unit_full(UNIT(s), prio, "New main PID "PID_FMT" is the manager, refusing.", pid); - return -EPERM; - } + if (pid == getpid_cached() || pid == 1) + return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the manager, refusing.", pid); - if (pid == s->control_pid) { - log_unit_full(UNIT(s), prio, "New main PID "PID_FMT" is the control process, refusing.", pid); - return -EPERM; - } + if (pid == s->control_pid) + return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the control process, refusing.", pid); - if (!pid_is_alive(pid)) { - log_unit_full(UNIT(s), prio, "New main PID "PID_FMT" does not exist or is a zombie.", pid); - return -ESRCH; - } + if (!pid_is_alive(pid)) + return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(ESRCH), "New main PID "PID_FMT" does not exist or is a zombie.", pid); owner = manager_get_unit_by_pid(UNIT(s)->manager, pid); if (owner == UNIT(s)) { @@ -988,20 +966,18 @@ static int service_load_pid_file(Service *s, bool may_warn) { if (r == 0) { struct stat st; - if (questionable_pid_file) { - log_unit_error(UNIT(s), "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file); - return -EPERM; - } + if (questionable_pid_file) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), + "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file); /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */ if (fstat(fd, &st) < 0) return log_unit_error_errno(UNIT(s), errno, "Failed to fstat() PID file O_PATH fd: %m"); - if (st.st_uid != 0) { - log_unit_error(UNIT(s), "New main PID "PID_FMT" does not belong to service, and PID file is not owned by root. Refusing.", pid); - return -EPERM; - } + if (st.st_uid != 0) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), + "New main PID "PID_FMT" does not belong to service, and PID file is not owned by root. Refusing.", pid); log_unit_debug(UNIT(s), "New main PID "PID_FMT" does not belong to service, but we'll accept it since PID file is owned by root.", pid); } @@ -2127,8 +2103,7 @@ static void service_enter_start(Service *s) { /* There's no command line configured for the main command? Hmm, that is strange. * This can only happen if the configuration changes at runtime. In this case, * let's enter a failure state. */ - log_unit_error(UNIT(s), "There's no 'start' task anymore we could start."); - r = -ENXIO; + r = log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENXIO), "There's no 'start' task anymore we could start."); goto fail; } diff --git a/src/core/slice.c b/src/core/slice.c index ee5c25932f..94eb56e1ca 100644 --- a/src/core/slice.c +++ b/src/core/slice.c @@ -94,19 +94,15 @@ static int slice_verify(Slice *s) { assert(s); assert(UNIT(s)->load_state == UNIT_LOADED); - if (!slice_name_is_valid(UNIT(s)->id)) { - log_unit_error(UNIT(s), "Slice name %s is not valid. Refusing.", UNIT(s)->id); - return -ENOEXEC; - } + if (!slice_name_is_valid(UNIT(s)->id)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Slice name %s is not valid. Refusing.", UNIT(s)->id); r = slice_build_parent_slice(UNIT(s)->id, &parent); if (r < 0) return log_unit_error_errno(UNIT(s), r, "Failed to determine parent slice: %m"); - if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) { - log_unit_error(UNIT(s), "Located outside of parent slice. Refusing."); - return -ENOEXEC; - } + if (parent ? !unit_has_name(UNIT_DEREF(UNIT(s)->slice), parent) : UNIT_ISSET(UNIT(s)->slice)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Located outside of parent slice. Refusing."); return 0; } diff --git a/src/core/smack-setup.c b/src/core/smack-setup.c index 1fe592af70..8cc1696a4f 100644 --- a/src/core/smack-setup.c +++ b/src/core/smack-setup.c @@ -384,8 +384,7 @@ int mac_smack_setup(bool *loaded_policy) { log_info("Successfully wrote Smack onlycap list."); break; default: - log_emergency_errno(r, "Failed to write Smack onlycap list: %m"); - return r; + return log_emergency_errno(r, "Failed to write Smack onlycap list: %m"); } *loaded_policy = true; diff --git a/src/core/socket.c b/src/core/socket.c index 7f8ac4eae9..1f799830d1 100644 --- a/src/core/socket.c +++ b/src/core/socket.c @@ -402,35 +402,23 @@ static int socket_verify(Socket *s) { assert(s); assert(UNIT(s)->load_state == UNIT_LOADED); - if (!s->ports) { - log_unit_error(UNIT(s), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing."); - return -ENOEXEC; - } + if (!s->ports) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has no Listen setting (ListenStream=, ListenDatagram=, ListenFIFO=, ...). Refusing."); - if (s->accept && have_non_accept_socket(s)) { - log_unit_error(UNIT(s), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing."); - return -ENOEXEC; - } + if (s->accept && have_non_accept_socket(s)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit configured for accepting sockets, but sockets are non-accepting. Refusing."); - if (s->accept && s->max_connections <= 0) { - log_unit_error(UNIT(s), "MaxConnection= setting too small. Refusing."); - return -ENOEXEC; - } + if (s->accept && s->max_connections <= 0) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "MaxConnection= setting too small. Refusing."); - if (s->accept && UNIT_DEREF(s->service)) { - log_unit_error(UNIT(s), "Explicit service configuration for accepting socket units not supported. Refusing."); - return -ENOEXEC; - } + if (s->accept && UNIT_DEREF(s->service)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Explicit service configuration for accepting socket units not supported. Refusing."); - if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) { - log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing."); - return -ENOEXEC; - } + if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing."); - if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) { - log_unit_error(UNIT(s), "Unit has symlinks set but none or more than one node in the file system. Refusing."); - return -ENOEXEC; - } + if (!strv_isempty(s->symlinks) && !socket_find_symlink_target(s)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has symlinks set but none or more than one node in the file system. Refusing."); return 0; } @@ -2507,17 +2495,13 @@ static int socket_start(Unit *u) { service = SERVICE(UNIT_DEREF(s->service)); - if (UNIT(service)->load_state != UNIT_LOADED) { - log_unit_error(u, "Socket service %s not loaded, refusing.", UNIT(service)->id); - return -ENOENT; - } + if (UNIT(service)->load_state != UNIT_LOADED) + return log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOENT), "Socket service %s not loaded, refusing.", UNIT(service)->id); /* If the service is already active we cannot start the * socket */ - if (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) { - log_unit_error(u, "Socket service %s already active, refusing.", UNIT(service)->id); - return -EBUSY; - } + if (!IN_SET(service->state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) + return log_unit_error_errno(u, SYNTHETIC_ERRNO(EBUSY), "Socket service %s already active, refusing.", UNIT(service)->id); } assert(IN_SET(s->state, SOCKET_DEAD, SOCKET_FAILED)); diff --git a/src/core/swap.c b/src/core/swap.c index 76e491ad94..ed06cf4b4b 100644 --- a/src/core/swap.c +++ b/src/core/swap.c @@ -287,15 +287,11 @@ static int swap_verify(Swap *s) { if (r < 0) return log_unit_error_errno(UNIT(s), r, "Failed to generate unit name from path: %m"); - if (!unit_has_name(UNIT(s), e)) { - log_unit_error(UNIT(s), "Value of What= and unit name do not match, not loading."); - return -ENOEXEC; - } + if (!unit_has_name(UNIT(s), e)) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Value of What= and unit name do not match, not loading."); - if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) { - log_unit_error(UNIT(s), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load."); - return -ENOEXEC; - } + if (s->exec_context.pam_name && s->kill_context.kill_mode != KILL_CONTROL_GROUP) + return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Unit has PAM enabled. Kill mode must be set to 'control-group'. Refusing to load."); return 0; } diff --git a/src/core/timer.c b/src/core/timer.c index 651f18b5a8..f42c86da76 100644 --- a/src/core/timer.c +++ b/src/core/timer.c @@ -75,10 +75,8 @@ static int timer_verify(Timer *t) { assert(t); assert(UNIT(t)->load_state == UNIT_LOADED); - if (!t->values && !t->on_clock_change && !t->on_timezone_change) { - log_unit_error(UNIT(t), "Timer unit lacks value setting. Refusing."); - return -ENOEXEC; - } + if (!t->values && !t->on_clock_change && !t->on_timezone_change) + return log_unit_error_errno(UNIT(t), SYNTHETIC_ERRNO(ENOEXEC), "Timer unit lacks value setting. Refusing."); return 0; } @@ -101,14 +99,13 @@ static int timer_add_default_dependencies(Timer *t) { if (r < 0) return r; - LIST_FOREACH(value, v, t->values) { + LIST_FOREACH(value, v, t->values) if (v->base == TIMER_CALENDAR) { r = unit_add_dependency_by_name(UNIT(t), UNIT_AFTER, SPECIAL_TIME_SYNC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); if (r < 0) return r; break; } - } } return unit_add_two_dependencies_by_name(UNIT(t), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); @@ -259,8 +256,7 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) { prefix, yes_no(t->on_clock_change), prefix, yes_no(t->on_timezone_change)); - LIST_FOREACH(value, v, t->values) { - + LIST_FOREACH(value, v, t->values) if (v->base == TIMER_CALENDAR) { _cleanup_free_ char *p = NULL; @@ -280,7 +276,6 @@ static void timer_dump(Unit *u, FILE *f, const char *prefix) { timer_base_to_string(v->base), format_timespan(timespan1, sizeof(timespan1), v->value, 0)); } - } } static void timer_set_state(Timer *t, TimerState state) { @@ -670,9 +665,7 @@ static int timer_start(Unit *u) { } } else if (errno == ENOENT) - /* The timer has never run before, - * make sure a stamp file exists. - */ + /* The timer has never run before, make sure a stamp file exists. */ (void) touch_file(t->stamp_path, true, USEC_INFINITY, UID_INVALID, GID_INVALID, MODE_INVALID); } diff --git a/src/core/unit-printf.c b/src/core/unit-printf.c index 0c1e20d9ce..8e50a81b97 100644 --- a/src/core/unit-printf.c +++ b/src/core/unit-printf.c @@ -181,7 +181,7 @@ int unit_name_printf(const Unit *u, const char* format, char **ret) { * %N: the id of the unit without the suffix (foo-aaa@bar) * %p: the prefix (foo-aaa) * %i: the instance (bar) - * %j: the last componet of the prefix (aaa) + * %j: the last component of the prefix (aaa) */ const Specifier table[] = { diff --git a/src/core/unit.c b/src/core/unit.c index 45a417a090..e209efd4a6 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -1668,8 +1668,8 @@ int unit_load(Unit *u) { goto fail; if (u->on_failure_job_mode == JOB_ISOLATE && hashmap_size(u->dependencies[UNIT_ON_FAILURE]) > 1) { - log_unit_error(u, "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); - r = -ENOEXEC; + r = log_unit_error_errno(u, SYNTHETIC_ERRNO(ENOEXEC), + "More than one OnFailure= dependencies specified but OnFailureJobMode=isolate set. Refusing."); goto fail; } @@ -2022,10 +2022,8 @@ int unit_reload(Unit *u) { if (state == UNIT_RELOADING) return -EAGAIN; - if (state != UNIT_ACTIVE) { - log_unit_warning(u, "Unit cannot be reloaded because it is inactive."); - return -ENOEXEC; - } + if (state != UNIT_ACTIVE) + return log_unit_warning_errno(u, SYNTHETIC_ERRNO(ENOEXEC), "Unit cannot be reloaded because it is inactive."); following = unit_following(u); if (following) { diff --git a/src/cryptsetup/cryptsetup-generator.c b/src/cryptsetup/cryptsetup-generator.c index 68c73499ce..0ae8ab35aa 100644 --- a/src/cryptsetup/cryptsetup-generator.c +++ b/src/cryptsetup/cryptsetup-generator.c @@ -445,14 +445,13 @@ static int create_disk( fprintf(f, "After=%s\n" "Requires=%s\n", unit, unit); - if (umount_unit) { + if (umount_unit) fprintf(f, "Wants=%s\n" "Before=%s\n", umount_unit, umount_unit ); - } } if (!nofail) diff --git a/src/network/netdev/netdev.c b/src/network/netdev/netdev.c index 71a4f603be..9223f43b99 100644 --- a/src/network/netdev/netdev.c +++ b/src/network/netdev/netdev.c @@ -400,10 +400,8 @@ int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) { if (r < 0) return log_netdev_error_errno(netdev, r, "Could not get rtnl message type: %m"); - if (type != RTM_NEWLINK) { - log_netdev_error(netdev, "Cannot set ifindex from unexpected rtnl message type."); - return -EINVAL; - } + if (type != RTM_NEWLINK) + return log_netdev_error_errno(netdev, SYNTHETIC_ERRNO(EINVAL), "Cannot set ifindex from unexpected rtnl message type."); r = sd_rtnl_message_link_get_ifindex(message, &ifindex); if (r < 0) { @@ -434,7 +432,7 @@ int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) { if (!streq(netdev->ifname, received_name)) { log_netdev_error(netdev, "Received newlink with wrong IFNAME %s", received_name); netdev_enter_failed(netdev); - return r; + return -EINVAL; } r = sd_netlink_message_enter_container(message, IFLA_LINKINFO); @@ -462,11 +460,10 @@ int netdev_set_ifindex(NetDev *netdev, sd_netlink_message *message) { } if (!streq(kind, received_kind)) { - log_netdev_error(netdev, - "Received newlink with wrong KIND %s, " - "expected %s", received_kind, kind); + log_netdev_error(netdev, "Received newlink with wrong KIND %s, expected %s", + received_kind, kind); netdev_enter_failed(netdev); - return r; + return -EINVAL; } netdev->ifindex = ifindex; diff --git a/src/network/networkd-can.c b/src/network/networkd-can.c index 7e31d2fc82..8610320698 100644 --- a/src/network/networkd-can.c +++ b/src/network/networkd-can.c @@ -195,10 +195,8 @@ static int link_set_can(Link *link) { format_timespan(time_string, FORMAT_TIMESPAN_MAX, restart_ms * 1000, MSEC_PER_SEC); - if (restart_ms > UINT32_MAX) { - log_link_error(link, "restart timeout (%s) too big.", time_string); - return -ERANGE; - } + if (restart_ms > UINT32_MAX) + return log_link_error_errno(link, SYNTHETIC_ERRNO(ERANGE), "restart timeout (%s) too big.", time_string); log_link_debug(link, "Setting restart = %s", time_string); diff --git a/src/oom/oomd-util.h b/src/oom/oomd-util.h index 87ecda80fb..0834cbf09d 100644 --- a/src/oom/oomd-util.h +++ b/src/oom/oomd-util.h @@ -64,24 +64,14 @@ static inline int compare_pgscan(OomdCGroupContext * const *c1, OomdCGroupContex assert(c1); assert(c2); - if ((*c1)->pgscan > (*c2)->pgscan) - return -1; - else if ((*c1)->pgscan < (*c2)->pgscan) - return 1; - else - return 0; + return CMP((*c2)->pgscan, (*c1)->pgscan); } static inline int compare_swap_usage(OomdCGroupContext * const *c1, OomdCGroupContext * const *c2) { assert(c1); assert(c2); - if ((*c1)->swap_usage > (*c2)->swap_usage) - return -1; - else if ((*c1)->swap_usage < (*c2)->swap_usage) - return 1; - else - return 0; + return CMP((*c2)->swap_usage, (*c1)->swap_usage); } /* Get an array of OomdCGroupContexts from `h`, qsorted from largest to smallest values according to `compare_func`. diff --git a/src/oom/test-oomd-util.c b/src/oom/test-oomd-util.c index 5df57107f9..8143408902 100644 --- a/src/oom/test-oomd-util.c +++ b/src/oom/test-oomd-util.c @@ -23,7 +23,7 @@ static int fork_and_sleep(unsigned sleep_min) { if (pid == 0) { timeout = sleep_min * USEC_PER_MINUTE; ts = now(CLOCK_MONOTONIC); - while (true) { + for (;;) { n = now(CLOCK_MONOTONIC); if (ts + timeout < n) { log_error("Child timed out waiting to be killed"); diff --git a/src/shared/mount-util.h b/src/shared/mount-util.h index 6202008a81..033fcd1f47 100644 --- a/src/shared/mount-util.h +++ b/src/shared/mount-util.h @@ -5,6 +5,7 @@ #include #include +#include "alloc-util.h" #include "errno-util.h" #include "macro.h" @@ -93,7 +94,6 @@ static inline char* umount_and_rmdir_and_free(char *p) { PROTECT_ERRNO; (void) umount_recursive(p, 0); (void) rmdir(p); - free(p); - return NULL; + return mfree(p); } DEFINE_TRIVIAL_CLEANUP_FUNC(char*, umount_and_rmdir_and_free);