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