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