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