logind-action.c revision f9cd6be10ece07e10488c05e270a0b5860779864
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.");
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering /* If we are docked don't react to lid closing */
2d62c530d2b4c2730abff715b7342f1402114513Lennart Poettering log_debug("Ignoring lid switch request, system is docked.");
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering /* If we have more than one or no displays connected,
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * don't react to lid closing. The no display case we
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * treat like this under the assumption that there is
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * no modern drm driver available. */
7e9110a29d90041b0364cb93a84aec9dd72363b6Lennart Poettering log_debug("Ignoring lid switch request, %i displays connected.", n);
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering /* If the last system suspend or startup is too close,
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * let's not suspend for now, to give USB docking
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * stations some time to settle so that we can
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering * properly watch its displays. */
f9cd6be10ece07e10488c05e270a0b5860779864Lennart Poettering log_debug("Ignoring lid switch request, system startup or resume too close.");
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering /* If the key handling is inhibited, don't do anything */
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
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 */
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering _cleanup_free_ char *comm = NULL, *u = NULL;
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering /* If this is just a recheck of the lid switch then don't warn about anything */
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering log_debug("Refusing operation, %s is inhibited by UID %lu/%s, PID %lu/%s.",
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering inhibit_what_to_string(inhibit_operation),
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering (unsigned long) offending->uid, strna(u),
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering (unsigned long) offending->pid, strna(comm));
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering log_error("Refusing operation, %s is inhibited by UID %lu/%s, PID %lu/%s.",
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering inhibit_what_to_string(inhibit_operation),
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering (unsigned long) offending->uid, strna(u),
85a428c69465b047731b6abb5005f01824f1444eLennart Poettering (unsigned long) offending->pid, strna(comm));
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");