1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 #include "alloc-util.h"
12 #include "bus-error.h"
13 #include "bus-locator.h"
14 #include "bus-log-control-api.h"
15 #include "bus-polkit.h"
16 #include "cgroup-util.h"
18 #include "device-util.h"
19 #include "dirent-util.h"
21 #include "format-util.h"
23 #include "logind-dbus.h"
24 #include "logind-seat-dbus.h"
25 #include "logind-session-dbus.h"
26 #include "logind-user-dbus.h"
28 #include "main-func.h"
29 #include "parse-util.h"
30 #include "process-util.h"
31 #include "selinux-util.h"
32 #include "service-util.h"
33 #include "signal-util.h"
35 #include "terminal-util.h"
36 #include "udev-util.h"
37 #include "user-util.h"
39 static Manager
* manager_unref(Manager
*m
);
40 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
42 static int manager_new(Manager
**ret
) {
43 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
53 .console_active_fd
= -1,
55 .idle_action_not_before_usec
= now(CLOCK_MONOTONIC
),
58 m
->devices
= hashmap_new(&string_hash_ops
);
59 m
->seats
= hashmap_new(&string_hash_ops
);
60 m
->sessions
= hashmap_new(&string_hash_ops
);
61 m
->sessions_by_leader
= hashmap_new(NULL
);
62 m
->users
= hashmap_new(NULL
);
63 m
->inhibitors
= hashmap_new(&string_hash_ops
);
64 m
->buttons
= hashmap_new(&string_hash_ops
);
66 m
->user_units
= hashmap_new(&string_hash_ops
);
67 m
->session_units
= hashmap_new(&string_hash_ops
);
69 if (!m
->devices
|| !m
->seats
|| !m
->sessions
|| !m
->sessions_by_leader
|| !m
->users
|| !m
->inhibitors
|| !m
->buttons
|| !m
->user_units
|| !m
->session_units
)
72 r
= sd_event_default(&m
->event
);
76 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
80 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
84 (void) sd_event_set_watchdog(m
->event
, true);
86 manager_reset_config(m
);
92 static Manager
* manager_unref(Manager
*m
) {
103 while ((session
= hashmap_first(m
->sessions
)))
104 session_free(session
);
106 while ((u
= hashmap_first(m
->users
)))
109 while ((d
= hashmap_first(m
->devices
)))
112 while ((s
= hashmap_first(m
->seats
)))
115 while ((i
= hashmap_first(m
->inhibitors
)))
118 while ((b
= hashmap_first(m
->buttons
)))
121 hashmap_free(m
->devices
);
122 hashmap_free(m
->seats
);
123 hashmap_free(m
->sessions
);
124 hashmap_free(m
->sessions_by_leader
);
125 hashmap_free(m
->users
);
126 hashmap_free(m
->inhibitors
);
127 hashmap_free(m
->buttons
);
128 hashmap_free(m
->brightness_writers
);
130 hashmap_free(m
->user_units
);
131 hashmap_free(m
->session_units
);
133 sd_event_source_unref(m
->idle_action_event_source
);
134 sd_event_source_unref(m
->inhibit_timeout_source
);
135 sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
136 sd_event_source_unref(m
->nologin_timeout_source
);
137 sd_event_source_unref(m
->wall_message_timeout_source
);
139 sd_event_source_unref(m
->console_active_event_source
);
140 sd_event_source_unref(m
->lid_switch_ignore_event_source
);
143 sd_event_source_unref(m
->utmp_event_source
);
146 safe_close(m
->console_active_fd
);
148 sd_device_monitor_unref(m
->device_seat_monitor
);
149 sd_device_monitor_unref(m
->device_monitor
);
150 sd_device_monitor_unref(m
->device_vcsa_monitor
);
151 sd_device_monitor_unref(m
->device_button_monitor
);
153 if (m
->unlink_nologin
)
154 (void) unlink_or_warn("/run/nologin");
156 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
158 sd_bus_flush_close_unref(m
->bus
);
159 sd_event_unref(m
->event
);
161 safe_close(m
->reserve_vt_fd
);
163 strv_free(m
->kill_only_users
);
164 strv_free(m
->kill_exclude_users
);
166 free(m
->scheduled_shutdown_type
);
167 free(m
->scheduled_shutdown_tty
);
168 free(m
->wall_message
);
171 strv_free(m
->efi_boot_loader_entries
);
172 free(m
->efi_loader_entry_one_shot
);
177 static int manager_enumerate_devices(Manager
*m
) {
178 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
184 /* Loads devices from udev and creates seats for them as
187 r
= sd_device_enumerator_new(&e
);
191 r
= sd_device_enumerator_add_match_tag(e
, "master-of-seat");
195 FOREACH_DEVICE(e
, d
) {
198 k
= manager_process_seat_device(m
, d
);
206 static int manager_enumerate_buttons(Manager
*m
) {
207 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
213 /* Loads buttons from udev */
215 if (manager_all_buttons_ignored(m
))
218 r
= sd_device_enumerator_new(&e
);
222 r
= sd_device_enumerator_add_match_subsystem(e
, "input", true);
226 r
= sd_device_enumerator_add_match_tag(e
, "power-switch");
230 FOREACH_DEVICE(e
, d
) {
233 k
= manager_process_button_device(m
, d
);
241 static int manager_enumerate_seats(Manager
*m
) {
242 _cleanup_closedir_
DIR *d
= NULL
;
248 /* This loads data about seats stored on disk, but does not
249 * actually create any seats. Removes data of seats that no
252 d
= opendir("/run/systemd/seats");
257 return log_error_errno(errno
, "Failed to open /run/systemd/seats: %m");
260 FOREACH_DIRENT(de
, d
, return -errno
) {
264 if (!dirent_is_file(de
))
267 s
= hashmap_get(m
->seats
, de
->d_name
);
269 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
270 log_warning("Failed to remove /run/systemd/seats/%s: %m",
283 static int manager_enumerate_linger_users(Manager
*m
) {
284 _cleanup_closedir_
DIR *d
= NULL
;
290 d
= opendir("/var/lib/systemd/linger");
295 return log_error_errno(errno
, "Failed to open /var/lib/systemd/linger/: %m");
298 FOREACH_DIRENT(de
, d
, return -errno
) {
301 dirent_ensure_type(d
, de
);
302 if (!dirent_is_file(de
))
305 k
= manager_add_user_by_name(m
, de
->d_name
, NULL
);
307 r
= log_warning_errno(k
, "Couldn't add lingering user %s, ignoring: %m", de
->d_name
);
313 static int manager_enumerate_users(Manager
*m
) {
314 _cleanup_closedir_
DIR *d
= NULL
;
320 /* Add lingering users */
321 r
= manager_enumerate_linger_users(m
);
323 /* Read in user data stored on disk */
324 d
= opendir("/run/systemd/users");
329 return log_error_errno(errno
, "Failed to open /run/systemd/users: %m");
332 FOREACH_DIRENT(de
, d
, return -errno
) {
336 if (!dirent_is_file(de
))
339 k
= parse_uid(de
->d_name
, &uid
);
341 r
= log_warning_errno(k
, "Failed to parse filename /run/systemd/users/%s as UID.", de
->d_name
);
345 k
= manager_add_user_by_uid(m
, uid
, &u
);
347 r
= log_warning_errno(k
, "Failed to add user by file name %s, ignoring: %m", de
->d_name
);
351 user_add_to_gc_queue(u
);
361 static int parse_fdname(const char *fdname
, char **session_id
, dev_t
*dev
) {
362 _cleanup_strv_free_
char **parts
= NULL
;
363 _cleanup_free_
char *id
= NULL
;
364 unsigned major
, minor
;
367 parts
= strv_split(fdname
, "-");
370 if (strv_length(parts
) != 5)
373 if (!streq(parts
[0], "session"))
376 id
= strdup(parts
[1]);
380 if (!streq(parts
[2], "device"))
383 r
= safe_atou(parts
[3], &major
);
386 r
= safe_atou(parts
[4], &minor
);
390 *dev
= makedev(major
, minor
);
391 *session_id
= TAKE_PTR(id
);
396 static int deliver_fd(Manager
*m
, const char *fdname
, int fd
) {
397 _cleanup_free_
char *id
= NULL
;
407 r
= parse_fdname(fdname
, &id
, &dev
);
409 return log_debug_errno(r
, "Failed to parse fd name %s: %m", fdname
);
411 s
= hashmap_get(m
->sessions
, id
);
413 /* If the session doesn't exist anymore, the associated session device attached to this fd
414 * doesn't either. Let's simply close this fd. */
415 return log_debug_errno(SYNTHETIC_ERRNO(ENXIO
), "Failed to attach fd for unknown session: %s", id
);
417 if (fstat(fd
, &st
) < 0)
418 /* The device is allowed to go away at a random point, in which case fstat() failing is
420 return log_debug_errno(errno
, "Failed to stat device fd for session %s: %m", id
);
422 if (!S_ISCHR(st
.st_mode
) || st
.st_rdev
!= dev
)
423 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
), "Device fd doesn't point to the expected character device node");
425 sd
= hashmap_get(s
->devices
, &dev
);
427 /* Weird, we got an fd for a session device which wasn't recorded in the session state
429 return log_warning_errno(SYNTHETIC_ERRNO(ENODEV
), "Got fd for missing session device [%u:%u] in session %s",
430 major(dev
), minor(dev
), s
->id
);
432 log_debug("Attaching fd to session device [%u:%u] for session %s",
433 major(dev
), minor(dev
), s
->id
);
435 session_device_attach_fd(sd
, fd
, s
->was_active
);
439 static int manager_attach_fds(Manager
*m
) {
440 _cleanup_strv_free_
char **fdnames
= NULL
;
443 /* Upon restart, PID1 will send us back all fds of session devices that we previously opened. Each
444 * file descriptor is associated with a given session. The session ids are passed through FDNAMES. */
446 n
= sd_listen_fds_with_names(true, &fdnames
);
448 return log_warning_errno(n
, "Failed to acquire passed fd list: %m");
452 for (int i
= 0; i
< n
; i
++) {
453 int fd
= SD_LISTEN_FDS_START
+ i
;
455 if (deliver_fd(m
, fdnames
[i
], fd
) >= 0)
458 /* Hmm, we couldn't deliver the fd to any session device object? If so, let's close the fd */
461 /* Remove from fdstore as well */
462 (void) sd_notifyf(false,
464 "FDNAME=%s", fdnames
[i
]);
470 static int manager_enumerate_sessions(Manager
*m
) {
471 _cleanup_closedir_
DIR *d
= NULL
;
477 /* Read in session data stored on disk */
478 d
= opendir("/run/systemd/sessions");
483 return log_error_errno(errno
, "Failed to open /run/systemd/sessions: %m");
486 FOREACH_DIRENT(de
, d
, return -errno
) {
489 if (!dirent_is_file(de
))
492 k
= manager_add_session(m
, de
->d_name
, &s
);
494 r
= log_warning_errno(k
, "Failed to add session by file name %s, ignoring: %m", de
->d_name
);
498 session_add_to_gc_queue(s
);
505 /* We might be restarted and PID1 could have sent us back the session device fds we previously
507 (void) manager_attach_fds(m
);
512 static int manager_enumerate_inhibitors(Manager
*m
) {
513 _cleanup_closedir_
DIR *d
= NULL
;
519 d
= opendir("/run/systemd/inhibit");
524 return log_error_errno(errno
, "Failed to open /run/systemd/inhibit: %m");
527 FOREACH_DIRENT(de
, d
, return -errno
) {
531 if (!dirent_is_file(de
))
534 k
= manager_add_inhibitor(m
, de
->d_name
, &i
);
536 r
= log_warning_errno(k
, "Couldn't add inhibitor %s, ignoring: %m", de
->d_name
);
540 k
= inhibitor_load(i
);
548 static int manager_dispatch_seat_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
549 Manager
*m
= userdata
;
554 manager_process_seat_device(m
, device
);
558 static int manager_dispatch_device_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
559 Manager
*m
= userdata
;
564 manager_process_seat_device(m
, device
);
568 static int manager_dispatch_vcsa_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
569 Manager
*m
= userdata
;
575 /* Whenever a VCSA device is removed try to reallocate our
576 * VTs, to make sure our auto VTs never go away. */
578 if (sd_device_get_sysname(device
, &name
) >= 0 &&
579 startswith(name
, "vcsa") &&
580 device_for_action(device
, DEVICE_ACTION_REMOVE
))
581 seat_preallocate_vts(m
->seat0
);
586 static int manager_dispatch_button_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
587 Manager
*m
= userdata
;
592 manager_process_button_device(m
, device
);
596 static int manager_dispatch_console(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
597 Manager
*m
= userdata
;
601 assert(m
->console_active_fd
== fd
);
603 seat_read_active_vt(m
->seat0
);
607 static int manager_reserve_vt(Manager
*m
) {
608 _cleanup_free_
char *p
= NULL
;
612 if (m
->reserve_vt
<= 0)
615 if (asprintf(&p
, "/dev/tty%u", m
->reserve_vt
) < 0)
618 m
->reserve_vt_fd
= open(p
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
619 if (m
->reserve_vt_fd
< 0) {
621 /* Don't complain on VT-less systems */
623 log_warning_errno(errno
, "Failed to pin reserved VT: %m");
630 static int manager_connect_bus(Manager
*m
) {
636 r
= sd_bus_default_system(&m
->bus
);
638 return log_error_errno(r
, "Failed to connect to system bus: %m");
640 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
644 r
= bus_log_control_api_register(m
->bus
);
648 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "JobRemoved", match_job_removed
, NULL
, m
);
650 return log_error_errno(r
, "Failed to request match for JobRemoved: %m");
652 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "UnitRemoved", match_unit_removed
, NULL
, m
);
654 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
656 r
= sd_bus_match_signal_async(
659 "org.freedesktop.systemd1",
661 "org.freedesktop.DBus.Properties",
663 match_properties_changed
, NULL
, m
);
665 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
667 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "Reloading", match_reloading
, NULL
, m
);
669 return log_error_errno(r
, "Failed to request match for Reloading: %m");
671 r
= bus_call_method_async(m
->bus
, NULL
, bus_systemd_mgr
, "Subscribe", NULL
, NULL
, NULL
);
673 return log_error_errno(r
, "Failed to enable subscription: %m");
675 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.login1", 0, NULL
, NULL
);
677 return log_error_errno(r
, "Failed to request name: %m");
679 r
= sd_bus_attach_event(m
->bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
681 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
686 static int manager_vt_switch(sd_event_source
*src
, const struct signalfd_siginfo
*si
, void *data
) {
688 Session
*active
, *iter
;
691 * We got a VT-switch signal and we have to acknowledge it immediately.
692 * Preferably, we'd just use m->seat0->active->vtfd, but unfortunately,
693 * old user-space might run multiple sessions on a single VT, *sigh*.
694 * Therefore, we have to iterate all sessions and find one with a vtfd
695 * on the requested VT.
696 * As only VTs with active controllers have VT_PROCESS set, our current
697 * notion of the active VT might be wrong (for instance if the switch
698 * happens while we setup VT_PROCESS). Therefore, read the current VT
699 * first and then use s->active->vtnr as reference. Note that this is
700 * not racy, as no further VT-switch can happen as long as we're in
701 * synchronous VT_PROCESS mode.
705 seat_read_active_vt(m
->seat0
);
707 active
= m
->seat0
->active
;
708 if (!active
|| active
->vtnr
< 1) {
709 _cleanup_close_
int fd
= -1;
712 /* We are requested to acknowledge the VT-switch signal by the kernel but
713 * there's no registered sessions for the current VT. Normally this
714 * shouldn't happen but something wrong might have happened when we tried
715 * to release the VT. Better be safe than sorry, and try to release the VT
716 * one more time otherwise the user will be locked with the current VT. */
718 log_warning("Received VT_PROCESS signal without a registered session, restoring VT.");
720 /* At this point we only have the kernel mapping for referring to the
722 fd
= open_terminal("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
724 log_warning_errno(fd
, "Failed to open, ignoring: %m");
728 r
= vt_release(fd
, true);
730 log_warning_errno(r
, "Failed to release VT, ignoring: %m");
735 if (active
->vtfd
>= 0) {
736 session_leave_vt(active
);
738 LIST_FOREACH(sessions_by_seat
, iter
, m
->seat0
->sessions
) {
739 if (iter
->vtnr
== active
->vtnr
&& iter
->vtfd
>= 0) {
740 session_leave_vt(iter
);
749 static int manager_connect_console(Manager
*m
) {
753 assert(m
->console_active_fd
< 0);
755 /* On certain systems (such as S390, Xen, and containers) /dev/tty0 does not exist (as there is no VC), so
756 * don't fail if we can't open it. */
758 if (access("/dev/tty0", F_OK
) < 0)
761 m
->console_active_fd
= open("/sys/class/tty/tty0/active", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
762 if (m
->console_active_fd
< 0) {
764 /* On some systems /dev/tty0 may exist even though /sys/class/tty/tty0 does not. These are broken, but
765 * common. Let's complain but continue anyway. */
766 if (errno
== ENOENT
) {
767 log_warning_errno(errno
, "System has /dev/tty0 but not /sys/class/tty/tty0/active which is broken, ignoring: %m");
771 return log_error_errno(errno
, "Failed to open /sys/class/tty/tty0/active: %m");
774 r
= sd_event_add_io(m
->event
, &m
->console_active_event_source
, m
->console_active_fd
, 0, manager_dispatch_console
, m
);
776 return log_error_errno(r
, "Failed to watch foreground console: %m");
779 * SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used
780 * as VT-acquire signal. We ignore any acquire-events (yes, we still
781 * have to provide a valid signal-number for it!) and acknowledge all
782 * release events immediately.
785 if (SIGRTMIN
+ 1 > SIGRTMAX
)
786 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
787 "Not enough real-time signals available: %u-%u",
790 assert_se(ignore_signals(SIGRTMIN
+ 1, -1) >= 0);
791 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGRTMIN
, -1) >= 0);
793 r
= sd_event_add_signal(m
->event
, NULL
, SIGRTMIN
, manager_vt_switch
, m
);
795 return log_error_errno(r
, "Failed to subscribe to signal: %m");
800 static int manager_connect_udev(Manager
*m
) {
804 assert(!m
->device_seat_monitor
);
805 assert(!m
->device_monitor
);
806 assert(!m
->device_vcsa_monitor
);
807 assert(!m
->device_button_monitor
);
809 r
= sd_device_monitor_new(&m
->device_seat_monitor
);
813 r
= sd_device_monitor_filter_add_match_tag(m
->device_seat_monitor
, "master-of-seat");
817 r
= sd_device_monitor_attach_event(m
->device_seat_monitor
, m
->event
);
821 r
= sd_device_monitor_start(m
->device_seat_monitor
, manager_dispatch_seat_udev
, m
);
825 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_seat_monitor
), "logind-seat-monitor");
827 r
= sd_device_monitor_new(&m
->device_monitor
);
831 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "input", NULL
);
835 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "graphics", NULL
);
839 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "drm", NULL
);
843 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
847 r
= sd_device_monitor_start(m
->device_monitor
, manager_dispatch_device_udev
, m
);
851 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_monitor
), "logind-device-monitor");
853 /* Don't watch keys if nobody cares */
854 if (!manager_all_buttons_ignored(m
)) {
855 r
= sd_device_monitor_new(&m
->device_button_monitor
);
859 r
= sd_device_monitor_filter_add_match_tag(m
->device_button_monitor
, "power-switch");
863 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_button_monitor
, "input", NULL
);
867 r
= sd_device_monitor_attach_event(m
->device_button_monitor
, m
->event
);
871 r
= sd_device_monitor_start(m
->device_button_monitor
, manager_dispatch_button_udev
, m
);
875 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_button_monitor
), "logind-button-monitor");
878 /* Don't bother watching VCSA devices, if nobody cares */
879 if (m
->n_autovts
> 0 && m
->console_active_fd
>= 0) {
881 r
= sd_device_monitor_new(&m
->device_vcsa_monitor
);
885 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_vcsa_monitor
, "vc", NULL
);
889 r
= sd_device_monitor_attach_event(m
->device_vcsa_monitor
, m
->event
);
893 r
= sd_device_monitor_start(m
->device_vcsa_monitor
, manager_dispatch_vcsa_udev
, m
);
897 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_vcsa_monitor
), "logind-vcsa-monitor");
903 static void manager_gc(Manager
*m
, bool drop_not_started
) {
910 while ((seat
= m
->seat_gc_queue
)) {
911 LIST_REMOVE(gc_queue
, m
->seat_gc_queue
, seat
);
912 seat
->in_gc_queue
= false;
914 if (seat_may_gc(seat
, drop_not_started
)) {
915 seat_stop(seat
, false);
920 while ((session
= m
->session_gc_queue
)) {
921 LIST_REMOVE(gc_queue
, m
->session_gc_queue
, session
);
922 session
->in_gc_queue
= false;
924 /* First, if we are not closing yet, initiate stopping */
925 if (session_may_gc(session
, drop_not_started
) &&
926 session_get_state(session
) != SESSION_CLOSING
)
927 (void) session_stop(session
, false);
929 /* Normally, this should make the session referenced
930 * again, if it doesn't then let's get rid of it
932 if (session_may_gc(session
, drop_not_started
)) {
933 (void) session_finalize(session
);
934 session_free(session
);
938 while ((user
= m
->user_gc_queue
)) {
939 LIST_REMOVE(gc_queue
, m
->user_gc_queue
, user
);
940 user
->in_gc_queue
= false;
942 /* First step: queue stop jobs */
943 if (user_may_gc(user
, drop_not_started
))
944 (void) user_stop(user
, false);
946 /* Second step: finalize user */
947 if (user_may_gc(user
, drop_not_started
)) {
948 (void) user_finalize(user
);
954 static int manager_dispatch_idle_action(sd_event_source
*s
, uint64_t t
, void *userdata
) {
955 Manager
*m
= userdata
;
956 struct dual_timestamp since
;
962 if (m
->idle_action
== HANDLE_IGNORE
||
963 m
->idle_action_usec
<= 0)
966 n
= now(CLOCK_MONOTONIC
);
968 r
= manager_get_idle_hint(m
, &since
);
970 /* Not idle. Let's check if after a timeout it might be idle then. */
971 elapse
= n
+ m
->idle_action_usec
;
973 /* Idle! Let's see if it's time to do something, or if
974 * we shall sleep for longer. */
976 if (n
>= since
.monotonic
+ m
->idle_action_usec
&&
977 (m
->idle_action_not_before_usec
<= 0 || n
>= m
->idle_action_not_before_usec
+ m
->idle_action_usec
)) {
978 log_info("System idle. Doing %s operation.", handle_action_to_string(m
->idle_action
));
980 manager_handle_action(m
, 0, m
->idle_action
, false, false);
981 m
->idle_action_not_before_usec
= n
;
984 elapse
= MAX(since
.monotonic
, m
->idle_action_not_before_usec
) + m
->idle_action_usec
;
987 if (!m
->idle_action_event_source
) {
989 r
= sd_event_add_time(
991 &m
->idle_action_event_source
,
993 elapse
, USEC_PER_SEC
*30,
994 manager_dispatch_idle_action
, m
);
996 return log_error_errno(r
, "Failed to add idle event source: %m");
998 r
= sd_event_source_set_priority(m
->idle_action_event_source
, SD_EVENT_PRIORITY_IDLE
+10);
1000 return log_error_errno(r
, "Failed to set idle event source priority: %m");
1002 r
= sd_event_source_set_time(m
->idle_action_event_source
, elapse
);
1004 return log_error_errno(r
, "Failed to set idle event timer: %m");
1006 r
= sd_event_source_set_enabled(m
->idle_action_event_source
, SD_EVENT_ONESHOT
);
1008 return log_error_errno(r
, "Failed to enable idle event timer: %m");
1014 static int manager_dispatch_reload_signal(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1015 Manager
*m
= userdata
;
1018 manager_reset_config(m
);
1019 r
= manager_parse_config_file(m
);
1021 log_warning_errno(r
, "Failed to parse config file, using defaults: %m");
1023 log_info("Config file reloaded.");
1028 static int manager_startup(Manager
*m
) {
1034 Inhibitor
*inhibitor
;
1039 r
= sd_event_add_signal(m
->event
, NULL
, SIGHUP
, manager_dispatch_reload_signal
, m
);
1041 return log_error_errno(r
, "Failed to register SIGHUP handler: %m");
1043 /* Connect to utmp */
1044 manager_connect_utmp(m
);
1046 /* Connect to console */
1047 r
= manager_connect_console(m
);
1051 /* Connect to udev */
1052 r
= manager_connect_udev(m
);
1054 return log_error_errno(r
, "Failed to create udev watchers: %m");
1056 /* Connect to the bus */
1057 r
= manager_connect_bus(m
);
1061 /* Instantiate magic seat 0 */
1062 r
= manager_add_seat(m
, "seat0", &m
->seat0
);
1064 return log_error_errno(r
, "Failed to add seat0: %m");
1066 r
= manager_set_lid_switch_ignore(m
, 0 + m
->holdoff_timeout_usec
);
1068 log_warning_errno(r
, "Failed to set up lid switch ignore event source: %m");
1070 /* Deserialize state */
1071 r
= manager_enumerate_devices(m
);
1073 log_warning_errno(r
, "Device enumeration failed: %m");
1075 r
= manager_enumerate_seats(m
);
1077 log_warning_errno(r
, "Seat enumeration failed: %m");
1079 r
= manager_enumerate_users(m
);
1081 log_warning_errno(r
, "User enumeration failed: %m");
1083 r
= manager_enumerate_sessions(m
);
1085 log_warning_errno(r
, "Session enumeration failed: %m");
1087 r
= manager_enumerate_inhibitors(m
);
1089 log_warning_errno(r
, "Inhibitor enumeration failed: %m");
1091 r
= manager_enumerate_buttons(m
);
1093 log_warning_errno(r
, "Button enumeration failed: %m");
1095 /* Remove stale objects before we start them */
1096 manager_gc(m
, false);
1098 /* Reserve the special reserved VT */
1099 manager_reserve_vt(m
);
1101 /* Read in utmp if it exists */
1102 manager_read_utmp(m
);
1104 /* And start everything */
1105 HASHMAP_FOREACH(seat
, m
->seats
, i
)
1106 (void) seat_start(seat
);
1108 HASHMAP_FOREACH(user
, m
->users
, i
)
1109 (void) user_start(user
);
1111 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1112 (void) session_start(session
, NULL
, NULL
);
1114 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
1115 (void) inhibitor_start(inhibitor
);
1117 /* Let's see if the inhibitor is dead now, then remove it */
1118 if (inhibitor_is_orphan(inhibitor
)) {
1119 inhibitor_stop(inhibitor
);
1120 inhibitor_free(inhibitor
);
1124 HASHMAP_FOREACH(button
, m
->buttons
, i
)
1125 button_check_switches(button
);
1127 manager_dispatch_idle_action(NULL
, 0, m
);
1132 static int manager_run(Manager
*m
) {
1138 r
= sd_event_get_state(m
->event
);
1141 if (r
== SD_EVENT_FINISHED
)
1144 manager_gc(m
, true);
1146 r
= manager_dispatch_delayed(m
, false);
1152 r
= sd_event_run(m
->event
, (uint64_t) -1);
1158 static int run(int argc
, char *argv
[]) {
1159 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
1162 log_set_facility(LOG_AUTH
);
1163 log_setup_service();
1165 r
= service_parse_argv("systemd-logind.service",
1166 "Manager for user logins and devices and privileged operations.",
1167 BUS_IMPLEMENTATIONS(&manager_object
,
1168 &log_control_object
),
1175 r
= mac_selinux_init();
1179 /* Always create the directories people can create inotify watches in. Note that some applications might check
1180 * for the existence of /run/systemd/seats/ to determine whether logind is available, so please always make
1181 * sure these directories are created early on and unconditionally. */
1182 (void) mkdir_label("/run/systemd/seats", 0755);
1183 (void) mkdir_label("/run/systemd/users", 0755);
1184 (void) mkdir_label("/run/systemd/sessions", 0755);
1186 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGHUP
, SIGTERM
, SIGINT
, SIGCHLD
, -1) >= 0);
1188 r
= manager_new(&m
);
1190 return log_error_errno(r
, "Failed to allocate manager object: %m");
1192 (void) manager_parse_config_file(m
);
1194 r
= manager_startup(m
);
1196 return log_error_errno(r
, "Failed to fully start up daemon: %m");
1198 log_debug("systemd-logind running as pid "PID_FMT
, getpid_cached());
1199 (void) sd_notify(false,
1201 "STATUS=Processing requests...");
1205 log_debug("systemd-logind stopped as pid "PID_FMT
, getpid_cached());
1206 (void) sd_notify(false,
1208 "STATUS=Shutting down...");
1213 DEFINE_MAIN_FUNCTION(run
);