X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Flogin%2Flogind-button.c;h=e53dd63c29756e79088cf83c70f9dd361ffd2bbf;hp=8fdab789bcb346f2b73222d984376023bea7ba0e;hb=f3f6ae7c983865ea37e8ddfbd676586e55280f1e;hpb=8e7fd6ade44ce5dde0867ba748c7978ed1206865 diff --git a/src/login/logind-button.c b/src/login/logind-button.c index 8fdab789b..e53dd63c2 100644 --- a/src/login/logind-button.c +++ b/src/login/logind-button.c @@ -1,5 +1,3 @@ -/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ - /*** This file is part of systemd. @@ -19,20 +17,32 @@ along with systemd; If not, see . ***/ -#include -#include #include #include +#include #include #include #include -#include -#include "conf-parser.h" -#include "util.h" +#include "sd-messages.h" + +#include "alloc-util.h" +#include "fd-util.h" #include "logind-button.h" -#include "special.h" -#include "dbus-common.h" +#include "string-util.h" +#include "util.h" + +#define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d)) + +#define ULONG_BITS (sizeof(unsigned long)*8) + +static bool bitset_get(const unsigned long *bits, unsigned i) { + return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL; +} + +static void bitset_put(unsigned long *bits, unsigned i) { + bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS); +} Button* button_new(Manager *m, const char *name) { Button *b; @@ -45,15 +55,12 @@ Button* button_new(Manager *m, const char *name) { return NULL; b->name = strdup(name); - if (!b->name) { - free(b); - return NULL; - } + if (!b->name) + return mfree(b); if (hashmap_put(m->buttons, b->name, b) < 0) { free(b->name); - free(b); - return NULL; + return mfree(b); } b->manager = m; @@ -67,11 +74,14 @@ void button_free(Button *b) { hashmap_remove(b->manager->buttons, b->name); - if (b->fd >= 0) { - hashmap_remove(b->manager->button_fds, INT_TO_PTR(b->fd + 1)); - assert_se(epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_DEL, b->fd, NULL) == 0); - close_nointr_nofail(b->fd); - } + sd_event_source_unref(b->io_event_source); + sd_event_source_unref(b->check_event_source); + + if (b->fd >= 0) + /* If the device has been unplugged close() returns + * ENODEV, let's ignore this, hence we don't use + * safe_close() */ + (void) close(b->fd); free(b->name); free(b->seat); @@ -94,144 +104,53 @@ int button_set_seat(Button *b, const char *sn) { return 0; } -int button_open(Button *b) { - char name[256], *p; - struct epoll_event ev; - int r; - - assert(b); - - if (b->fd >= 0) { - close_nointr_nofail(b->fd); - b->fd = -1; - } - - p = strappend("/dev/input/", b->name); - if (!p) - return log_oom(); - - b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); - free(p); - if (b->fd < 0) { - log_warning("Failed to open %s: %m", b->name); - return -errno; - } - - if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) { - log_error("Failed to get input name: %m"); - r = -errno; - goto fail; - } +static void button_lid_switch_handle_action(Manager *manager, bool is_edge) { + HandleAction handle_action; - zero(ev); - ev.events = EPOLLIN; - ev.data.u32 = FD_OTHER_BASE + b->fd; + assert(manager); - if (epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_ADD, b->fd, &ev) < 0) { - log_error("Failed to add to epoll: %m"); - r = -errno; - goto fail; - } + /* If we are docked, handle the lid switch differently */ + if (manager_is_docked_or_external_displays(manager)) + handle_action = manager->handle_lid_switch_docked; + else + handle_action = manager->handle_lid_switch; - r = hashmap_put(b->manager->button_fds, INT_TO_PTR(b->fd + 1), b); - if (r < 0) { - log_error("Failed to add to hash map: %s", strerror(-r)); - assert_se(epoll_ctl(b->manager->epoll_fd, EPOLL_CTL_DEL, b->fd, NULL) == 0); - goto fail; - } + manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge); +} - log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name); +static int button_recheck(sd_event_source *e, void *userdata) { + Button *b = userdata; - return 0; + assert(b); + assert(b->lid_closed); -fail: - close_nointr_nofail(b->fd); - b->fd = -1; - return r; + button_lid_switch_handle_action(b->manager, false); + return 1; } -static int button_handle( - Button *b, - InhibitWhat inhibit_key, - HandleButton handle, - bool ignore_inhibited, - bool is_edge) { - - static const char * const message_table[_HANDLE_BUTTON_MAX] = { - [HANDLE_POWEROFF] = "Powering Off...", - [HANDLE_REBOOT] = "Rebooting...", - [HANDLE_HALT] = "Halting...", - [HANDLE_KEXEC] = "Rebooting via kexec...", - [HANDLE_SUSPEND] = "Suspending...", - [HANDLE_HIBERNATE] = "Hibernating..." - }; - - static const char * const target_table[_HANDLE_BUTTON_MAX] = { - [HANDLE_POWEROFF] = "poweroff.target", - [HANDLE_REBOOT] = "reboot.target", - [HANDLE_HALT] = "halt.target", - [HANDLE_KEXEC] = "kexec.target", - [HANDLE_SUSPEND] = "suspend.target", - [HANDLE_HIBERNATE] = "hibernate.target" - }; - - DBusError error; +static int button_install_check_event_source(Button *b) { int r; - InhibitWhat inhibit_operation; - assert(b); - /* If the key handling is turned off, don't do anything */ - if (handle == HANDLE_IGNORE) { - log_debug("Refusing key handling, as it is turned off."); - return 0; - } + /* Install a post handler, so that we keep rechecking as long as the lid is closed. */ - /* If the key handling is inhibited, don't do anything */ - if (manager_is_inhibited(b->manager, inhibit_key, INHIBIT_BLOCK, NULL, true, false, 0)) { - log_debug("Refusing key handling, %s is inhibited.", inhibit_what_to_string(inhibit_key)); + if (b->check_event_source) return 0; - } - - inhibit_operation = handle == HANDLE_SUSPEND || handle == HANDLE_HIBERNATE ? INHIBIT_SLEEP : INHIBIT_SHUTDOWN; - /* If the actual operation is inhibited, warn and fail */ - if (!ignore_inhibited && - manager_is_inhibited(b->manager, inhibit_operation, INHIBIT_BLOCK, NULL, false, false, 0)) { - - - /* If this is just a recheck of the lid switch then don't warn about anything */ - if (!is_edge) { - log_debug("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation)); - return 0; - } - - log_error("Refusing operation, %s is inhibited.", inhibit_what_to_string(inhibit_operation)); - warn_melody(); - return -EPERM; - } - - log_info("%s", message_table[handle]); - - /* We are executing the operation, so make sure we don't - * execute another one until the lid is opened/closed again */ - b->lid_close_queued = false; - - dbus_error_init(&error); - r = bus_manager_shutdown_or_sleep_now_or_later(b->manager, target_table[handle], inhibit_operation, &error); - if (r < 0) { - log_error("Failed to execute operation: %s", bus_error_message(&error)); - dbus_error_free(&error); + r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b); + if (r < 0) return r; - } - return 1; + return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1); } -int button_process(Button *b) { +static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) { + Button *b = userdata; struct input_event ev; ssize_t l; + assert(s); + assert(fd == b->fd); assert(b); l = read(b->fd, &ev, sizeof(ev)); @@ -246,8 +165,13 @@ int button_process(Button *b) { case KEY_POWER: case KEY_POWER2: - log_info("Power key pressed."); - return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true); + log_struct(LOG_INFO, + LOG_MESSAGE("Power key pressed."), + "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR, + NULL); + + manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true); + break; /* The kernel is a bit confused here: @@ -256,56 +180,217 @@ int button_process(Button *b) { */ case KEY_SLEEP: - log_info("Suspend key pressed."); - return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true); + log_struct(LOG_INFO, + LOG_MESSAGE("Suspend key pressed."), + "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR, + NULL); + + manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true); + break; case KEY_SUSPEND: - log_info("Hibernate key pressed."); - return button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true); + log_struct(LOG_INFO, + LOG_MESSAGE("Hibernate key pressed."), + "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR, + NULL); + + manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true); + break; } } else if (ev.type == EV_SW && ev.value > 0) { - switch (ev.code) { + if (ev.code == SW_LID) { + log_struct(LOG_INFO, + LOG_MESSAGE("Lid closed."), + "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR, + NULL); + + b->lid_closed = true; + button_lid_switch_handle_action(b->manager, true); + button_install_check_event_source(b); - case SW_LID: - log_info("Lid closed."); - b->lid_close_queued = true; + } else if (ev.code == SW_DOCK) { + log_struct(LOG_INFO, + LOG_MESSAGE("System docked."), + "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR, + NULL); - return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true); + b->docked = true; } } else if (ev.type == EV_SW && ev.value == 0) { - switch (ev.code) { + if (ev.code == SW_LID) { + log_struct(LOG_INFO, + LOG_MESSAGE("Lid opened."), + "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR, + NULL); - case SW_LID: - log_info("Lid opened."); - b->lid_close_queued = false; - break; + b->lid_closed = false; + b->check_event_source = sd_event_source_unref(b->check_event_source); + + } else if (ev.code == SW_DOCK) { + log_struct(LOG_INFO, + LOG_MESSAGE("System undocked."), + "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR, + NULL); + + b->docked = false; } } return 0; } -int button_recheck(Button *b) { +static int button_suitable(Button *b) { + unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1]; + assert(b); + assert(b->fd); - if (!b->lid_close_queued) - return 0; + if (ioctl(b->fd, EVIOCGBIT(EV_SYN, sizeof(types)), types) < 0) + return -errno; + + if (bitset_get(types, EV_KEY)) { + unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1]; + + if (ioctl(b->fd, EVIOCGBIT(EV_KEY, sizeof(keys)), keys) < 0) + return -errno; + + if (bitset_get(keys, KEY_POWER) || + bitset_get(keys, KEY_POWER2) || + bitset_get(keys, KEY_SLEEP) || + bitset_get(keys, KEY_SUSPEND)) + return true; + } + + if (bitset_get(types, EV_SW)) { + unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1]; + + if (ioctl(b->fd, EVIOCGBIT(EV_SW, sizeof(switches)), switches) < 0) + return -errno; + + if (bitset_get(switches, SW_LID) || + bitset_get(switches, SW_DOCK)) + return true; + } + + return false; +} + +static int button_set_mask(Button *b) { + unsigned long + types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {}, + keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1] = {}, + switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {}; + struct input_mask mask; + + assert(b); + assert(b->fd >= 0); + + bitset_put(types, EV_KEY); + bitset_put(types, EV_SW); + + mask = (struct input_mask) { + .type = EV_SYN, + .codes_size = sizeof(types), + .codes_ptr = PTR_TO_UINT64(types), + }; + + if (ioctl(b->fd, EVIOCSMASK, &mask) < 0) + /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */ + return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING, + errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", b->name); + + bitset_put(keys, KEY_POWER); + bitset_put(keys, KEY_POWER2); + bitset_put(keys, KEY_SLEEP); + bitset_put(keys, KEY_SUSPEND); - return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false); + mask = (struct input_mask) { + .type = EV_KEY, + .codes_size = sizeof(keys), + .codes_ptr = PTR_TO_UINT64(keys), + }; + + if (ioctl(b->fd, EVIOCSMASK, &mask) < 0) + return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", b->name); + + bitset_put(switches, SW_LID); + bitset_put(switches, SW_DOCK); + + mask = (struct input_mask) { + .type = EV_SW, + .codes_size = sizeof(switches), + .codes_ptr = PTR_TO_UINT64(switches), + }; + + if (ioctl(b->fd, EVIOCSMASK, &mask) < 0) + return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", b->name); + + return 0; } -static const char* const handle_button_table[_HANDLE_BUTTON_MAX] = { - [HANDLE_IGNORE] = "ignore", - [HANDLE_POWEROFF] = "poweroff", - [HANDLE_REBOOT] = "reboot", - [HANDLE_HALT] = "halt", - [HANDLE_KEXEC] = "kexec", - [HANDLE_SUSPEND] = "suspend", - [HANDLE_HIBERNATE] = "hibernate" -}; -DEFINE_STRING_TABLE_LOOKUP(handle_button, HandleButton); -DEFINE_CONFIG_PARSE_ENUM(config_parse_handle_button, handle_button, HandleButton, "Failed to parse handle button setting"); +int button_open(Button *b) { + char *p, name[256]; + int r; + + assert(b); + + b->fd = safe_close(b->fd); + + p = strjoina("/dev/input/", b->name); + + b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK); + if (b->fd < 0) + return log_warning_errno(errno, "Failed to open %s: %m", p); + + r = button_suitable(b); + if (r < 0) + return log_warning_errno(r, "Failed to determine whether input device is relevant to us: %m"); + if (r == 0) { + log_debug("Device %s does not expose keys or switches relevant to us, ignoring.", p); + return -EADDRNOTAVAIL; + } + + if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) { + r = log_error_errno(errno, "Failed to get input name: %m"); + goto fail; + } + + (void) button_set_mask(b); + + r = sd_event_add_io(b->manager->event, &b->io_event_source, b->fd, EPOLLIN, button_dispatch, b); + if (r < 0) { + log_error_errno(r, "Failed to add button event: %m"); + goto fail; + } + + log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name); + + return 0; + +fail: + b->fd = safe_close(b->fd); + return r; +} + +int button_check_switches(Button *b) { + unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {}; + assert(b); + + if (b->fd < 0) + return -EINVAL; + + if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0) + return -errno; + + b->lid_closed = bitset_get(switches, SW_LID); + b->docked = bitset_get(switches, SW_DOCK); + + if (b->lid_closed) + button_install_check_event_source(b); + + return 0; +}