logind-action.c revision 6a79c58603ea816a1b4fa1520397b4e138bc1ca0
3802a3d3d7af51ddff31943d5514382f01265770Lennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek This file is part of systemd.
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek Copyright 2012 Lennart Poettering
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek systemd is free software; you can redistribute it and/or modify it
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek under the terms of the GNU Lesser General Public License as published by
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek the Free Software Foundation; either version 2.1 of the License, or
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek (at your option) any later version.
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek systemd is distributed in the hope that it will be useful, but
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek WITHOUT ANY WARRANTY; without even the implied warranty of
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek Lesser General Public License for more details.
b975b0d514321f169b3c4599a8ea92e13741b4e4Zbigniew Jędrzejewski-Szmek You should have received a copy of the GNU Lesser General Public License
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek along with systemd; If not, see <http://www.gnu.org/licenses/>.
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek static const char * const message_table[_HANDLE_ACTION_MAX] = {
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_POWEROFF] = "Powering Off...",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_REBOOT] = "Rebooting...",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_KEXEC] = "Rebooting via kexec...",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_SUSPEND] = "Suspending...",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_HIBERNATE] = "Hibernating...",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek static const char * const target_table[_HANDLE_ACTION_MAX] = {
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_HALT] = SPECIAL_HALT_TARGET,
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
330427e271c37400f091bf4570b5d8fa96574d36Zbigniew Jędrzejewski-Szmek [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL;
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek /* If the key handling is turned off, don't do anything */
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_debug("Refusing operation, as it is turned off.");
a8ca47227d1ab716ba928d8b9334b655ca5a840cZbigniew Jędrzejewski-Szmek /* If we are docked don't react to lid closing */
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek if (inhibit_key == INHIBIT_HANDLE_LID_SWITCH) {
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_debug("Ignoring lid switch request, system is docked.");
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_debug("Ignoring lid switch request, %s displays connected.");
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek /* If the key handling is inhibited, don't do anything */
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0, NULL)) {
b8bde11658366290521e3d03316378b482600323Jan Engelhardt log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek /* Locking is handled differently from the rest. */
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_info("Locking sessions...");
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek supported = can_sleep("suspend") > 0;
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek else if (handle == HANDLE_HIBERNATE)
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek supported = can_sleep("hibernate") > 0;
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek else if (handle == HANDLE_HYBRID_SLEEP)
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek supported = can_sleep("hybrid-sleep") > 0;
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek else if (handle == HANDLE_KEXEC)
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek supported = access(KEXEC, X_OK) >= 0;
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_warning("Requested operation not supported, ignoring.");
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_debug("Action already in progress, ignoring.");
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek /* If the actual operation is inhibited, warn and fail */
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0, &offending)) {
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek _cleanup_free_ char *comm = NULL, *u = NULL;
cc64d0175a3c2c974709e9962c00fbe04d74c43fZbigniew Jędrzejewski-Szmek get_process_comm(offending->pid, &comm);
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek /* If this is just a recheck of the lid switch then don't warn about anything */
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek log_debug("Refusing operation, %s is inhibited by UID %lu/%s, PID %lu/%s.",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek inhibit_what_to_string(inhibit_operation),
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek (unsigned long) offending->uid, strna(u),
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmek (unsigned long) offending->pid, strna(comm));
cc64d0175a3c2c974709e9962c00fbe04d74c43fZbigniew Jędrzejewski-Szmek log_error("Refusing operation, %s is inhibited by UID %lu/%s, PID %lu/%s.",
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek inhibit_what_to_string(inhibit_operation),
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek (unsigned long) offending->uid, strna(u),
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek (unsigned long) offending->pid, strna(comm));
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek log_info("%s", message_table[handle]);
ff9b60f38bf68eba4a47cabff14547d92e083214Torstein Husebø r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek log_error("Failed to execute operation: %s", bus_error_message(&error, r));
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-Szmekstatic const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek [HANDLE_HIBERNATE] = "hibernate",
8a8d55f2c104cf99efe5f244fc4857a15e9f60deZbigniew Jędrzejewski-Szmek [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-SzmekDEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
fdfccdbc985944a57017a25f44dd6acc1a937babZbigniew Jędrzejewski-SzmekDEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");