logind-button.c revision 8e7fd6ade44ce5dde0867ba748c7978ed1206865
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering This file is part of systemd.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Copyright 2012 Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering systemd is free software; you can redistribute it and/or modify it
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering under the terms of the GNU Lesser General Public License as published by
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering the Free Software Foundation; either version 2.1 of the License, or
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering (at your option) any later version.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering systemd is distributed in the hope that it will be useful, but
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering WITHOUT ANY WARRANTY; without even the implied warranty of
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Lesser General Public License for more details.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering You should have received a copy of the GNU Lesser General Public License
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering along with systemd; If not, see <http://www.gnu.org/licenses/>.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart PoetteringButton* button_new(Manager *m, const char *name) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (hashmap_put(m->buttons, b->name, b) < 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering hashmap_remove(b->manager->buttons, b->name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (b->fd >= 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering hashmap_remove(b->manager->button_fds, INT_TO_PTR(b->fd + 1));
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert_se(epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_DEL, b->fd, NULL) == 0);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringint button_set_seat(Button *b, const char *sn) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (b->fd >= 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (b->fd < 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering log_warning("Failed to open %s: %m", b->name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering log_error("Failed to get input name: %m");
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_ADD, b->fd, &ev) < 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering r = hashmap_put(b->manager->button_fds, INT_TO_PTR(b->fd + 1), b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering log_error("Failed to add to hash map: %s", strerror(-r));
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert_se(epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_DEL, b->fd, NULL) == 0);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering static const char * const message_table[_HANDLE_BUTTON_MAX] = {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering [HANDLE_KEXEC] = "Rebooting via kexec...",
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering static const char * const target_table[_HANDLE_BUTTON_MAX] = {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* If the key handling is turned off, don't do anything */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering log_debug("Refusing key handling, as it is turned off.");
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* If the key handling is inhibited, don't do anything */
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering if (manager_is_inhibited(b->manager, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0)) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering log_debug("Refusing key handling, %s is inhibited.", inhibit_what_to_string(inhibit_key));
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN;
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering /* If the actual operation is inhibited, warn and fail */
409133be63387fc04d927e8aecd2f6ba03d2f143Lennart Poettering manager_is_inhibited(b->manager, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0)) {
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering /* If this is just a recheck of the lid switch then don't warn about anything */
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering log_error("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation));
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering /* We are executing the operation, so make sure we don't
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering * execute another one until the lid is opened/closed again */
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, target_table[handle], inhibit_operation, &error);
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering log_error("Failed to execute operation: %s", bus_error_message(&error));
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering /* The kernel is a bit confused here:
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering } else if (ev.type == EV_SW && ev.value > 0) {
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering } else if (ev.type == EV_SW && ev.value == 0) {
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringstatic const char* const handle_button_table[_HANDLE_BUTTON_MAX] = {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart PoetteringDEFINE_STRING_TABLE_LOOKUP(handle_button, HandleButton);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart PoetteringDEFINE_CONFIG_PARSE_ENUM(config_parse_handle_button, handle_button, HandleButton, "Failed to parse handle button setting");