1 /* SPDX-License-Identifier: LGPL-2.1+ */
10 #include "alloc-util.h"
11 #include "bus-error.h"
12 #include "bus-log-control-api.h"
13 #include "bus-polkit.h"
15 #include "cgroup-util.h"
17 #include "device-util.h"
18 #include "dirent-util.h"
20 #include "format-util.h"
22 #include "logind-dbus.h"
23 #include "logind-seat-dbus.h"
24 #include "logind-session-dbus.h"
25 #include "logind-user-dbus.h"
27 #include "main-func.h"
28 #include "parse-util.h"
29 #include "process-util.h"
30 #include "selinux-util.h"
31 #include "service-util.h"
32 #include "signal-util.h"
34 #include "terminal-util.h"
35 #include "udev-util.h"
37 static Manager
* manager_unref(Manager
*m
);
38 DEFINE_TRIVIAL_CLEANUP_FUNC(Manager
*, manager_unref
);
40 static int manager_new(Manager
**ret
) {
41 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
51 .console_active_fd
= -1,
53 .idle_action_not_before_usec
= now(CLOCK_MONOTONIC
),
56 m
->devices
= hashmap_new(&string_hash_ops
);
57 m
->seats
= hashmap_new(&string_hash_ops
);
58 m
->sessions
= hashmap_new(&string_hash_ops
);
59 m
->sessions_by_leader
= hashmap_new(NULL
);
60 m
->users
= hashmap_new(NULL
);
61 m
->inhibitors
= hashmap_new(&string_hash_ops
);
62 m
->buttons
= hashmap_new(&string_hash_ops
);
64 m
->user_units
= hashmap_new(&string_hash_ops
);
65 m
->session_units
= hashmap_new(&string_hash_ops
);
67 if (!m
->devices
|| !m
->seats
|| !m
->sessions
|| !m
->sessions_by_leader
|| !m
->users
|| !m
->inhibitors
|| !m
->buttons
|| !m
->user_units
|| !m
->session_units
)
70 r
= sd_event_default(&m
->event
);
74 r
= sd_event_add_signal(m
->event
, NULL
, SIGINT
, NULL
, NULL
);
78 r
= sd_event_add_signal(m
->event
, NULL
, SIGTERM
, NULL
, NULL
);
82 (void) sd_event_set_watchdog(m
->event
, true);
84 manager_reset_config(m
);
90 static Manager
* manager_unref(Manager
*m
) {
101 while ((session
= hashmap_first(m
->sessions
)))
102 session_free(session
);
104 while ((u
= hashmap_first(m
->users
)))
107 while ((d
= hashmap_first(m
->devices
)))
110 while ((s
= hashmap_first(m
->seats
)))
113 while ((i
= hashmap_first(m
->inhibitors
)))
116 while ((b
= hashmap_first(m
->buttons
)))
119 hashmap_free(m
->devices
);
120 hashmap_free(m
->seats
);
121 hashmap_free(m
->sessions
);
122 hashmap_free(m
->sessions_by_leader
);
123 hashmap_free(m
->users
);
124 hashmap_free(m
->inhibitors
);
125 hashmap_free(m
->buttons
);
126 hashmap_free(m
->brightness_writers
);
128 hashmap_free(m
->user_units
);
129 hashmap_free(m
->session_units
);
131 sd_event_source_unref(m
->idle_action_event_source
);
132 sd_event_source_unref(m
->inhibit_timeout_source
);
133 sd_event_source_unref(m
->scheduled_shutdown_timeout_source
);
134 sd_event_source_unref(m
->nologin_timeout_source
);
135 sd_event_source_unref(m
->wall_message_timeout_source
);
137 sd_event_source_unref(m
->console_active_event_source
);
138 sd_event_source_unref(m
->lid_switch_ignore_event_source
);
141 sd_event_source_unref(m
->utmp_event_source
);
144 safe_close(m
->console_active_fd
);
146 sd_device_monitor_unref(m
->device_seat_monitor
);
147 sd_device_monitor_unref(m
->device_monitor
);
148 sd_device_monitor_unref(m
->device_vcsa_monitor
);
149 sd_device_monitor_unref(m
->device_button_monitor
);
151 if (m
->unlink_nologin
)
152 (void) unlink_or_warn("/run/nologin");
154 bus_verify_polkit_async_registry_free(m
->polkit_registry
);
156 sd_bus_flush_close_unref(m
->bus
);
157 sd_event_unref(m
->event
);
159 safe_close(m
->reserve_vt_fd
);
161 strv_free(m
->kill_only_users
);
162 strv_free(m
->kill_exclude_users
);
164 free(m
->scheduled_shutdown_type
);
165 free(m
->scheduled_shutdown_tty
);
166 free(m
->wall_message
);
172 static int manager_enumerate_devices(Manager
*m
) {
173 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
179 /* Loads devices from udev and creates seats for them as
182 r
= sd_device_enumerator_new(&e
);
186 r
= sd_device_enumerator_add_match_tag(e
, "master-of-seat");
190 FOREACH_DEVICE(e
, d
) {
193 k
= manager_process_seat_device(m
, d
);
201 static int manager_enumerate_buttons(Manager
*m
) {
202 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
208 /* Loads buttons from udev */
210 if (manager_all_buttons_ignored(m
))
213 r
= sd_device_enumerator_new(&e
);
217 r
= sd_device_enumerator_add_match_subsystem(e
, "input", true);
221 r
= sd_device_enumerator_add_match_tag(e
, "power-switch");
225 FOREACH_DEVICE(e
, d
) {
228 k
= manager_process_button_device(m
, d
);
236 static int manager_enumerate_seats(Manager
*m
) {
237 _cleanup_closedir_
DIR *d
= NULL
;
243 /* This loads data about seats stored on disk, but does not
244 * actually create any seats. Removes data of seats that no
247 d
= opendir("/run/systemd/seats");
252 return log_error_errno(errno
, "Failed to open /run/systemd/seats: %m");
255 FOREACH_DIRENT(de
, d
, return -errno
) {
259 if (!dirent_is_file(de
))
262 s
= hashmap_get(m
->seats
, de
->d_name
);
264 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
265 log_warning("Failed to remove /run/systemd/seats/%s: %m",
278 static int manager_enumerate_linger_users(Manager
*m
) {
279 _cleanup_closedir_
DIR *d
= NULL
;
285 d
= opendir("/var/lib/systemd/linger");
290 return log_error_errno(errno
, "Failed to open /var/lib/systemd/linger/: %m");
293 FOREACH_DIRENT(de
, d
, return -errno
) {
296 dirent_ensure_type(d
, de
);
297 if (!dirent_is_file(de
))
300 k
= manager_add_user_by_name(m
, de
->d_name
, NULL
);
302 r
= log_warning_errno(k
, "Couldn't add lingering user %s, ignoring: %m", de
->d_name
);
308 static int manager_enumerate_users(Manager
*m
) {
309 _cleanup_closedir_
DIR *d
= NULL
;
315 /* Add lingering users */
316 r
= manager_enumerate_linger_users(m
);
318 /* Read in user data stored on disk */
319 d
= opendir("/run/systemd/users");
324 return log_error_errno(errno
, "Failed to open /run/systemd/users: %m");
327 FOREACH_DIRENT(de
, d
, return -errno
) {
330 if (!dirent_is_file(de
))
333 k
= manager_add_user_by_name(m
, de
->d_name
, &u
);
335 r
= log_warning_errno(k
, "Failed to add user by file name %s, ignoring: %m", de
->d_name
);
339 user_add_to_gc_queue(u
);
349 static int parse_fdname(const char *fdname
, char **session_id
, dev_t
*dev
) {
350 _cleanup_strv_free_
char **parts
= NULL
;
351 _cleanup_free_
char *id
= NULL
;
352 unsigned major
, minor
;
355 parts
= strv_split(fdname
, "-");
358 if (strv_length(parts
) != 5)
361 if (!streq(parts
[0], "session"))
364 id
= strdup(parts
[1]);
368 if (!streq(parts
[2], "device"))
371 r
= safe_atou(parts
[3], &major
);
374 r
= safe_atou(parts
[4], &minor
);
378 *dev
= makedev(major
, minor
);
379 *session_id
= TAKE_PTR(id
);
384 static int deliver_fd(Manager
*m
, const char *fdname
, int fd
) {
385 _cleanup_free_
char *id
= NULL
;
395 r
= parse_fdname(fdname
, &id
, &dev
);
397 return log_debug_errno(r
, "Failed to parse fd name %s: %m", fdname
);
399 s
= hashmap_get(m
->sessions
, id
);
401 /* If the session doesn't exist anymore, the associated session device attached to this fd
402 * doesn't either. Let's simply close this fd. */
403 return log_debug_errno(SYNTHETIC_ERRNO(ENXIO
), "Failed to attach fd for unknown session: %s", id
);
405 if (fstat(fd
, &st
) < 0)
406 /* The device is allowed to go away at a random point, in which case fstat() failing is
408 return log_debug_errno(errno
, "Failed to stat device fd for session %s: %m", id
);
410 if (!S_ISCHR(st
.st_mode
) || st
.st_rdev
!= dev
)
411 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
), "Device fd doesn't point to the expected character device node");
413 sd
= hashmap_get(s
->devices
, &dev
);
415 /* Weird, we got an fd for a session device which wasn't recorded in the session state
417 return log_warning_errno(SYNTHETIC_ERRNO(ENODEV
), "Got fd for missing session device [%u:%u] in session %s",
418 major(dev
), minor(dev
), s
->id
);
420 log_debug("Attaching fd to session device [%u:%u] for session %s",
421 major(dev
), minor(dev
), s
->id
);
423 session_device_attach_fd(sd
, fd
, s
->was_active
);
427 static int manager_attach_fds(Manager
*m
) {
428 _cleanup_strv_free_
char **fdnames
= NULL
;
431 /* Upon restart, PID1 will send us back all fds of session devices that we previously opened. Each
432 * file descriptor is associated with a given session. The session ids are passed through FDNAMES. */
434 n
= sd_listen_fds_with_names(true, &fdnames
);
436 return log_warning_errno(n
, "Failed to acquire passed fd list: %m");
440 for (int i
= 0; i
< n
; i
++) {
441 int fd
= SD_LISTEN_FDS_START
+ i
;
443 if (deliver_fd(m
, fdnames
[i
], fd
) >= 0)
446 /* Hmm, we couldn't deliver the fd to any session device object? If so, let's close the fd */
449 /* Remove from fdstore as well */
450 (void) sd_notifyf(false,
452 "FDNAME=%s", fdnames
[i
]);
458 static int manager_enumerate_sessions(Manager
*m
) {
459 _cleanup_closedir_
DIR *d
= NULL
;
465 /* Read in session data stored on disk */
466 d
= opendir("/run/systemd/sessions");
471 return log_error_errno(errno
, "Failed to open /run/systemd/sessions: %m");
474 FOREACH_DIRENT(de
, d
, return -errno
) {
477 if (!dirent_is_file(de
))
480 k
= manager_add_session(m
, de
->d_name
, &s
);
482 r
= log_warning_errno(k
, "Failed to add session by file name %s, ignoring: %m", de
->d_name
);
486 session_add_to_gc_queue(s
);
493 /* We might be restarted and PID1 could have sent us back the session device fds we previously
495 (void) manager_attach_fds(m
);
500 static int manager_enumerate_inhibitors(Manager
*m
) {
501 _cleanup_closedir_
DIR *d
= NULL
;
507 d
= opendir("/run/systemd/inhibit");
512 return log_error_errno(errno
, "Failed to open /run/systemd/inhibit: %m");
515 FOREACH_DIRENT(de
, d
, return -errno
) {
519 if (!dirent_is_file(de
))
522 k
= manager_add_inhibitor(m
, de
->d_name
, &i
);
524 r
= log_warning_errno(k
, "Couldn't add inhibitor %s, ignoring: %m", de
->d_name
);
528 k
= inhibitor_load(i
);
536 static int manager_dispatch_seat_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
537 Manager
*m
= userdata
;
542 manager_process_seat_device(m
, device
);
546 static int manager_dispatch_device_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
547 Manager
*m
= userdata
;
552 manager_process_seat_device(m
, device
);
556 static int manager_dispatch_vcsa_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
557 Manager
*m
= userdata
;
563 /* Whenever a VCSA device is removed try to reallocate our
564 * VTs, to make sure our auto VTs never go away. */
566 if (sd_device_get_sysname(device
, &name
) >= 0 &&
567 startswith(name
, "vcsa") &&
568 device_for_action(device
, DEVICE_ACTION_REMOVE
))
569 seat_preallocate_vts(m
->seat0
);
574 static int manager_dispatch_button_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
575 Manager
*m
= userdata
;
580 manager_process_button_device(m
, device
);
584 static int manager_dispatch_console(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
585 Manager
*m
= userdata
;
589 assert(m
->console_active_fd
== fd
);
591 seat_read_active_vt(m
->seat0
);
595 static int manager_reserve_vt(Manager
*m
) {
596 _cleanup_free_
char *p
= NULL
;
600 if (m
->reserve_vt
<= 0)
603 if (asprintf(&p
, "/dev/tty%u", m
->reserve_vt
) < 0)
606 m
->reserve_vt_fd
= open(p
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
607 if (m
->reserve_vt_fd
< 0) {
609 /* Don't complain on VT-less systems */
611 log_warning_errno(errno
, "Failed to pin reserved VT: %m");
618 static int manager_connect_bus(Manager
*m
) {
624 r
= sd_bus_default_system(&m
->bus
);
626 return log_error_errno(r
, "Failed to connect to system bus: %m");
628 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
632 r
= bus_log_control_api_register(m
->bus
);
636 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "JobRemoved", match_job_removed
, NULL
, m
);
638 return log_error_errno(r
, "Failed to request match for JobRemoved: %m");
640 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "UnitRemoved", match_unit_removed
, NULL
, m
);
642 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
644 r
= sd_bus_match_signal_async(
647 "org.freedesktop.systemd1",
649 "org.freedesktop.DBus.Properties",
651 match_properties_changed
, NULL
, m
);
653 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
655 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "Reloading", match_reloading
, NULL
, m
);
657 return log_error_errno(r
, "Failed to request match for Reloading: %m");
659 r
= bus_call_method_async(m
->bus
, NULL
, bus_systemd_mgr
, "Subscribe", NULL
, NULL
, NULL
);
661 return log_error_errno(r
, "Failed to enable subscription: %m");
663 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.login1", 0, NULL
, NULL
);
665 return log_error_errno(r
, "Failed to request name: %m");
667 r
= sd_bus_attach_event(m
->bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
669 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
674 static int manager_vt_switch(sd_event_source
*src
, const struct signalfd_siginfo
*si
, void *data
) {
676 Session
*active
, *iter
;
679 * We got a VT-switch signal and we have to acknowledge it immediately.
680 * Preferably, we'd just use m->seat0->active->vtfd, but unfortunately,
681 * old user-space might run multiple sessions on a single VT, *sigh*.
682 * Therefore, we have to iterate all sessions and find one with a vtfd
683 * on the requested VT.
684 * As only VTs with active controllers have VT_PROCESS set, our current
685 * notion of the active VT might be wrong (for instance if the switch
686 * happens while we setup VT_PROCESS). Therefore, read the current VT
687 * first and then use s->active->vtnr as reference. Note that this is
688 * not racy, as no further VT-switch can happen as long as we're in
689 * synchronous VT_PROCESS mode.
693 seat_read_active_vt(m
->seat0
);
695 active
= m
->seat0
->active
;
696 if (!active
|| active
->vtnr
< 1) {
697 _cleanup_close_
int fd
= -1;
700 /* We are requested to acknowledge the VT-switch signal by the kernel but
701 * there's no registered sessions for the current VT. Normally this
702 * shouldn't happen but something wrong might have happened when we tried
703 * to release the VT. Better be safe than sorry, and try to release the VT
704 * one more time otherwise the user will be locked with the current VT. */
706 log_warning("Received VT_PROCESS signal without a registered session, restoring VT.");
708 /* At this point we only have the kernel mapping for referring to the
710 fd
= open_terminal("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
712 log_warning_errno(fd
, "Failed to open, ignoring: %m");
716 r
= vt_release(fd
, true);
718 log_warning_errno(r
, "Failed to release VT, ignoring: %m");
723 if (active
->vtfd
>= 0) {
724 session_leave_vt(active
);
726 LIST_FOREACH(sessions_by_seat
, iter
, m
->seat0
->sessions
) {
727 if (iter
->vtnr
== active
->vtnr
&& iter
->vtfd
>= 0) {
728 session_leave_vt(iter
);
737 static int manager_connect_console(Manager
*m
) {
741 assert(m
->console_active_fd
< 0);
743 /* On certain systems (such as S390, Xen, and containers) /dev/tty0 does not exist (as there is no VC), so
744 * don't fail if we can't open it. */
746 if (access("/dev/tty0", F_OK
) < 0)
749 m
->console_active_fd
= open("/sys/class/tty/tty0/active", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
750 if (m
->console_active_fd
< 0) {
752 /* On some systems /dev/tty0 may exist even though /sys/class/tty/tty0 does not. These are broken, but
753 * common. Let's complain but continue anyway. */
754 if (errno
== ENOENT
) {
755 log_warning_errno(errno
, "System has /dev/tty0 but not /sys/class/tty/tty0/active which is broken, ignoring: %m");
759 return log_error_errno(errno
, "Failed to open /sys/class/tty/tty0/active: %m");
762 r
= sd_event_add_io(m
->event
, &m
->console_active_event_source
, m
->console_active_fd
, 0, manager_dispatch_console
, m
);
764 return log_error_errno(r
, "Failed to watch foreground console: %m");
767 * SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used
768 * as VT-acquire signal. We ignore any acquire-events (yes, we still
769 * have to provide a valid signal-number for it!) and acknowledge all
770 * release events immediately.
773 if (SIGRTMIN
+ 1 > SIGRTMAX
)
774 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
775 "Not enough real-time signals available: %u-%u",
778 assert_se(ignore_signals(SIGRTMIN
+ 1, -1) >= 0);
779 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGRTMIN
, -1) >= 0);
781 r
= sd_event_add_signal(m
->event
, NULL
, SIGRTMIN
, manager_vt_switch
, m
);
783 return log_error_errno(r
, "Failed to subscribe to signal: %m");
788 static int manager_connect_udev(Manager
*m
) {
792 assert(!m
->device_seat_monitor
);
793 assert(!m
->device_monitor
);
794 assert(!m
->device_vcsa_monitor
);
795 assert(!m
->device_button_monitor
);
797 r
= sd_device_monitor_new(&m
->device_seat_monitor
);
801 r
= sd_device_monitor_filter_add_match_tag(m
->device_seat_monitor
, "master-of-seat");
805 r
= sd_device_monitor_attach_event(m
->device_seat_monitor
, m
->event
);
809 r
= sd_device_monitor_start(m
->device_seat_monitor
, manager_dispatch_seat_udev
, m
);
813 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_seat_monitor
), "logind-seat-monitor");
815 r
= sd_device_monitor_new(&m
->device_monitor
);
819 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "input", NULL
);
823 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "graphics", NULL
);
827 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "drm", NULL
);
831 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
835 r
= sd_device_monitor_start(m
->device_monitor
, manager_dispatch_device_udev
, m
);
839 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_monitor
), "logind-device-monitor");
841 /* Don't watch keys if nobody cares */
842 if (!manager_all_buttons_ignored(m
)) {
843 r
= sd_device_monitor_new(&m
->device_button_monitor
);
847 r
= sd_device_monitor_filter_add_match_tag(m
->device_button_monitor
, "power-switch");
851 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_button_monitor
, "input", NULL
);
855 r
= sd_device_monitor_attach_event(m
->device_button_monitor
, m
->event
);
859 r
= sd_device_monitor_start(m
->device_button_monitor
, manager_dispatch_button_udev
, m
);
863 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_button_monitor
), "logind-button-monitor");
866 /* Don't bother watching VCSA devices, if nobody cares */
867 if (m
->n_autovts
> 0 && m
->console_active_fd
>= 0) {
869 r
= sd_device_monitor_new(&m
->device_vcsa_monitor
);
873 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_vcsa_monitor
, "vc", NULL
);
877 r
= sd_device_monitor_attach_event(m
->device_vcsa_monitor
, m
->event
);
881 r
= sd_device_monitor_start(m
->device_vcsa_monitor
, manager_dispatch_vcsa_udev
, m
);
885 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_vcsa_monitor
), "logind-vcsa-monitor");
891 static void manager_gc(Manager
*m
, bool drop_not_started
) {
898 while ((seat
= m
->seat_gc_queue
)) {
899 LIST_REMOVE(gc_queue
, m
->seat_gc_queue
, seat
);
900 seat
->in_gc_queue
= false;
902 if (seat_may_gc(seat
, drop_not_started
)) {
903 seat_stop(seat
, false);
908 while ((session
= m
->session_gc_queue
)) {
909 LIST_REMOVE(gc_queue
, m
->session_gc_queue
, session
);
910 session
->in_gc_queue
= false;
912 /* First, if we are not closing yet, initiate stopping */
913 if (session_may_gc(session
, drop_not_started
) &&
914 session_get_state(session
) != SESSION_CLOSING
)
915 (void) session_stop(session
, false);
917 /* Normally, this should make the session referenced
918 * again, if it doesn't then let's get rid of it
920 if (session_may_gc(session
, drop_not_started
)) {
921 (void) session_finalize(session
);
922 session_free(session
);
926 while ((user
= m
->user_gc_queue
)) {
927 LIST_REMOVE(gc_queue
, m
->user_gc_queue
, user
);
928 user
->in_gc_queue
= false;
930 /* First step: queue stop jobs */
931 if (user_may_gc(user
, drop_not_started
))
932 (void) user_stop(user
, false);
934 /* Second step: finalize user */
935 if (user_may_gc(user
, drop_not_started
)) {
936 (void) user_finalize(user
);
942 static int manager_dispatch_idle_action(sd_event_source
*s
, uint64_t t
, void *userdata
) {
943 Manager
*m
= userdata
;
944 struct dual_timestamp since
;
950 if (m
->idle_action
== HANDLE_IGNORE
||
951 m
->idle_action_usec
<= 0)
954 n
= now(CLOCK_MONOTONIC
);
956 r
= manager_get_idle_hint(m
, &since
);
958 /* Not idle. Let's check if after a timeout it might be idle then. */
959 elapse
= n
+ m
->idle_action_usec
;
961 /* Idle! Let's see if it's time to do something, or if
962 * we shall sleep for longer. */
964 if (n
>= since
.monotonic
+ m
->idle_action_usec
&&
965 (m
->idle_action_not_before_usec
<= 0 || n
>= m
->idle_action_not_before_usec
+ m
->idle_action_usec
)) {
966 log_info("System idle. Doing %s operation.", handle_action_to_string(m
->idle_action
));
968 manager_handle_action(m
, 0, m
->idle_action
, false, false);
969 m
->idle_action_not_before_usec
= n
;
972 elapse
= MAX(since
.monotonic
, m
->idle_action_not_before_usec
) + m
->idle_action_usec
;
975 if (!m
->idle_action_event_source
) {
977 r
= sd_event_add_time(
979 &m
->idle_action_event_source
,
981 elapse
, USEC_PER_SEC
*30,
982 manager_dispatch_idle_action
, m
);
984 return log_error_errno(r
, "Failed to add idle event source: %m");
986 r
= sd_event_source_set_priority(m
->idle_action_event_source
, SD_EVENT_PRIORITY_IDLE
+10);
988 return log_error_errno(r
, "Failed to set idle event source priority: %m");
990 r
= sd_event_source_set_time(m
->idle_action_event_source
, elapse
);
992 return log_error_errno(r
, "Failed to set idle event timer: %m");
994 r
= sd_event_source_set_enabled(m
->idle_action_event_source
, SD_EVENT_ONESHOT
);
996 return log_error_errno(r
, "Failed to enable idle event timer: %m");
1002 static int manager_dispatch_reload_signal(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1003 Manager
*m
= userdata
;
1006 manager_reset_config(m
);
1007 r
= manager_parse_config_file(m
);
1009 log_warning_errno(r
, "Failed to parse config file, using defaults: %m");
1011 log_info("Config file reloaded.");
1016 static int manager_startup(Manager
*m
) {
1022 Inhibitor
*inhibitor
;
1027 r
= sd_event_add_signal(m
->event
, NULL
, SIGHUP
, manager_dispatch_reload_signal
, m
);
1029 return log_error_errno(r
, "Failed to register SIGHUP handler: %m");
1031 /* Connect to utmp */
1032 manager_connect_utmp(m
);
1034 /* Connect to console */
1035 r
= manager_connect_console(m
);
1039 /* Connect to udev */
1040 r
= manager_connect_udev(m
);
1042 return log_error_errno(r
, "Failed to create udev watchers: %m");
1044 /* Connect to the bus */
1045 r
= manager_connect_bus(m
);
1049 /* Instantiate magic seat 0 */
1050 r
= manager_add_seat(m
, "seat0", &m
->seat0
);
1052 return log_error_errno(r
, "Failed to add seat0: %m");
1054 r
= manager_set_lid_switch_ignore(m
, 0 + m
->holdoff_timeout_usec
);
1056 log_warning_errno(r
, "Failed to set up lid switch ignore event source: %m");
1058 /* Deserialize state */
1059 r
= manager_enumerate_devices(m
);
1061 log_warning_errno(r
, "Device enumeration failed: %m");
1063 r
= manager_enumerate_seats(m
);
1065 log_warning_errno(r
, "Seat enumeration failed: %m");
1067 r
= manager_enumerate_users(m
);
1069 log_warning_errno(r
, "User enumeration failed: %m");
1071 r
= manager_enumerate_sessions(m
);
1073 log_warning_errno(r
, "Session enumeration failed: %m");
1075 r
= manager_enumerate_inhibitors(m
);
1077 log_warning_errno(r
, "Inhibitor enumeration failed: %m");
1079 r
= manager_enumerate_buttons(m
);
1081 log_warning_errno(r
, "Button enumeration failed: %m");
1083 /* Remove stale objects before we start them */
1084 manager_gc(m
, false);
1086 /* Reserve the special reserved VT */
1087 manager_reserve_vt(m
);
1089 /* Read in utmp if it exists */
1090 manager_read_utmp(m
);
1092 /* And start everything */
1093 HASHMAP_FOREACH(seat
, m
->seats
, i
)
1094 (void) seat_start(seat
);
1096 HASHMAP_FOREACH(user
, m
->users
, i
)
1097 (void) user_start(user
);
1099 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1100 (void) session_start(session
, NULL
, NULL
);
1102 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
1103 (void) inhibitor_start(inhibitor
);
1105 /* Let's see if the inhibitor is dead now, then remove it */
1106 if (inhibitor_is_orphan(inhibitor
)) {
1107 inhibitor_stop(inhibitor
);
1108 inhibitor_free(inhibitor
);
1112 HASHMAP_FOREACH(button
, m
->buttons
, i
)
1113 button_check_switches(button
);
1115 manager_dispatch_idle_action(NULL
, 0, m
);
1120 static int manager_run(Manager
*m
) {
1126 r
= sd_event_get_state(m
->event
);
1129 if (r
== SD_EVENT_FINISHED
)
1132 manager_gc(m
, true);
1134 r
= manager_dispatch_delayed(m
, false);
1140 r
= sd_event_run(m
->event
, (uint64_t) -1);
1146 static int run(int argc
, char *argv
[]) {
1147 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
1150 log_set_facility(LOG_AUTH
);
1151 log_setup_service();
1153 r
= service_parse_argv("systemd-logind.service",
1154 "Manager for user logins and devices and privileged operations.",
1155 BUS_IMPLEMENTATIONS(&manager_object
,
1156 &log_control_object
),
1163 r
= mac_selinux_init();
1165 return log_error_errno(r
, "Could not initialize labelling: %m");
1167 /* Always create the directories people can create inotify watches in. Note that some applications might check
1168 * for the existence of /run/systemd/seats/ to determine whether logind is available, so please always make
1169 * sure these directories are created early on and unconditionally. */
1170 (void) mkdir_label("/run/systemd/seats", 0755);
1171 (void) mkdir_label("/run/systemd/users", 0755);
1172 (void) mkdir_label("/run/systemd/sessions", 0755);
1174 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGHUP
, SIGTERM
, SIGINT
, SIGCHLD
, -1) >= 0);
1176 r
= manager_new(&m
);
1178 return log_error_errno(r
, "Failed to allocate manager object: %m");
1180 (void) manager_parse_config_file(m
);
1182 r
= manager_startup(m
);
1184 return log_error_errno(r
, "Failed to fully start up daemon: %m");
1186 log_debug("systemd-logind running as pid "PID_FMT
, getpid_cached());
1187 (void) sd_notify(false,
1189 "STATUS=Processing requests...");
1193 log_debug("systemd-logind stopped as pid "PID_FMT
, getpid_cached());
1194 (void) sd_notify(false,
1196 "STATUS=Shutting down...");
1201 DEFINE_MAIN_FUNCTION(run
);