HALF ECHO LEARN / REFERENCE COLLECTION
v262.2src/login/logind.c
Git blob: 566bc649ae14f47d892498e83683e7a4f17ed944
Lines 1001–1408 of 1,408
return r;
(void) sd_device_monitor_set_description(m->device_monitor, "input,graphics,drm");
r = sd_device_monitor_new(&m->device_uaccess_monitor);
if (r < 0)
return r;
r = sd_device_monitor_filter_add_match_tag(m->device_uaccess_monitor, "uaccess");
if (r < 0)
return r;
r = sd_device_monitor_attach_event(m->device_uaccess_monitor, m->event);
if (r < 0)
return r;
r = sd_device_monitor_start(m->device_uaccess_monitor, manager_dispatch_uaccess_udev, m);
if (r < 0)
return r;
(void) sd_device_monitor_set_description(m->device_uaccess_monitor, "uaccess");
/* Don't watch keys if nobody cares */
if (!manager_all_buttons_ignored(m)) {
r = sd_device_monitor_new(&m->device_button_monitor);
if (r < 0)
return r;
r = sd_device_monitor_filter_add_match_tag(m->device_button_monitor, "power-switch");
if (r < 0)
return r;
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_button_monitor, "input", NULL);
if (r < 0)
return r;
r = sd_device_monitor_attach_event(m->device_button_monitor, m->event);
if (r < 0)
return r;
r = sd_device_monitor_start(m->device_button_monitor, manager_dispatch_button_udev, m);
if (r < 0)
return r;
(void) sd_device_monitor_set_description(m->device_button_monitor, "button");
}
/* Don't bother watching VCSA devices, if nobody cares */
if (m->n_autovts > 0 && m->console_active_fd >= 0) {
r = sd_device_monitor_new(&m->device_vcsa_monitor);
if (r < 0)
return r;
r = sd_device_monitor_filter_add_match_subsystem_devtype(m->device_vcsa_monitor, "vc", NULL);
if (r < 0)
return r;
r = sd_device_monitor_attach_event(m->device_vcsa_monitor, m->event);
if (r < 0)
return r;
r = sd_device_monitor_start(m->device_vcsa_monitor, manager_dispatch_vcsa_udev, m);
if (r < 0)
return r;
(void) sd_device_monitor_set_description(m->device_vcsa_monitor, "vcsa");
}
return 0;
}
static void manager_gc(Manager *m, bool drop_not_started) {
Seat *seat;
Session *session;
User *user;
assert(m);
while ((seat = LIST_POP(gc_queue, m->seat_gc_queue))) {
seat->in_gc_queue = false;
if (seat_may_gc(seat, drop_not_started)) {
seat_stop(seat, /* force= */ false);
seat_free(seat);
}
}
while ((session = LIST_POP(gc_queue, m->session_gc_queue))) {
session->in_gc_queue = false;
/* First, if we are not closing yet, initiate stopping. */
if (session_may_gc(session, drop_not_started) &&
session_get_state(session) != SESSION_CLOSING)
(void) session_stop(session, /* force= */ false);
/* Normally, this should make the session referenced again, if it doesn't then let's get rid
* of it immediately. */
if (session_may_gc(session, drop_not_started)) {
(void) session_finalize(session);
session_free(session);
}
}
while ((user = LIST_POP(gc_queue, m->user_gc_queue))) {
user->in_gc_queue = false;
/* First step: queue stop jobs */
if (user_may_gc(user, drop_not_started))
(void) user_stop(user, false);
/* Second step: finalize user */
if (user_may_gc(user, drop_not_started)) {
(void) user_finalize(user);
user_free(user);
}
}
}
static int manager_dispatch_idle_action(sd_event_source *s, uint64_t t, void *userdata) {
Manager *m = ASSERT_PTR(userdata);
struct dual_timestamp since;
usec_t n, elapse;
bool idle;
int r;
if (m->idle_action == HANDLE_IGNORE ||
m->idle_action_usec <= 0)
return 0;
n = now(CLOCK_MONOTONIC);
idle = manager_get_idle_hint(m, &since);
if (!idle) {
/* Not idle. Let's check if after a timeout it might be idle then. */
elapse = n + m->idle_action_usec;
m->was_idle = false;
} else {
/* Idle! Let's see if it's time to do something, or if
* we shall sleep for longer. */
if (n >= since.monotonic + m->idle_action_usec &&
(m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
bool is_edge = false;
/* We weren't idle previously or some activity happened while we were sleeping, and now we are
* idle. Let's remember that for the next time and make this an edge transition. */
if (!m->was_idle || since.monotonic >= m->idle_action_not_before_usec) {
is_edge = true;
m->was_idle = true;
}
if (m->idle_action == HANDLE_LOCK && !is_edge)
/* We are idle and we were before so we are actually not taking any action. */
log_debug("System idle.");
else
log_info("System idle. Will %s now.", handle_action_verb_to_string(m->idle_action));
manager_handle_action(m, /* inhibit_key= */ 0, m->idle_action, /* ignore_inhibited= */ false, is_edge, /* action_seat= */ NULL);
m->idle_action_not_before_usec = n;
}
elapse = MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec;
}
if (!m->idle_action_event_source) {
r = sd_event_add_time(
m->event,
&m->idle_action_event_source,
CLOCK_MONOTONIC,
elapse, MIN(USEC_PER_SEC*30, m->idle_action_usec), /* accuracy of 30s, but don't have an accuracy lower than the idle action timeout */
manager_dispatch_idle_action, m);
if (r < 0)
return log_error_errno(r, "Failed to add idle event source: %m");
r = sd_event_source_set_priority(m->idle_action_event_source, SD_EVENT_PRIORITY_IDLE+10);
if (r < 0)
return log_error_errno(r, "Failed to set idle event source priority: %m");
} else {
r = sd_event_source_set_time(m->idle_action_event_source, elapse);
if (r < 0)
return log_error_errno(r, "Failed to set idle event timer: %m");
r = sd_event_source_set_enabled(m->idle_action_event_source, SD_EVENT_ONESHOT);
if (r < 0)
return log_error_errno(r, "Failed to enable idle event timer: %m");
}
return 0;
}
static int manager_dispatch_reload_signal(sd_event_source *s, const struct signalfd_siginfo *si, void *userdata) {
Manager *m = userdata;
int r;
(void) notify_reloading();
manager_reset_config(m);
r = manager_parse_config_file(m);
if (r < 0)
log_warning_errno(r, "Failed to parse config file, using defaults: %m");
else
log_info("Config file reloaded.");
(void) sd_notify(/* unset_environment= */ false, NOTIFY_READY_MESSAGE);
return 0;
}
static int manager_startup(Manager *m) {
_cleanup_close_ int varlink_fd = -EBADF;
int r;
Seat *seat;
Session *session;
User *user;
Button *button;
Inhibitor *inhibitor;
assert(m);
r = sd_event_add_signal(m->event, /* ret= */ NULL, SIGHUP|SD_EVENT_SIGNAL_PROCMASK, manager_dispatch_reload_signal, m);
if (r < 0)
return log_error_errno(r, "Failed to register SIGHUP handler: %m");
/* Connect to utmp */
manager_connect_utmp(m);
/* Connect to console */
r = manager_connect_console(m);
if (r < 0)
return r;
/* Connect to udev */
r = manager_connect_udev(m);
if (r < 0)
return log_error_errno(r, "Failed to create udev watchers: %m");
/* Connect to the bus */
r = manager_connect_bus(m);
if (r < 0)
return r;
/* Instantiate magic seat 0 */
r = manager_add_seat(m, "seat0", &m->seat0);
if (r < 0)
return log_error_errno(r, "Failed to add seat0: %m");
r = manager_set_lid_switch_ignore(m, 0 + m->holdoff_timeout_usec);
if (r < 0)
log_warning_errno(r, "Failed to set up lid switch ignore event source: %m");
/* Deserialize state */
r = manager_enumerate_devices(m);
if (r < 0)
log_warning_errno(r, "Device enumeration failed: %m");
r = manager_enumerate_seats(m);
if (r < 0)
log_warning_errno(r, "Seat enumeration failed: %m");
r = manager_enumerate_users(m);
if (r < 0)
log_warning_errno(r, "User enumeration failed: %m");
r = manager_enumerate_sessions(m);
if (r < 0)
log_warning_errno(r, "Session enumeration failed: %m");
r = manager_enumerate_fds(m, &varlink_fd);
if (r < 0)
log_warning_errno(r, "File descriptor enumeration failed: %m");
r = manager_enumerate_inhibitors(m);
if (r < 0)
log_warning_errno(r, "Inhibitor enumeration failed: %m");
r = manager_enumerate_buttons(m);
if (r < 0)
log_warning_errno(r, "Button enumeration failed: %m");
r = manager_varlink_init(m, TAKE_FD(varlink_fd));
if (r < 0)
return r;
manager_load_scheduled_shutdown(m);
/* Remove stale objects before we start them */
manager_gc(m, false);
/* Reserve the special reserved VT */
manager_reserve_vt(m);
/* Read in utmp if it exists */
manager_read_utmp(m);
/* And start everything */
HASHMAP_FOREACH(seat, m->seats)
(void) seat_start(seat);
HASHMAP_FOREACH(user, m->users)
(void) user_start(user);
HASHMAP_FOREACH(session, m->sessions)
(void) session_start(session, NULL, NULL);
HASHMAP_FOREACH(inhibitor, m->inhibitors) {
(void) inhibitor_start(inhibitor);
/* Let's see if the inhibitor is dead now, then remove it */
if (inhibitor_is_orphan(inhibitor)) {
inhibitor_stop(inhibitor);
inhibitor_free(inhibitor);
}
}
HASHMAP_FOREACH(button, m->buttons)
button_check_switches(button);
manager_dispatch_idle_action(NULL, 0, m);
return 0;
}
static int manager_run(Manager *m) {
int r;
assert(m);
for (;;) {
r = sd_event_get_state(m->event);
if (r < 0)
return r;
if (r == SD_EVENT_FINISHED)
return 0;
manager_gc(m, true);
r = manager_dispatch_delayed(m, false);
if (r < 0)
return r;
if (r > 0)
continue;
r = sd_event_run(m->event, UINT64_MAX);
if (r < 0)
return r;
}
}
COMMAND(
"systemd-logind\0",
"Manage user logins and devices and privileged operations.",
.man_pages = "systemd-logind.service(8)\0",
.option_namespace = "service",
.option_groups =
"Options\0"
"Bus introspection\0",
);
static int run(int argc, char *argv[]) {
_cleanup_(manager_freep) Manager *m = NULL;
_unused_ _cleanup_(notify_on_cleanup) const char *notify_message = NULL;
int r;
LIBACL_NOTE(recommended);
LIBBLKID_NOTE(recommended);
LIBSELINUX_NOTE(recommended);
log_set_facility(LOG_AUTH);
log_setup();
r = service_parse_argv(BUS_IMPLEMENTATIONS(&manager_object,
&log_control_object),
/* runtime_scope= */ NULL,
argc, argv);
if (r <= 0)
return r;
umask(0022);
r = mac_init();
if (r < 0)
return r;
/* Always create the directories people can create inotify watches in. Note that some applications
* might check for the existence of /run/systemd/seats/ to determine whether logind is available, so
* please always make sure these directories are created early on and unconditionally. */
(void) mkdir_label("/run/systemd/seats", 0755);
(void) mkdir_label("/run/systemd/users", 0755);
(void) mkdir_label("/run/systemd/sessions", 0755);
r = manager_new(&m);
if (r < 0)
return log_error_errno(r, "Failed to allocate manager object: %m");
(void) manager_parse_config_file(m);
r = manager_startup(m);
if (r < 0)
return log_error_errno(r, "Failed to fully start up daemon: %m");
notify_message = notify_start(NOTIFY_READY_MESSAGE, NOTIFY_STOPPING_MESSAGE);
return manager_run(m);
}
DEFINE_MAIN_FUNCTION(run);