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