]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-button.c
Merge pull request #12390 from poettering/string-file-mkdir
[thirdparty/systemd.git] / src / login / logind-button.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
e9d21f24 2
e9d21f24
LP
3#include <errno.h>
4#include <fcntl.h>
07630cea 5#include <string.h>
e9d21f24
LP
6#include <sys/ioctl.h>
7#include <unistd.h>
8#include <linux/input.h>
e9d21f24 9
cc377381 10#include "sd-messages.h"
07630cea 11
b5efdb8a 12#include "alloc-util.h"
3ffd4af2
LP
13#include "fd-util.h"
14#include "logind-button.h"
36dd5ffd 15#include "missing_input.h"
07630cea 16#include "string-util.h"
e9d21f24 17#include "util.h"
e9d21f24 18
d5dd44b0
LP
19#define CONST_MAX4(a, b, c, d) CONST_MAX(CONST_MAX(a, b), CONST_MAX(c, d))
20
21#define ULONG_BITS (sizeof(unsigned long)*8)
22
23static bool bitset_get(const unsigned long *bits, unsigned i) {
24 return (bits[i / ULONG_BITS] >> (i % ULONG_BITS)) & 1UL;
25}
26
27static void bitset_put(unsigned long *bits, unsigned i) {
28 bits[i / ULONG_BITS] |= (unsigned long) 1 << (i % ULONG_BITS);
29}
30
e9d21f24
LP
31Button* button_new(Manager *m, const char *name) {
32 Button *b;
33
34 assert(m);
35 assert(name);
36
37 b = new0(Button, 1);
38 if (!b)
39 return NULL;
40
41 b->name = strdup(name);
6b430fdb
ZJS
42 if (!b->name)
43 return mfree(b);
e9d21f24
LP
44
45 if (hashmap_put(m->buttons, b->name, b) < 0) {
46 free(b->name);
6b430fdb 47 return mfree(b);
e9d21f24
LP
48 }
49
50 b->manager = m;
51 b->fd = -1;
52
53 return b;
54}
55
56void button_free(Button *b) {
57 assert(b);
58
59 hashmap_remove(b->manager->buttons, b->name);
60
ed4ba7e4
LP
61 sd_event_source_unref(b->io_event_source);
62 sd_event_source_unref(b->check_event_source);
c30a0c62 63
ece174c5 64 if (b->fd >= 0)
c30a0c62
LP
65 /* If the device has been unplugged close() returns
66 * ENODEV, let's ignore this, hence we don't use
03e334a1 67 * safe_close() */
4fba5796 68 (void) close(b->fd);
e9d21f24
LP
69
70 free(b->name);
71 free(b->seat);
72 free(b);
73}
74
75int button_set_seat(Button *b, const char *sn) {
76 char *s;
77
78 assert(b);
79 assert(sn);
80
81 s = strdup(sn);
82 if (!s)
83 return -ENOMEM;
84
85 free(b->seat);
86 b->seat = s;
87
88 return 0;
89}
90
3c56cab4
BW
91static void button_lid_switch_handle_action(Manager *manager, bool is_edge) {
92 HandleAction handle_action;
93
94 assert(manager);
95
e25937a3
SF
96 /* If we are docked or on external power, handle the lid switch
97 * differently */
602a41c2 98 if (manager_is_docked_or_external_displays(manager))
3c56cab4 99 handle_action = manager->handle_lid_switch_docked;
e25937a3
SF
100 else if (manager->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
101 manager_is_on_external_power())
102 handle_action = manager->handle_lid_switch_ep;
3c56cab4
BW
103 else
104 handle_action = manager->handle_lid_switch;
105
106 manager_handle_action(manager, INHIBIT_HANDLE_LID_SWITCH, handle_action, manager->lid_switch_ignore_inhibited, is_edge);
107}
108
ed4ba7e4
LP
109static int button_recheck(sd_event_source *e, void *userdata) {
110 Button *b = userdata;
beaafb2e 111
ed4ba7e4
LP
112 assert(b);
113 assert(b->lid_closed);
114
3c56cab4 115 button_lid_switch_handle_action(b->manager, false);
ed4ba7e4
LP
116 return 1;
117}
e9d21f24 118
ed4ba7e4
LP
119static int button_install_check_event_source(Button *b) {
120 int r;
e9d21f24
LP
121 assert(b);
122
ed4ba7e4 123 /* Install a post handler, so that we keep rechecking as long as the lid is closed. */
6de0e0e5 124
ed4ba7e4
LP
125 if (b->check_event_source)
126 return 0;
127
128 r = sd_event_add_post(b->manager->event, &b->check_event_source, button_recheck, b);
129 if (r < 0)
130 return r;
131
132 return sd_event_source_set_priority(b->check_event_source, SD_EVENT_PRIORITY_IDLE+1);
e9d21f24
LP
133}
134
cc377381
LP
135static int button_dispatch(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
136 Button *b = userdata;
e9d21f24
LP
137 struct input_event ev;
138 ssize_t l;
139
cc377381
LP
140 assert(s);
141 assert(fd == b->fd);
e9d21f24
LP
142 assert(b);
143
144 l = read(b->fd, &ev, sizeof(ev));
145 if (l < 0)
146 return errno != EAGAIN ? -errno : 0;
147 if ((size_t) l < sizeof(ev))
148 return -EIO;
149
e9d21f24
LP
150 if (ev.type == EV_KEY && ev.value > 0) {
151
152 switch (ev.code) {
153
154 case KEY_POWER:
155 case KEY_POWER2:
c485437f 156 log_struct(LOG_INFO,
e2cc6eca 157 LOG_MESSAGE("Power key pressed."),
a1230ff9 158 "MESSAGE_ID=" SD_MESSAGE_POWER_KEY_STR);
7b77ed8c 159
ed4ba7e4 160 manager_handle_action(b->manager, INHIBIT_HANDLE_POWER_KEY, b->manager->handle_power_key, b->manager->power_key_ignore_inhibited, true);
7b77ed8c 161 break;
e9d21f24 162
8e7fd6ad
LP
163 /* The kernel is a bit confused here:
164
165 KEY_SLEEP = suspend-to-ram, which everybody else calls "suspend"
166 KEY_SUSPEND = suspend-to-disk, which everybody else calls "hibernate"
167 */
168
e9d21f24 169 case KEY_SLEEP:
c485437f 170 log_struct(LOG_INFO,
e2cc6eca 171 LOG_MESSAGE("Suspend key pressed."),
a1230ff9 172 "MESSAGE_ID=" SD_MESSAGE_SUSPEND_KEY_STR);
7b77ed8c 173
ed4ba7e4 174 manager_handle_action(b->manager, INHIBIT_HANDLE_SUSPEND_KEY, b->manager->handle_suspend_key, b->manager->suspend_key_ignore_inhibited, true);
7b77ed8c 175 break;
e9d21f24 176
8e7fd6ad 177 case KEY_SUSPEND:
c485437f 178 log_struct(LOG_INFO,
e2cc6eca 179 LOG_MESSAGE("Hibernate key pressed."),
a1230ff9 180 "MESSAGE_ID=" SD_MESSAGE_HIBERNATE_KEY_STR);
7b77ed8c 181
ed4ba7e4 182 manager_handle_action(b->manager, INHIBIT_HANDLE_HIBERNATE_KEY, b->manager->handle_hibernate_key, b->manager->hibernate_key_ignore_inhibited, true);
7b77ed8c 183 break;
e9d21f24 184 }
8e7fd6ad 185
e9d21f24
LP
186 } else if (ev.type == EV_SW && ev.value > 0) {
187
7b77ed8c 188 if (ev.code == SW_LID) {
c485437f 189 log_struct(LOG_INFO,
e2cc6eca 190 LOG_MESSAGE("Lid closed."),
a1230ff9 191 "MESSAGE_ID=" SD_MESSAGE_LID_CLOSED_STR);
65b51162 192
ed4ba7e4 193 b->lid_closed = true;
3c56cab4 194 button_lid_switch_handle_action(b->manager, true);
ed4ba7e4 195 button_install_check_event_source(b);
2d62c530
LP
196
197 } else if (ev.code == SW_DOCK) {
198 log_struct(LOG_INFO,
e2cc6eca 199 LOG_MESSAGE("System docked."),
a1230ff9 200 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_DOCKED_STR);
2d62c530
LP
201
202 b->docked = true;
65b51162
LP
203 }
204
205 } else if (ev.type == EV_SW && ev.value == 0) {
206
7b77ed8c 207 if (ev.code == SW_LID) {
c485437f 208 log_struct(LOG_INFO,
e2cc6eca 209 LOG_MESSAGE("Lid opened."),
a1230ff9 210 "MESSAGE_ID=" SD_MESSAGE_LID_OPENED_STR);
7b77ed8c 211
ed4ba7e4
LP
212 b->lid_closed = false;
213 b->check_event_source = sd_event_source_unref(b->check_event_source);
2d62c530
LP
214
215 } else if (ev.code == SW_DOCK) {
216 log_struct(LOG_INFO,
e2cc6eca 217 LOG_MESSAGE("System undocked."),
a1230ff9 218 "MESSAGE_ID=" SD_MESSAGE_SYSTEM_UNDOCKED_STR);
2d62c530
LP
219
220 b->docked = false;
e9d21f24
LP
221 }
222 }
223
224 return 0;
225}
226
92c60579 227static int button_suitable(int fd) {
2546b70a
LP
228 unsigned long types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1];
229
92c60579 230 assert(fd >= 0);
2546b70a 231
92c60579 232 if (ioctl(fd, EVIOCGBIT(EV_SYN, sizeof types), types) < 0)
2546b70a
LP
233 return -errno;
234
235 if (bitset_get(types, EV_KEY)) {
236 unsigned long keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1];
237
92c60579 238 if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof keys), keys) < 0)
2546b70a
LP
239 return -errno;
240
241 if (bitset_get(keys, KEY_POWER) ||
242 bitset_get(keys, KEY_POWER2) ||
243 bitset_get(keys, KEY_SLEEP) ||
244 bitset_get(keys, KEY_SUSPEND))
245 return true;
246 }
247
248 if (bitset_get(types, EV_SW)) {
249 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1];
250
92c60579 251 if (ioctl(fd, EVIOCGBIT(EV_SW, sizeof switches), switches) < 0)
2546b70a
LP
252 return -errno;
253
254 if (bitset_get(switches, SW_LID) ||
255 bitset_get(switches, SW_DOCK))
256 return true;
257 }
258
259 return false;
260}
261
92c60579 262static int button_set_mask(const char *name, int fd) {
d5dd44b0
LP
263 unsigned long
264 types[CONST_MAX(EV_KEY, EV_SW)/ULONG_BITS+1] = {},
265 keys[CONST_MAX4(KEY_POWER, KEY_POWER2, KEY_SLEEP, KEY_SUSPEND)/ULONG_BITS+1] = {},
266 switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
267 struct input_mask mask;
268
92c60579
ZJS
269 assert(name);
270 assert(fd >= 0);
d5dd44b0
LP
271
272 bitset_put(types, EV_KEY);
273 bitset_put(types, EV_SW);
274
275 mask = (struct input_mask) {
276 .type = EV_SYN,
277 .codes_size = sizeof(types),
278 .codes_ptr = PTR_TO_UINT64(types),
279 };
280
92c60579 281 if (ioctl(fd, EVIOCSMASK, &mask) < 0)
d5dd44b0
LP
282 /* Log only at debug level if the kernel doesn't do EVIOCSMASK yet */
283 return log_full_errno(IN_SET(errno, ENOTTY, EOPNOTSUPP, EINVAL) ? LOG_DEBUG : LOG_WARNING,
92c60579 284 errno, "Failed to set EV_SYN event mask on /dev/input/%s: %m", name);
d5dd44b0
LP
285
286 bitset_put(keys, KEY_POWER);
287 bitset_put(keys, KEY_POWER2);
288 bitset_put(keys, KEY_SLEEP);
289 bitset_put(keys, KEY_SUSPEND);
290
291 mask = (struct input_mask) {
292 .type = EV_KEY,
293 .codes_size = sizeof(keys),
294 .codes_ptr = PTR_TO_UINT64(keys),
295 };
296
92c60579
ZJS
297 if (ioctl(fd, EVIOCSMASK, &mask) < 0)
298 return log_warning_errno(errno, "Failed to set EV_KEY event mask on /dev/input/%s: %m", name);
d5dd44b0
LP
299
300 bitset_put(switches, SW_LID);
301 bitset_put(switches, SW_DOCK);
302
303 mask = (struct input_mask) {
304 .type = EV_SW,
305 .codes_size = sizeof(switches),
306 .codes_ptr = PTR_TO_UINT64(switches),
307 };
308
92c60579
ZJS
309 if (ioctl(fd, EVIOCSMASK, &mask) < 0)
310 return log_warning_errno(errno, "Failed to set EV_SW event mask on /dev/input/%s: %m", name);
d5dd44b0
LP
311
312 return 0;
313}
314
cc377381 315int button_open(Button *b) {
92c60579
ZJS
316 _cleanup_close_ int fd = -1;
317 const char *p;
318 char name[256];
cc377381
LP
319 int r;
320
321 assert(b);
322
7f6e12b0 323 b->fd = safe_close(b->fd);
cc377381 324
63c372cb 325 p = strjoina("/dev/input/", b->name);
cc377381 326
92c60579
ZJS
327 fd = open(p, O_RDWR|O_CLOEXEC|O_NOCTTY|O_NONBLOCK);
328 if (fd < 0)
2546b70a
LP
329 return log_warning_errno(errno, "Failed to open %s: %m", p);
330
92c60579 331 r = button_suitable(fd);
2546b70a 332 if (r < 0)
92c60579 333 return log_warning_errno(r, "Failed to determine whether input device %s is relevant to us: %m", p);
baaa35ad
ZJS
334 if (r == 0)
335 return log_debug_errno(SYNTHETIC_ERRNO(EADDRNOTAVAIL),
92c60579 336 "Device %s does not expose keys or switches relevant to us, ignoring.", p);
cc377381 337
92c60579
ZJS
338 if (ioctl(fd, EVIOCGNAME(sizeof name), name) < 0)
339 return log_error_errno(errno, "Failed to get input name for %s: %m", p);
cc377381 340
92c60579 341 (void) button_set_mask(b->name, fd);
b2774a3a 342
343 b->io_event_source = sd_event_source_unref(b->io_event_source);
12e98242 344 r = sd_event_add_io(b->manager->event, &b->io_event_source, fd, EPOLLIN, button_dispatch, b);
92c60579
ZJS
345 if (r < 0)
346 return log_error_errno(r, "Failed to add button event for %s: %m", p);
cc377381 347
92c60579
ZJS
348 b->fd = TAKE_FD(fd);
349 log_info("Watching system buttons on %s (%s)", p, name);
cc377381 350 return 0;
cc377381
LP
351}
352
2d62c530 353int button_check_switches(Button *b) {
d5dd44b0 354 unsigned long switches[CONST_MAX(SW_LID, SW_DOCK)/ULONG_BITS+1] = {};
65b51162
LP
355 assert(b);
356
ed4ba7e4
LP
357 if (b->fd < 0)
358 return -EINVAL;
359
360 if (ioctl(b->fd, EVIOCGSW(sizeof(switches)), switches) < 0)
361 return -errno;
65b51162 362
d5dd44b0
LP
363 b->lid_closed = bitset_get(switches, SW_LID);
364 b->docked = bitset_get(switches, SW_DOCK);
ed4ba7e4 365
2d62c530 366 if (b->lid_closed)
ed4ba7e4 367 button_install_check_event_source(b);
ed4ba7e4
LP
368
369 return 0;
65b51162 370}