logind: add HANDLE_ACTION_IS_SLEEP() and HANDLE_ACTION_IS_SHUTDOWN() helpers

Let's group this HandleAction types in nice little helpers.

Prompted by #28579
This commit is contained in:
Lennart Poettering
2023-09-29 22:17:12 +02:00
committed by Luca Boccassi
parent 4f45ae4d1d
commit cb88da8254
4 changed files with 16 additions and 9 deletions

View File

@@ -213,7 +213,7 @@ int manager_handle_action(
else
supported = true;
if (!supported && IN_SET(handle, HANDLE_HIBERNATE, HANDLE_HYBRID_SLEEP, HANDLE_SUSPEND_THEN_HIBERNATE)) {
if (!supported && HANDLE_ACTION_IS_SLEEP(handle) && handle != HANDLE_SUSPEND) {
supported = can_sleep(SLEEP_SUSPEND) > 0;
if (supported) {
log_notice("Requested %s operation is not supported, using regular suspend instead.",

View File

@@ -6,14 +6,18 @@
typedef enum HandleAction {
HANDLE_IGNORE,
HANDLE_POWEROFF,
_HANDLE_ACTION_SHUTDOWN_FIRST = HANDLE_POWEROFF,
HANDLE_REBOOT,
HANDLE_HALT,
HANDLE_KEXEC,
HANDLE_SOFT_REBOOT,
_HANDLE_ACTION_SHUTDOWN_LAST = HANDLE_SOFT_REBOOT,
HANDLE_SUSPEND,
_HANDLE_ACTION_SLEEP_FIRST = HANDLE_SUSPEND,
HANDLE_HIBERNATE,
HANDLE_HYBRID_SLEEP,
HANDLE_SUSPEND_THEN_HIBERNATE,
_HANDLE_ACTION_SLEEP_LAST = HANDLE_SUSPEND_THEN_HIBERNATE,
HANDLE_LOCK,
HANDLE_FACTORY_RESET,
_HANDLE_ACTION_MAX,
@@ -30,6 +34,14 @@ static inline bool handle_action_valid(HandleAction a) {
return a >= 0 && a < _HANDLE_ACTION_MAX;
}
static inline bool HANDLE_ACTION_IS_SHUTDOWN(HandleAction a) {
return a >= _HANDLE_ACTION_SHUTDOWN_FIRST && a <= _HANDLE_ACTION_SHUTDOWN_LAST;
}
static inline bool HANDLE_ACTION_IS_SLEEP(HandleAction a) {
return a >= _HANDLE_ACTION_SLEEP_FIRST && a <= _HANDLE_ACTION_SLEEP_LAST;
}
struct HandleActionData {
HandleAction handle;
const char *target;

View File

@@ -2259,8 +2259,8 @@ static int method_schedule_shutdown(sd_bus_message *message, void *userdata, sd_
}
handle = handle_action_from_string(type);
if (!IN_SET(handle, HANDLE_POWEROFF, HANDLE_REBOOT, HANDLE_SOFT_REBOOT, HANDLE_HALT, HANDLE_KEXEC))
return sd_bus_error_set(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type");
if (!HANDLE_ACTION_IS_SHUTDOWN(handle))
return sd_bus_error_setf(error, SD_BUS_ERROR_INVALID_ARGS, "Unsupported shutdown type: %s", type);
a = handle_action_lookup(handle);
assert(a);

View File

@@ -52,12 +52,7 @@ bool logind_wall_tty_filter(const char *tty, bool is_local, void *userdata) {
* can assume that if the system enters sleep or hibernation, this will be visible in an obvious way
* for any local user. And once the systems exits sleep or hibernation, the notification would be
* just noise, in particular for auto-suspend. */
if (is_local &&
IN_SET(m->scheduled_shutdown_action->handle,
HANDLE_SUSPEND,
HANDLE_HIBERNATE,
HANDLE_HYBRID_SLEEP,
HANDLE_SUSPEND_THEN_HIBERNATE))
if (is_local && HANDLE_ACTION_IS_SLEEP(m->scheduled_shutdown_action->handle))
return false;
return !streq_ptr(p, m->scheduled_shutdown_tty);