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