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
);
169 strv_free(m
->efi_boot_loader_entries
);
174 static int manager_enumerate_devices(Manager
*m
) {
175 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
181 /* Loads devices from udev and creates seats for them as
184 r
= sd_device_enumerator_new(&e
);
188 r
= sd_device_enumerator_add_match_tag(e
, "master-of-seat");
192 FOREACH_DEVICE(e
, d
) {
195 k
= manager_process_seat_device(m
, d
);
203 static int manager_enumerate_buttons(Manager
*m
) {
204 _cleanup_(sd_device_enumerator_unrefp
) sd_device_enumerator
*e
= NULL
;
210 /* Loads buttons from udev */
212 if (manager_all_buttons_ignored(m
))
215 r
= sd_device_enumerator_new(&e
);
219 r
= sd_device_enumerator_add_match_subsystem(e
, "input", true);
223 r
= sd_device_enumerator_add_match_tag(e
, "power-switch");
227 FOREACH_DEVICE(e
, d
) {
230 k
= manager_process_button_device(m
, d
);
238 static int manager_enumerate_seats(Manager
*m
) {
239 _cleanup_closedir_
DIR *d
= NULL
;
245 /* This loads data about seats stored on disk, but does not
246 * actually create any seats. Removes data of seats that no
249 d
= opendir("/run/systemd/seats");
254 return log_error_errno(errno
, "Failed to open /run/systemd/seats: %m");
257 FOREACH_DIRENT(de
, d
, return -errno
) {
261 if (!dirent_is_file(de
))
264 s
= hashmap_get(m
->seats
, de
->d_name
);
266 if (unlinkat(dirfd(d
), de
->d_name
, 0) < 0)
267 log_warning("Failed to remove /run/systemd/seats/%s: %m",
280 static int manager_enumerate_linger_users(Manager
*m
) {
281 _cleanup_closedir_
DIR *d
= NULL
;
287 d
= opendir("/var/lib/systemd/linger");
292 return log_error_errno(errno
, "Failed to open /var/lib/systemd/linger/: %m");
295 FOREACH_DIRENT(de
, d
, return -errno
) {
298 dirent_ensure_type(d
, de
);
299 if (!dirent_is_file(de
))
302 k
= manager_add_user_by_name(m
, de
->d_name
, NULL
);
304 r
= log_warning_errno(k
, "Couldn't add lingering user %s, ignoring: %m", de
->d_name
);
310 static int manager_enumerate_users(Manager
*m
) {
311 _cleanup_closedir_
DIR *d
= NULL
;
317 /* Add lingering users */
318 r
= manager_enumerate_linger_users(m
);
320 /* Read in user data stored on disk */
321 d
= opendir("/run/systemd/users");
326 return log_error_errno(errno
, "Failed to open /run/systemd/users: %m");
329 FOREACH_DIRENT(de
, d
, return -errno
) {
332 if (!dirent_is_file(de
))
335 k
= manager_add_user_by_name(m
, de
->d_name
, &u
);
337 r
= log_warning_errno(k
, "Failed to add user by file name %s, ignoring: %m", de
->d_name
);
341 user_add_to_gc_queue(u
);
351 static int parse_fdname(const char *fdname
, char **session_id
, dev_t
*dev
) {
352 _cleanup_strv_free_
char **parts
= NULL
;
353 _cleanup_free_
char *id
= NULL
;
354 unsigned major
, minor
;
357 parts
= strv_split(fdname
, "-");
360 if (strv_length(parts
) != 5)
363 if (!streq(parts
[0], "session"))
366 id
= strdup(parts
[1]);
370 if (!streq(parts
[2], "device"))
373 r
= safe_atou(parts
[3], &major
);
376 r
= safe_atou(parts
[4], &minor
);
380 *dev
= makedev(major
, minor
);
381 *session_id
= TAKE_PTR(id
);
386 static int deliver_fd(Manager
*m
, const char *fdname
, int fd
) {
387 _cleanup_free_
char *id
= NULL
;
397 r
= parse_fdname(fdname
, &id
, &dev
);
399 return log_debug_errno(r
, "Failed to parse fd name %s: %m", fdname
);
401 s
= hashmap_get(m
->sessions
, id
);
403 /* If the session doesn't exist anymore, the associated session device attached to this fd
404 * doesn't either. Let's simply close this fd. */
405 return log_debug_errno(SYNTHETIC_ERRNO(ENXIO
), "Failed to attach fd for unknown session: %s", id
);
407 if (fstat(fd
, &st
) < 0)
408 /* The device is allowed to go away at a random point, in which case fstat() failing is
410 return log_debug_errno(errno
, "Failed to stat device fd for session %s: %m", id
);
412 if (!S_ISCHR(st
.st_mode
) || st
.st_rdev
!= dev
)
413 return log_debug_errno(SYNTHETIC_ERRNO(ENODEV
), "Device fd doesn't point to the expected character device node");
415 sd
= hashmap_get(s
->devices
, &dev
);
417 /* Weird, we got an fd for a session device which wasn't recorded in the session state
419 return log_warning_errno(SYNTHETIC_ERRNO(ENODEV
), "Got fd for missing session device [%u:%u] in session %s",
420 major(dev
), minor(dev
), s
->id
);
422 log_debug("Attaching fd to session device [%u:%u] for session %s",
423 major(dev
), minor(dev
), s
->id
);
425 session_device_attach_fd(sd
, fd
, s
->was_active
);
429 static int manager_attach_fds(Manager
*m
) {
430 _cleanup_strv_free_
char **fdnames
= NULL
;
433 /* Upon restart, PID1 will send us back all fds of session devices that we previously opened. Each
434 * file descriptor is associated with a given session. The session ids are passed through FDNAMES. */
436 n
= sd_listen_fds_with_names(true, &fdnames
);
438 return log_warning_errno(n
, "Failed to acquire passed fd list: %m");
442 for (int i
= 0; i
< n
; i
++) {
443 int fd
= SD_LISTEN_FDS_START
+ i
;
445 if (deliver_fd(m
, fdnames
[i
], fd
) >= 0)
448 /* Hmm, we couldn't deliver the fd to any session device object? If so, let's close the fd */
451 /* Remove from fdstore as well */
452 (void) sd_notifyf(false,
454 "FDNAME=%s", fdnames
[i
]);
460 static int manager_enumerate_sessions(Manager
*m
) {
461 _cleanup_closedir_
DIR *d
= NULL
;
467 /* Read in session data stored on disk */
468 d
= opendir("/run/systemd/sessions");
473 return log_error_errno(errno
, "Failed to open /run/systemd/sessions: %m");
476 FOREACH_DIRENT(de
, d
, return -errno
) {
479 if (!dirent_is_file(de
))
482 k
= manager_add_session(m
, de
->d_name
, &s
);
484 r
= log_warning_errno(k
, "Failed to add session by file name %s, ignoring: %m", de
->d_name
);
488 session_add_to_gc_queue(s
);
495 /* We might be restarted and PID1 could have sent us back the session device fds we previously
497 (void) manager_attach_fds(m
);
502 static int manager_enumerate_inhibitors(Manager
*m
) {
503 _cleanup_closedir_
DIR *d
= NULL
;
509 d
= opendir("/run/systemd/inhibit");
514 return log_error_errno(errno
, "Failed to open /run/systemd/inhibit: %m");
517 FOREACH_DIRENT(de
, d
, return -errno
) {
521 if (!dirent_is_file(de
))
524 k
= manager_add_inhibitor(m
, de
->d_name
, &i
);
526 r
= log_warning_errno(k
, "Couldn't add inhibitor %s, ignoring: %m", de
->d_name
);
530 k
= inhibitor_load(i
);
538 static int manager_dispatch_seat_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
539 Manager
*m
= userdata
;
544 manager_process_seat_device(m
, device
);
548 static int manager_dispatch_device_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_vcsa_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
559 Manager
*m
= userdata
;
565 /* Whenever a VCSA device is removed try to reallocate our
566 * VTs, to make sure our auto VTs never go away. */
568 if (sd_device_get_sysname(device
, &name
) >= 0 &&
569 startswith(name
, "vcsa") &&
570 device_for_action(device
, DEVICE_ACTION_REMOVE
))
571 seat_preallocate_vts(m
->seat0
);
576 static int manager_dispatch_button_udev(sd_device_monitor
*monitor
, sd_device
*device
, void *userdata
) {
577 Manager
*m
= userdata
;
582 manager_process_button_device(m
, device
);
586 static int manager_dispatch_console(sd_event_source
*s
, int fd
, uint32_t revents
, void *userdata
) {
587 Manager
*m
= userdata
;
591 assert(m
->console_active_fd
== fd
);
593 seat_read_active_vt(m
->seat0
);
597 static int manager_reserve_vt(Manager
*m
) {
598 _cleanup_free_
char *p
= NULL
;
602 if (m
->reserve_vt
<= 0)
605 if (asprintf(&p
, "/dev/tty%u", m
->reserve_vt
) < 0)
608 m
->reserve_vt_fd
= open(p
, O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
609 if (m
->reserve_vt_fd
< 0) {
611 /* Don't complain on VT-less systems */
613 log_warning_errno(errno
, "Failed to pin reserved VT: %m");
620 static int manager_connect_bus(Manager
*m
) {
626 r
= sd_bus_default_system(&m
->bus
);
628 return log_error_errno(r
, "Failed to connect to system bus: %m");
630 r
= bus_add_implementation(m
->bus
, &manager_object
, m
);
634 r
= bus_log_control_api_register(m
->bus
);
638 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "JobRemoved", match_job_removed
, NULL
, m
);
640 return log_error_errno(r
, "Failed to request match for JobRemoved: %m");
642 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "UnitRemoved", match_unit_removed
, NULL
, m
);
644 return log_error_errno(r
, "Failed to request match for UnitRemoved: %m");
646 r
= sd_bus_match_signal_async(
649 "org.freedesktop.systemd1",
651 "org.freedesktop.DBus.Properties",
653 match_properties_changed
, NULL
, m
);
655 return log_error_errno(r
, "Failed to request match for PropertiesChanged: %m");
657 r
= bus_match_signal_async(m
->bus
, NULL
, bus_systemd_mgr
, "Reloading", match_reloading
, NULL
, m
);
659 return log_error_errno(r
, "Failed to request match for Reloading: %m");
661 r
= bus_call_method_async(m
->bus
, NULL
, bus_systemd_mgr
, "Subscribe", NULL
, NULL
, NULL
);
663 return log_error_errno(r
, "Failed to enable subscription: %m");
665 r
= sd_bus_request_name_async(m
->bus
, NULL
, "org.freedesktop.login1", 0, NULL
, NULL
);
667 return log_error_errno(r
, "Failed to request name: %m");
669 r
= sd_bus_attach_event(m
->bus
, m
->event
, SD_EVENT_PRIORITY_NORMAL
);
671 return log_error_errno(r
, "Failed to attach bus to event loop: %m");
676 static int manager_vt_switch(sd_event_source
*src
, const struct signalfd_siginfo
*si
, void *data
) {
678 Session
*active
, *iter
;
681 * We got a VT-switch signal and we have to acknowledge it immediately.
682 * Preferably, we'd just use m->seat0->active->vtfd, but unfortunately,
683 * old user-space might run multiple sessions on a single VT, *sigh*.
684 * Therefore, we have to iterate all sessions and find one with a vtfd
685 * on the requested VT.
686 * As only VTs with active controllers have VT_PROCESS set, our current
687 * notion of the active VT might be wrong (for instance if the switch
688 * happens while we setup VT_PROCESS). Therefore, read the current VT
689 * first and then use s->active->vtnr as reference. Note that this is
690 * not racy, as no further VT-switch can happen as long as we're in
691 * synchronous VT_PROCESS mode.
695 seat_read_active_vt(m
->seat0
);
697 active
= m
->seat0
->active
;
698 if (!active
|| active
->vtnr
< 1) {
699 _cleanup_close_
int fd
= -1;
702 /* We are requested to acknowledge the VT-switch signal by the kernel but
703 * there's no registered sessions for the current VT. Normally this
704 * shouldn't happen but something wrong might have happened when we tried
705 * to release the VT. Better be safe than sorry, and try to release the VT
706 * one more time otherwise the user will be locked with the current VT. */
708 log_warning("Received VT_PROCESS signal without a registered session, restoring VT.");
710 /* At this point we only have the kernel mapping for referring to the
712 fd
= open_terminal("/dev/tty0", O_RDWR
|O_NOCTTY
|O_CLOEXEC
|O_NONBLOCK
);
714 log_warning_errno(fd
, "Failed to open, ignoring: %m");
718 r
= vt_release(fd
, true);
720 log_warning_errno(r
, "Failed to release VT, ignoring: %m");
725 if (active
->vtfd
>= 0) {
726 session_leave_vt(active
);
728 LIST_FOREACH(sessions_by_seat
, iter
, m
->seat0
->sessions
) {
729 if (iter
->vtnr
== active
->vtnr
&& iter
->vtfd
>= 0) {
730 session_leave_vt(iter
);
739 static int manager_connect_console(Manager
*m
) {
743 assert(m
->console_active_fd
< 0);
745 /* On certain systems (such as S390, Xen, and containers) /dev/tty0 does not exist (as there is no VC), so
746 * don't fail if we can't open it. */
748 if (access("/dev/tty0", F_OK
) < 0)
751 m
->console_active_fd
= open("/sys/class/tty/tty0/active", O_RDONLY
|O_NOCTTY
|O_CLOEXEC
);
752 if (m
->console_active_fd
< 0) {
754 /* On some systems /dev/tty0 may exist even though /sys/class/tty/tty0 does not. These are broken, but
755 * common. Let's complain but continue anyway. */
756 if (errno
== ENOENT
) {
757 log_warning_errno(errno
, "System has /dev/tty0 but not /sys/class/tty/tty0/active which is broken, ignoring: %m");
761 return log_error_errno(errno
, "Failed to open /sys/class/tty/tty0/active: %m");
764 r
= sd_event_add_io(m
->event
, &m
->console_active_event_source
, m
->console_active_fd
, 0, manager_dispatch_console
, m
);
766 return log_error_errno(r
, "Failed to watch foreground console: %m");
769 * SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used
770 * as VT-acquire signal. We ignore any acquire-events (yes, we still
771 * have to provide a valid signal-number for it!) and acknowledge all
772 * release events immediately.
775 if (SIGRTMIN
+ 1 > SIGRTMAX
)
776 return log_error_errno(SYNTHETIC_ERRNO(EINVAL
),
777 "Not enough real-time signals available: %u-%u",
780 assert_se(ignore_signals(SIGRTMIN
+ 1, -1) >= 0);
781 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGRTMIN
, -1) >= 0);
783 r
= sd_event_add_signal(m
->event
, NULL
, SIGRTMIN
, manager_vt_switch
, m
);
785 return log_error_errno(r
, "Failed to subscribe to signal: %m");
790 static int manager_connect_udev(Manager
*m
) {
794 assert(!m
->device_seat_monitor
);
795 assert(!m
->device_monitor
);
796 assert(!m
->device_vcsa_monitor
);
797 assert(!m
->device_button_monitor
);
799 r
= sd_device_monitor_new(&m
->device_seat_monitor
);
803 r
= sd_device_monitor_filter_add_match_tag(m
->device_seat_monitor
, "master-of-seat");
807 r
= sd_device_monitor_attach_event(m
->device_seat_monitor
, m
->event
);
811 r
= sd_device_monitor_start(m
->device_seat_monitor
, manager_dispatch_seat_udev
, m
);
815 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_seat_monitor
), "logind-seat-monitor");
817 r
= sd_device_monitor_new(&m
->device_monitor
);
821 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "input", NULL
);
825 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "graphics", NULL
);
829 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_monitor
, "drm", NULL
);
833 r
= sd_device_monitor_attach_event(m
->device_monitor
, m
->event
);
837 r
= sd_device_monitor_start(m
->device_monitor
, manager_dispatch_device_udev
, m
);
841 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_monitor
), "logind-device-monitor");
843 /* Don't watch keys if nobody cares */
844 if (!manager_all_buttons_ignored(m
)) {
845 r
= sd_device_monitor_new(&m
->device_button_monitor
);
849 r
= sd_device_monitor_filter_add_match_tag(m
->device_button_monitor
, "power-switch");
853 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_button_monitor
, "input", NULL
);
857 r
= sd_device_monitor_attach_event(m
->device_button_monitor
, m
->event
);
861 r
= sd_device_monitor_start(m
->device_button_monitor
, manager_dispatch_button_udev
, m
);
865 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_button_monitor
), "logind-button-monitor");
868 /* Don't bother watching VCSA devices, if nobody cares */
869 if (m
->n_autovts
> 0 && m
->console_active_fd
>= 0) {
871 r
= sd_device_monitor_new(&m
->device_vcsa_monitor
);
875 r
= sd_device_monitor_filter_add_match_subsystem_devtype(m
->device_vcsa_monitor
, "vc", NULL
);
879 r
= sd_device_monitor_attach_event(m
->device_vcsa_monitor
, m
->event
);
883 r
= sd_device_monitor_start(m
->device_vcsa_monitor
, manager_dispatch_vcsa_udev
, m
);
887 (void) sd_event_source_set_description(sd_device_monitor_get_event_source(m
->device_vcsa_monitor
), "logind-vcsa-monitor");
893 static void manager_gc(Manager
*m
, bool drop_not_started
) {
900 while ((seat
= m
->seat_gc_queue
)) {
901 LIST_REMOVE(gc_queue
, m
->seat_gc_queue
, seat
);
902 seat
->in_gc_queue
= false;
904 if (seat_may_gc(seat
, drop_not_started
)) {
905 seat_stop(seat
, false);
910 while ((session
= m
->session_gc_queue
)) {
911 LIST_REMOVE(gc_queue
, m
->session_gc_queue
, session
);
912 session
->in_gc_queue
= false;
914 /* First, if we are not closing yet, initiate stopping */
915 if (session_may_gc(session
, drop_not_started
) &&
916 session_get_state(session
) != SESSION_CLOSING
)
917 (void) session_stop(session
, false);
919 /* Normally, this should make the session referenced
920 * again, if it doesn't then let's get rid of it
922 if (session_may_gc(session
, drop_not_started
)) {
923 (void) session_finalize(session
);
924 session_free(session
);
928 while ((user
= m
->user_gc_queue
)) {
929 LIST_REMOVE(gc_queue
, m
->user_gc_queue
, user
);
930 user
->in_gc_queue
= false;
932 /* First step: queue stop jobs */
933 if (user_may_gc(user
, drop_not_started
))
934 (void) user_stop(user
, false);
936 /* Second step: finalize user */
937 if (user_may_gc(user
, drop_not_started
)) {
938 (void) user_finalize(user
);
944 static int manager_dispatch_idle_action(sd_event_source
*s
, uint64_t t
, void *userdata
) {
945 Manager
*m
= userdata
;
946 struct dual_timestamp since
;
952 if (m
->idle_action
== HANDLE_IGNORE
||
953 m
->idle_action_usec
<= 0)
956 n
= now(CLOCK_MONOTONIC
);
958 r
= manager_get_idle_hint(m
, &since
);
960 /* Not idle. Let's check if after a timeout it might be idle then. */
961 elapse
= n
+ m
->idle_action_usec
;
963 /* Idle! Let's see if it's time to do something, or if
964 * we shall sleep for longer. */
966 if (n
>= since
.monotonic
+ m
->idle_action_usec
&&
967 (m
->idle_action_not_before_usec
<= 0 || n
>= m
->idle_action_not_before_usec
+ m
->idle_action_usec
)) {
968 log_info("System idle. Doing %s operation.", handle_action_to_string(m
->idle_action
));
970 manager_handle_action(m
, 0, m
->idle_action
, false, false);
971 m
->idle_action_not_before_usec
= n
;
974 elapse
= MAX(since
.monotonic
, m
->idle_action_not_before_usec
) + m
->idle_action_usec
;
977 if (!m
->idle_action_event_source
) {
979 r
= sd_event_add_time(
981 &m
->idle_action_event_source
,
983 elapse
, USEC_PER_SEC
*30,
984 manager_dispatch_idle_action
, m
);
986 return log_error_errno(r
, "Failed to add idle event source: %m");
988 r
= sd_event_source_set_priority(m
->idle_action_event_source
, SD_EVENT_PRIORITY_IDLE
+10);
990 return log_error_errno(r
, "Failed to set idle event source priority: %m");
992 r
= sd_event_source_set_time(m
->idle_action_event_source
, elapse
);
994 return log_error_errno(r
, "Failed to set idle event timer: %m");
996 r
= sd_event_source_set_enabled(m
->idle_action_event_source
, SD_EVENT_ONESHOT
);
998 return log_error_errno(r
, "Failed to enable idle event timer: %m");
1004 static int manager_dispatch_reload_signal(sd_event_source
*s
, const struct signalfd_siginfo
*si
, void *userdata
) {
1005 Manager
*m
= userdata
;
1008 manager_reset_config(m
);
1009 r
= manager_parse_config_file(m
);
1011 log_warning_errno(r
, "Failed to parse config file, using defaults: %m");
1013 log_info("Config file reloaded.");
1018 static int manager_startup(Manager
*m
) {
1024 Inhibitor
*inhibitor
;
1029 r
= sd_event_add_signal(m
->event
, NULL
, SIGHUP
, manager_dispatch_reload_signal
, m
);
1031 return log_error_errno(r
, "Failed to register SIGHUP handler: %m");
1033 /* Connect to utmp */
1034 manager_connect_utmp(m
);
1036 /* Connect to console */
1037 r
= manager_connect_console(m
);
1041 /* Connect to udev */
1042 r
= manager_connect_udev(m
);
1044 return log_error_errno(r
, "Failed to create udev watchers: %m");
1046 /* Connect to the bus */
1047 r
= manager_connect_bus(m
);
1051 /* Instantiate magic seat 0 */
1052 r
= manager_add_seat(m
, "seat0", &m
->seat0
);
1054 return log_error_errno(r
, "Failed to add seat0: %m");
1056 r
= manager_set_lid_switch_ignore(m
, 0 + m
->holdoff_timeout_usec
);
1058 log_warning_errno(r
, "Failed to set up lid switch ignore event source: %m");
1060 /* Deserialize state */
1061 r
= manager_enumerate_devices(m
);
1063 log_warning_errno(r
, "Device enumeration failed: %m");
1065 r
= manager_enumerate_seats(m
);
1067 log_warning_errno(r
, "Seat enumeration failed: %m");
1069 r
= manager_enumerate_users(m
);
1071 log_warning_errno(r
, "User enumeration failed: %m");
1073 r
= manager_enumerate_sessions(m
);
1075 log_warning_errno(r
, "Session enumeration failed: %m");
1077 r
= manager_enumerate_inhibitors(m
);
1079 log_warning_errno(r
, "Inhibitor enumeration failed: %m");
1081 r
= manager_enumerate_buttons(m
);
1083 log_warning_errno(r
, "Button enumeration failed: %m");
1085 /* Remove stale objects before we start them */
1086 manager_gc(m
, false);
1088 /* Reserve the special reserved VT */
1089 manager_reserve_vt(m
);
1091 /* Read in utmp if it exists */
1092 manager_read_utmp(m
);
1094 /* And start everything */
1095 HASHMAP_FOREACH(seat
, m
->seats
, i
)
1096 (void) seat_start(seat
);
1098 HASHMAP_FOREACH(user
, m
->users
, i
)
1099 (void) user_start(user
);
1101 HASHMAP_FOREACH(session
, m
->sessions
, i
)
1102 (void) session_start(session
, NULL
, NULL
);
1104 HASHMAP_FOREACH(inhibitor
, m
->inhibitors
, i
) {
1105 (void) inhibitor_start(inhibitor
);
1107 /* Let's see if the inhibitor is dead now, then remove it */
1108 if (inhibitor_is_orphan(inhibitor
)) {
1109 inhibitor_stop(inhibitor
);
1110 inhibitor_free(inhibitor
);
1114 HASHMAP_FOREACH(button
, m
->buttons
, i
)
1115 button_check_switches(button
);
1117 manager_dispatch_idle_action(NULL
, 0, m
);
1122 static int manager_run(Manager
*m
) {
1128 r
= sd_event_get_state(m
->event
);
1131 if (r
== SD_EVENT_FINISHED
)
1134 manager_gc(m
, true);
1136 r
= manager_dispatch_delayed(m
, false);
1142 r
= sd_event_run(m
->event
, (uint64_t) -1);
1148 static int run(int argc
, char *argv
[]) {
1149 _cleanup_(manager_unrefp
) Manager
*m
= NULL
;
1152 log_set_facility(LOG_AUTH
);
1153 log_setup_service();
1155 r
= service_parse_argv("systemd-logind.service",
1156 "Manager for user logins and devices and privileged operations.",
1157 BUS_IMPLEMENTATIONS(&manager_object
,
1158 &log_control_object
),
1165 r
= mac_selinux_init();
1167 return log_error_errno(r
, "Could not initialize labelling: %m");
1169 /* Always create the directories people can create inotify watches in. Note that some applications might check
1170 * for the existence of /run/systemd/seats/ to determine whether logind is available, so please always make
1171 * sure these directories are created early on and unconditionally. */
1172 (void) mkdir_label("/run/systemd/seats", 0755);
1173 (void) mkdir_label("/run/systemd/users", 0755);
1174 (void) mkdir_label("/run/systemd/sessions", 0755);
1176 assert_se(sigprocmask_many(SIG_BLOCK
, NULL
, SIGHUP
, SIGTERM
, SIGINT
, SIGCHLD
, -1) >= 0);
1178 r
= manager_new(&m
);
1180 return log_error_errno(r
, "Failed to allocate manager object: %m");
1182 (void) manager_parse_config_file(m
);
1184 r
= manager_startup(m
);
1186 return log_error_errno(r
, "Failed to fully start up daemon: %m");
1188 log_debug("systemd-logind running as pid "PID_FMT
, getpid_cached());
1189 (void) sd_notify(false,
1191 "STATUS=Processing requests...");
1195 log_debug("systemd-logind stopped as pid "PID_FMT
, getpid_cached());
1196 (void) sd_notify(false,
1198 "STATUS=Shutting down...");
1203 DEFINE_MAIN_FUNCTION(run
);