From: Mike Yuan Date: Thu, 2 Nov 2023 10:23:21 +0000 (+0800) Subject: logind-action: split out logic for handle_action_sleep X-Git-Tag: v255-rc3~47^2~2 X-Git-Url: http://git-history.diyao.me/?a=commitdiff_plain;h=ddd0c2be81c5f432ef6923871089d09192c50476;p=systemd%2F.git logind-action: split out logic for handle_action_sleep Preparation for #29853 --- diff --git a/src/login/logind-action.c b/src/login/logind-action.c index d4d53bddd7..c1d60af759 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -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",