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