logind-action.c revision cc3773810855956bad92337cee8fa193584ab62e
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering This file is part of systemd.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering Copyright 2012 Lennart Poettering
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering systemd is free software; you can redistribute it and/or modify it
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering under the terms of the GNU Lesser General Public License as published by
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering the Free Software Foundation; either version 2.1 of the License, or
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering (at your option) any later version.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering systemd is distributed in the hope that it will be useful, but
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering Lesser General Public License for more details.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering You should have received a copy of the GNU Lesser General Public License
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering static const char * const message_table[_HANDLE_ACTION_MAX] = {
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering [HANDLE_KEXEC] = "Rebooting via kexec...",
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering static const char * const target_table[_HANDLE_ACTION_MAX] = {
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering /* If the key handling is turned off, don't do anything */
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering log_debug("Refusing operation, as it is turned off.");
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering /* If the key handling is inhibited, don't do anything */
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0)) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering /* Locking is handled differently from the rest. */
19adb8a3204fefd91411b5f0f350c8bc6bcf75feZbigniew Jędrzejewski-Szmek supported = can_sleep("suspend") > 0;
19adb8a3204fefd91411b5f0f350c8bc6bcf75feZbigniew Jędrzejewski-Szmek supported = can_sleep("hibernate") > 0;
19adb8a3204fefd91411b5f0f350c8bc6bcf75feZbigniew Jędrzejewski-Szmek supported = can_sleep("hybrid-sleep") > 0;
7801356442578ff6e1c65844eb9e65c819af4660Zbigniew Jędrzejewski-Szmek supported = access(KEXEC, X_OK) >= 0;
dc3a1b76a6a6f9dfe9b451f534587251b50a0685Lennart Poettering log_warning("Requested operation not supported, ignoring.");
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_debug("Action already in progress, ignoring.");
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering /* If the actual operation is inhibited, warn and fail */
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0)) {
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering /* If this is just a recheck of the lid switch then don't warn about anything */
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering log_error("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_error("Failed to execute operation: %s", bus_error_message(&error, r));
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poetteringstatic const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart PoetteringDEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");