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