]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/login/logind-core.c
Merge pull request #7191 from Mic92/systemd
[thirdparty/systemd.git] / src / login / logind-core.c
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
2b3ab29d
ZJS
2/***
3 This file is part of systemd.
4
5 Copyright 2011 Lennart Poettering
6
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
11
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
19***/
20
2b3ab29d
ZJS
21#include <fcntl.h>
22#include <pwd.h>
cf0fbc49
TA
23#include <sys/ioctl.h>
24#include <sys/types.h>
2b3ab29d
ZJS
25#include <linux/vt.h>
26
b5efdb8a 27#include "alloc-util.h"
cc377381 28#include "bus-error.h"
3ffd4af2
LP
29#include "bus-util.h"
30#include "cgroup-util.h"
31#include "fd-util.h"
cc377381 32#include "logind.h"
c29b65f7 33#include "parse-util.h"
3ffd4af2 34#include "strv.h"
288a74cc 35#include "terminal-util.h"
3ffd4af2 36#include "udev-util.h"
b1d4f8e1 37#include "user-util.h"
2b3ab29d
ZJS
38
39int manager_add_device(Manager *m, const char *sysfs, bool master, Device **_device) {
40 Device *d;
41
42 assert(m);
43 assert(sysfs);
44
45 d = hashmap_get(m->devices, sysfs);
61376f96 46 if (d)
2b3ab29d
ZJS
47 /* we support adding master-flags, but not removing them */
48 d->master = d->master || master;
61376f96
ZJS
49 else {
50 d = device_new(m, sysfs, master);
51 if (!d)
52 return -ENOMEM;
2b3ab29d
ZJS
53 }
54
2b3ab29d
ZJS
55 if (_device)
56 *_device = d;
57
58 return 0;
59}
60
61int manager_add_seat(Manager *m, const char *id, Seat **_seat) {
62 Seat *s;
63
64 assert(m);
65 assert(id);
66
67 s = hashmap_get(m->seats, id);
61376f96
ZJS
68 if (!s) {
69 s = seat_new(m, id);
70 if (!s)
71 return -ENOMEM;
2b3ab29d
ZJS
72 }
73
2b3ab29d
ZJS
74 if (_seat)
75 *_seat = s;
76
77 return 0;
78}
79
80int manager_add_session(Manager *m, const char *id, Session **_session) {
81 Session *s;
82
83 assert(m);
84 assert(id);
85
86 s = hashmap_get(m->sessions, id);
61376f96
ZJS
87 if (!s) {
88 s = session_new(m, id);
89 if (!s)
90 return -ENOMEM;
2b3ab29d
ZJS
91 }
92
2b3ab29d
ZJS
93 if (_session)
94 *_session = s;
95
96 return 0;
97}
98
99int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) {
100 User *u;
157f5057 101 int r;
2b3ab29d
ZJS
102
103 assert(m);
104 assert(name);
105
8cb4ab00 106 u = hashmap_get(m->users, UID_TO_PTR(uid));
61376f96 107 if (!u) {
157f5057
DH
108 r = user_new(&u, m, uid, gid, name);
109 if (r < 0)
110 return r;
2b3ab29d
ZJS
111 }
112
2b3ab29d
ZJS
113 if (_user)
114 *_user = u;
115
116 return 0;
117}
118
119int manager_add_user_by_name(Manager *m, const char *name, User **_user) {
120 uid_t uid;
121 gid_t gid;
122 int r;
123
124 assert(m);
125 assert(name);
126
127 r = get_user_creds(&name, &uid, &gid, NULL, NULL);
128 if (r < 0)
129 return r;
130
131 return manager_add_user(m, uid, gid, name, _user);
132}
133
134int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) {
135 struct passwd *p;
136
137 assert(m);
138
139 errno = 0;
140 p = getpwuid(uid);
141 if (!p)
f5e5c28f 142 return errno > 0 ? -errno : -ENOENT;
2b3ab29d
ZJS
143
144 return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user);
145}
146
147int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) {
148 Inhibitor *i;
149
150 assert(m);
151 assert(id);
152
153 i = hashmap_get(m->inhibitors, id);
154 if (i) {
155 if (_inhibitor)
156 *_inhibitor = i;
157
158 return 0;
159 }
160
161 i = inhibitor_new(m, id);
162 if (!i)
163 return -ENOMEM;
164
165 if (_inhibitor)
166 *_inhibitor = i;
167
168 return 0;
169}
170
171int manager_add_button(Manager *m, const char *name, Button **_button) {
172 Button *b;
173
174 assert(m);
175 assert(name);
176
177 b = hashmap_get(m->buttons, name);
61376f96
ZJS
178 if (!b) {
179 b = button_new(m, name);
180 if (!b)
181 return -ENOMEM;
2b3ab29d
ZJS
182 }
183
2b3ab29d
ZJS
184 if (_button)
185 *_button = b;
186
187 return 0;
188}
189
2b3ab29d
ZJS
190int manager_process_seat_device(Manager *m, struct udev_device *d) {
191 Device *device;
192 int r;
193
194 assert(m);
195
196 if (streq_ptr(udev_device_get_action(d), "remove")) {
197
198 device = hashmap_get(m->devices, udev_device_get_syspath(d));
199 if (!device)
200 return 0;
201
202 seat_add_to_gc_queue(device->seat);
203 device_free(device);
204
205 } else {
206 const char *sn;
207 Seat *seat = NULL;
208 bool master;
209
210 sn = udev_device_get_property_value(d, "ID_SEAT");
211 if (isempty(sn))
212 sn = "seat0";
213
214 if (!seat_name_is_valid(sn)) {
215 log_warning("Device with invalid seat name %s found, ignoring.", sn);
216 return 0;
217 }
218
6a79c586 219 seat = hashmap_get(m->seats, sn);
2b3ab29d 220 master = udev_device_has_tag(d, "master-of-seat");
6a79c586
LP
221
222 /* Ignore non-master devices for unknown seats */
223 if (!master && !seat)
2b3ab29d
ZJS
224 return 0;
225
226 r = manager_add_device(m, udev_device_get_syspath(d), master, &device);
227 if (r < 0)
228 return r;
229
230 if (!seat) {
231 r = manager_add_seat(m, sn, &seat);
232 if (r < 0) {
233 if (!device->seat)
234 device_free(device);
235
236 return r;
237 }
238 }
239
240 device_attach(device, seat);
241 seat_start(seat);
242 }
243
244 return 0;
245}
246
247int manager_process_button_device(Manager *m, struct udev_device *d) {
248 Button *b;
249
250 int r;
251
252 assert(m);
253
254 if (streq_ptr(udev_device_get_action(d), "remove")) {
255
256 b = hashmap_get(m->buttons, udev_device_get_sysname(d));
257 if (!b)
258 return 0;
259
260 button_free(b);
261
262 } else {
263 const char *sn;
264
265 r = manager_add_button(m, udev_device_get_sysname(d), &b);
266 if (r < 0)
267 return r;
268
269 sn = udev_device_get_property_value(d, "ID_SEAT");
270 if (isempty(sn))
271 sn = "seat0";
272
273 button_set_seat(b, sn);
2546b70a
LP
274
275 r = button_open(b);
276 if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error
277 * opening the device?) let's close the button again. */
278 button_free(b);
2b3ab29d
ZJS
279 }
280
281 return 0;
282}
283
284int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) {
285 _cleanup_free_ char *unit = NULL;
286 Session *s;
287 int r;
288
289 assert(m);
2b3ab29d 290
6f876815 291 if (!pid_is_valid(pid))
2b3ab29d
ZJS
292 return -EINVAL;
293
294 r = cg_pid_get_unit(pid, &unit);
295 if (r < 0)
d3e84ddb 296 return 0;
2b3ab29d
ZJS
297
298 s = hashmap_get(m->session_units, unit);
299 if (!s)
300 return 0;
301
b80120c4
DH
302 if (session)
303 *session = s;
2b3ab29d
ZJS
304 return 1;
305}
306
307int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) {
308 _cleanup_free_ char *unit = NULL;
309 User *u;
310 int r;
311
312 assert(m);
313 assert(user);
314
6f876815 315 if (!pid_is_valid(pid))
2b3ab29d
ZJS
316 return -EINVAL;
317
318 r = cg_pid_get_slice(pid, &unit);
319 if (r < 0)
d3e84ddb 320 return 0;
2b3ab29d
ZJS
321
322 u = hashmap_get(m->user_units, unit);
323 if (!u)
324 return 0;
325
326 *user = u;
327 return 1;
328}
329
330int manager_get_idle_hint(Manager *m, dual_timestamp *t) {
331 Session *s;
332 bool idle_hint;
5cb14b37 333 dual_timestamp ts = DUAL_TIMESTAMP_NULL;
2b3ab29d
ZJS
334 Iterator i;
335
336 assert(m);
337
85a428c6 338 idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL);
2b3ab29d
ZJS
339
340 HASHMAP_FOREACH(s, m->sessions, i) {
341 dual_timestamp k;
342 int ih;
343
344 ih = session_get_idle_hint(s, &k);
345 if (ih < 0)
346 return ih;
347
348 if (!ih) {
349 if (!idle_hint) {
350 if (k.monotonic < ts.monotonic)
351 ts = k;
352 } else {
353 idle_hint = false;
354 ts = k;
355 }
356 } else if (idle_hint) {
357
358 if (k.monotonic > ts.monotonic)
359 ts = k;
360 }
361 }
362
363 if (t)
364 *t = ts;
365
366 return idle_hint;
367}
368
369bool manager_shall_kill(Manager *m, const char *user) {
370 assert(m);
371 assert(user);
372
a2ed7077
ZJS
373 if (!m->kill_exclude_users && streq(user, "root"))
374 return false;
375
2b3ab29d
ZJS
376 if (strv_contains(m->kill_exclude_users, user))
377 return false;
378
921f831d
ZJS
379 if (!strv_isempty(m->kill_only_users))
380 return strv_contains(m->kill_only_users, user);
2b3ab29d 381
921f831d 382 return m->kill_user_processes;
2b3ab29d
ZJS
383}
384
c29b65f7
AJ
385int config_parse_n_autovts(
386 const char *unit,
387 const char *filename,
388 unsigned line,
389 const char *section,
390 unsigned section_line,
391 const char *lvalue,
392 int ltype,
393 const char *rvalue,
394 void *data,
395 void *userdata) {
396
397 unsigned *n = data;
398 unsigned o;
399 int r;
400
401 assert(filename);
402 assert(lvalue);
403 assert(rvalue);
404 assert(data);
405
406 r = safe_atou(rvalue, &o);
407 if (r < 0) {
408 log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue);
409 return 0;
410 }
411
412 if (o > 15) {
413 log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue);
414 return 0;
415 }
416
417 *n = o;
418 return 0;
419}
420
92bd5ff3 421static int vt_is_busy(unsigned int vtnr) {
2b3ab29d 422 struct vt_stat vt_stat;
61376f96
ZJS
423 int r = 0;
424 _cleanup_close_ int fd;
2b3ab29d
ZJS
425
426 assert(vtnr >= 1);
427
c29b65f7
AJ
428 /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */
429 assert(vtnr <= 15);
430
2b3ab29d
ZJS
431 /* We explicitly open /dev/tty1 here instead of /dev/tty0. If
432 * we'd open the latter we'd open the foreground tty which
433 * hence would be unconditionally busy. By opening /dev/tty1
434 * we avoid this. Since tty1 is special and needs to be an
435 * explicitly loaded getty or DM this is safe. */
436
437 fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC);
438 if (fd < 0)
439 return -errno;
440
441 if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0)
442 r = -errno;
443 else
444 r = !!(vt_stat.v_state & (1 << vtnr));
445
2b3ab29d
ZJS
446 return r;
447}
448
92bd5ff3 449int manager_spawn_autovt(Manager *m, unsigned int vtnr) {
4afd3348 450 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
61376f96 451 char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned int)];
2b3ab29d 452 int r;
2b3ab29d
ZJS
453
454 assert(m);
455 assert(vtnr >= 1);
456
92bd5ff3
DH
457 if (vtnr > m->n_autovts &&
458 vtnr != m->reserve_vt)
2b3ab29d
ZJS
459 return 0;
460
92bd5ff3 461 if (vtnr != m->reserve_vt) {
2b3ab29d
ZJS
462 /* If this is the reserved TTY, we'll start the getty
463 * on it in any case, but otherwise only if it is not
464 * busy. */
465
466 r = vt_is_busy(vtnr);
467 if (r < 0)
468 return r;
469 else if (r > 0)
470 return -EBUSY;
471 }
472
61376f96 473 snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr);
cc377381 474 r = sd_bus_call_method(
2b3ab29d
ZJS
475 m->bus,
476 "org.freedesktop.systemd1",
477 "/org/freedesktop/systemd1",
478 "org.freedesktop.systemd1.Manager",
479 "StartUnit",
cc377381 480 &error,
2b3ab29d 481 NULL,
cc377381
LP
482 "ss", name, "fail");
483 if (r < 0)
484 log_error("Failed to start %s: %s", name, bus_error_message(&error, r));
2b3ab29d
ZJS
485
486 return r;
487}
2d62c530 488
602a41c2 489static bool manager_is_docked(Manager *m) {
2d62c530
LP
490 Iterator i;
491 Button *b;
492
493 HASHMAP_FOREACH(b, m->buttons, i)
494 if (b->docked)
495 return true;
496
497 return false;
498}
6a79c586 499
602a41c2 500static int manager_count_external_displays(Manager *m) {
6a79c586
LP
501 _cleanup_udev_enumerate_unref_ struct udev_enumerate *e = NULL;
502 struct udev_list_entry *item = NULL, *first = NULL;
503 int r;
504 int n = 0;
505
506 e = udev_enumerate_new(m->udev);
507 if (!e)
508 return -ENOMEM;
509
510 r = udev_enumerate_add_match_subsystem(e, "drm");
511 if (r < 0)
512 return r;
513
514 r = udev_enumerate_scan_devices(e);
515 if (r < 0)
516 return r;
517
518 first = udev_enumerate_get_list_entry(e);
519 udev_list_entry_foreach(item, first) {
520 _cleanup_udev_device_unref_ struct udev_device *d = NULL;
521 struct udev_device *p;
602a41c2
LP
522 const char *status, *enabled, *dash, *nn, *i;
523 bool external = false;
6a79c586
LP
524
525 d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item));
526 if (!d)
527 return -ENOMEM;
528
529 p = udev_device_get_parent(d);
530 if (!p)
94036de8 531 continue;
6a79c586
LP
532
533 /* If the parent shares the same subsystem as the
534 * device we are looking at then it is a connector,
535 * which is what we are interested in. */
536 if (!streq_ptr(udev_device_get_subsystem(p), "drm"))
537 continue;
538
602a41c2
LP
539 nn = udev_device_get_sysname(d);
540 if (!nn)
541 continue;
542
543 /* Ignore internal displays: the type is encoded in
6dd6a9c4 544 * the sysfs name, as the second dash separated item
602a41c2
LP
545 * (the first is the card name, the last the connector
546 * number). We implement a whitelist of external
547 * displays here, rather than a whitelist, to ensure
548 * we don't block suspends too eagerly. */
549 dash = strchr(nn, '-');
550 if (!dash)
551 continue;
552
553 dash++;
554 FOREACH_STRING(i, "VGA-", "DVI-I-", "DVI-D-", "DVI-A-"
555 "Composite-", "SVIDEO-", "Component-",
556 "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-") {
557
558 if (startswith(dash, i)) {
559 external = true;
560 break;
561 }
562 }
563 if (!external)
564 continue;
565
566 /* Ignore ports that are not enabled */
567 enabled = udev_device_get_sysattr_value(d, "enabled");
568 if (!enabled)
569 continue;
570 if (!streq_ptr(enabled, "enabled"))
571 continue;
572
6a79c586
LP
573 /* We count any connector which is not explicitly
574 * "disconnected" as connected. */
575 status = udev_device_get_sysattr_value(d, "status");
576 if (!streq_ptr(status, "disconnected"))
577 n++;
578 }
579
580 return n;
581}
3c56cab4 582
602a41c2 583bool manager_is_docked_or_external_displays(Manager *m) {
3c56cab4
BW
584 int n;
585
586 /* If we are docked don't react to lid closing */
587 if (manager_is_docked(m)) {
588 log_debug("System is docked.");
589 return true;
590 }
591
592 /* If we have more than one display connected,
593 * assume that we are docked. */
602a41c2 594 n = manager_count_external_displays(m);
3c56cab4 595 if (n < 0)
da927ba9 596 log_warning_errno(n, "Display counting failed: %m");
602a41c2
LP
597 else if (n >= 1) {
598 log_debug("External (%i) displays connected.", n);
3c56cab4
BW
599 return true;
600 }
601
602 return false;
603}