]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2b3ab29d | 2 | |
2b3ab29d ZJS |
3 | #include <fcntl.h> |
4 | #include <pwd.h> | |
cf0fbc49 TA |
5 | #include <sys/ioctl.h> |
6 | #include <sys/types.h> | |
2b3ab29d | 7 | #include <linux/vt.h> |
3d0ef5c7 LP |
8 | #if ENABLE_UTMP |
9 | #include <utmpx.h> | |
10 | #endif | |
2b3ab29d | 11 | |
4f209af7 YW |
12 | #include "sd-device.h" |
13 | ||
b5efdb8a | 14 | #include "alloc-util.h" |
cc377381 | 15 | #include "bus-error.h" |
3ffd4af2 LP |
16 | #include "bus-util.h" |
17 | #include "cgroup-util.h" | |
ae98d374 | 18 | #include "conf-parser.h" |
8437c059 | 19 | #include "device-util.h" |
66855de7 | 20 | #include "errno-util.h" |
3ffd4af2 | 21 | #include "fd-util.h" |
eefc66aa | 22 | #include "limits-util.h" |
cc377381 | 23 | #include "logind.h" |
c29b65f7 | 24 | #include "parse-util.h" |
3d0ef5c7 | 25 | #include "path-util.h" |
dccca82b | 26 | #include "process-util.h" |
3ffd4af2 | 27 | #include "strv.h" |
288a74cc | 28 | #include "terminal-util.h" |
91bd2c34 | 29 | #include "udev-util.h" |
b1d4f8e1 | 30 | #include "user-util.h" |
2b3ab29d | 31 | |
ae98d374 | 32 | void manager_reset_config(Manager *m) { |
23462168 LP |
33 | assert(m); |
34 | ||
ae98d374 ZJS |
35 | m->n_autovts = 6; |
36 | m->reserve_vt = 6; | |
37 | m->remove_ipc = true; | |
38 | m->inhibit_delay_max = 5 * USEC_PER_SEC; | |
9afe9efb LP |
39 | m->user_stop_delay = 10 * USEC_PER_SEC; |
40 | ||
ae98d374 ZJS |
41 | m->handle_power_key = HANDLE_POWEROFF; |
42 | m->handle_suspend_key = HANDLE_SUSPEND; | |
43 | m->handle_hibernate_key = HANDLE_HIBERNATE; | |
44 | m->handle_lid_switch = HANDLE_SUSPEND; | |
45 | m->handle_lid_switch_ep = _HANDLE_ACTION_INVALID; | |
46 | m->handle_lid_switch_docked = HANDLE_IGNORE; | |
47 | m->power_key_ignore_inhibited = false; | |
48 | m->suspend_key_ignore_inhibited = false; | |
49 | m->hibernate_key_ignore_inhibited = false; | |
50 | m->lid_switch_ignore_inhibited = true; | |
51 | ||
52 | m->holdoff_timeout_usec = 30 * USEC_PER_SEC; | |
53 | ||
54 | m->idle_action_usec = 30 * USEC_PER_MINUTE; | |
55 | m->idle_action = HANDLE_IGNORE; | |
56 | ||
57 | m->runtime_dir_size = physical_memory_scale(10U, 100U); /* 10% */ | |
58 | m->user_tasks_max = system_tasks_max_scale(DEFAULT_USER_TASKS_MAX_PERCENTAGE, 100U); /* 33% */ | |
59 | m->sessions_max = 8192; | |
60 | m->inhibitors_max = 8192; | |
61 | ||
62 | m->kill_user_processes = KILL_USER_PROCESSES; | |
63 | ||
64 | m->kill_only_users = strv_free(m->kill_only_users); | |
65 | m->kill_exclude_users = strv_free(m->kill_exclude_users); | |
66 | } | |
67 | ||
68 | int manager_parse_config_file(Manager *m) { | |
69 | assert(m); | |
70 | ||
71 | return config_parse_many_nulstr(PKGSYSCONFDIR "/logind.conf", | |
72 | CONF_PATHS_NULSTR("systemd/logind.conf.d"), | |
73 | "Login\0", | |
74 | config_item_perf_lookup, logind_gperf_lookup, | |
75 | CONFIG_PARSE_WARN, m); | |
76 | } | |
77 | ||
f68d1485 | 78 | int manager_add_device(Manager *m, const char *sysfs, bool master, Device **ret_device) { |
2b3ab29d ZJS |
79 | Device *d; |
80 | ||
81 | assert(m); | |
82 | assert(sysfs); | |
83 | ||
84 | d = hashmap_get(m->devices, sysfs); | |
61376f96 | 85 | if (d) |
2b3ab29d ZJS |
86 | /* we support adding master-flags, but not removing them */ |
87 | d->master = d->master || master; | |
61376f96 ZJS |
88 | else { |
89 | d = device_new(m, sysfs, master); | |
90 | if (!d) | |
91 | return -ENOMEM; | |
2b3ab29d ZJS |
92 | } |
93 | ||
f68d1485 ZJS |
94 | if (ret_device) |
95 | *ret_device = d; | |
2b3ab29d ZJS |
96 | |
97 | return 0; | |
98 | } | |
99 | ||
f68d1485 | 100 | int manager_add_seat(Manager *m, const char *id, Seat **ret_seat) { |
2b3ab29d | 101 | Seat *s; |
8c29a457 | 102 | int r; |
2b3ab29d ZJS |
103 | |
104 | assert(m); | |
105 | assert(id); | |
106 | ||
107 | s = hashmap_get(m->seats, id); | |
61376f96 | 108 | if (!s) { |
8c29a457 LP |
109 | r = seat_new(&s, m, id); |
110 | if (r < 0) | |
111 | return r; | |
2b3ab29d ZJS |
112 | } |
113 | ||
f68d1485 ZJS |
114 | if (ret_seat) |
115 | *ret_seat = s; | |
2b3ab29d ZJS |
116 | |
117 | return 0; | |
118 | } | |
119 | ||
f68d1485 | 120 | int manager_add_session(Manager *m, const char *id, Session **ret_session) { |
2b3ab29d | 121 | Session *s; |
8c29a457 | 122 | int r; |
2b3ab29d ZJS |
123 | |
124 | assert(m); | |
125 | assert(id); | |
126 | ||
127 | s = hashmap_get(m->sessions, id); | |
61376f96 | 128 | if (!s) { |
8c29a457 LP |
129 | r = session_new(&s, m, id); |
130 | if (r < 0) | |
131 | return r; | |
2b3ab29d ZJS |
132 | } |
133 | ||
f68d1485 ZJS |
134 | if (ret_session) |
135 | *ret_session = s; | |
2b3ab29d ZJS |
136 | |
137 | return 0; | |
138 | } | |
139 | ||
d5ac9d06 LP |
140 | int manager_add_user( |
141 | Manager *m, | |
142 | uid_t uid, | |
143 | gid_t gid, | |
144 | const char *name, | |
145 | const char *home, | |
f68d1485 | 146 | User **ret_user) { |
d5ac9d06 | 147 | |
2b3ab29d | 148 | User *u; |
157f5057 | 149 | int r; |
2b3ab29d ZJS |
150 | |
151 | assert(m); | |
152 | assert(name); | |
153 | ||
8cb4ab00 | 154 | u = hashmap_get(m->users, UID_TO_PTR(uid)); |
61376f96 | 155 | if (!u) { |
d5ac9d06 | 156 | r = user_new(&u, m, uid, gid, name, home); |
157f5057 DH |
157 | if (r < 0) |
158 | return r; | |
2b3ab29d ZJS |
159 | } |
160 | ||
f68d1485 ZJS |
161 | if (ret_user) |
162 | *ret_user = u; | |
2b3ab29d ZJS |
163 | |
164 | return 0; | |
165 | } | |
166 | ||
d5ac9d06 LP |
167 | int manager_add_user_by_name( |
168 | Manager *m, | |
169 | const char *name, | |
f68d1485 | 170 | User **ret_user) { |
d5ac9d06 LP |
171 | |
172 | const char *home = NULL; | |
2b3ab29d ZJS |
173 | uid_t uid; |
174 | gid_t gid; | |
175 | int r; | |
176 | ||
177 | assert(m); | |
178 | assert(name); | |
179 | ||
d5ac9d06 | 180 | r = get_user_creds(&name, &uid, &gid, &home, NULL, 0); |
2b3ab29d ZJS |
181 | if (r < 0) |
182 | return r; | |
183 | ||
f68d1485 | 184 | return manager_add_user(m, uid, gid, name, home, ret_user); |
2b3ab29d ZJS |
185 | } |
186 | ||
f68d1485 | 187 | int manager_add_user_by_uid(Manager *m, uid_t uid, User **ret_user) { |
2b3ab29d ZJS |
188 | struct passwd *p; |
189 | ||
190 | assert(m); | |
191 | ||
192 | errno = 0; | |
193 | p = getpwuid(uid); | |
194 | if (!p) | |
66855de7 | 195 | return errno_or_else(ENOENT); |
2b3ab29d | 196 | |
f68d1485 | 197 | return manager_add_user(m, uid, p->pw_gid, p->pw_name, p->pw_dir, ret_user); |
2b3ab29d ZJS |
198 | } |
199 | ||
81280b2a | 200 | int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **ret) { |
2b3ab29d | 201 | Inhibitor *i; |
81280b2a | 202 | int r; |
2b3ab29d ZJS |
203 | |
204 | assert(m); | |
205 | assert(id); | |
206 | ||
207 | i = hashmap_get(m->inhibitors, id); | |
81280b2a LP |
208 | if (!i) { |
209 | r = inhibitor_new(&i, m, id); | |
210 | if (r < 0) | |
211 | return r; | |
2b3ab29d ZJS |
212 | } |
213 | ||
81280b2a LP |
214 | if (ret) |
215 | *ret = i; | |
2b3ab29d ZJS |
216 | |
217 | return 0; | |
218 | } | |
219 | ||
f68d1485 | 220 | int manager_add_button(Manager *m, const char *name, Button **ret_button) { |
2b3ab29d ZJS |
221 | Button *b; |
222 | ||
223 | assert(m); | |
224 | assert(name); | |
225 | ||
226 | b = hashmap_get(m->buttons, name); | |
61376f96 ZJS |
227 | if (!b) { |
228 | b = button_new(m, name); | |
229 | if (!b) | |
230 | return -ENOMEM; | |
2b3ab29d ZJS |
231 | } |
232 | ||
f68d1485 ZJS |
233 | if (ret_button) |
234 | *ret_button = b; | |
2b3ab29d ZJS |
235 | |
236 | return 0; | |
237 | } | |
238 | ||
4f209af7 | 239 | int manager_process_seat_device(Manager *m, sd_device *d) { |
2b3ab29d ZJS |
240 | Device *device; |
241 | int r; | |
242 | ||
243 | assert(m); | |
244 | ||
91bd2c34 | 245 | if (device_for_action(d, DEVICE_ACTION_REMOVE)) { |
4f209af7 YW |
246 | const char *syspath; |
247 | ||
248 | r = sd_device_get_syspath(d, &syspath); | |
249 | if (r < 0) | |
250 | return 0; | |
2b3ab29d | 251 | |
4f209af7 | 252 | device = hashmap_get(m->devices, syspath); |
2b3ab29d ZJS |
253 | if (!device) |
254 | return 0; | |
255 | ||
256 | seat_add_to_gc_queue(device->seat); | |
257 | device_free(device); | |
258 | ||
259 | } else { | |
4f209af7 | 260 | const char *sn, *syspath; |
2b3ab29d | 261 | bool master; |
4f209af7 | 262 | Seat *seat; |
2b3ab29d | 263 | |
4f209af7 | 264 | if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn)) |
2b3ab29d ZJS |
265 | sn = "seat0"; |
266 | ||
267 | if (!seat_name_is_valid(sn)) { | |
76386309 | 268 | log_device_warning(d, "Device with invalid seat name %s found, ignoring.", sn); |
2b3ab29d ZJS |
269 | return 0; |
270 | } | |
271 | ||
6a79c586 | 272 | seat = hashmap_get(m->seats, sn); |
4f209af7 | 273 | master = sd_device_has_tag(d, "master-of-seat") > 0; |
6a79c586 LP |
274 | |
275 | /* Ignore non-master devices for unknown seats */ | |
276 | if (!master && !seat) | |
2b3ab29d ZJS |
277 | return 0; |
278 | ||
4f209af7 YW |
279 | r = sd_device_get_syspath(d, &syspath); |
280 | if (r < 0) | |
281 | return r; | |
282 | ||
283 | r = manager_add_device(m, syspath, master, &device); | |
2b3ab29d ZJS |
284 | if (r < 0) |
285 | return r; | |
286 | ||
287 | if (!seat) { | |
288 | r = manager_add_seat(m, sn, &seat); | |
289 | if (r < 0) { | |
290 | if (!device->seat) | |
291 | device_free(device); | |
292 | ||
293 | return r; | |
294 | } | |
295 | } | |
296 | ||
297 | device_attach(device, seat); | |
298 | seat_start(seat); | |
299 | } | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
4f209af7 | 304 | int manager_process_button_device(Manager *m, sd_device *d) { |
91bd2c34 | 305 | const char *sysname; |
2b3ab29d | 306 | Button *b; |
2b3ab29d ZJS |
307 | int r; |
308 | ||
309 | assert(m); | |
310 | ||
4f209af7 YW |
311 | r = sd_device_get_sysname(d, &sysname); |
312 | if (r < 0) | |
313 | return r; | |
314 | ||
91bd2c34 | 315 | if (device_for_action(d, DEVICE_ACTION_REMOVE)) { |
2b3ab29d | 316 | |
4f209af7 | 317 | b = hashmap_get(m->buttons, sysname); |
2b3ab29d ZJS |
318 | if (!b) |
319 | return 0; | |
320 | ||
321 | button_free(b); | |
322 | ||
323 | } else { | |
c679e12a | 324 | const char *sn; |
2b3ab29d | 325 | |
4f209af7 | 326 | r = manager_add_button(m, sysname, &b); |
2b3ab29d ZJS |
327 | if (r < 0) |
328 | return r; | |
329 | ||
4f209af7 | 330 | if (sd_device_get_property_value(d, "ID_SEAT", &sn) < 0 || isempty(sn)) |
2b3ab29d ZJS |
331 | sn = "seat0"; |
332 | ||
333 | button_set_seat(b, sn); | |
2546b70a LP |
334 | |
335 | r = button_open(b); | |
336 | if (r < 0) /* event device doesn't have any keys or switches relevant to us? (or any other error | |
337 | * opening the device?) let's close the button again. */ | |
338 | button_free(b); | |
2b3ab29d ZJS |
339 | } |
340 | ||
341 | return 0; | |
342 | } | |
343 | ||
14ef4a65 | 344 | int manager_get_session_by_pid(Manager *m, pid_t pid, Session **ret) { |
2b3ab29d ZJS |
345 | _cleanup_free_ char *unit = NULL; |
346 | Session *s; | |
347 | int r; | |
348 | ||
349 | assert(m); | |
2b3ab29d | 350 | |
6f876815 | 351 | if (!pid_is_valid(pid)) |
2b3ab29d ZJS |
352 | return -EINVAL; |
353 | ||
238794b1 LP |
354 | s = hashmap_get(m->sessions_by_leader, PID_TO_PTR(pid)); |
355 | if (!s) { | |
356 | r = cg_pid_get_unit(pid, &unit); | |
c9ee7160 ZJS |
357 | if (r >= 0) |
358 | s = hashmap_get(m->session_units, unit); | |
238794b1 | 359 | } |
14ef4a65 LP |
360 | |
361 | if (ret) | |
362 | *ret = s; | |
2b3ab29d | 363 | |
c9ee7160 | 364 | return !!s; |
2b3ab29d ZJS |
365 | } |
366 | ||
14ef4a65 | 367 | int manager_get_user_by_pid(Manager *m, pid_t pid, User **ret) { |
2b3ab29d | 368 | _cleanup_free_ char *unit = NULL; |
c9ee7160 | 369 | User *u = NULL; |
2b3ab29d ZJS |
370 | int r; |
371 | ||
372 | assert(m); | |
2b3ab29d | 373 | |
6f876815 | 374 | if (!pid_is_valid(pid)) |
2b3ab29d ZJS |
375 | return -EINVAL; |
376 | ||
377 | r = cg_pid_get_slice(pid, &unit); | |
c9ee7160 ZJS |
378 | if (r >= 0) |
379 | u = hashmap_get(m->user_units, unit); | |
14ef4a65 LP |
380 | |
381 | if (ret) | |
382 | *ret = u; | |
2b3ab29d | 383 | |
c9ee7160 | 384 | return !!u; |
2b3ab29d ZJS |
385 | } |
386 | ||
387 | int manager_get_idle_hint(Manager *m, dual_timestamp *t) { | |
388 | Session *s; | |
389 | bool idle_hint; | |
5cb14b37 | 390 | dual_timestamp ts = DUAL_TIMESTAMP_NULL; |
2b3ab29d ZJS |
391 | Iterator i; |
392 | ||
393 | assert(m); | |
394 | ||
85a428c6 | 395 | idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t, false, false, 0, NULL); |
2b3ab29d ZJS |
396 | |
397 | HASHMAP_FOREACH(s, m->sessions, i) { | |
398 | dual_timestamp k; | |
399 | int ih; | |
400 | ||
401 | ih = session_get_idle_hint(s, &k); | |
402 | if (ih < 0) | |
403 | return ih; | |
404 | ||
405 | if (!ih) { | |
406 | if (!idle_hint) { | |
407 | if (k.monotonic < ts.monotonic) | |
408 | ts = k; | |
409 | } else { | |
410 | idle_hint = false; | |
411 | ts = k; | |
412 | } | |
413 | } else if (idle_hint) { | |
414 | ||
415 | if (k.monotonic > ts.monotonic) | |
416 | ts = k; | |
417 | } | |
418 | } | |
419 | ||
420 | if (t) | |
421 | *t = ts; | |
422 | ||
423 | return idle_hint; | |
424 | } | |
425 | ||
426 | bool manager_shall_kill(Manager *m, const char *user) { | |
427 | assert(m); | |
428 | assert(user); | |
429 | ||
a2ed7077 ZJS |
430 | if (!m->kill_exclude_users && streq(user, "root")) |
431 | return false; | |
432 | ||
2b3ab29d ZJS |
433 | if (strv_contains(m->kill_exclude_users, user)) |
434 | return false; | |
435 | ||
921f831d ZJS |
436 | if (!strv_isempty(m->kill_only_users)) |
437 | return strv_contains(m->kill_only_users, user); | |
2b3ab29d | 438 | |
921f831d | 439 | return m->kill_user_processes; |
2b3ab29d ZJS |
440 | } |
441 | ||
c29b65f7 AJ |
442 | int config_parse_n_autovts( |
443 | const char *unit, | |
444 | const char *filename, | |
445 | unsigned line, | |
446 | const char *section, | |
447 | unsigned section_line, | |
448 | const char *lvalue, | |
449 | int ltype, | |
450 | const char *rvalue, | |
451 | void *data, | |
452 | void *userdata) { | |
453 | ||
454 | unsigned *n = data; | |
455 | unsigned o; | |
456 | int r; | |
457 | ||
458 | assert(filename); | |
459 | assert(lvalue); | |
460 | assert(rvalue); | |
461 | assert(data); | |
462 | ||
463 | r = safe_atou(rvalue, &o); | |
464 | if (r < 0) { | |
465 | log_syntax(unit, LOG_ERR, filename, line, r, "Failed to parse number of autovts, ignoring: %s", rvalue); | |
466 | return 0; | |
467 | } | |
468 | ||
469 | if (o > 15) { | |
470 | log_syntax(unit, LOG_ERR, filename, line, r, "A maximum of 15 autovts are supported, ignoring: %s", rvalue); | |
471 | return 0; | |
472 | } | |
473 | ||
474 | *n = o; | |
475 | return 0; | |
476 | } | |
477 | ||
14cb109d | 478 | static int vt_is_busy(unsigned vtnr) { |
2b3ab29d | 479 | struct vt_stat vt_stat; |
61376f96 ZJS |
480 | int r = 0; |
481 | _cleanup_close_ int fd; | |
2b3ab29d ZJS |
482 | |
483 | assert(vtnr >= 1); | |
484 | ||
c29b65f7 AJ |
485 | /* VT_GETSTATE "cannot return state for more than 16 VTs, since v_state is short" */ |
486 | assert(vtnr <= 15); | |
487 | ||
2b3ab29d ZJS |
488 | /* We explicitly open /dev/tty1 here instead of /dev/tty0. If |
489 | * we'd open the latter we'd open the foreground tty which | |
490 | * hence would be unconditionally busy. By opening /dev/tty1 | |
491 | * we avoid this. Since tty1 is special and needs to be an | |
492 | * explicitly loaded getty or DM this is safe. */ | |
493 | ||
494 | fd = open_terminal("/dev/tty1", O_RDWR|O_NOCTTY|O_CLOEXEC); | |
495 | if (fd < 0) | |
496 | return -errno; | |
497 | ||
498 | if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0) | |
499 | r = -errno; | |
500 | else | |
501 | r = !!(vt_stat.v_state & (1 << vtnr)); | |
502 | ||
2b3ab29d ZJS |
503 | return r; |
504 | } | |
505 | ||
14cb109d | 506 | int manager_spawn_autovt(Manager *m, unsigned vtnr) { |
4afd3348 | 507 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
14cb109d | 508 | char name[sizeof("autovt@tty.service") + DECIMAL_STR_MAX(unsigned)]; |
2b3ab29d | 509 | int r; |
2b3ab29d ZJS |
510 | |
511 | assert(m); | |
512 | assert(vtnr >= 1); | |
513 | ||
92bd5ff3 DH |
514 | if (vtnr > m->n_autovts && |
515 | vtnr != m->reserve_vt) | |
2b3ab29d ZJS |
516 | return 0; |
517 | ||
92bd5ff3 | 518 | if (vtnr != m->reserve_vt) { |
2b3ab29d ZJS |
519 | /* If this is the reserved TTY, we'll start the getty |
520 | * on it in any case, but otherwise only if it is not | |
521 | * busy. */ | |
522 | ||
523 | r = vt_is_busy(vtnr); | |
524 | if (r < 0) | |
525 | return r; | |
526 | else if (r > 0) | |
527 | return -EBUSY; | |
528 | } | |
529 | ||
61376f96 | 530 | snprintf(name, sizeof(name), "autovt@tty%u.service", vtnr); |
cc377381 | 531 | r = sd_bus_call_method( |
2b3ab29d ZJS |
532 | m->bus, |
533 | "org.freedesktop.systemd1", | |
534 | "/org/freedesktop/systemd1", | |
535 | "org.freedesktop.systemd1.Manager", | |
536 | "StartUnit", | |
cc377381 | 537 | &error, |
2b3ab29d | 538 | NULL, |
cc377381 LP |
539 | "ss", name, "fail"); |
540 | if (r < 0) | |
4ae25393 | 541 | return log_error_errno(r, "Failed to start %s: %s", name, bus_error_message(&error, r)); |
2b3ab29d | 542 | |
4ae25393 | 543 | return 0; |
2b3ab29d | 544 | } |
2d62c530 | 545 | |
9b9c23da LP |
546 | bool manager_is_lid_closed(Manager *m) { |
547 | Iterator i; | |
548 | Button *b; | |
549 | ||
550 | HASHMAP_FOREACH(b, m->buttons, i) | |
551 | if (b->lid_closed) | |
552 | return true; | |
553 | ||
554 | return false; | |
555 | } | |
556 | ||
602a41c2 | 557 | static bool manager_is_docked(Manager *m) { |
2d62c530 LP |
558 | Iterator i; |
559 | Button *b; | |
560 | ||
561 | HASHMAP_FOREACH(b, m->buttons, i) | |
562 | if (b->docked) | |
563 | return true; | |
564 | ||
565 | return false; | |
566 | } | |
6a79c586 | 567 | |
602a41c2 | 568 | static int manager_count_external_displays(Manager *m) { |
4f209af7 YW |
569 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
570 | sd_device *d; | |
571 | int r, n = 0; | |
6a79c586 | 572 | |
4f209af7 YW |
573 | r = sd_device_enumerator_new(&e); |
574 | if (r < 0) | |
575 | return r; | |
6a79c586 | 576 | |
4f209af7 | 577 | r = sd_device_enumerator_allow_uninitialized(e); |
6a79c586 LP |
578 | if (r < 0) |
579 | return r; | |
580 | ||
4f209af7 | 581 | r = sd_device_enumerator_add_match_subsystem(e, "drm", true); |
6a79c586 LP |
582 | if (r < 0) |
583 | return r; | |
584 | ||
8437c059 | 585 | FOREACH_DEVICE(e, d) { |
49fe5c09 | 586 | const char *status, *enabled, *dash, *nn, *subsys; |
4f209af7 | 587 | sd_device *p; |
6a79c586 | 588 | |
4f209af7 | 589 | if (sd_device_get_parent(d, &p) < 0) |
94036de8 | 590 | continue; |
6a79c586 LP |
591 | |
592 | /* If the parent shares the same subsystem as the | |
593 | * device we are looking at then it is a connector, | |
594 | * which is what we are interested in. */ | |
4f209af7 | 595 | if (sd_device_get_subsystem(p, &subsys) < 0 || !streq(subsys, "drm")) |
6a79c586 LP |
596 | continue; |
597 | ||
4f209af7 | 598 | if (sd_device_get_sysname(d, &nn) < 0) |
602a41c2 LP |
599 | continue; |
600 | ||
6ac38685 LP |
601 | /* Ignore internal displays: the type is encoded in the sysfs name, as the second dash separated item |
602 | * (the first is the card name, the last the connector number). We implement a blacklist of external | |
603 | * displays here, rather than a whitelist of internal ones, to ensure we don't block suspends too | |
604 | * eagerly. */ | |
602a41c2 LP |
605 | dash = strchr(nn, '-'); |
606 | if (!dash) | |
607 | continue; | |
608 | ||
609 | dash++; | |
49fe5c09 LP |
610 | if (!STARTSWITH_SET(dash, |
611 | "VGA-", "DVI-I-", "DVI-D-", "DVI-A-" | |
612 | "Composite-", "SVIDEO-", "Component-", | |
613 | "DIN-", "DP-", "HDMI-A-", "HDMI-B-", "TV-")) | |
602a41c2 LP |
614 | continue; |
615 | ||
616 | /* Ignore ports that are not enabled */ | |
4f209af7 | 617 | if (sd_device_get_sysattr_value(d, "enabled", &enabled) < 0 || !streq(enabled, "enabled")) |
602a41c2 LP |
618 | continue; |
619 | ||
6a79c586 LP |
620 | /* We count any connector which is not explicitly |
621 | * "disconnected" as connected. */ | |
4f209af7 | 622 | if (sd_device_get_sysattr_value(d, "status", &status) < 0 || !streq(status, "disconnected")) |
6a79c586 LP |
623 | n++; |
624 | } | |
625 | ||
626 | return n; | |
627 | } | |
3c56cab4 | 628 | |
602a41c2 | 629 | bool manager_is_docked_or_external_displays(Manager *m) { |
3c56cab4 BW |
630 | int n; |
631 | ||
632 | /* If we are docked don't react to lid closing */ | |
633 | if (manager_is_docked(m)) { | |
634 | log_debug("System is docked."); | |
635 | return true; | |
636 | } | |
637 | ||
638 | /* If we have more than one display connected, | |
639 | * assume that we are docked. */ | |
602a41c2 | 640 | n = manager_count_external_displays(m); |
3c56cab4 | 641 | if (n < 0) |
da927ba9 | 642 | log_warning_errno(n, "Display counting failed: %m"); |
602a41c2 LP |
643 | else if (n >= 1) { |
644 | log_debug("External (%i) displays connected.", n); | |
3c56cab4 BW |
645 | return true; |
646 | } | |
647 | ||
648 | return false; | |
649 | } | |
e25937a3 SF |
650 | |
651 | bool manager_is_on_external_power(void) { | |
652 | int r; | |
653 | ||
e455380b LP |
654 | /* For now we only check for AC power, but 'external power' can apply to anything that isn't an internal |
655 | * battery */ | |
e25937a3 SF |
656 | r = on_ac_power(); |
657 | if (r < 0) | |
658 | log_warning_errno(r, "Failed to read AC power status: %m"); | |
e25937a3 | 659 | |
e455380b | 660 | return r != 0; /* Treat failure as 'on AC' */ |
e25937a3 SF |
661 | } |
662 | ||
663 | bool manager_all_buttons_ignored(Manager *m) { | |
864fe630 LP |
664 | assert(m); |
665 | ||
e25937a3 SF |
666 | if (m->handle_power_key != HANDLE_IGNORE) |
667 | return false; | |
668 | if (m->handle_suspend_key != HANDLE_IGNORE) | |
669 | return false; | |
670 | if (m->handle_hibernate_key != HANDLE_IGNORE) | |
671 | return false; | |
672 | if (m->handle_lid_switch != HANDLE_IGNORE) | |
673 | return false; | |
ed0cb346 | 674 | if (!IN_SET(m->handle_lid_switch_ep, _HANDLE_ACTION_INVALID, HANDLE_IGNORE)) |
e25937a3 SF |
675 | return false; |
676 | if (m->handle_lid_switch_docked != HANDLE_IGNORE) | |
677 | return false; | |
864fe630 | 678 | |
e25937a3 SF |
679 | return true; |
680 | } | |
3d0ef5c7 LP |
681 | |
682 | int manager_read_utmp(Manager *m) { | |
683 | #if ENABLE_UTMP | |
684 | int r; | |
685 | ||
686 | assert(m); | |
687 | ||
688 | if (utmpxname(_PATH_UTMPX) < 0) | |
689 | return log_error_errno(errno, "Failed to set utmp path to " _PATH_UTMPX ": %m"); | |
690 | ||
691 | setutxent(); | |
692 | ||
693 | for (;;) { | |
694 | _cleanup_free_ char *t = NULL; | |
695 | struct utmpx *u; | |
696 | const char *c; | |
697 | Session *s; | |
698 | ||
699 | errno = 0; | |
700 | u = getutxent(); | |
701 | if (!u) { | |
702 | if (errno != 0) | |
703 | log_warning_errno(errno, "Failed to read " _PATH_UTMPX ", ignoring: %m"); | |
704 | r = 0; | |
705 | break; | |
706 | } | |
707 | ||
708 | if (u->ut_type != USER_PROCESS) | |
709 | continue; | |
710 | ||
711 | if (!pid_is_valid(u->ut_pid)) | |
712 | continue; | |
713 | ||
714 | t = strndup(u->ut_line, sizeof(u->ut_line)); | |
715 | if (!t) { | |
716 | r = log_oom(); | |
717 | break; | |
718 | } | |
719 | ||
720 | c = path_startswith(t, "/dev/"); | |
721 | if (c) { | |
722 | r = free_and_strdup(&t, c); | |
723 | if (r < 0) { | |
724 | log_oom(); | |
725 | break; | |
726 | } | |
727 | } | |
728 | ||
729 | if (isempty(t)) | |
730 | continue; | |
731 | ||
732 | s = hashmap_get(m->sessions_by_leader, PID_TO_PTR(u->ut_pid)); | |
733 | if (!s) | |
734 | continue; | |
735 | ||
736 | if (s->tty_validity == TTY_FROM_UTMP && !streq_ptr(s->tty, t)) { | |
737 | /* This may happen on multiplexed SSH connection (i.e. 'SSH connection sharing'). In | |
738 | * this case PAM and utmp sessions don't match. In such a case let's invalidate the TTY | |
739 | * information and never acquire it again. */ | |
740 | ||
741 | s->tty = mfree(s->tty); | |
742 | s->tty_validity = TTY_UTMP_INCONSISTENT; | |
743 | log_debug("Session '%s' has inconsistent TTY information, dropping TTY information.", s->id); | |
744 | continue; | |
745 | } | |
746 | ||
747 | /* Never override what we figured out once */ | |
748 | if (s->tty || s->tty_validity >= 0) | |
749 | continue; | |
750 | ||
751 | s->tty = TAKE_PTR(t); | |
752 | s->tty_validity = TTY_FROM_UTMP; | |
753 | log_debug("Acquired TTY information '%s' from utmp for session '%s'.", s->tty, s->id); | |
754 | } | |
755 | ||
756 | endutxent(); | |
757 | return r; | |
758 | #else | |
86cf4554 | 759 | return 0; |
3d0ef5c7 LP |
760 | #endif |
761 | } | |
762 | ||
763 | #if ENABLE_UTMP | |
764 | static int manager_dispatch_utmp(sd_event_source *s, const struct inotify_event *event, void *userdata) { | |
765 | Manager *m = userdata; | |
766 | ||
767 | assert(m); | |
768 | ||
769 | /* If there's indication the file itself might have been removed or became otherwise unavailable, then let's | |
770 | * reestablish the watch on whatever there's now. */ | |
771 | if ((event->mask & (IN_ATTRIB|IN_DELETE_SELF|IN_MOVE_SELF|IN_Q_OVERFLOW|IN_UNMOUNT)) != 0) | |
772 | manager_connect_utmp(m); | |
773 | ||
774 | (void) manager_read_utmp(m); | |
775 | return 0; | |
776 | } | |
777 | #endif | |
778 | ||
779 | void manager_connect_utmp(Manager *m) { | |
780 | #if ENABLE_UTMP | |
781 | sd_event_source *s = NULL; | |
782 | int r; | |
783 | ||
784 | assert(m); | |
785 | ||
786 | /* Watch utmp for changes via inotify. We do this to deal with tools such as ssh, which will register the PAM | |
787 | * session early, and acquire a TTY only much later for the connection. Thus during PAM the TTY won't be known | |
788 | * yet. ssh will register itself with utmp when it finally acquired the TTY. Hence, let's make use of this, and | |
789 | * watch utmp for the TTY asynchronously. We use the PAM session's leader PID as key, to find the right entry. | |
790 | * | |
791 | * Yes, relying on utmp is pretty ugly, but it's good enough for informational purposes, as well as idle | |
792 | * detection (which, for tty sessions, relies on the TTY used) */ | |
793 | ||
794 | r = sd_event_add_inotify(m->event, &s, _PATH_UTMPX, IN_MODIFY|IN_MOVE_SELF|IN_DELETE_SELF|IN_ATTRIB, manager_dispatch_utmp, m); | |
795 | if (r < 0) | |
796 | log_full_errno(r == -ENOENT ? LOG_DEBUG: LOG_WARNING, r, "Failed to create inotify watch on " _PATH_UTMPX ", ignoring: %m"); | |
797 | else { | |
798 | r = sd_event_source_set_priority(s, SD_EVENT_PRIORITY_IDLE); | |
799 | if (r < 0) | |
800 | log_warning_errno(r, "Failed to adjust utmp event source priority, ignoring: %m"); | |
801 | ||
802 | (void) sd_event_source_set_description(s, "utmp"); | |
803 | } | |
804 | ||
805 | sd_event_source_unref(m->utmp_event_source); | |
806 | m->utmp_event_source = s; | |
807 | #endif | |
808 | } | |
809 | ||
810 | void manager_reconnect_utmp(Manager *m) { | |
811 | #if ENABLE_UTMP | |
812 | assert(m); | |
813 | ||
814 | if (m->utmp_event_source) | |
815 | return; | |
816 | ||
817 | manager_connect_utmp(m); | |
818 | #endif | |
819 | } |