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