]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
20263082 LP |
2 | |
3 | #include <errno.h> | |
20263082 LP |
4 | #include <fcntl.h> |
5 | #include <string.h> | |
6 | #include <unistd.h> | |
20263082 | 7 | |
cc377381 | 8 | #include "sd-daemon.h" |
4f209af7 | 9 | #include "sd-device.h" |
07630cea | 10 | |
b5efdb8a | 11 | #include "alloc-util.h" |
cc377381 | 12 | #include "bus-error.h" |
07630cea | 13 | #include "bus-util.h" |
dccca82b | 14 | #include "cgroup-util.h" |
a0f29c76 | 15 | #include "def.h" |
8437c059 | 16 | #include "device-util.h" |
a0956174 | 17 | #include "dirent-util.h" |
3ffd4af2 | 18 | #include "fd-util.h" |
f97b34a6 | 19 | #include "format-util.h" |
af229d7a | 20 | #include "fs-util.h" |
3ffd4af2 | 21 | #include "logind.h" |
1bef256c | 22 | #include "parse-util.h" |
dccca82b | 23 | #include "process-util.h" |
4b51966c | 24 | #include "selinux-util.h" |
24882e06 | 25 | #include "signal-util.h" |
07630cea | 26 | #include "strv.h" |
20263082 | 27 | |
23462168 LP |
28 | static Manager* manager_unref(Manager *m); |
29 | DEFINE_TRIVIAL_CLEANUP_FUNC(Manager*, manager_unref); | |
905f0a39 | 30 | |
23462168 LP |
31 | static int manager_new(Manager **ret) { |
32 | _cleanup_(manager_unrefp) Manager *m = NULL; | |
a2ed7077 ZJS |
33 | int r; |
34 | ||
23462168 LP |
35 | assert(ret); |
36 | ||
b25ba6cf | 37 | m = new(Manager, 1); |
a2ed7077 | 38 | if (!m) |
23462168 | 39 | return -ENOMEM; |
a2ed7077 | 40 | |
b25ba6cf LP |
41 | *m = (Manager) { |
42 | .console_active_fd = -1, | |
43 | .reserve_vt_fd = -1, | |
44 | }; | |
a2ed7077 ZJS |
45 | |
46 | m->idle_action_not_before_usec = now(CLOCK_MONOTONIC); | |
47 | ||
d5099efc MS |
48 | m->devices = hashmap_new(&string_hash_ops); |
49 | m->seats = hashmap_new(&string_hash_ops); | |
50 | m->sessions = hashmap_new(&string_hash_ops); | |
238794b1 | 51 | m->sessions_by_leader = hashmap_new(NULL); |
d5099efc MS |
52 | m->users = hashmap_new(NULL); |
53 | m->inhibitors = hashmap_new(&string_hash_ops); | |
54 | m->buttons = hashmap_new(&string_hash_ops); | |
f8e2fb7b | 55 | |
d5099efc MS |
56 | m->user_units = hashmap_new(&string_hash_ops); |
57 | m->session_units = hashmap_new(&string_hash_ops); | |
8c8c4351 | 58 | |
238794b1 | 59 | if (!m->devices || !m->seats || !m->sessions || !m->sessions_by_leader || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units) |
23462168 | 60 | return -ENOMEM; |
25d93491 | 61 | |
afc6adb5 | 62 | r = sd_event_default(&m->event); |
ed4ba7e4 | 63 | if (r < 0) |
23462168 | 64 | return r; |
cc377381 | 65 | |
fcfa765d LP |
66 | r = sd_event_add_signal(m->event, NULL, SIGINT, NULL, NULL); |
67 | if (r < 0) | |
68 | return r; | |
69 | ||
70 | r = sd_event_add_signal(m->event, NULL, SIGTERM, NULL, NULL); | |
71 | if (r < 0) | |
72 | return r; | |
73 | ||
23462168 | 74 | (void) sd_event_set_watchdog(m->event, true); |
cde93897 | 75 | |
a2ed7077 ZJS |
76 | manager_reset_config(m); |
77 | ||
23462168 LP |
78 | *ret = TAKE_PTR(m); |
79 | return 0; | |
20263082 LP |
80 | } |
81 | ||
23462168 | 82 | static Manager* manager_unref(Manager *m) { |
20263082 LP |
83 | Session *session; |
84 | User *u; | |
85 | Device *d; | |
86 | Seat *s; | |
f8e2fb7b | 87 | Inhibitor *i; |
069cfc85 | 88 | Button *b; |
20263082 | 89 | |
84a4e660 | 90 | if (!m) |
23462168 | 91 | return NULL; |
20263082 LP |
92 | |
93 | while ((session = hashmap_first(m->sessions))) | |
94 | session_free(session); | |
95 | ||
96 | while ((u = hashmap_first(m->users))) | |
97 | user_free(u); | |
98 | ||
99 | while ((d = hashmap_first(m->devices))) | |
100 | device_free(d); | |
101 | ||
102 | while ((s = hashmap_first(m->seats))) | |
103 | seat_free(s); | |
104 | ||
f8e2fb7b LP |
105 | while ((i = hashmap_first(m->inhibitors))) |
106 | inhibitor_free(i); | |
107 | ||
069cfc85 LP |
108 | while ((b = hashmap_first(m->buttons))) |
109 | button_free(b); | |
110 | ||
20263082 LP |
111 | hashmap_free(m->devices); |
112 | hashmap_free(m->seats); | |
f8e2fb7b | 113 | hashmap_free(m->sessions); |
238794b1 | 114 | hashmap_free(m->sessions_by_leader); |
f8e2fb7b LP |
115 | hashmap_free(m->users); |
116 | hashmap_free(m->inhibitors); | |
069cfc85 | 117 | hashmap_free(m->buttons); |
f8e2fb7b | 118 | |
fb6becb4 LP |
119 | hashmap_free(m->user_units); |
120 | hashmap_free(m->session_units); | |
8c8c4351 | 121 | |
cc377381 | 122 | sd_event_source_unref(m->idle_action_event_source); |
c0f32805 | 123 | sd_event_source_unref(m->inhibit_timeout_source); |
8aaa023a | 124 | sd_event_source_unref(m->scheduled_shutdown_timeout_source); |
867c37f6 | 125 | sd_event_source_unref(m->nologin_timeout_source); |
e2fa5721 | 126 | sd_event_source_unref(m->wall_message_timeout_source); |
cc377381 LP |
127 | |
128 | sd_event_source_unref(m->console_active_event_source); | |
f9cd6be1 | 129 | sd_event_source_unref(m->lid_switch_ignore_event_source); |
20263082 | 130 | |
3d0ef5c7 LP |
131 | #if ENABLE_UTMP |
132 | sd_event_source_unref(m->utmp_event_source); | |
133 | #endif | |
134 | ||
03e334a1 | 135 | safe_close(m->console_active_fd); |
20263082 | 136 | |
eb968396 YW |
137 | sd_device_monitor_unref(m->device_seat_monitor); |
138 | sd_device_monitor_unref(m->device_monitor); | |
139 | sd_device_monitor_unref(m->device_vcsa_monitor); | |
140 | sd_device_monitor_unref(m->device_button_monitor); | |
3e044c49 | 141 | |
867c37f6 | 142 | if (m->unlink_nologin) |
af229d7a | 143 | (void) unlink_or_warn("/run/nologin"); |
867c37f6 | 144 | |
36e34057 | 145 | bus_verify_polkit_async_registry_free(m->polkit_registry); |
20263082 | 146 | |
cc377381 LP |
147 | sd_bus_unref(m->bus); |
148 | sd_event_unref(m->event); | |
20263082 | 149 | |
03e334a1 | 150 | safe_close(m->reserve_vt_fd); |
98a77df5 | 151 | |
193197e8 LP |
152 | strv_free(m->kill_only_users); |
153 | strv_free(m->kill_exclude_users); | |
154 | ||
8aaa023a | 155 | free(m->scheduled_shutdown_type); |
e2fa5721 DM |
156 | free(m->scheduled_shutdown_tty); |
157 | free(m->wall_message); | |
af9792ac | 158 | free(m->action_job); |
23462168 LP |
159 | |
160 | return mfree(m); | |
20263082 LP |
161 | } |
162 | ||
9588bc32 | 163 | static int manager_enumerate_devices(Manager *m) { |
4f209af7 YW |
164 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
165 | sd_device *d; | |
14c3baca | 166 | int r; |
20263082 LP |
167 | |
168 | assert(m); | |
169 | ||
170 | /* Loads devices from udev and creates seats for them as | |
171 | * necessary */ | |
172 | ||
4f209af7 | 173 | r = sd_device_enumerator_new(&e); |
14c3baca | 174 | if (r < 0) |
bf5332d2 | 175 | return r; |
20263082 | 176 | |
4f209af7 | 177 | r = sd_device_enumerator_add_match_tag(e, "master-of-seat"); |
e1202047 LP |
178 | if (r < 0) |
179 | return r; | |
180 | ||
8437c059 | 181 | FOREACH_DEVICE(e, d) { |
20263082 LP |
182 | int k; |
183 | ||
bf5332d2 | 184 | k = manager_process_seat_device(m, d); |
20263082 LP |
185 | if (k < 0) |
186 | r = k; | |
187 | } | |
188 | ||
20263082 LP |
189 | return r; |
190 | } | |
191 | ||
9588bc32 | 192 | static int manager_enumerate_buttons(Manager *m) { |
4f209af7 YW |
193 | _cleanup_(sd_device_enumerator_unrefp) sd_device_enumerator *e = NULL; |
194 | sd_device *d; | |
069cfc85 LP |
195 | int r; |
196 | ||
197 | assert(m); | |
198 | ||
199 | /* Loads buttons from udev */ | |
200 | ||
e25937a3 | 201 | if (manager_all_buttons_ignored(m)) |
6de0e0e5 LP |
202 | return 0; |
203 | ||
4f209af7 | 204 | r = sd_device_enumerator_new(&e); |
069cfc85 | 205 | if (r < 0) |
bf5332d2 | 206 | return r; |
069cfc85 | 207 | |
4f209af7 | 208 | r = sd_device_enumerator_add_match_subsystem(e, "input", true); |
069cfc85 | 209 | if (r < 0) |
bf5332d2 | 210 | return r; |
069cfc85 | 211 | |
4f209af7 | 212 | r = sd_device_enumerator_add_match_tag(e, "power-switch"); |
e1202047 LP |
213 | if (r < 0) |
214 | return r; | |
215 | ||
8437c059 | 216 | FOREACH_DEVICE(e, d) { |
069cfc85 LP |
217 | int k; |
218 | ||
bf5332d2 | 219 | k = manager_process_button_device(m, d); |
069cfc85 LP |
220 | if (k < 0) |
221 | r = k; | |
222 | } | |
223 | ||
069cfc85 LP |
224 | return r; |
225 | } | |
226 | ||
9588bc32 | 227 | static int manager_enumerate_seats(Manager *m) { |
9444b1f2 | 228 | _cleanup_closedir_ DIR *d = NULL; |
20263082 LP |
229 | struct dirent *de; |
230 | int r = 0; | |
231 | ||
232 | assert(m); | |
233 | ||
234 | /* This loads data about seats stored on disk, but does not | |
235 | * actually create any seats. Removes data of seats that no | |
236 | * longer exist. */ | |
237 | ||
238 | d = opendir("/run/systemd/seats"); | |
239 | if (!d) { | |
240 | if (errno == ENOENT) | |
241 | return 0; | |
242 | ||
e1427b13 | 243 | return log_error_errno(errno, "Failed to open /run/systemd/seats: %m"); |
20263082 LP |
244 | } |
245 | ||
9444b1f2 | 246 | FOREACH_DIRENT(de, d, return -errno) { |
20263082 LP |
247 | Seat *s; |
248 | int k; | |
249 | ||
250 | if (!dirent_is_file(de)) | |
251 | continue; | |
252 | ||
253 | s = hashmap_get(m->seats, de->d_name); | |
254 | if (!s) { | |
cb51ee7a ZJS |
255 | if (unlinkat(dirfd(d), de->d_name, 0) < 0) |
256 | log_warning("Failed to remove /run/systemd/seats/%s: %m", | |
257 | de->d_name); | |
20263082 LP |
258 | continue; |
259 | } | |
260 | ||
261 | k = seat_load(s); | |
262 | if (k < 0) | |
263 | r = k; | |
264 | } | |
265 | ||
14c3baca LP |
266 | return r; |
267 | } | |
20263082 | 268 | |
14c3baca | 269 | static int manager_enumerate_linger_users(Manager *m) { |
9444b1f2 | 270 | _cleanup_closedir_ DIR *d = NULL; |
14c3baca LP |
271 | struct dirent *de; |
272 | int r = 0; | |
273 | ||
9444b1f2 LP |
274 | assert(m); |
275 | ||
14c3baca LP |
276 | d = opendir("/var/lib/systemd/linger"); |
277 | if (!d) { | |
278 | if (errno == ENOENT) | |
279 | return 0; | |
280 | ||
e1427b13 | 281 | return log_error_errno(errno, "Failed to open /var/lib/systemd/linger/: %m"); |
14c3baca LP |
282 | } |
283 | ||
9444b1f2 | 284 | FOREACH_DIRENT(de, d, return -errno) { |
14c3baca LP |
285 | int k; |
286 | ||
287 | if (!dirent_is_file(de)) | |
20263082 LP |
288 | continue; |
289 | ||
14c3baca LP |
290 | k = manager_add_user_by_name(m, de->d_name, NULL); |
291 | if (k < 0) { | |
da927ba9 | 292 | log_notice_errno(k, "Couldn't add lingering user %s: %m", de->d_name); |
14c3baca | 293 | r = k; |
20263082 LP |
294 | } |
295 | } | |
296 | ||
20263082 LP |
297 | return r; |
298 | } | |
299 | ||
9588bc32 | 300 | static int manager_enumerate_users(Manager *m) { |
9444b1f2 | 301 | _cleanup_closedir_ DIR *d = NULL; |
20263082 | 302 | struct dirent *de; |
14c3baca | 303 | int r, k; |
20263082 LP |
304 | |
305 | assert(m); | |
306 | ||
9444b1f2 | 307 | /* Add lingering users */ |
b3629c7d | 308 | r = manager_enumerate_linger_users(m); |
14c3baca | 309 | |
9444b1f2 | 310 | /* Read in user data stored on disk */ |
20263082 LP |
311 | d = opendir("/run/systemd/users"); |
312 | if (!d) { | |
313 | if (errno == ENOENT) | |
314 | return 0; | |
315 | ||
e1427b13 | 316 | return log_error_errno(errno, "Failed to open /run/systemd/users: %m"); |
20263082 LP |
317 | } |
318 | ||
9444b1f2 | 319 | FOREACH_DIRENT(de, d, return -errno) { |
20263082 | 320 | User *u; |
20263082 LP |
321 | |
322 | if (!dirent_is_file(de)) | |
323 | continue; | |
324 | ||
9444b1f2 | 325 | k = manager_add_user_by_name(m, de->d_name, &u); |
20263082 | 326 | if (k < 0) { |
da927ba9 | 327 | log_error_errno(k, "Failed to add user by file name %s: %m", de->d_name); |
20263082 | 328 | |
9444b1f2 | 329 | r = k; |
20263082 LP |
330 | continue; |
331 | } | |
332 | ||
9444b1f2 LP |
333 | user_add_to_gc_queue(u); |
334 | ||
20263082 LP |
335 | k = user_load(u); |
336 | if (k < 0) | |
337 | r = k; | |
338 | } | |
339 | ||
20263082 LP |
340 | return r; |
341 | } | |
342 | ||
1bef256c AJ |
343 | static int parse_fdname(const char *fdname, char **session_id, dev_t *dev) { |
344 | _cleanup_strv_free_ char **parts = NULL; | |
345 | _cleanup_free_ char *id = NULL; | |
14cb109d | 346 | unsigned major, minor; |
1bef256c AJ |
347 | int r; |
348 | ||
349 | parts = strv_split(fdname, "-"); | |
350 | if (!parts) | |
351 | return -ENOMEM; | |
352 | if (strv_length(parts) != 5) | |
353 | return -EINVAL; | |
354 | ||
355 | if (!streq(parts[0], "session")) | |
356 | return -EINVAL; | |
36591e10 | 357 | |
1bef256c AJ |
358 | id = strdup(parts[1]); |
359 | if (!id) | |
360 | return -ENOMEM; | |
361 | ||
362 | if (!streq(parts[2], "device")) | |
363 | return -EINVAL; | |
36591e10 ZJS |
364 | |
365 | r = safe_atou(parts[3], &major); | |
366 | if (r < 0) | |
367 | return r; | |
368 | r = safe_atou(parts[4], &minor); | |
1bef256c AJ |
369 | if (r < 0) |
370 | return r; | |
371 | ||
372 | *dev = makedev(major, minor); | |
ae2a15bc | 373 | *session_id = TAKE_PTR(id); |
1bef256c AJ |
374 | |
375 | return 0; | |
376 | } | |
377 | ||
aed24c4c FB |
378 | static int manager_attach_fds(Manager *m) { |
379 | _cleanup_strv_free_ char **fdnames = NULL; | |
380 | int n, i, fd; | |
381 | ||
382 | /* Upon restart, PID1 will send us back all fds of session devices | |
383 | * that we previously opened. Each file descriptor is associated | |
384 | * with a given session. The session ids are passed through FDNAMES. */ | |
385 | ||
386 | n = sd_listen_fds_with_names(true, &fdnames); | |
387 | if (n <= 0) | |
388 | return n; | |
389 | ||
390 | for (i = 0; i < n; i++) { | |
1bef256c AJ |
391 | _cleanup_free_ char *id = NULL; |
392 | dev_t dev; | |
aed24c4c FB |
393 | struct stat st; |
394 | SessionDevice *sd; | |
395 | Session *s; | |
1bef256c | 396 | int r; |
aed24c4c FB |
397 | |
398 | fd = SD_LISTEN_FDS_START + i; | |
399 | ||
1bef256c AJ |
400 | r = parse_fdname(fdnames[i], &id, &dev); |
401 | if (r < 0) { | |
402 | log_debug_errno(r, "Failed to parse fd name %s: %m", fdnames[i]); | |
403 | close_nointr(fd); | |
aed24c4c | 404 | continue; |
1bef256c | 405 | } |
aed24c4c FB |
406 | |
407 | s = hashmap_get(m->sessions, id); | |
408 | if (!s) { | |
409 | /* If the session doesn't exist anymore, the associated session | |
410 | * device attached to this fd doesn't either. Let's simply close | |
411 | * this fd. */ | |
412 | log_debug("Failed to attach fd for unknown session: %s", id); | |
413 | close_nointr(fd); | |
414 | continue; | |
415 | } | |
416 | ||
417 | if (fstat(fd, &st) < 0) { | |
418 | /* The device is allowed to go away at a random point, in which | |
419 | * case fstat failing is expected. */ | |
420 | log_debug_errno(errno, "Failed to stat device fd for session %s: %m", id); | |
421 | close_nointr(fd); | |
422 | continue; | |
423 | } | |
424 | ||
1bef256c AJ |
425 | if (!S_ISCHR(st.st_mode) || st.st_rdev != dev) { |
426 | log_debug("Device fd doesn't point to the expected character device node"); | |
51ead3e3 LP |
427 | close_nointr(fd); |
428 | continue; | |
429 | } | |
430 | ||
1bef256c | 431 | sd = hashmap_get(s->devices, &dev); |
aed24c4c | 432 | if (!sd) { |
864fe630 | 433 | /* Weird, we got an fd for a session device which wasn't |
1bef256c | 434 | * recorded in the session state file... */ |
aed24c4c | 435 | log_warning("Got fd for missing session device [%u:%u] in session %s", |
1bef256c | 436 | major(dev), minor(dev), s->id); |
aed24c4c FB |
437 | close_nointr(fd); |
438 | continue; | |
439 | } | |
440 | ||
441 | log_debug("Attaching fd to session device [%u:%u] for session %s", | |
1bef256c | 442 | major(dev), minor(dev), s->id); |
aed24c4c FB |
443 | |
444 | session_device_attach_fd(sd, fd, s->was_active); | |
445 | } | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
9588bc32 | 450 | static int manager_enumerate_sessions(Manager *m) { |
9444b1f2 LP |
451 | _cleanup_closedir_ DIR *d = NULL; |
452 | struct dirent *de; | |
aed24c4c | 453 | int r = 0, k; |
20263082 | 454 | |
9444b1f2 LP |
455 | assert(m); |
456 | ||
457 | /* Read in session data stored on disk */ | |
458 | d = opendir("/run/systemd/sessions"); | |
459 | if (!d) { | |
460 | if (errno == ENOENT) | |
461 | return 0; | |
462 | ||
e1427b13 | 463 | return log_error_errno(errno, "Failed to open /run/systemd/sessions: %m"); |
9444b1f2 LP |
464 | } |
465 | ||
466 | FOREACH_DIRENT(de, d, return -errno) { | |
467 | struct Session *s; | |
20263082 | 468 | |
9444b1f2 | 469 | if (!dirent_is_file(de)) |
14c3baca LP |
470 | continue; |
471 | ||
4b549144 ZJS |
472 | if (!session_id_valid(de->d_name)) { |
473 | log_warning("Invalid session file name '%s', ignoring.", de->d_name); | |
474 | r = -EINVAL; | |
475 | continue; | |
476 | } | |
477 | ||
9444b1f2 | 478 | k = manager_add_session(m, de->d_name, &s); |
20263082 | 479 | if (k < 0) { |
da927ba9 | 480 | log_error_errno(k, "Failed to add session by file name %s: %m", de->d_name); |
20263082 LP |
481 | r = k; |
482 | continue; | |
483 | } | |
484 | ||
9444b1f2 | 485 | session_add_to_gc_queue(s); |
20263082 | 486 | |
9444b1f2 | 487 | k = session_load(s); |
20263082 LP |
488 | if (k < 0) |
489 | r = k; | |
490 | } | |
491 | ||
aed24c4c FB |
492 | /* We might be restarted and PID1 could have sent us back the |
493 | * session device fds we previously saved. */ | |
494 | k = manager_attach_fds(m); | |
495 | if (k < 0) | |
496 | log_warning_errno(k, "Failed to reattach session device fds: %m"); | |
497 | ||
20263082 LP |
498 | return r; |
499 | } | |
500 | ||
9588bc32 | 501 | static int manager_enumerate_inhibitors(Manager *m) { |
9444b1f2 | 502 | _cleanup_closedir_ DIR *d = NULL; |
20263082 LP |
503 | struct dirent *de; |
504 | int r = 0; | |
505 | ||
506 | assert(m); | |
507 | ||
9444b1f2 | 508 | d = opendir("/run/systemd/inhibit"); |
20263082 LP |
509 | if (!d) { |
510 | if (errno == ENOENT) | |
511 | return 0; | |
512 | ||
e1427b13 | 513 | return log_error_errno(errno, "Failed to open /run/systemd/inhibit: %m"); |
20263082 LP |
514 | } |
515 | ||
9444b1f2 | 516 | FOREACH_DIRENT(de, d, return -errno) { |
20263082 | 517 | int k; |
9444b1f2 | 518 | Inhibitor *i; |
20263082 LP |
519 | |
520 | if (!dirent_is_file(de)) | |
521 | continue; | |
522 | ||
9444b1f2 LP |
523 | k = manager_add_inhibitor(m, de->d_name, &i); |
524 | if (k < 0) { | |
da927ba9 | 525 | log_notice_errno(k, "Couldn't add inhibitor %s: %m", de->d_name); |
9444b1f2 | 526 | r = k; |
20263082 LP |
527 | continue; |
528 | } | |
529 | ||
9444b1f2 | 530 | k = inhibitor_load(i); |
20263082 LP |
531 | if (k < 0) |
532 | r = k; | |
533 | } | |
534 | ||
20263082 LP |
535 | return r; |
536 | } | |
537 | ||
eb968396 | 538 | static int manager_dispatch_seat_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
cc377381 | 539 | Manager *m = userdata; |
20263082 LP |
540 | |
541 | assert(m); | |
eb968396 | 542 | assert(device); |
20263082 | 543 | |
eb968396 | 544 | manager_process_seat_device(m, device); |
7b77ed8c | 545 | return 0; |
30ed21ce LP |
546 | } |
547 | ||
eb968396 | 548 | static int manager_dispatch_device_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
cc377381 | 549 | Manager *m = userdata; |
718d006a DH |
550 | |
551 | assert(m); | |
eb968396 | 552 | assert(device); |
718d006a | 553 | |
eb968396 | 554 | manager_process_seat_device(m, device); |
7b77ed8c | 555 | return 0; |
718d006a DH |
556 | } |
557 | ||
eb968396 | 558 | static int manager_dispatch_vcsa_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
cc377381 | 559 | Manager *m = userdata; |
4f209af7 | 560 | const char *name, *action; |
30ed21ce LP |
561 | |
562 | assert(m); | |
eb968396 | 563 | assert(device); |
30ed21ce LP |
564 | |
565 | /* Whenever a VCSA device is removed try to reallocate our | |
566 | * VTs, to make sure our auto VTs never go away. */ | |
567 | ||
eb968396 | 568 | if (sd_device_get_sysname(device, &name) >= 0 && |
4f209af7 | 569 | startswith(name, "vcsa") && |
eb968396 | 570 | sd_device_get_property_value(device, "ACTION", &action) >= 0 && |
403660c5 | 571 | streq(action, "remove")) |
7b77ed8c | 572 | seat_preallocate_vts(m->seat0); |
30ed21ce | 573 | |
7b77ed8c | 574 | return 0; |
20263082 LP |
575 | } |
576 | ||
eb968396 | 577 | static int manager_dispatch_button_udev(sd_device_monitor *monitor, sd_device *device, void *userdata) { |
cc377381 | 578 | Manager *m = userdata; |
069cfc85 LP |
579 | |
580 | assert(m); | |
eb968396 | 581 | assert(device); |
069cfc85 | 582 | |
eb968396 | 583 | manager_process_button_device(m, device); |
7b77ed8c | 584 | return 0; |
069cfc85 LP |
585 | } |
586 | ||
cc377381 LP |
587 | static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents, void *userdata) { |
588 | Manager *m = userdata; | |
589 | ||
20263082 | 590 | assert(m); |
92432fcc | 591 | assert(m->seat0); |
cc377381 | 592 | assert(m->console_active_fd == fd); |
20263082 | 593 | |
92432fcc | 594 | seat_read_active_vt(m->seat0); |
20263082 LP |
595 | return 0; |
596 | } | |
597 | ||
98a77df5 LP |
598 | static int manager_reserve_vt(Manager *m) { |
599 | _cleanup_free_ char *p = NULL; | |
600 | ||
601 | assert(m); | |
602 | ||
603 | if (m->reserve_vt <= 0) | |
604 | return 0; | |
605 | ||
606 | if (asprintf(&p, "/dev/tty%u", m->reserve_vt) < 0) | |
607 | return log_oom(); | |
608 | ||
609 | m->reserve_vt_fd = open(p, O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); | |
610 | if (m->reserve_vt_fd < 0) { | |
d4d882e5 LP |
611 | |
612 | /* Don't complain on VT-less systems */ | |
613 | if (errno != ENOENT) | |
56f64d95 | 614 | log_warning_errno(errno, "Failed to pin reserved VT: %m"); |
98a77df5 LP |
615 | return -errno; |
616 | } | |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
cc377381 | 621 | static int manager_connect_bus(Manager *m) { |
cc377381 | 622 | int r; |
31b79c2b | 623 | |
cc377381 LP |
624 | assert(m); |
625 | assert(!m->bus); | |
31b79c2b | 626 | |
76b54375 | 627 | r = sd_bus_default_system(&m->bus); |
f647962d MS |
628 | if (r < 0) |
629 | return log_error_errno(r, "Failed to connect to system bus: %m"); | |
31b79c2b | 630 | |
19befb2d | 631 | r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/login1", "org.freedesktop.login1.Manager", manager_vtable, m); |
f647962d MS |
632 | if (r < 0) |
633 | return log_error_errno(r, "Failed to add manager object vtable: %m"); | |
f8e2fb7b | 634 | |
19befb2d | 635 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m); |
f647962d MS |
636 | if (r < 0) |
637 | return log_error_errno(r, "Failed to add seat object vtable: %m"); | |
069cfc85 | 638 | |
19befb2d | 639 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/seat", seat_node_enumerator, m); |
f647962d MS |
640 | if (r < 0) |
641 | return log_error_errno(r, "Failed to add seat enumerator: %m"); | |
20263082 | 642 | |
19befb2d | 643 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/session", "org.freedesktop.login1.Session", session_vtable, session_object_find, m); |
f647962d MS |
644 | if (r < 0) |
645 | return log_error_errno(r, "Failed to add session object vtable: %m"); | |
20263082 | 646 | |
19befb2d | 647 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/session", session_node_enumerator, m); |
f647962d MS |
648 | if (r < 0) |
649 | return log_error_errno(r, "Failed to add session enumerator: %m"); | |
20263082 | 650 | |
19befb2d | 651 | r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/user", "org.freedesktop.login1.User", user_vtable, user_object_find, m); |
f647962d MS |
652 | if (r < 0) |
653 | return log_error_errno(r, "Failed to add user object vtable: %m"); | |
20263082 | 654 | |
19befb2d | 655 | r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/user", user_node_enumerator, m); |
f647962d MS |
656 | if (r < 0) |
657 | return log_error_errno(r, "Failed to add user enumerator: %m"); | |
20263082 | 658 | |
75152a4d LP |
659 | r = sd_bus_match_signal_async( |
660 | m->bus, | |
661 | NULL, | |
662 | "org.freedesktop.systemd1", | |
663 | "/org/freedesktop/systemd1", | |
664 | "org.freedesktop.systemd1.Manager", | |
665 | "JobRemoved", | |
666 | match_job_removed, NULL, m); | |
f647962d | 667 | if (r < 0) |
75152a4d LP |
668 | return log_error_errno(r, "Failed to request match for JobRemoved: %m"); |
669 | ||
670 | r = sd_bus_match_signal_async( | |
671 | m->bus, | |
672 | NULL, | |
673 | "org.freedesktop.systemd1", | |
674 | "/org/freedesktop/systemd1", | |
675 | "org.freedesktop.systemd1.Manager", | |
676 | "UnitRemoved", | |
677 | match_unit_removed, NULL, m); | |
f647962d | 678 | if (r < 0) |
75152a4d LP |
679 | return log_error_errno(r, "Failed to request match for UnitRemoved: %m"); |
680 | ||
681 | r = sd_bus_match_signal_async( | |
682 | m->bus, | |
683 | NULL, | |
684 | "org.freedesktop.systemd1", | |
685 | NULL, | |
686 | "org.freedesktop.DBus.Properties", | |
687 | "PropertiesChanged", | |
688 | match_properties_changed, NULL, m); | |
f647962d | 689 | if (r < 0) |
75152a4d LP |
690 | return log_error_errno(r, "Failed to request match for PropertiesChanged: %m"); |
691 | ||
692 | r = sd_bus_match_signal_async( | |
693 | m->bus, | |
694 | NULL, | |
695 | "org.freedesktop.systemd1", | |
696 | "/org/freedesktop/systemd1", | |
697 | "org.freedesktop.systemd1.Manager", | |
698 | "Reloading", | |
699 | match_reloading, NULL, m); | |
f647962d | 700 | if (r < 0) |
75152a4d | 701 | return log_error_errno(r, "Failed to request match for Reloading: %m"); |
6797c324 | 702 | |
31b2cd5d | 703 | r = sd_bus_call_method_async( |
fb6becb4 | 704 | m->bus, |
31b2cd5d | 705 | NULL, |
fb6becb4 LP |
706 | "org.freedesktop.systemd1", |
707 | "/org/freedesktop/systemd1", | |
708 | "org.freedesktop.systemd1.Manager", | |
709 | "Subscribe", | |
31b2cd5d LP |
710 | NULL, NULL, |
711 | NULL); | |
712 | if (r < 0) | |
713 | return log_error_errno(r, "Failed to enable subscription: %m"); | |
20263082 | 714 | |
0c0b9306 | 715 | r = sd_bus_request_name_async(m->bus, NULL, "org.freedesktop.login1", 0, NULL, NULL); |
f647962d | 716 | if (r < 0) |
0c0b9306 | 717 | return log_error_errno(r, "Failed to request name: %m"); |
bafd4449 | 718 | |
e11544a8 | 719 | r = sd_bus_attach_event(m->bus, m->event, SD_EVENT_PRIORITY_NORMAL); |
f647962d MS |
720 | if (r < 0) |
721 | return log_error_errno(r, "Failed to attach bus to event loop: %m"); | |
20263082 | 722 | |
20263082 | 723 | return 0; |
20263082 LP |
724 | } |
725 | ||
92683ad2 DH |
726 | static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) { |
727 | Manager *m = data; | |
728 | Session *active, *iter; | |
729 | ||
730 | /* | |
731 | * We got a VT-switch signal and we have to acknowledge it immediately. | |
732 | * Preferably, we'd just use m->seat0->active->vtfd, but unfortunately, | |
733 | * old user-space might run multiple sessions on a single VT, *sigh*. | |
734 | * Therefore, we have to iterate all sessions and find one with a vtfd | |
735 | * on the requested VT. | |
736 | * As only VTs with active controllers have VT_PROCESS set, our current | |
737 | * notion of the active VT might be wrong (for instance if the switch | |
738 | * happens while we setup VT_PROCESS). Therefore, read the current VT | |
739 | * first and then use s->active->vtnr as reference. Note that this is | |
740 | * not racy, as no further VT-switch can happen as long as we're in | |
741 | * synchronous VT_PROCESS mode. | |
742 | */ | |
743 | ||
744 | assert(m->seat0); | |
745 | seat_read_active_vt(m->seat0); | |
746 | ||
747 | active = m->seat0->active; | |
748 | if (!active || active->vtnr < 1) { | |
749 | log_warning("Received VT_PROCESS signal without a registered session on that VT."); | |
750 | return 0; | |
751 | } | |
752 | ||
753 | if (active->vtfd >= 0) { | |
2ec3ff66 | 754 | session_leave_vt(active); |
92683ad2 DH |
755 | } else { |
756 | LIST_FOREACH(sessions_by_seat, iter, m->seat0->sessions) { | |
757 | if (iter->vtnr == active->vtnr && iter->vtfd >= 0) { | |
2ec3ff66 | 758 | session_leave_vt(iter); |
92683ad2 DH |
759 | break; |
760 | } | |
761 | } | |
762 | } | |
763 | ||
764 | return 0; | |
765 | } | |
766 | ||
20263082 | 767 | static int manager_connect_console(Manager *m) { |
cc377381 | 768 | int r; |
20263082 LP |
769 | |
770 | assert(m); | |
771 | assert(m->console_active_fd < 0); | |
772 | ||
0b6d55ca LP |
773 | /* On certain systems (such as S390, Xen, and containers) /dev/tty0 does not exist (as there is no VC), so |
774 | * don't fail if we can't open it. */ | |
775 | ||
cc377381 | 776 | if (access("/dev/tty0", F_OK) < 0) |
74afee9c | 777 | return 0; |
74afee9c | 778 | |
20263082 LP |
779 | m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC); |
780 | if (m->console_active_fd < 0) { | |
cdc564d2 | 781 | |
0b6d55ca LP |
782 | /* On some systems /dev/tty0 may exist even though /sys/class/tty/tty0 does not. These are broken, but |
783 | * common. Let's complain but continue anyway. */ | |
784 | if (errno == ENOENT) { | |
785 | log_warning_errno(errno, "System has /dev/tty0 but not /sys/class/tty/tty0/active which is broken, ignoring: %m"); | |
cdc564d2 | 786 | return 0; |
0b6d55ca | 787 | } |
cdc564d2 | 788 | |
e1427b13 | 789 | return log_error_errno(errno, "Failed to open /sys/class/tty/tty0/active: %m"); |
20263082 LP |
790 | } |
791 | ||
151b9b96 | 792 | r = sd_event_add_io(m->event, &m->console_active_event_source, m->console_active_fd, 0, manager_dispatch_console, m); |
0b6d55ca LP |
793 | if (r < 0) |
794 | return log_error_errno(r, "Failed to watch foreground console: %m"); | |
20263082 | 795 | |
92683ad2 DH |
796 | /* |
797 | * SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used | |
798 | * as VT-acquire signal. We ignore any acquire-events (yes, we still | |
799 | * have to provide a valid signal-number for it!) and acknowledge all | |
800 | * release events immediately. | |
801 | */ | |
802 | ||
803 | if (SIGRTMIN + 1 > SIGRTMAX) { | |
804 | log_error("Not enough real-time signals available: %u-%u", SIGRTMIN, SIGRTMAX); | |
805 | return -EINVAL; | |
806 | } | |
807 | ||
72c0a2c2 LP |
808 | assert_se(ignore_signals(SIGRTMIN + 1, -1) >= 0); |
809 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGRTMIN, -1) >= 0); | |
92683ad2 DH |
810 | |
811 | r = sd_event_add_signal(m->event, NULL, SIGRTMIN, manager_vt_switch, m); | |
812 | if (r < 0) | |
0b6d55ca | 813 | return log_error_errno(r, "Failed to subscribe to signal: %m"); |
92683ad2 | 814 | |
20263082 LP |
815 | return 0; |
816 | } | |
817 | ||
818 | static int manager_connect_udev(Manager *m) { | |
14c3baca | 819 | int r; |
20263082 LP |
820 | |
821 | assert(m); | |
eb968396 YW |
822 | assert(!m->device_seat_monitor); |
823 | assert(!m->device_monitor); | |
824 | assert(!m->device_vcsa_monitor); | |
825 | assert(!m->device_button_monitor); | |
20263082 | 826 | |
eb968396 YW |
827 | r = sd_device_monitor_new(&m->device_seat_monitor); |
828 | if (r < 0) | |
829 | return r; | |
20263082 | 830 | |
eb968396 | 831 | r = sd_device_monitor_filter_add_match_tag(m->device_seat_monitor, "master-of-seat"); |
14c3baca LP |
832 | if (r < 0) |
833 | return r; | |
20263082 | 834 | |
eb968396 | 835 | r = sd_device_monitor_attach_event(m->device_seat_monitor, m->event, 0); |
14c3baca LP |
836 | if (r < 0) |
837 | return r; | |
20263082 | 838 | |
eb968396 | 839 | r = sd_device_monitor_start(m->device_seat_monitor, manager_dispatch_seat_udev, m, "logind-seat-monitor"); |
cc377381 LP |
840 | if (r < 0) |
841 | return r; | |
069cfc85 | 842 | |
eb968396 YW |
843 | r = sd_device_monitor_new(&m->device_monitor); |
844 | if (r < 0) | |
845 | return r; | |
718d006a | 846 | |
eb968396 | 847 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "input", NULL); |
718d006a DH |
848 | if (r < 0) |
849 | return r; | |
850 | ||
eb968396 | 851 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "graphics", NULL); |
718d006a DH |
852 | if (r < 0) |
853 | return r; | |
854 | ||
eb968396 | 855 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_monitor, "drm", NULL); |
718d006a DH |
856 | if (r < 0) |
857 | return r; | |
858 | ||
eb968396 | 859 | r = sd_device_monitor_attach_event(m->device_monitor, m->event, 0); |
718d006a DH |
860 | if (r < 0) |
861 | return r; | |
862 | ||
eb968396 | 863 | r = sd_device_monitor_start(m->device_monitor, manager_dispatch_device_udev, m, "logind-device-monitor"); |
cc377381 LP |
864 | if (r < 0) |
865 | return r; | |
718d006a | 866 | |
6de0e0e5 | 867 | /* Don't watch keys if nobody cares */ |
e25937a3 | 868 | if (!manager_all_buttons_ignored(m)) { |
eb968396 YW |
869 | r = sd_device_monitor_new(&m->device_button_monitor); |
870 | if (r < 0) | |
871 | return r; | |
069cfc85 | 872 | |
eb968396 | 873 | r = sd_device_monitor_filter_add_match_tag(m->device_button_monitor, "power-switch"); |
6de0e0e5 LP |
874 | if (r < 0) |
875 | return r; | |
069cfc85 | 876 | |
eb968396 | 877 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_button_monitor, "input", NULL); |
6de0e0e5 LP |
878 | if (r < 0) |
879 | return r; | |
069cfc85 | 880 | |
eb968396 | 881 | r = sd_device_monitor_attach_event(m->device_button_monitor, m->event, 0); |
6de0e0e5 LP |
882 | if (r < 0) |
883 | return r; | |
069cfc85 | 884 | |
eb968396 | 885 | r = sd_device_monitor_start(m->device_button_monitor, manager_dispatch_button_udev, m, "logind-button-monitor"); |
cc377381 LP |
886 | if (r < 0) |
887 | return r; | |
6de0e0e5 | 888 | } |
069cfc85 | 889 | |
976c088a | 890 | /* Don't bother watching VCSA devices, if nobody cares */ |
6de0e0e5 | 891 | if (m->n_autovts > 0 && m->console_active_fd >= 0) { |
30ed21ce | 892 | |
eb968396 YW |
893 | r = sd_device_monitor_new(&m->device_vcsa_monitor); |
894 | if (r < 0) | |
895 | return r; | |
30ed21ce | 896 | |
eb968396 | 897 | r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_vcsa_monitor, "vc", NULL); |
6de0e0e5 LP |
898 | if (r < 0) |
899 | return r; | |
30ed21ce | 900 | |
eb968396 | 901 | r = sd_device_monitor_attach_event(m->device_vcsa_monitor, m->event, 0); |
6de0e0e5 LP |
902 | if (r < 0) |
903 | return r; | |
30ed21ce | 904 | |
eb968396 | 905 | r = sd_device_monitor_start(m->device_vcsa_monitor, manager_dispatch_vcsa_udev, m, "logind-vcsa-monitor"); |
cc377381 LP |
906 | if (r < 0) |
907 | return r; | |
6de0e0e5 | 908 | } |
20263082 LP |
909 | |
910 | return 0; | |
911 | } | |
912 | ||
905f0a39 | 913 | static void manager_gc(Manager *m, bool drop_not_started) { |
14c3baca LP |
914 | Seat *seat; |
915 | Session *session; | |
916 | User *user; | |
917 | ||
918 | assert(m); | |
919 | ||
920 | while ((seat = m->seat_gc_queue)) { | |
71fda00f | 921 | LIST_REMOVE(gc_queue, m->seat_gc_queue, seat); |
14c3baca LP |
922 | seat->in_gc_queue = false; |
923 | ||
5c093a23 | 924 | if (seat_may_gc(seat, drop_not_started)) { |
9bb69af4 | 925 | seat_stop(seat, false); |
14c3baca LP |
926 | seat_free(seat); |
927 | } | |
928 | } | |
929 | ||
930 | while ((session = m->session_gc_queue)) { | |
71fda00f | 931 | LIST_REMOVE(gc_queue, m->session_gc_queue, session); |
14c3baca LP |
932 | session->in_gc_queue = false; |
933 | ||
5f41d1f1 | 934 | /* First, if we are not closing yet, initiate stopping */ |
5c093a23 | 935 | if (session_may_gc(session, drop_not_started) && |
5f41d1f1 | 936 | session_get_state(session) != SESSION_CLOSING) |
75bbdf47 | 937 | (void) session_stop(session, false); |
5f41d1f1 | 938 | |
491ac9f2 LP |
939 | /* Normally, this should make the session referenced |
940 | * again, if it doesn't then let's get rid of it | |
5f41d1f1 | 941 | * immediately */ |
5c093a23 | 942 | if (session_may_gc(session, drop_not_started)) { |
75bbdf47 | 943 | (void) session_finalize(session); |
14c3baca LP |
944 | session_free(session); |
945 | } | |
946 | } | |
947 | ||
948 | while ((user = m->user_gc_queue)) { | |
71fda00f | 949 | LIST_REMOVE(gc_queue, m->user_gc_queue, user); |
14c3baca LP |
950 | user->in_gc_queue = false; |
951 | ||
b58b227a | 952 | /* First step: queue stop jobs */ |
5c093a23 | 953 | if (user_may_gc(user, drop_not_started)) |
75bbdf47 | 954 | (void) user_stop(user, false); |
5f41d1f1 | 955 | |
b58b227a | 956 | /* Second step: finalize user */ |
5c093a23 | 957 | if (user_may_gc(user, drop_not_started)) { |
75bbdf47 | 958 | (void) user_finalize(user); |
14c3baca LP |
959 | user_free(user); |
960 | } | |
961 | } | |
962 | } | |
963 | ||
cc377381 LP |
964 | static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *userdata) { |
965 | Manager *m = userdata; | |
23406ce5 | 966 | struct dual_timestamp since; |
cc377381 | 967 | usec_t n, elapse; |
23406ce5 | 968 | int r; |
23406ce5 LP |
969 | |
970 | assert(m); | |
971 | ||
972 | if (m->idle_action == HANDLE_IGNORE || | |
cc377381 LP |
973 | m->idle_action_usec <= 0) |
974 | return 0; | |
23406ce5 | 975 | |
23406ce5 LP |
976 | n = now(CLOCK_MONOTONIC); |
977 | ||
978 | r = manager_get_idle_hint(m, &since); | |
979 | if (r <= 0) | |
39ccc87c | 980 | /* Not idle. Let's check if after a timeout it might be idle then. */ |
cc377381 | 981 | elapse = n + m->idle_action_usec; |
23406ce5 LP |
982 | else { |
983 | /* Idle! Let's see if it's time to do something, or if | |
984 | * we shall sleep for longer. */ | |
985 | ||
986 | if (n >= since.monotonic + m->idle_action_usec && | |
987 | (m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) { | |
988 | log_info("System idle. Taking action."); | |
989 | ||
990 | manager_handle_action(m, 0, m->idle_action, false, false); | |
991 | m->idle_action_not_before_usec = n; | |
992 | } | |
993 | ||
cc377381 | 994 | elapse = MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec; |
23406ce5 LP |
995 | } |
996 | ||
cc377381 | 997 | if (!m->idle_action_event_source) { |
23406ce5 | 998 | |
6a0f1f6d LP |
999 | r = sd_event_add_time( |
1000 | m->event, | |
1001 | &m->idle_action_event_source, | |
1002 | CLOCK_MONOTONIC, | |
1003 | elapse, USEC_PER_SEC*30, | |
1004 | manager_dispatch_idle_action, m); | |
f647962d MS |
1005 | if (r < 0) |
1006 | return log_error_errno(r, "Failed to add idle event source: %m"); | |
23406ce5 | 1007 | |
718db961 | 1008 | r = sd_event_source_set_priority(m->idle_action_event_source, SD_EVENT_PRIORITY_IDLE+10); |
f647962d MS |
1009 | if (r < 0) |
1010 | return log_error_errno(r, "Failed to set idle event source priority: %m"); | |
cc377381 LP |
1011 | } else { |
1012 | r = sd_event_source_set_time(m->idle_action_event_source, elapse); | |
f647962d MS |
1013 | if (r < 0) |
1014 | return log_error_errno(r, "Failed to set idle event timer: %m"); | |
23406ce5 | 1015 | |
cc377381 | 1016 | r = sd_event_source_set_enabled(m->idle_action_event_source, SD_EVENT_ONESHOT); |
f647962d MS |
1017 | if (r < 0) |
1018 | return log_error_errno(r, "Failed to enable idle event timer: %m"); | |
23406ce5 LP |
1019 | } |
1020 | ||
1021 | return 0; | |
23406ce5 | 1022 | } |
cc377381 | 1023 | |
a2ed7077 ZJS |
1024 | static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) { |
1025 | Manager *m = userdata; | |
1026 | int r; | |
1027 | ||
1028 | manager_reset_config(m); | |
1029 | r = manager_parse_config_file(m); | |
1030 | if (r < 0) | |
1031 | log_warning_errno(r, "Failed to parse config file, using defaults: %m"); | |
1032 | else | |
1033 | log_info("Config file reloaded."); | |
1034 | ||
1035 | return 0; | |
1036 | } | |
1037 | ||
905f0a39 | 1038 | static int manager_startup(Manager *m) { |
20263082 | 1039 | int r; |
14c3baca LP |
1040 | Seat *seat; |
1041 | Session *session; | |
1042 | User *user; | |
ed4ba7e4 | 1043 | Button *button; |
f8e2fb7b | 1044 | Inhibitor *inhibitor; |
14c3baca | 1045 | Iterator i; |
20263082 LP |
1046 | |
1047 | assert(m); | |
20263082 | 1048 | |
a2ed7077 ZJS |
1049 | r = sd_event_add_signal(m->event, NULL, SIGHUP, manager_dispatch_reload_signal, m); |
1050 | if (r < 0) | |
1051 | return log_error_errno(r, "Failed to register SIGHUP handler: %m"); | |
1052 | ||
3d0ef5c7 LP |
1053 | /* Connect to utmp */ |
1054 | manager_connect_utmp(m); | |
1055 | ||
976c088a LP |
1056 | /* Connect to console */ |
1057 | r = manager_connect_console(m); | |
20263082 LP |
1058 | if (r < 0) |
1059 | return r; | |
1060 | ||
976c088a LP |
1061 | /* Connect to udev */ |
1062 | r = manager_connect_udev(m); | |
f647962d MS |
1063 | if (r < 0) |
1064 | return log_error_errno(r, "Failed to create udev watchers: %m"); | |
20263082 LP |
1065 | |
1066 | /* Connect to the bus */ | |
1067 | r = manager_connect_bus(m); | |
1068 | if (r < 0) | |
1069 | return r; | |
1070 | ||
14c3baca | 1071 | /* Instantiate magic seat 0 */ |
92432fcc | 1072 | r = manager_add_seat(m, "seat0", &m->seat0); |
f647962d MS |
1073 | if (r < 0) |
1074 | return log_error_errno(r, "Failed to add seat0: %m"); | |
14c3baca | 1075 | |
9d10cbee | 1076 | r = manager_set_lid_switch_ignore(m, 0 + m->holdoff_timeout_usec); |
f9cd6be1 | 1077 | if (r < 0) |
da927ba9 | 1078 | log_warning_errno(r, "Failed to set up lid switch ignore event source: %m"); |
f9cd6be1 | 1079 | |
20263082 | 1080 | /* Deserialize state */ |
042f5988 ZJS |
1081 | r = manager_enumerate_devices(m); |
1082 | if (r < 0) | |
da927ba9 | 1083 | log_warning_errno(r, "Device enumeration failed: %m"); |
042f5988 ZJS |
1084 | |
1085 | r = manager_enumerate_seats(m); | |
1086 | if (r < 0) | |
da927ba9 | 1087 | log_warning_errno(r, "Seat enumeration failed: %m"); |
042f5988 ZJS |
1088 | |
1089 | r = manager_enumerate_users(m); | |
1090 | if (r < 0) | |
da927ba9 | 1091 | log_warning_errno(r, "User enumeration failed: %m"); |
042f5988 ZJS |
1092 | |
1093 | r = manager_enumerate_sessions(m); | |
1094 | if (r < 0) | |
da927ba9 | 1095 | log_warning_errno(r, "Session enumeration failed: %m"); |
042f5988 ZJS |
1096 | |
1097 | r = manager_enumerate_inhibitors(m); | |
1098 | if (r < 0) | |
da927ba9 | 1099 | log_warning_errno(r, "Inhibitor enumeration failed: %m"); |
042f5988 ZJS |
1100 | |
1101 | r = manager_enumerate_buttons(m); | |
1102 | if (r < 0) | |
da927ba9 | 1103 | log_warning_errno(r, "Button enumeration failed: %m"); |
20263082 | 1104 | |
4a4b033f LP |
1105 | /* Remove stale objects before we start them */ |
1106 | manager_gc(m, false); | |
1107 | ||
98a77df5 LP |
1108 | /* Reserve the special reserved VT */ |
1109 | manager_reserve_vt(m); | |
1110 | ||
3d0ef5c7 LP |
1111 | /* Read in utmp if it exists */ |
1112 | manager_read_utmp(m); | |
1113 | ||
14c3baca LP |
1114 | /* And start everything */ |
1115 | HASHMAP_FOREACH(seat, m->seats, i) | |
75bbdf47 | 1116 | (void) seat_start(seat); |
14c3baca LP |
1117 | |
1118 | HASHMAP_FOREACH(user, m->users, i) | |
75bbdf47 | 1119 | (void) user_start(user); |
14c3baca LP |
1120 | |
1121 | HASHMAP_FOREACH(session, m->sessions, i) | |
25a1ab4e | 1122 | (void) session_start(session, NULL, NULL); |
20263082 | 1123 | |
f8e2fb7b LP |
1124 | HASHMAP_FOREACH(inhibitor, m->inhibitors, i) |
1125 | inhibitor_start(inhibitor); | |
1126 | ||
ed4ba7e4 | 1127 | HASHMAP_FOREACH(button, m->buttons, i) |
2d62c530 | 1128 | button_check_switches(button); |
ed4ba7e4 | 1129 | |
cc377381 | 1130 | manager_dispatch_idle_action(NULL, 0, m); |
23406ce5 | 1131 | |
20263082 LP |
1132 | return 0; |
1133 | } | |
1134 | ||
905f0a39 | 1135 | static int manager_run(Manager *m) { |
cc377381 LP |
1136 | int r; |
1137 | ||
20263082 LP |
1138 | assert(m); |
1139 | ||
1140 | for (;;) { | |
cc377381 LP |
1141 | r = sd_event_get_state(m->event); |
1142 | if (r < 0) | |
1143 | return r; | |
1144 | if (r == SD_EVENT_FINISHED) | |
1145 | return 0; | |
20263082 | 1146 | |
4a4b033f | 1147 | manager_gc(m, true); |
14c3baca | 1148 | |
418b22b8 DM |
1149 | r = manager_dispatch_delayed(m, false); |
1150 | if (r < 0) | |
1151 | return r; | |
1152 | if (r > 0) | |
1153 | continue; | |
1154 | ||
c0f32805 | 1155 | r = sd_event_run(m->event, (uint64_t) -1); |
cc377381 LP |
1156 | if (r < 0) |
1157 | return r; | |
20263082 | 1158 | } |
20263082 LP |
1159 | } |
1160 | ||
1161 | int main(int argc, char *argv[]) { | |
23462168 | 1162 | _cleanup_(manager_unrefp) Manager *m = NULL; |
5eda94dd | 1163 | int r; |
20263082 LP |
1164 | |
1165 | log_set_target(LOG_TARGET_AUTO); | |
3eff4208 | 1166 | log_set_facility(LOG_AUTH); |
20263082 LP |
1167 | log_parse_environment(); |
1168 | log_open(); | |
1169 | ||
4c12626c LP |
1170 | umask(0022); |
1171 | ||
20263082 LP |
1172 | if (argc != 1) { |
1173 | log_error("This program takes no arguments."); | |
1174 | r = -EINVAL; | |
1175 | goto finish; | |
1176 | } | |
1177 | ||
c3dacc8b | 1178 | r = mac_selinux_init(); |
4b51966c NI |
1179 | if (r < 0) { |
1180 | log_error_errno(r, "Could not initialize labelling: %m"); | |
1181 | goto finish; | |
1182 | } | |
1183 | ||
90b8a009 LP |
1184 | /* Always create the directories people can create inotify watches in. Note that some applications might check |
1185 | * for the existence of /run/systemd/seats/ to determine whether logind is available, so please always make | |
1186 | * sure these directories are created early on and unconditionally. */ | |
1187 | (void) mkdir_label("/run/systemd/seats", 0755); | |
1188 | (void) mkdir_label("/run/systemd/users", 0755); | |
1189 | (void) mkdir_label("/run/systemd/sessions", 0755); | |
bb27ff66 | 1190 | |
fcfa765d LP |
1191 | assert_se(sigprocmask_many(SIG_BLOCK, NULL, SIGHUP, SIGTERM, SIGINT, -1) >= 0); |
1192 | ||
23462168 LP |
1193 | r = manager_new(&m); |
1194 | if (r < 0) { | |
1195 | log_error_errno(r, "Failed to allocate manager object: %m"); | |
20263082 LP |
1196 | goto finish; |
1197 | } | |
1198 | ||
23462168 | 1199 | (void) manager_parse_config_file(m); |
193197e8 | 1200 | |
20263082 LP |
1201 | r = manager_startup(m); |
1202 | if (r < 0) { | |
da927ba9 | 1203 | log_error_errno(r, "Failed to fully start up daemon: %m"); |
20263082 LP |
1204 | goto finish; |
1205 | } | |
1206 | ||
df0ff127 | 1207 | log_debug("systemd-logind running as pid "PID_FMT, getpid_cached()); |
e6960940 | 1208 | |
23462168 LP |
1209 | (void) sd_notify(false, |
1210 | "READY=1\n" | |
1211 | "STATUS=Processing requests..."); | |
e6960940 | 1212 | |
20263082 LP |
1213 | r = manager_run(m); |
1214 | ||
df0ff127 | 1215 | log_debug("systemd-logind stopped as pid "PID_FMT, getpid_cached()); |
e6960940 | 1216 | |
23462168 LP |
1217 | (void) sd_notify(false, |
1218 | "STOPPING=1\n" | |
1219 | "STATUS=Shutting down..."); | |
20263082 | 1220 | |
23462168 | 1221 | finish: |
20263082 LP |
1222 | return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; |
1223 | } |