logind.c revision 7586f4d172dd9c3ccc3126fc47dca9e49adec132
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2011 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include "libudev.h"
#include "sd-daemon.h"
#include "alloc-util.h"
#include "bus-error.h"
#include "bus-util.h"
#include "conf-parser.h"
#include "def.h"
#include "dirent-util.h"
#include "fd-util.h"
#include "formats-util.h"
#include "logind.h"
#include "signal-util.h"
#include "strv.h"
#include "udev-util.h"
static void manager_free(Manager *m);
static Manager *manager_new(void) {
Manager *m;
int r;
if (!m)
return NULL;
m->console_active_fd = -1;
m->reserve_vt_fd = -1;
m->n_autovts = 6;
m->reserve_vt = 6;
m->remove_ipc = true;
m->lid_switch_ignore_inhibited = true;
m->idle_action = HANDLE_IGNORE;
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units)
goto fail;
if (!m->kill_exclude_users)
goto fail;
if (!m->udev)
goto fail;
r = sd_event_default(&m->event);
if (r < 0)
goto fail;
sd_event_set_watchdog(m->event, true);
return m;
fail:
manager_free(m);
return NULL;
}
static void manager_free(Manager *m) {
User *u;
Device *d;
Seat *s;
Inhibitor *i;
Button *b;
assert(m);
while ((u = hashmap_first(m->users)))
user_free(u);
while ((d = hashmap_first(m->devices)))
device_free(d);
while ((s = hashmap_first(m->seats)))
seat_free(s);
while ((i = hashmap_first(m->inhibitors)))
inhibitor_free(i);
while ((b = hashmap_first(m->buttons)))
button_free(b);
hashmap_free(m->devices);
hashmap_free(m->seats);
hashmap_free(m->sessions);
hashmap_free(m->users);
hashmap_free(m->inhibitors);
hashmap_free(m->buttons);
hashmap_free(m->user_units);
udev_unref(m->udev);
if (m->unlink_nologin)
sd_bus_unref(m->bus);
sd_event_unref(m->event);
safe_close(m->reserve_vt_fd);
strv_free(m->kill_only_users);
free(m->wall_message);
free(m->action_job);
free(m);
}
static int manager_enumerate_devices(Manager *m) {
int r;
assert(m);
/* Loads devices from udev and creates seats for them as
* necessary */
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_tag(e, "master-of-seat");
if (r < 0)
return r;
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
int k;
if (!d)
return -ENOMEM;
k = manager_process_seat_device(m, d);
if (k < 0)
r = k;
}
return r;
}
static int manager_enumerate_buttons(Manager *m) {
int r;
assert(m);
/* Loads buttons from udev */
if (m->handle_power_key == HANDLE_IGNORE &&
m->handle_suspend_key == HANDLE_IGNORE &&
m->handle_hibernate_key == HANDLE_IGNORE &&
m->handle_lid_switch == HANDLE_IGNORE &&
return 0;
e = udev_enumerate_new(m->udev);
if (!e)
return -ENOMEM;
r = udev_enumerate_add_match_subsystem(e, "input");
if (r < 0)
return r;
r = udev_enumerate_add_match_tag(e, "power-switch");
if (r < 0)
return r;
if (r < 0)
return r;
r = udev_enumerate_scan_devices(e);
if (r < 0)
return r;
int k;
if (!d)
return -ENOMEM;
k = manager_process_button_device(m, d);
if (k < 0)
r = k;
}
return r;
}
static int manager_enumerate_seats(Manager *m) {
int r = 0;
assert(m);
/* This loads data about seats stored on disk, but does not
* actually create any seats. Removes data of seats that no
* longer exist. */
if (!d) {
return 0;
}
Seat *s;
int k;
if (!dirent_is_file(de))
continue;
if (!s) {
continue;
}
k = seat_load(s);
if (k < 0)
r = k;
}
return r;
}
static int manager_enumerate_linger_users(Manager *m) {
int r = 0;
assert(m);
if (!d) {
return 0;
}
int k;
if (!dirent_is_file(de))
continue;
if (k < 0) {
r = k;
}
}
return r;
}
static int manager_enumerate_users(Manager *m) {
int r, k;
assert(m);
/* Add lingering users */
r = manager_enumerate_linger_users(m);
/* Read in user data stored on disk */
if (!d) {
return 0;
}
User *u;
if (!dirent_is_file(de))
continue;
if (k < 0) {
r = k;
continue;
}
k = user_load(u);
if (k < 0)
r = k;
}
return r;
}
static int manager_enumerate_sessions(Manager *m) {
int r = 0;
assert(m);
/* Read in session data stored on disk */
if (!d) {
return 0;
}
struct Session *s;
int k;
if (!dirent_is_file(de))
continue;
r = -EINVAL;
continue;
}
if (k < 0) {
r = k;
continue;
}
k = session_load(s);
if (k < 0)
r = k;
}
return r;
}
static int manager_enumerate_inhibitors(Manager *m) {
int r = 0;
assert(m);
if (!d) {
return 0;
}
int k;
Inhibitor *i;
if (!dirent_is_file(de))
continue;
if (k < 0) {
r = k;
continue;
}
k = inhibitor_load(i);
if (k < 0)
r = k;
}
return r;
}
static int manager_dispatch_seat_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
assert(m);
if (!d)
return -ENOMEM;
manager_process_seat_device(m, d);
return 0;
}
static int manager_dispatch_device_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
assert(m);
if (!d)
return -ENOMEM;
manager_process_seat_device(m, d);
return 0;
}
static int manager_dispatch_vcsa_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
const char *name;
assert(m);
if (!d)
return -ENOMEM;
name = udev_device_get_sysname(d);
/* Whenever a VCSA device is removed try to reallocate our
* VTs, to make sure our auto VTs never go away. */
return 0;
}
static int manager_dispatch_button_udev(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
assert(m);
if (!d)
return -ENOMEM;
return 0;
}
assert(m);
seat_read_active_vt(m->seat0);
return 0;
}
static int manager_reserve_vt(Manager *m) {
_cleanup_free_ char *p = NULL;
assert(m);
if (m->reserve_vt <= 0)
return 0;
return log_oom();
if (m->reserve_vt_fd < 0) {
/* Don't complain on VT-less systems */
return -errno;
}
return 0;
}
static int manager_connect_bus(Manager *m) {
int r;
assert(m);
r = sd_bus_default_system(&m->bus);
if (r < 0)
return log_error_errno(r, "Failed to connect to system bus: %m");
r = sd_bus_add_object_vtable(m->bus, NULL, "/org/freedesktop/login1", "org.freedesktop.login1.Manager", manager_vtable, m);
if (r < 0)
return log_error_errno(r, "Failed to add manager object vtable: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/seat", "org.freedesktop.login1.Seat", seat_vtable, seat_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add seat object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/seat", seat_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add seat enumerator: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/session", "org.freedesktop.login1.Session", session_vtable, session_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add session object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/session", session_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add session enumerator: %m");
r = sd_bus_add_fallback_vtable(m->bus, NULL, "/org/freedesktop/login1/user", "org.freedesktop.login1.User", user_vtable, user_object_find, m);
if (r < 0)
return log_error_errno(r, "Failed to add user object vtable: %m");
r = sd_bus_add_node_enumerator(m->bus, NULL, "/org/freedesktop/login1/user", user_node_enumerator, m);
if (r < 0)
return log_error_errno(r, "Failed to add user enumerator: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='JobRemoved',"
"path='/org/freedesktop/systemd1'",
match_job_removed, m);
if (r < 0)
return log_error_errno(r, "Failed to add match for JobRemoved: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='UnitRemoved',"
"path='/org/freedesktop/systemd1'",
match_unit_removed, m);
if (r < 0)
return log_error_errno(r, "Failed to add match for UnitRemoved: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
if (r < 0)
return log_error_errno(r, "Failed to add match for PropertiesChanged: %m");
r = sd_bus_add_match(m->bus,
NULL,
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='Reloading',"
"path='/org/freedesktop/systemd1'",
match_reloading, m);
if (r < 0)
return log_error_errno(r, "Failed to add match for Reloading: %m");
r = sd_bus_call_method(
m->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Subscribe",
&error,
if (r < 0) {
return r;
}
if (r < 0)
return log_error_errno(r, "Failed to register name: %m");
if (r < 0)
return log_error_errno(r, "Failed to attach bus to event loop: %m");
return 0;
}
/*
* We got a VT-switch signal and we have to acknowledge it immediately.
* Preferably, we'd just use m->seat0->active->vtfd, but unfortunately,
* old user-space might run multiple sessions on a single VT, *sigh*.
* Therefore, we have to iterate all sessions and find one with a vtfd
* on the requested VT.
* As only VTs with active controllers have VT_PROCESS set, our current
* notion of the active VT might be wrong (for instance if the switch
* happens while we setup VT_PROCESS). Therefore, read the current VT
* first and then use s->active->vtnr as reference. Note that this is
* not racy, as no further VT-switch can happen as long as we're in
* synchronous VT_PROCESS mode.
*/
seat_read_active_vt(m->seat0);
log_warning("Received VT_PROCESS signal without a registered session on that VT.");
return 0;
}
} else {
break;
}
}
}
return 0;
}
static int manager_connect_console(Manager *m) {
int r;
assert(m);
assert(m->console_active_fd < 0);
/* On certain architectures (S390 and Xen, and containers),
it. */
return 0;
if (m->console_active_fd < 0) {
return 0;
}
r = sd_event_add_io(m->event, &m->console_active_event_source, m->console_active_fd, 0, manager_dispatch_console, m);
if (r < 0) {
log_error("Failed to watch foreground console");
return r;
}
/*
* SIGRTMIN is used as global VT-release signal, SIGRTMIN + 1 is used
* as VT-acquire signal. We ignore any acquire-events (yes, we still
* have to provide a valid signal-number for it!) and acknowledge all
* release events immediately.
*/
return -EINVAL;
}
if (r < 0)
return r;
return 0;
}
static int manager_connect_udev(Manager *m) {
int r;
assert(m);
assert(!m->udev_seat_monitor);
assert(!m->udev_device_monitor);
assert(!m->udev_vcsa_monitor);
assert(!m->udev_button_monitor);
if (!m->udev_seat_monitor)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
r = sd_event_add_io(m->event, &m->udev_seat_event_source, udev_monitor_get_fd(m->udev_seat_monitor), EPOLLIN, manager_dispatch_seat_udev, m);
if (r < 0)
return r;
if (!m->udev_device_monitor)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = sd_event_add_io(m->event, &m->udev_device_event_source, udev_monitor_get_fd(m->udev_device_monitor), EPOLLIN, manager_dispatch_device_udev, m);
if (r < 0)
return r;
/* Don't watch keys if nobody cares */
if (m->handle_power_key != HANDLE_IGNORE ||
m->handle_suspend_key != HANDLE_IGNORE ||
m->handle_hibernate_key != HANDLE_IGNORE ||
m->handle_lid_switch != HANDLE_IGNORE ||
m->handle_lid_switch_docked != HANDLE_IGNORE) {
if (!m->udev_button_monitor)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
r = sd_event_add_io(m->event, &m->udev_button_event_source, udev_monitor_get_fd(m->udev_button_monitor), EPOLLIN, manager_dispatch_button_udev, m);
if (r < 0)
return r;
}
/* Don't bother watching VCSA devices, if nobody cares */
if (m->n_autovts > 0 && m->console_active_fd >= 0) {
if (!m->udev_vcsa_monitor)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
r = sd_event_add_io(m->event, &m->udev_vcsa_event_source, udev_monitor_get_fd(m->udev_vcsa_monitor), EPOLLIN, manager_dispatch_vcsa_udev, m);
if (r < 0)
return r;
}
return 0;
}
assert(m);
while ((seat = m->seat_gc_queue)) {
seat->in_gc_queue = false;
}
}
while ((session = m->session_gc_queue)) {
session->in_gc_queue = false;
/* First, if we are not closing yet, initiate stopping */
session_stop(session, false);
/* Normally, this should make the session referenced
* again, if it doesn't then let's get rid of it
* immediately */
}
}
while ((user = m->user_gc_queue)) {
user->in_gc_queue = false;
/* First step: queue stop jobs */
/* Second step: finalize user */
}
}
}
struct dual_timestamp since;
int r;
assert(m);
if (m->idle_action == HANDLE_IGNORE ||
m->idle_action_usec <= 0)
return 0;
n = now(CLOCK_MONOTONIC);
r = manager_get_idle_hint(m, &since);
if (r <= 0)
/* Not idle. Let's check if after a timeout it might be idle then. */
elapse = n + m->idle_action_usec;
else {
/* Idle! Let's see if it's time to do something, or if
* we shall sleep for longer. */
(m->idle_action_not_before_usec <= 0 || n >= m->idle_action_not_before_usec + m->idle_action_usec)) {
log_info("System idle. Taking action.");
manager_handle_action(m, 0, m->idle_action, false, false);
m->idle_action_not_before_usec = n;
}
}
if (!m->idle_action_event_source) {
r = sd_event_add_time(
m->event,
if (r < 0)
return log_error_errno(r, "Failed to add idle event source: %m");
if (r < 0)
return log_error_errno(r, "Failed to set idle event source priority: %m");
} else {
if (r < 0)
return log_error_errno(r, "Failed to set idle event timer: %m");
if (r < 0)
return log_error_errno(r, "Failed to enable idle event timer: %m");
}
return 0;
}
static int manager_startup(Manager *m) {
int r;
Iterator i;
assert(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 */
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_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");
/* Remove stale objects before we start them */
manager_gc(m, false);
/* Reserve the special reserved VT */
/* And start everything */
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;
if (r < 0)
return r;
}
}
static int manager_parse_config_file(Manager *m) {
assert(m);
"Login\0",
false, m);
}
int r;
log_open();
umask(0022);
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
/* Always create the directories people can create inotify
* watches in. Note that some applications might check for the
* logind is available, so please always make sure this check
* stays in. */
m = manager_new();
if (!m) {
r = log_oom();
goto finish;
}
r = manager_startup(m);
if (r < 0) {
log_error_errno(r, "Failed to fully start up daemon: %m");
goto finish;
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
r = manager_run(m);
sd_notify(false,
"STOPPING=1\n"
"STATUS=Shutting down...");
manager_free(m);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}