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