logind-button.c revision cc3773810855956bad92337cee8fa193584ab62e
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering/***
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering This file is part of systemd.
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Copyright 2012 Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart 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
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
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 Poettering***/
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <assert.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <string.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <errno.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <fcntl.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <sys/ioctl.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <unistd.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include <linux/input.h>
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering#include "sd-messages.h"
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include "conf-parser.h"
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include "util.h"
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering#include "special.h"
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering#include "logind-button.h"
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart PoetteringButton* button_new(Manager *m, const char *name) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering Button *b;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(m);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering b = new0(Button, 1);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (!b)
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return NULL;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering b->name = strdup(name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (!b->name) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return NULL;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering }
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (hashmap_put(m->buttons, b->name, b) < 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b->name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return NULL;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering }
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering b->manager = m;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering b->fd = -1;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return b;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering}
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringvoid button_free(Button *b) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering hashmap_remove(b->manager->buttons, b->name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering sd_event_source_unref(b->event_source);
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd >= 0) {
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering /* If the device has been unplugged close() returns
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering * ENODEV, let's ignore this, hence we don't use
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering * close_nointr_nofail() */
c30a0c62fdbf6f11902be9db64ade99fb508adfdLennart Poettering close(b->fd);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering }
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b->name);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b->seat);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering}
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poetteringint button_set_seat(Button *b, const char *sn) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering char *s;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(sn);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering s = strdup(sn);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (!s)
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return -ENOMEM;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering free(b->seat);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering b->seat = s;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return 0;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering}
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poetteringstatic int button_handle(
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering Button *b,
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering InhibitWhat inhibit_key,
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering HandleAction handle,
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering bool ignore_inhibited,
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering bool is_edge) {
beaafb2ea6be591882aef21fe19b88e3b2461087Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering int r;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering r = manager_handle_action(b->manager, inhibit_key, handle, ignore_inhibited, is_edge);
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering if (r > 0)
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering /* We are executing the operation, so make sure we don't
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering * execute another one until the lid is opened/closed again */
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering b->lid_close_queued = false;
6de0e0e500d9d534c6e4baab242fc2a146f021faLennart Poettering
23406ce58aa7142e8df3c5c9e5ac34a01e90e3e0Lennart Poettering return r;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering}
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringstatic int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering Button *b = userdata;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering struct input_event ev;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering ssize_t l;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(s);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(fd == b->fd);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering assert(b);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering l = read(b->fd, &ev, sizeof(ev));
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (l < 0)
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return errno != EAGAIN ? -errno : 0;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if ((size_t) l < sizeof(ev))
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return -EIO;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering if (ev.type == EV_KEY && ev.value > 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering switch (ev.code) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering case KEY_POWER:
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering case KEY_POWER2:
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek log_struct(LOG_INFO,
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Power key pressed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_POWER_KEY),
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek NULL);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering /* The kernel is a bit confused here:
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering
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 */
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering case KEY_SLEEP:
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek log_struct(LOG_INFO,
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Suspend key pressed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_SUSPEND_KEY),
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek NULL);
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return button_handle(b, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering case KEY_SUSPEND:
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek log_struct(LOG_INFO,
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Hibernate key pressed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_HIBERNATE_KEY),
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek NULL);
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering return button_handle(b, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering }
8e7fd6ade44ce5dde0867ba748c7978ed1206865Lennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering } else if (ev.type == EV_SW && ev.value > 0) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering switch (ev.code) {
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering case SW_LID:
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek log_struct(LOG_INFO,
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Lid closed.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_LID_CLOSED),
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek NULL);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering b->lid_close_queued = true;
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, true);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering }
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering } else if (ev.type == EV_SW && ev.value == 0) {
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering switch (ev.code) {
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering case SW_LID:
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek log_struct(LOG_INFO,
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek "MESSAGE=Lid opened.",
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek MESSAGE_ID(SD_MESSAGE_LID_OPENED),
c485437f50450c0087bae3df4aed462e4a86ee83Zbigniew Jędrzejewski-Szmek NULL);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering b->lid_close_queued = false;
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering break;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering }
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering }
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering return 0;
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering}
e9d21f240704f87c6bb5f7fca1c5e6d0f31c84cdLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringint button_open(Button *b) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering char *p, name[256];
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering int r;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering assert(b);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd >= 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering close(b->fd);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering b->fd = -1;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering p = strappenda("/dev/input/", b->name);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering b->fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (b->fd < 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_warning("Failed to open %s: %m", b->name);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return -errno;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (ioctl(b->fd, EVIOCGNAME(sizeof(name)), name) < 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_error("Failed to get input name: %m");
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering r = -errno;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering goto fail;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering r = sd_event_add_io(b->manager->event, b->fd, EPOLLIN, button_dispatch, b, &b->event_source);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering if (r < 0) {
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_error("Failed to add button event: %s", strerror(-r));
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering goto fail;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering }
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering log_info("Watching system buttons on /dev/input/%s (%s)", b->name, name);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return 0;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
cc3773810855956bad92337cee8fa193584ab62eLennart Poetteringfail:
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering close(b->fd);
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering b->fd = -1;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering return r;
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering}
cc3773810855956bad92337cee8fa193584ab62eLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poetteringint button_recheck(Button *b) {
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering assert(b);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering if (!b->lid_close_queued)
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return 0;
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering return button_handle(b, INHIBIT_HANDLE_LID_SWITCH, b->manager->handle_lid_switch, b->manager->lid_switch_ignore_inhibited, false);
65b5116220a8ebf8a260716152409aa05377aaccLennart Poettering}