1
0
mirror of https://github.com/systemd/systemd.git synced 2024-10-27 01:55:22 +03:00

logind-action: split out logic for handle_action_sleep

Preparation for #29853
This commit is contained in:
Mike Yuan 2023-11-02 18:23:21 +08:00
parent 51eeeb7bde
commit ddd0c2be81
No known key found for this signature in database
GPG Key ID: 417471C0A40F58B3

View File

@ -133,9 +133,8 @@ const HandleActionData* handle_action_lookup(HandleAction action) {
return &handle_action_data_table[action];
}
int manager_handle_action(
static int handle_action_execute(
Manager *m,
InhibitWhat inhibit_key,
HandleAction handle,
bool ignore_inhibited,
bool is_edge) {
@ -156,73 +155,11 @@ int manager_handle_action(
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
InhibitWhat inhibit_operation;
Inhibitor *offending = NULL;
bool supported;
int r;
assert(m);
/* If the key handling is turned off, don't do anything */
if (handle == HANDLE_IGNORE) {
log_debug("Handling of %s (%s) is disabled, taking no action.",
inhibit_key == 0 ? "idle timeout" : inhibit_what_to_string(inhibit_key),
is_edge ? "edge" : "level");
return 0;
}
if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
/* If the last system suspend or startup is too close,
* let's not suspend for now, to give USB docking
* stations some time to settle so that we can
* properly watch its displays. */
if (m->lid_switch_ignore_event_source) {
log_debug("Ignoring lid switch request, system startup or resume too close.");
return 0;
}
}
/* If the key handling is inhibited, don't do anything */
if (inhibit_key > 0) {
if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
log_debug("Refusing %s operation, %s is inhibited.",
handle_action_to_string(handle),
inhibit_what_to_string(inhibit_key));
return 0;
}
}
/* Locking is handled differently from the rest. */
if (handle == HANDLE_LOCK) {
if (!is_edge)
return 0;
log_info("Locking sessions...");
session_send_lock_all(m, true);
return 1;
}
if (handle == HANDLE_SUSPEND)
supported = sleep_supported(SLEEP_SUSPEND) > 0;
else if (handle == HANDLE_HIBERNATE)
supported = sleep_supported(SLEEP_HIBERNATE) > 0;
else if (handle == HANDLE_HYBRID_SLEEP)
supported = sleep_supported(SLEEP_HYBRID_SLEEP) > 0;
else if (handle == HANDLE_SUSPEND_THEN_HIBERNATE)
supported = sleep_supported(SLEEP_SUSPEND_THEN_HIBERNATE) > 0;
else if (handle == HANDLE_KEXEC)
supported = access(KEXEC, X_OK) >= 0;
else
supported = true;
if (!supported && HANDLE_ACTION_IS_SLEEP(handle) && handle != HANDLE_SUSPEND) {
supported = sleep_supported(SLEEP_SUSPEND) > 0;
if (supported) {
log_notice("Requested %s operation is not supported, using regular suspend instead.",
handle_action_to_string(handle));
handle = HANDLE_SUSPEND;
}
}
if (!supported)
if (handle == HANDLE_KEXEC && access(KEXEC, X_OK) < 0)
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Requested %s operation not supported, ignoring.", handle_action_to_string(handle));
@ -264,6 +201,97 @@ int manager_handle_action(
return 1;
}
static int handle_action_sleep_execute(
Manager *m,
HandleAction handle,
bool ignore_inhibited,
bool is_edge) {
bool supported;
assert(m);
assert(HANDLE_ACTION_IS_SLEEP(handle));
if (handle == HANDLE_SUSPEND)
supported = sleep_supported(SLEEP_SUSPEND) > 0;
else if (handle == HANDLE_HIBERNATE)
supported = sleep_supported(SLEEP_HIBERNATE) > 0;
else if (handle == HANDLE_HYBRID_SLEEP)
supported = sleep_supported(SLEEP_HYBRID_SLEEP) > 0;
else if (handle == HANDLE_SUSPEND_THEN_HIBERNATE)
supported = sleep_supported(SLEEP_SUSPEND_THEN_HIBERNATE) > 0;
else
assert_not_reached();
if (!supported && handle != HANDLE_SUSPEND) {
supported = sleep_supported(SLEEP_SUSPEND) > 0;
if (supported) {
log_notice("Requested %s operation is not supported, using regular suspend instead.",
handle_action_to_string(handle));
handle = HANDLE_SUSPEND;
}
}
if (!supported)
return log_warning_errno(SYNTHETIC_ERRNO(EOPNOTSUPP),
"Requested %s operation not supported, ignoring.", handle_action_to_string(handle));
return handle_action_execute(m, handle, ignore_inhibited, is_edge);
}
int manager_handle_action(
Manager *m,
InhibitWhat inhibit_key,
HandleAction handle,
bool ignore_inhibited,
bool is_edge) {
assert(m);
assert(handle_action_valid(handle));
/* If the key handling is turned off, don't do anything */
if (handle == HANDLE_IGNORE) {
log_debug("Handling of %s (%s) is disabled, taking no action.",
inhibit_key == 0 ? "idle timeout" : inhibit_what_to_string(inhibit_key),
is_edge ? "edge" : "level");
return 0;
}
if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
/* If the last system suspend or startup is too close, let's not suspend for now, to give
* USB docking stations some time to settle so that we can properly watch its displays. */
if (m->lid_switch_ignore_event_source) {
log_debug("Ignoring lid switch request, system startup or resume too close.");
return 0;
}
}
/* If the key handling is inhibited, don't do anything */
if (inhibit_key > 0) {
if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
log_debug("Refusing %s operation, %s is inhibited.",
handle_action_to_string(handle),
inhibit_what_to_string(inhibit_key));
return 0;
}
}
/* Locking is handled differently from the rest. */
if (handle == HANDLE_LOCK) {
if (!is_edge)
return 0;
log_info("Locking sessions...");
session_send_lock_all(m, true);
return 1;
}
if (HANDLE_ACTION_IS_SLEEP(handle))
return handle_action_sleep_execute(m, handle, ignore_inhibited, is_edge);
return handle_action_execute(m, handle, ignore_inhibited, is_edge);
}
static const char* const handle_action_verb_table[_HANDLE_ACTION_MAX] = {
[HANDLE_IGNORE] = "do nothing",
[HANDLE_POWEROFF] = "power off",