chiark / gitweb /
49c1cbcfb20337a886e4fbefaca0eeb61e41c69c
[elogind.git] / src / login / logind-core.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <fcntl.h>
4 #include <pwd.h>
5 #include <sys/ioctl.h>
6 #include <sys/types.h>
7 #include <linux/vt.h>
8
9 #include "alloc-util.h"
10 #include "bus-error.h"
11 #include "bus-util.h"
12 #include "cgroup-util.h"
13 //#include "conf-parser.h"
14 #include "fd-util.h"
15 #include "logind.h"
16 #include "parse-util.h"
17 #include "process-util.h"
18 #include "strv.h"
19 #include "terminal-util.h"
20 #include "udev-util.h"
21 #include "user-util.h"
22
23 void manager_reset_config(Manager *m) {
24         assert(m);
25
26 #if 0 /// elogind does not support autospawning of vts
27         m->n_autovts = 6;
28         m->reserve_vt = 6;
29 #endif // 0
30         m->remove_ipc = true;
31         m->inhibit_delay_max = 5 * USEC_PER_SEC;
32         m->handle_power_key = HANDLE_POWEROFF;
33         m->handle_suspend_key = HANDLE_SUSPEND;
34         m->handle_hibernate_key = HANDLE_HIBERNATE;
35         m->handle_lid_switch = HANDLE_SUSPEND;
36         m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID;
37         m->handle_lid_switch_docked = HANDLE_IGNORE;
38         m->power_key_ignore_inhibited = false;
39         m->suspend_key_ignore_inhibited = false;
40         m->hibernate_key_ignore_inhibited = false;
41         m->lid_switch_ignore_inhibited = true;
42
43         m->holdoff_timeout_usec = 30 * USEC_PER_SEC;
44
45         m->idle_action_usec = 30 * USEC_PER_MINUTE;
46         m->idle_action = HANDLE_IGNORE;
47
48         m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */
49         m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */
50         m->sessions_max = 8192;
51         m->inhibitors_max = 8192;
52
53         m->kill_user_processes = KILL_USER_PROCESSES;
54
55         m->kill_only_users = strv_free(m->kill_only_users);
56         m->kill_exclude_users = strv_free(m->kill_exclude_users);
57 }
58
59 int manager_parse_config_file(Manager *m) {
60         assert(m);
61
62         return config_parse_many_nulstr(PKGSYSCONFDIR "/logind.conf",
63                                         CONF_PATHS_NULSTR("elogind/logind.conf.d"),
64                                         "Login\0",
65                                         config_item_perf_lookup, logind_gperf_lookup,
66                                         CONFIG_PARSE_WARN, m);
67 }
68
69 int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
70         Device *d;
71
72         assert(m);
73         assert(sysfs);
74
75         d = hashmap_get(m->devices, sysfs);
76         if (d)
77                 /* we support adding master-flags, but not removing them */
78                 d->master = d->master || master;
79         else {
80                 d = device_new(m, sysfs, master);
81                 if (!d)
82                         return -ENOMEM;
83         }
84
85         if (_device)
86                 *_device = d;
87
88         return 0;
89 }
90
91 int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
92         Seat *s;
93
94         assert(m);
95         assert(id);
96
97         s = hashmap_get(m->seats, id);
98         if (!s) {
99                 s = seat_new(m, id);
100                 if (!s)
101                         return -ENOMEM;
102         }
103
104         if (_seat)
105                 *_seat = s;
106
107         return 0;
108 }
109
110 int manager_add_session(Manager *m, const char *id, Session **_session) {
111         Session *s;
112
113         assert(m);
114         assert(id);
115
116         s = hashmap_get(m->sessions, id);
117         if (!s) {
118                 s = session_new(m, id);
119                 if (!s)
120                         return -ENOMEM;
121         }
122
123         if (_session)
124                 *_session = s;
125
126         return 0;
127 }
128
129 int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
130         User *u;
131         int r;
132
133         assert(m);
134         assert(name);
135
136         u = hashmap_get(m->users, UID_TO_PTR(uid));
137         if (!u) {
138                 r = user_new(&u, m, uid, gid, name);
139                 if (r < 0)
140                         return r;
141         }
142
143         if (_user)
144                 *_user = u;
145
146         return 0;
147 }
148
149 int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
150         uid_t uid;
151         gid_t gid;
152         int r;
153
154         assert(m);
155         assert(name);
156
157         r = get_user_creds(&name, &uid, &gid, NULL, NULL);
158         if (r < 0)
159                 return r;
160
161         return manager_add_user(m, uid, gid, name, _user);
162 }
163
164 int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
165         struct passwd *p;
166
167         assert(m);
168
169         errno = 0;
170         p = getpwuid(uid);
171         if (!p)
172                 return errno > 0 ? -errno : -ENOENT;
173
174         return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
175 }
176
177 int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
178         Inhibitor *i;
179
180         assert(m);
181         assert(id);
182
183         i = hashmap_get(m->inhibitors, id);
184         if (i) {
185                 if (_inhibitor)
186                         *_inhibitor = i;
187
188                 return 0;
189         }
190
191         i = inhibitor_new(m, id);
192         if (!i)
193                 return -ENOMEM;
194
195         if (_inhibitor)
196                 *_inhibitor = i;
197
198         return 0;
199 }
200
201 int manager_add_button(Manager *m, const char *name, Button **_button) {
202         Button *b;
203
204         assert(m);
205         assert(name);
206
207         b = hashmap_get(m->buttons, name);
208         if (!b) {
209                 b = button_new(m, name);
210                 if (!b)
211                         return -ENOMEM;
212         }
213
214         if (_button)
215                 *_button = b;
216
217         return 0;
218 }
219
220 int manager_process_seat_device(Manager *m, struct udev_device *d) {
221         Device *device;
222         int r;
223
224         assert(m);
225
226         if (streq_ptr(udev_device_get_action(d), "remove")) {
227
228                 device = hashmap_get(m->devices, udev_device_get_syspath(d));
229                 if (!device)
230                         return 0;
231
232                 seat_add_to_gc_queue(device->seat);
233                 device_free(device);
234
235         } else {
236                 const char *sn;
237                 Seat *seat = NULL;
238                 bool master;
239
240                 sn = udev_device_get_property_value(d, "ID_SEAT");
241                 if (isempty(sn))
242                         sn = "seat0";
243
244                 if (!seat_name_is_valid(sn)) {
245                         log_warning("Device with invalid seat name %s found, ignoring.", sn);
246                         return 0;
247                 }
248
249                 seat = hashmap_get(m->seats, sn);
250                 master = udev_device_has_tag(d, "master-of-seat");
251
252                 /* Ignore non-master devices for unknown seats */
253                 if (!master && !seat)
254                         return 0;
255
256                 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
257                 if (r < 0)
258                         return r;
259
260                 if (!seat) {
261                         r = manager_add_seat(m, sn, &seat);
262                         if (r < 0) {
263                                 if (!device->seat)
264                                         device_free(device);
265
266                                 return r;
267                         }
268                 }
269
270                 device_attach(device, seat);
271                 seat_start(seat);
272         }
273
274         return 0;
275 }
276
277 int manager_process_button_device(Manager *m, struct udev_device *d) {
278         Button *b;
279
280         int r;
281
282         assert(m);
283
284         if (streq_ptr(udev_device_get_action(d), "remove")) {
285
286                 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
287                 if (!b)
288                         return 0;
289
290                 button_free(b);
291
292         } else {
293                 const char *sn;
294
295                 r = manager_add_button(m, udev_device_get_sysname(d), &b);
296                 if (r < 0)
297                         return r;
298
299                 sn = udev_device_get_property_value(d, "ID_SEAT");
300                 if (isempty(sn))
301                         sn = "seat0";
302
303                 button_set_seat(b, sn);
304
305                 r = button_open(b);
306                 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
307                             * opening the device?) let's close the button again. */
308                         button_free(b);
309         }
310
311         return 0;
312 }
313
314 int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) {
315 #if 0 /// elogind does not support systemd units, but its own session system
316         _cleanup_free_ char *unit = NULL;
317 #else
318         _cleanup_free_ char *session_name = NULL;
319 #endif // 0
320         Session *s;
321         int r;
322
323         assert(m);
324
325         if (!pid_is_valid(pid))
326                 return -EINVAL;
327
328 #if 0 /// elogind does not support systemd units, but its own session system
329         r = cg_pid_get_unit(pid, &unit);
330         if (r < 0)
331                 goto not_found;
332
333         s = hashmap_get(m->session_units, unit);
334         if (!s)
335                 goto not_found;
336 #else
337         log_debug_elogind("Searching session for PID %u", pid);
338         r = cg_pid_get_session(pid, &session_name);
339         if (r < 0)
340                 goto not_found;
341
342         s = hashmap_get(m->sessions, session_name);
343         log_debug_elogind("Session Name \"%s\" -> Session \"%s\"",
344                           session_name, s && s->id ? s->id : "NULL");
345         if (NULL == s)
346                 goto not_found;
347 #endif // 0
348
349         if (ret)
350                 *ret = s;
351
352         return 1;
353
354 not_found:
355         if (ret)
356                 *ret = NULL;
357         return 0;
358 }
359
360 int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) {
361 #if 0 /// elogind does not support systemd units, but its own session system
362         _cleanup_free_ char *unit = NULL;
363         User *u;
364 #else
365         Session *s;
366 #endif // 0
367         int r;
368
369         assert(m);
370
371         if (!pid_is_valid(pid))
372                 return -EINVAL;
373
374 #if 0 /// elogind does not support systemd units, but its own session system
375         r = cg_pid_get_slice(pid, &unit);
376         if (r < 0)
377                 goto not_found;
378
379         u = hashmap_get(m->user_units, unit);
380         if (!u)
381                 goto not_found;
382
383         if (ret)
384                 *ret = u;
385
386 #else
387         r = manager_get_session_by_pid (m, pid, &s);
388         if (r <= 0)
389                 goto not_found;
390
391         if (ret)
392                 *ret =  s->user;
393 #endif // 0
394         return 1;
395
396 not_found:
397         if (ret)
398                 *ret = NULL;
399
400         return 0;
401 }
402
403 int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
404         Session *s;
405         bool idle_hint;
406         dual_timestamp ts = DUAL_TIMESTAMP_NULL;
407         Iterator i;
408
409         assert(m);
410
411         idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
412
413         HASHMAP_FOREACH(s, m->sessions, i) {
414                 dual_timestamp k;
415                 int ih;
416
417                 ih = session_get_idle_hint(s, &k);
418                 if (ih < 0)
419                         return ih;
420
421                 if (!ih) {
422                         if (!idle_hint) {
423                                 if (k.monotonic < ts.monotonic)
424                                         ts = k;
425                         } else {
426                                 idle_hint = false;
427                                 ts = k;
428                         }
429                 } else if (idle_hint) {
430
431                         if (k.monotonic > ts.monotonic)
432                                 ts = k;
433                 }
434         }
435
436         if (t)
437                 *t = ts;
438
439         return idle_hint;
440 }
441
442 bool manager_shall_kill(Manager *m, const char *user) {
443         assert(m);
444         assert(user);
445
446         if (!m->kill_exclude_users && streq(user, "root"))
447                 return false;
448
449         if (strv_contains(m->kill_exclude_users, user))
450                 return false;
451
452         if (!strv_isempty(m->kill_only_users))
453                 return strv_contains(m->kill_only_users, user);
454
455         return m->kill_user_processes;
456 }
457
458 #if 0 /// UNNEEDED by elogind
459 int config_parse_n_autovts(
460                 const char *unit,
461                 const char *filename,
462                 unsigned line,
463                 const char *section,
464                 unsigned section_line,
465                 const char *lvalue,
466                 int ltype,
467                 const char *rvalue,
468                 void *data,
469                 void *userdata) {
470
471         unsigned *n = data;
472         unsigned o;
473         int r;
474
475         assert(filename);
476         assert(lvalue);
477         assert(rvalue);
478         assert(data);
479
480         r = safe_atou(rvalue, &o);
481         if (r < 0) {
482                 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue);
483                 return 0;
484         }
485
486         if (o > 15) {
487                 log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
488                 return 0;
489         }
490
491         *n = o;
492         return 0;
493 }
494
495 static int vt_is_busy(unsigned int vtnr) {
496         struct vt_stat vt_stat;
497         int r = 0;
498         _cleanup_close_ int fd;
499
500         assert(vtnr >= 1);
501
502         /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
503         assert(vtnr <= 15);
504
505         /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
506          * we'd open the latter we'd open the foreground tty which
507          * hence would be unconditionally busy. By opening /dev/tty1
508          * we avoid this. Since tty1 is special and needs to be an
509          * explicitly loaded getty or DM this is safe. */
510
511         fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
512         if (fd < 0)
513                 return -errno;
514
515         if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
516                 r = -errno;
517         else
518                 r = !!(vt_stat.v_state & (1 << vtnr));
519
520         return r;
521 }
522
523 int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
524         _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
525         char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
526         int r;
527
528         assert(m);
529         assert(vtnr >= 1);
530
531         if (vtnr > m->n_autovts &&
532             vtnr != m->reserve_vt)
533                 return 0;
534
535         if (vtnr != m->reserve_vt) {
536                 /* If this is the reserved TTY, we'll start the getty
537                  * on it in any case, but otherwise only if it is not
538                  * busy. */
539
540                 r = vt_is_busy(vtnr);
541                 if (r < 0)
542                         return r;
543                 else if (r > 0)
544                         return -EBUSY;
545         }
546
547         snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
548         r = sd_bus_call_method(
549                         m->bus,
550                         "org.freedesktop.systemd1",
551                         "/org/freedesktop/systemd1",
552                         "org.freedesktop.systemd1.Manager",
553                         "StartUnit",
554                         &error,
555                         NULL,
556                         "ss", name, "fail");
557         if (r < 0)
558                 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
559
560         return r;
561 }
562 #endif // 0
563
564 static bool manager_is_docked(Manager *m) {
565         Iterator i;
566         Button *b;
567
568         HASHMAP_FOREACH(b, m->buttons, i)
569                 if (b->docked)
570                         return true;
571
572         return false;
573 }
574
575 static int manager_count_external_displays(Manager *m) {
576         _cleanup_(udev_enumerate_unrefp) struct udev_enumerate *e = NULL;
577         struct udev_list_entry *item = NULL, *first = NULL;
578         int r;
579         int n = 0;
580
581         e = udev_enumerate_new(m->udev);
582         if (!e)
583                 return -ENOMEM;
584
585         r = udev_enumerate_add_match_subsystem(e, "drm");
586         if (r < 0)
587                 return r;
588
589         r = udev_enumerate_scan_devices(e);
590         if (r < 0)
591                 return r;
592
593         first = udev_enumerate_get_list_entry(e);
594         udev_list_entry_foreach(item, first) {
595                 _cleanup_(udev_device_unrefp) struct udev_device *d = NULL;
596                 struct udev_device *p;
597                 const char *status, *enabled, *dash, *nn, *i;
598                 bool external = false;
599
600                 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
601                 if (!d)
602                         return -ENOMEM;
603
604                 p = udev_device_get_parent(d);
605                 if (!p)
606                         continue;
607
608                 /* If the parent shares the same subsystem as the
609                  * device we are looking at then it is a connector,
610                  * which is what we are interested in. */
611                 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
612                         continue;
613
614                 nn = udev_device_get_sysname(d);
615                 if (!nn)
616                         continue;
617
618                 /* Ignore internal displays: the type is encoded in
619                  * the sysfs name, as the second dash separated item
620                  * (the first is the card name, the last the connector
621                  * number). We implement a whitelist of external
622                  * displays here, rather than a whitelist, to ensure
623                  * we don't block suspends too eagerly. */
624                 dash = strchr(nn, '-');
625                 if (!dash)
626                         continue;
627
628                 dash++;
629                 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
630                                "Composite-", "SVIDEO-", "Component-",
631                                "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
632
633                         if (startswith(dash, i)) {
634                                 external = true;
635                                 break;
636                         }
637                 }
638                 if (!external)
639                         continue;
640
641                 /* Ignore ports that are not enabled */
642                 enabled = udev_device_get_sysattr_value(d, "enabled");
643                 if (!enabled)
644                         continue;
645                 if (!streq_ptr(enabled, "enabled"))
646                         continue;
647
648                 /* We count any connector which is not explicitly
649                  * "disconnected" as connected. */
650                 status = udev_device_get_sysattr_value(d, "status");
651                 if (!streq_ptr(status, "disconnected"))
652                         n++;
653         }
654
655         return n;
656 }
657
658 bool manager_is_docked_or_external_displays(Manager *m) {
659         int n;
660
661         /* If we are docked don't react to lid closing */
662         if (manager_is_docked(m)) {
663                 log_debug("System is docked.");
664                 return true;
665         }
666
667         /* If we have more than one display connected,
668          * assume that we are docked. */
669         n = manager_count_external_displays(m);
670         if (n < 0)
671                 log_warning_errno(n, "Display counting failed: %m");
672         else if (n >= 1) {
673                 log_debug("External (%i) displays connected.", n);
674                 return true;
675         }
676
677         return false;
678 }
679
680 bool manager_is_on_external_power(void) {
681         int r;
682
683         /* For now we only check for AC power, but 'external power' can apply
684          * to anything that isn't an internal battery */
685         r = on_ac_power();
686         if (r < 0)
687                 log_warning_errno(r, "Failed to read AC power status: %m");
688         else if (r > 0)
689                 return true;
690
691         return false;
692 }
693
694 bool manager_all_buttons_ignored(Manager *m) {
695         assert(m);
696
697         if (m->handle_power_key != HANDLE_IGNORE)
698                 return false;
699         if (m->handle_suspend_key != HANDLE_IGNORE)
700                 return false;
701         if (m->handle_hibernate_key != HANDLE_IGNORE)
702                 return false;
703         if (m->handle_lid_switch != HANDLE_IGNORE)
704                 return false;
705         if (m->handle_lid_switch_ep != _HANDLE_ACTION_INVALID &&
706             m->handle_lid_switch_ep != HANDLE_IGNORE)
707                 return false;
708         if (m->handle_lid_switch_docked != HANDLE_IGNORE)
709                 return false;
710
711         return true;
712 }