From 5aa48b6de1119a204ddb19ae23b069036a3ca78d Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Sat, 17 Aug 2024 20:33:12 +0200 Subject: [PATCH 1/4] systemctl: use the retval of must_be_root() --- src/systemctl/systemctl-compat-halt.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/systemctl/systemctl-compat-halt.c b/src/systemctl/systemctl-compat-halt.c index 4f6e304816..9d69230097 100644 --- a/src/systemctl/systemctl-compat-halt.c +++ b/src/systemctl/systemctl-compat-halt.c @@ -180,10 +180,9 @@ int halt_main(void) { return start_with_fallback(); } - if (geteuid() != 0) { - (void) must_be_root(); - return -EPERM; - } + r = must_be_root(); + if (r < 0) + return r; if (!arg_no_wtmp) { if (sd_booted() > 0) From 57d8134d45226d298497895ee9a5c1228124aae6 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Thu, 31 Oct 2024 15:33:07 +0100 Subject: [PATCH 2/4] logind-dbus: return appropriate errno for unexpected errors Follow-up for 0e10c3d8724b0a5d07871c9de71565ac91dd55b7 --- src/login/logind-dbus.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/login/logind-dbus.c b/src/login/logind-dbus.c index 094f9a64bd..7557ab2d93 100644 --- a/src/login/logind-dbus.c +++ b/src/login/logind-dbus.c @@ -2663,11 +2663,11 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_ if (r < 0) { if (r == -ENOENT) return sd_bus_error_set(error, - BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED, - "No upcoming maintenance window scheduled"); - return sd_bus_error_setf(error, - BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED, - "Failed to determine next maintenance window"); + BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED, + "No upcoming maintenance window scheduled"); + + return sd_bus_error_set_errnof(error, r, + "Failed to determine next maintenance window: %m"); } log_info("Scheduled %s at maintenance window %s", type, FORMAT_TIMESTAMP(elapse)); From 249bb7f89433934b025f25e444358b39db3beb92 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Thu, 31 Oct 2024 15:45:15 +0100 Subject: [PATCH 3/4] systemctl: don't fall back to immediate shutdown silently if we cannot schedule one The previous behavior of systemctl --when= seems absurd, i.e. if we fail to schedule shutdown in the future it's performed immediately. Let's instead hard fail, which also removes the need of specializing on certain errnos (preparation for later commits). --- src/systemctl/systemctl-start-special.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/systemctl/systemctl-start-special.c b/src/systemctl/systemctl-start-special.c index 00dd05bff7..5fff5d4f18 100644 --- a/src/systemctl/systemctl-start-special.c +++ b/src/systemctl/systemctl-start-special.c @@ -201,14 +201,17 @@ int verb_start_special(int argc, char *argv[], void *userdata) { case ACTION_KEXEC: case ACTION_HALT: case ACTION_SOFT_REBOOT: - if (arg_when == 0) + if (arg_when == 0) { r = logind_reboot(a); - else + if (r >= 0 || IN_SET(r, -EACCES, -EOPNOTSUPP, -EINPROGRESS)) + /* The latter indicates that the requested operation requires auth, + * is not supported or already in progress, in which cases we ignore the error. */ + return r; + } else { r = logind_schedule_shutdown(a); - if (r >= 0 || IN_SET(r, -EACCES, -EOPNOTSUPP, -EINPROGRESS)) - /* The latter indicates that the requested operation requires auth, - * is not supported or already in progress, in which cases we ignore the error. */ - return r; + if (r != -ENOSYS) + return r; + } /* On all other errors, try low-level operation. In order to minimize the difference * between operation with and without logind, we explicitly enable non-blocking mode From 3e094f84899b17a947eaebe62d6447c1a360abc9 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Thu, 31 Oct 2024 15:46:27 +0100 Subject: [PATCH 4/4] bus-common-errors: use more appropriate errno for BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED Fixes #33429 --- src/libsystemd/sd-bus/bus-common-errors.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libsystemd/sd-bus/bus-common-errors.c b/src/libsystemd/sd-bus/bus-common-errors.c index f765238ba7..03423bdcd3 100644 --- a/src/libsystemd/sd-bus/bus-common-errors.c +++ b/src/libsystemd/sd-bus/bus-common-errors.c @@ -60,8 +60,7 @@ BUS_ERROR_MAP_ELF_REGISTER const sd_bus_error_map bus_common_errors[] = { SD_BUS_ERROR_MAP(BUS_ERROR_SLEEP_VERB_NOT_SUPPORTED, EOPNOTSUPP), SD_BUS_ERROR_MAP(BUS_ERROR_SESSION_BUSY, EBUSY), SD_BUS_ERROR_MAP(BUS_ERROR_NOT_YOUR_DEVICE, EPERM), - /* needs to be EOPNOTSUPP for proper handling in callers of logind_schedule_shutdown() */ - SD_BUS_ERROR_MAP(BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED, EOPNOTSUPP), + SD_BUS_ERROR_MAP(BUS_ERROR_DESIGNATED_MAINTENANCE_TIME_NOT_SCHEDULED, EBADSLT), SD_BUS_ERROR_MAP(BUS_ERROR_AUTOMATIC_TIME_SYNC_ENABLED, EALREADY), SD_BUS_ERROR_MAP(BUS_ERROR_NO_NTP_SUPPORT, EOPNOTSUPP),