Debian Codegraph @

src/login/logind-core.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <fcntl.h>
4 #include <linux/vt.h>
5 #include <sys/ioctl.h>
6
7 #include "sd-bus.h"
8 #include "sd-device.h"
9
10 #include "alloc-util.h"
11 #include "battery-util.h"
12 #include "bus-error.h"
13 #include "bus-locator.h"
14 #include "cgroup-util.h"
15 #include "conf-parser.h"
16 #include "device-private.h"
17 #include "device-util.h"
18 #include "efi-loader.h"
19 #include "errno-util.h"
20 #include "fd-util.h"
21 #include "hashmap.h"
22 #include "limits-util.h"
23 #include "logind.h"
24 #include "logind-button.h"
25 #include "logind-device.h"
26 #include "logind-seat.h"
27 #include "logind-session.h"
28 #include "logind-user.h"
29 #include "parse-util.h"
30 #include "process-util.h"
31 #include "stdio-util.h"
32 #include "strv.h"
33 #include "terminal-util.h"
34 #include "udev-util.h"
35 #include "user-util.h"
36 #include "userdb.h"
37
38 void manager_reset_config(Manager *m) {
39 assert(m);
40
41 m->n_autovts = 6;
42 m->reserve_vt = 6;
43 m->remove_ipc = true;
44 m->inhibit_delay_max = 5 * USEC_PER_SEC;
45 m->user_stop_delay = 10 * USEC_PER_SEC;
46 m->wall_messages = true;
47
48 m->handle_action_sleep_mask = HANDLE_ACTION_SLEEP_MASK_DEFAULT;
49
50 m->handle_power_key = HANDLE_POWEROFF;
51 m->handle_power_key_long_press = HANDLE_IGNORE;
52 m->handle_reboot_key = HANDLE_REBOOT;
53 m->handle_reboot_key_long_press = HANDLE_POWEROFF;
54 m->handle_suspend_key = HANDLE_SUSPEND;
55 m->handle_suspend_key_long_press = HANDLE_HIBERNATE;
56 m->handle_hibernate_key = HANDLE_HIBERNATE;
57 m->handle_hibernate_key_long_press = HANDLE_IGNORE;
58 m->handle_secure_attention_key = HANDLE_SECURE_ATTENTION_KEY;
59
60 m->handle_lid_switch = HANDLE_SUSPEND;
61 m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID;
62 m->handle_lid_switch_docked = HANDLE_IGNORE;
63
64 m->power_key_ignore_inhibited = false;
65 m->suspend_key_ignore_inhibited = false;
66 m->hibernate_key_ignore_inhibited = false;
67 m->lid_switch_ignore_inhibited = true;
68 m->reboot_key_ignore_inhibited = false;
69
70 m->holdoff_timeout_usec = 30 * USEC_PER_SEC;
71
72 m->idle_action_usec = 30 * USEC_PER_MINUTE;
73 m->idle_action = HANDLE_IGNORE;
74
75 m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
76 m->runtime_dir_inodes = DIV_ROUND_UP(m->runtime_dir_size, 4096); /* 4k per inode */
77 m->sessions_max = 8192;
78 m->inhibitors_max = 8192;
79
80 m->kill_user_processes = KILL_USER_PROCESSES;
81
82 m->kill_only_users = strv_free(m->kill_only_users);
83 m->kill_exclude_users = strv_free(m->kill_exclude_users);
84
85 m->stop_idle_session_usec = USEC_INFINITY;
86
87 m->maintenance_time = calendar_spec_free(m->maintenance_time);
88 }
89
90 int manager_parse_config_file(Manager *m) {
91 assert(m);
92
93 return config_parse_standard_file_with_dropins(
94 "systemd/logind.conf",
95 "Login\0",
96 config_item_perf_lookup, logind_gperf_lookup,
97 CONFIG_PARSE_WARN,
98 /* userdata= */ m);
99 }
100
101 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **ret_device) {
102 Device *d;
103
104 assert(m);
105 assert(sysfs);
106
107 d = hashmap_get(m->devices, sysfs);
108 if (d)
109 /* we support adding master-flags, but not removing them */
110 d->master = d->master || master;
111 else {
112 d = device_new(m, sysfs, master);
113 if (!d)
114 return -ENOMEM;
115 }
116
117 if (ret_device)
118 *ret_device = d;
119
120 return 0;
121 }
122
123 int manager_add_seat(Manager *m, const char *id, Seat **ret_seat) {
124 Seat *s;
125 int r;
126
127 assert(m);
128 assert(id);
129
130 s = hashmap_get(m->seats, id);
131 if (!s) {
132 r = seat_new(m, id, &s);
133 if (r < 0)
134 return r;
135 }
136
137 if (ret_seat)
138 *ret_seat = s;
139
140 return 0;
141 }
142
143 int manager_add_session(Manager *m, const char *id, Session **ret_session) {
144 Session *s;
145 int r;
146
147 assert(m);
148 assert(id);
149
150 s = hashmap_get(m->sessions, id);
151 if (!s) {
152 r = session_new(m, id, &s);
153 if (r < 0)
154 return r;
155 }
156
157 if (ret_session)
158 *ret_session = s;
159
160 return 0;
161 }
162
163 int manager_add_user(
164 Manager *m,
165 UserRecord *ur,
166 User **ret_user) {
167
168 User *u;
169 int r;
170
171 assert(m);
172 assert(ur);
173
174 u = hashmap_get(m->users, UID_TO_PTR(ur->uid));
175 if (!u) {
176 r = user_new(m, ur, &u);
177 if (r < 0)
178 return r;
179 }
180
181 if (ret_user)
182 *ret_user = u;
183
184 return 0;
185 }
186
187 int manager_add_user_by_name(
188 Manager *m,
189 const char *name,
190 User **ret_user) {
191
192 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
193 int r;
194
195 assert(m);
196 assert(name);
197
198 r = userdb_by_name(name, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &ur);
199 if (r < 0)
200 return r;
201
202 if (!uid_is_valid(ur->uid)) /* Refuse users without UID */
203 return -ESRCH;
204
205 return manager_add_user(m, ur, ret_user);
206 }
207
208 int manager_add_user_by_uid(
209 Manager *m,
210 uid_t uid,
211 User **ret_user) {
212
213 _cleanup_(user_record_unrefp) UserRecord *ur = NULL;
214 int r;
215
216 assert(m);
217 assert(uid_is_valid(uid));
218
219 r = userdb_by_uid(uid, /* match= */ NULL, USERDB_SUPPRESS_SHADOW, &ur);
220 if (r < 0)
221 return r;
222
223 return manager_add_user(m, ur, ret_user);
224 }
225
226 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **ret) {
227 Inhibitor *i;
228 int r;
229
230 assert(m);
231 assert(id);
232
233 i = hashmap_get(m->inhibitors, id);
234 if (!i) {
235 r = inhibitor_new(m, id, &i);
236 if (r < 0)
237 return r;
238 }
239
240 if (ret)
241 *ret = i;
242
243 return 0;
244 }
245
246 int manager_add_button(Manager *m, const char *name, Button **ret_button) {
247 Button *b;
248 bool is_new;
249
250 assert(m);
251 assert(name);
252
253 b = hashmap_get(m->buttons, name);
254 is_new = !b;
255
256 if (!b) {
257 b = button_new(m, name);
258 if (!b)
259 return -ENOMEM;
260 }
261
262 if (ret_button)
263 *ret_button = b;
264
265 return is_new;
266 }
267
268 int manager_process_seat_device(Manager *m, sd_device *d) {
269 Device *device;
270 int r;
271
272 assert(m);
273
274 if (device_for_action(d, SD_DEVICE_REMOVE) ||
275 sd_device_has_current_tag(d, "seat") <= 0) {
276 const char *syspath;
277
278 r = sd_device_get_syspath(d, &syspath);
279 if (r < 0)
280 return 0;
281
282 device = hashmap_get(m->devices, syspath);
283 if (!device)
284 return 0;
285
286 seat_add_to_gc_queue(device->seat);
287 device_free(device);
288
289 } else {
290 const char *sn, *syspath;
291 bool master;
292 Seat *seat;
293
294 r = device_get_seat(d, &sn);
295 if (r < 0)
296 return r;
297
298 if (!seat_name_is_valid(sn)) {
299 log_device_warning(d, "Device with invalid seat name %s found, ignoring.", sn);
300 return 0;
301 }
302
303 seat = hashmap_get(m->seats, sn);
304 master = sd_device_has_current_tag(d, "master-of-seat") > 0;
305
306 /* Ignore non-master devices for unknown seats */
307 if (!master && !seat)
308 return 0;
309
310 r = sd_device_get_syspath(d, &syspath);
311 if (r < 0)
312 return r;
313
314 r = manager_add_device(m, syspath, master, &device);
315 if (r < 0)
316 return r;
317
318 if (!seat) {
319 r = manager_add_seat(m, sn, &seat);
320 if (r < 0) {
321 if (!device->seat)
322 device_free(device);
323
324 return r;
325 }
326 }
327
328 device_attach(device, seat);
329 seat_start(seat);
330 }
331
332 return 0;
333 }
334
335 int manager_process_button_device(Manager *m, sd_device *d, Button **ret_button) {
336 const char *sysname;
337 Button *b;
338 int r;
339
340 assert(m);
341
342 r = sd_device_get_sysname(d, &sysname);
343 if (r < 0)
344 return r;
345
346 if (device_for_action(d, SD_DEVICE_REMOVE) ||
347 sd_device_has_current_tag(d, "power-switch") <= 0) {
348
349 b = hashmap_get(m->buttons, sysname);
350 goto unwatch;
351 }
352
353 r = manager_add_button(m, sysname, &b);
354 if (r < 0)
355 return r;
356 bool is_new = r > 0;
357
358 const char *sn;
359 r = device_get_seat(d, &sn);
360 if (r < 0)
361 return r;
362
363 button_set_seat(b, sn);
364
365 r = button_open(b);
366 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
367 * opening the device?) let's close the button again. */
368 goto unwatch;
369
370 if (ret_button)
371 *ret_button = b;
372
373 return is_new;
374
375 unwatch:
376 button_free(b);
377
378 if (ret_button)
379 *ret_button = NULL;
380
381 return 0;
382 }
383
384 int manager_get_session_by_pidref(Manager *m, const PidRef *pid, Session **ret) {
385 _cleanup_free_ char *unit = NULL;
386 Session *s = NULL;
387 int r;
388
389 assert(m);
390
391 if (!pidref_is_set(pid))
392 return -EINVAL;
393
394 r = manager_get_session_by_leader(m, pid, ret);
395 if (r != 0)
396 return r;
397
398 r = cg_pidref_get_unit(pid, &unit);
399 if (r >= 0)
400 s = hashmap_get(m->session_units, unit);
401
402 if (ret)
403 *ret = s;
404
405 return !!s;
406 }
407
408 int manager_get_session_by_leader(Manager *m, const PidRef *pid, Session **ret) {
409 Session *s;
410 int r;
411
412 assert(m);
413
414 if (!pidref_is_set(pid))
415 return -EINVAL;
416
417 s = hashmap_get(m->sessions_by_leader, pid);
418 if (s) {
419 r = pidref_verify(pid);
420 if (r < 0)
421 return r;
422 }
423
424 if (ret)
425 *ret = s;
426
427 return !!s;
428 }
429
430 int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) {
431 _cleanup_free_ char *unit = NULL;
432 User *u = NULL;
433 int r;
434
435 assert(m);
436
437 if (!pid_is_valid(pid))
438 return -EINVAL;
439
440 r = cg_pid_get_slice(pid, &unit);
441 if (r >= 0)
442 u = hashmap_get(m->user_units, unit);
443
444 if (ret)
445 *ret = u;
446
447 return !!u;
448 }
449
450 bool manager_get_idle_hint(Manager *m, dual_timestamp *ret_timestamp) {
451 Session *s;
452 bool idle_hint;
453 dual_timestamp ts;
454
455 assert(m);
456
457 /* Initialize the baseline timestamp with the time the manager got initialized to avoid reporting
458 * unreasonable large idle periods starting with the Unix epoch. */
459 ts = m->init_ts;
460
461 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, /* since= */ NULL, /* flags= */ 0, UID_INVALID, NULL);
462
463 HASHMAP_FOREACH(s, m->sessions) {
464 dual_timestamp k;
465 bool ih;
466
467 if (!SESSION_CLASS_CAN_IDLE(s->class))
468 continue;
469
470 ih = session_get_idle_hint(s, &k);
471 if (!ih) {
472 if (!idle_hint) {
473 if (k.monotonic < ts.monotonic)
474 ts = k;
475 } else {
476 idle_hint = false;
477 ts = k;
478 }
479 } else if (idle_hint) {
480
481 if (k.monotonic > ts.monotonic)
482 ts = k;
483 }
484 }
485
486 if (ret_timestamp)
487 *ret_timestamp = ts;
488
489 return idle_hint;
490 }
491
492 bool manager_shall_kill(Manager *m, const char *user) {
493 assert(m);
494 assert(user);
495
496 if (!m->kill_exclude_users && streq(user, "root"))
497 return false;
498
499 if (strv_contains(m->kill_exclude_users, user))
500 return false;
501
502 if (!strv_isempty(m->kill_only_users))
503 return strv_contains(m->kill_only_users, user);
504
505 return m->kill_user_processes;
506 }
507
508 int config_parse_n_autovts(
509 const char *unit,
510 const char *filename,
511 unsigned line,
512 const char *section,
513 unsigned section_line,
514 const char *lvalue,
515 int ltype,
516 const char *rvalue,
517 void *data,
518 void *userdata) {
519
520 unsigned *n = ASSERT_PTR(data);
521 unsigned o;
522 int r;
523
524 assert(filename);
525 assert(lvalue);
526 assert(rvalue);
527
528 r = safe_atou(rvalue, &o);
529 if (r < 0) {
530 log_syntax(unit, LOG_WARNING, filename, line, r,
531 "Failed to parse number of autovts, ignoring: %s", rvalue);
532 return 0;
533 }
534
535 if (o > 15) {
536 log_syntax(unit, LOG_WARNING, filename, line, 0,
537 "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
538 return 0;
539 }
540
541 *n = o;
542 return 0;
543 }
544
545 static int vt_is_busy(unsigned vtnr) {
546 struct vt_stat vt_stat;
547 int r;
548 _cleanup_close_ int fd = -EBADF;
549
550 assert(vtnr >= 1);
551
552 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
553 assert(vtnr <= 15);
554
555 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
556 * we'd open the latter we'd open the foreground tty which
557 * hence would be unconditionally busy. By opening /dev/tty1
558 * we avoid this. Since tty1 is special and needs to be an
559 * explicitly loaded getty or DM this is safe. */
560
561 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
562 if (fd < 0)
563 return -errno;
564
565 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
566 r = -errno;
567 else
568 r = !!(vt_stat.v_state & (1 << vtnr));
569
570 return r;
571 }
572
573 int manager_spawn_autovt(Manager *m, unsigned vtnr) {
574 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
575 char name[sizeof("[email protected]") + DECIMAL_STR_MAX(unsigned)];
576 int r;
577
578 assert(m);
579 assert(vtnr >= 1);
580
581 if (vtnr > m->n_autovts &&
582 vtnr != m->reserve_vt)
583 return 0;
584
585 if (vtnr != m->reserve_vt) {
586 /* If this is the reserved TTY, we'll start the getty
587 * on it in any case, but otherwise only if it is not
588 * busy. */
589
590 r = vt_is_busy(vtnr);
591 if (r < 0)
592 return r;
593 else if (r > 0)
594 return -EBUSY;
595 }
596
597 xsprintf(name, "autovt@tty%u.service", vtnr);
598 r = bus_call_method(m->bus, bus_systemd_mgr, "StartUnit", &error, NULL, "ss", name, "fail");
599 if (r < 0)
600 return log_error_errno(r, "Failed to start %s: %s", name, bus_error_message(&error, r));
601
602 return 0;
603 }
604
605 bool manager_is_lid_closed(Manager *m) {
606 Button *b;
607
608 HASHMAP_FOREACH(b, m->buttons)
609 if (b->lid_closed)
610 return true;
611
612 return false;
613 }
614
615 static bool manager_is_docked(Manager *m) {
616 Button *b;
617
618 HASHMAP_FOREACH(b, m->buttons)
619 if (b->docked)
620 return true;
621
622 return false;
623 }
624
625 static int manager_count_external_displays(Manager *m) {
626 _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL;
627 int r, n = 0;
628
629 r = sd_device_enumerator_new(&e);
630 if (r < 0)
631 return r;
632
633 r = sd_device_enumerator_allow_uninitialized(e);
634 if (r < 0)
635 return r;
636
637 r = sd_device_enumerator_add_match_subsystem(e, "drm", true);
638 if (r < 0)
639 return r;
640
641 FOREACH_DEVICE(e, d) {
642 sd_device *p;
643
644 if (sd_device_get_parent(d, &p) < 0)
645 continue;
646
647 /* If the parent shares the same subsystem as the
648 * device we are looking at then it is a connector,
649 * which is what we are interested in. */
650 r = device_in_subsystem(p, "drm");
651 if (r < 0)
652 return r;
653 if (r == 0)
654 continue;
655
656 const char *nn;
657 r = sd_device_get_sysname(d, &nn);
658 if (r < 0)
659 return r;
660
661 /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash
662 * separated item (the first is the card name, the last the connector number). We implement a
663 * deny list of external displays here, rather than an allow list of internal ones, to ensure
664 * we don't block suspends too eagerly. */
665 const char *dash = strchr(nn, '-');
666 if (!dash)
667 continue;
668
669 dash++;
670 if (!STARTSWITH_SET(dash,
671 "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
672 "Composite-", "SVIDEO-", "Component-",
673 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-"))
674 continue;
675
676 /* Ignore ports that are not enabled */
677 r = device_get_sysattr_streq(d, "enabled", "enabled");
678 if (IN_SET(r, 0, -ENOENT))
679 continue;
680 if (r < 0)
681 return r;
682
683 /* We count any connector which is not explicitly "disconnected" as connected. */
684 r = device_get_sysattr_streq(d, "status", "disconnected");
685 if (r < 0 && r != -ENOENT)
686 return r;
687 if (r <= 0)
688 n++;
689 }
690
691 return n;
692 }
693
694 bool manager_is_docked_or_external_displays(Manager *m) {
695 int n;
696
697 /* If we are docked don't react to lid closing */
698 if (manager_is_docked(m)) {
699 log_debug("System is docked.");
700 return true;
701 }
702
703 /* If we have more than one display connected,
704 * assume that we are docked. */
705 n = manager_count_external_displays(m);
706 if (n < 0)
707 log_warning_errno(n, "Display counting failed: %m");
708 else if (n >= 1) {
709 log_debug("External (%i) displays connected.", n);
710 return true;
711 }
712
713 return false;
714 }
715
716 bool manager_is_on_external_power(void) {
717 int r;
718
719 /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal
720 * battery */
721 r = on_ac_power();
722 if (r < 0)
723 log_warning_errno(r, "Failed to read AC power status: %m");
724
725 return r != 0; /* Treat failure as 'on AC' */
726 }
727
728 bool manager_all_buttons_ignored(Manager *m) {
729 assert(m);
730
731 if (m->handle_power_key != HANDLE_IGNORE)
732 return false;
733 if (m->handle_power_key_long_press != HANDLE_IGNORE)
734 return false;
735 if (m->handle_suspend_key != HANDLE_IGNORE)
736 return false;
737 if (m->handle_suspend_key_long_press != HANDLE_IGNORE)
738 return false;
739 if (m->handle_hibernate_key != HANDLE_IGNORE)
740 return false;
741 if (m->handle_hibernate_key_long_press != HANDLE_IGNORE)
742 return false;
743 if (m->handle_reboot_key != HANDLE_IGNORE)
744 return false;
745 if (m->handle_reboot_key_long_press != HANDLE_IGNORE)
746 return false;
747 if (m->handle_lid_switch != HANDLE_IGNORE)
748 return false;
749 if (!IN_SET(m->handle_lid_switch_ep, _HANDLE_ACTION_INVALID, HANDLE_IGNORE))
750 return false;
751 if (m->handle_lid_switch_docked != HANDLE_IGNORE)
752 return false;
753 if (m->handle_secure_attention_key != HANDLE_IGNORE)
754 return false;
755
756 return true;
757 }
758
759
760 int manager_read_efi_boot_loader_entries(Manager *m) {
761 #if ENABLE_EFI
762 int r;
763
764 assert(m);
765 if (m->efi_boot_loader_entries_set)
766 return 0;
767
768 r = efi_loader_get_entries(&m->efi_boot_loader_entries);
769 if (r == -ENOENT || ERRNO_IS_NEG_NOT_SUPPORTED(r)) {
770 log_debug_errno(r, "Boot loader reported no entries.");
771 m->efi_boot_loader_entries_set = true;
772 return 0;
773 }
774 if (r < 0)
775 return log_error_errno(r, "Failed to determine entries reported by boot loader: %m");
776
777 m->efi_boot_loader_entries_set = true;
778 return 1;
779 #else
780 return 0;
781 #endif
782 }