logind-action.c revision 23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye/***
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye This file is part of systemd.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye Copyright 2012 Lennart Poettering
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye systemd is free software; you can redistribute it and/or modify it
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye under the terms of the GNU Lesser General Public License as published by
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye the Free Software Foundation; either version 2.1 of the License, or
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye (at your option) any later version.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye systemd is distributed in the hope that it will be useful, but
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye WITHOUT ANY WARRANTY; without even the implied warranty of
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye Lesser General Public License for more details.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye You should have received a copy of the GNU Lesser General Public License
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye along with systemd; If not, see <http://www.gnu.org/licenses/>.
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye***/
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye#include "conf-parser.h"
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye#include "special.h"
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye#include "dbus-common.h"
cf1f7b5e81583dfca30972cfef322266a6928e7fKnut Anders Hatlen#include "logind-action.h"
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbyeint manager_handle_action(
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye Manager *m,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye InhibitWhat inhibit_key,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye HandleAction handle,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye bool ignore_inhibited,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye bool is_edge) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye static const char * const message_table[_HANDLE_ACTION_MAX] = {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_POWEROFF] = "Powering Off...",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_REBOOT] = "Rebooting...",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_HALT] = "Halting...",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_KEXEC] = "Rebooting via kexec...",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_SUSPEND] = "Suspending...",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_HIBERNATE] = "Hibernating...",
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_HYBRID_SLEEP] = "Hibernating and suspending..."
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye };
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye static const char * const target_table[_HANDLE_ACTION_MAX] = {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_POWEROFF] = SPECIAL_POWEROFF_TARGET,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_REBOOT] = SPECIAL_REBOOT_TARGET,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_HALT] = SPECIAL_HALT_TARGET,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_KEXEC] = SPECIAL_KEXEC_TARGET,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_SUSPEND] = SPECIAL_SUSPEND_TARGET,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_HIBERNATE] = SPECIAL_HIBERNATE_TARGET,
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye [HANDLE_HYBRID_SLEEP] = SPECIAL_HYBRID_SLEEP_TARGET
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye };
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye DBusError error;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye int r;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye InhibitWhat inhibit_operation;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye assert(m);
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* If the key handling is turned off, don't do anything */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if (handle == HANDLE_IGNORE) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye log_debug("Refusing operation, as it is turned off.");
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye return 0;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye }
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* If the key handling is inhibited, don't do anything */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if (inhibit_key > 0) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if (manager_is_inhibited(m, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0)) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_key));
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye return 0;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye }
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye }
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* Locking is handled differently from the rest. */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if (handle == HANDLE_LOCK) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye log_info("Locking sessions...");
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye session_send_lock_all(m, true);
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye return 1;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye }
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE || handle == HANDLE_HYBRID_SLEEP ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* If the actual operation is inhibited, warn and fail */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if (!ignore_inhibited &&
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye manager_is_inhibited(m, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0)) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye /* If this is just a recheck of the lid switch then don't warn about anything */
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye if (!is_edge) {
64b763950bf11e9357facbd2b5666631a895c085Trond Norbye log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
0a0811923cbbd2976425db6f4c78eed811c2825bKnut Anders Hatlen return 0;
b68083650aff0d1663971a216369f651559db2a1Knut Anders Hatlen }
a07b2874263e3c5f0cd2e83441719415d53059c2Knut Anders Hatlen
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik log_error("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen Austvik warn_melody();
49f592091468eac515dde6139fbc8efa26056b0aJorgen Austvik return -EPERM;
945f4c3c36a15447913781dfb1894b34f2941c57Jorgen Austvik }
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik log_info("%s", message_table[handle]);
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik dbus_error_init(&error);
d3d2404f9a49bf70b124053feabe666f85ef5361Knut Anders Hatlen r = bus_manager_shutdown_or_sleep_now_or_later(m, target_table[handle], inhibit_operation, &error);
d3d2404f9a49bf70b124053feabe666f85ef5361Knut Anders Hatlen if (r < 0) {
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik log_error("Failed to execute operation: %s", bus_error_message(&error));
780cc7d1b57609ff15fb283201e93cb501ebe9e6Jorgen Austvik dbus_error_free(&error);
7b9f9a1761f76744fc3772181877d5e301f122adKnut Anders Hatlen return r;
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco }
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen return 1;
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen}
0466de7c67573e1ce5e0733325c1e5383270f5d5Knut Anders Hatlen
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Koscostatic const char* const handle_action_table[_HANDLE_ACTION_MAX] = {
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco [HANDLE_IGNORE] = "ignore",
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos Kosco [HANDLE_POWEROFF] = "poweroff",
1ed6b730409d4740e941142599767d5eac7e7d92Lubos Kosco [HANDLE_REBOOT] = "reboot",
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik [HANDLE_HALT] = "halt",
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik [HANDLE_KEXEC] = "kexec",
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik [HANDLE_SUSPEND] = "suspend",
b34561d2c3d92fac37dbced05ba6a8738e3d20e9Lubos Kosco [HANDLE_HIBERNATE] = "hibernate",
b34561d2c3d92fac37dbced05ba6a8738e3d20e9Lubos Kosco [HANDLE_HYBRID_SLEEP] = "hybrid-sleep",
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik [HANDLE_LOCK] = "lock"
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik};
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik
c7eb123c8b2081a261deff3c401fbf92ddba1b58Jorgen AustvikDEFINE_STRING_TABLE_LOOKUP(handle_action, HandleAction);
5a0c5ad4116f5a4dd0dd5a0a4e6d02973cd5eef9Lubos KoscoDEFINE_CONFIG_PARSE_ENUM(config_parse_handle_action, handle_action, HandleAction, "Failed to parse handle action setting");
7ecd52b03dc1f0b03ff8f522b4891c8531896c3dJorgen Austvik