logind.c revision 2b3ab29de466ae6bd7c3243a5a48c7291cc2af0a
/*-*- 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 <libudev.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <systemd/sd-daemon.h>
#include "logind.h"
#include "dbus-common.h"
#include "dbus-loop.h"
#include "strv.h"
#include "conf-parser.h"
#include "mkdir.h"
Manager *manager_new(void) {
Manager *m;
if (!m)
return NULL;
m->console_active_fd = -1;
m->bus_fd = -1;
m->udev_seat_fd = -1;
m->udev_vcsa_fd = -1;
m->udev_button_fd = -1;
m->epoll_fd = -1;
m->reserve_vt_fd = -1;
m->n_autovts = 6;
m->reserve_vt = 6;
m->lid_switch_ignore_inhibited = true;
m->idle_action_fd = -1;
m->idle_action = HANDLE_IGNORE;
if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->busnames ||
!m->user_units || !m->session_units ||
manager_free(m);
return NULL;
}
if (!m->kill_exclude_users) {
manager_free(m);
return NULL;
}
if (!m->udev) {
manager_free(m);
return NULL;
}
return m;
}
void manager_free(Manager *m) {
User *u;
Device *d;
Seat *s;
Inhibitor *i;
Button *b;
char *n;
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);
while ((n = hashmap_first(m->busnames)))
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->busnames);
hashmap_free(m->user_units);
hashmap_free(m->session_fds);
hashmap_free(m->button_fds);
if (m->console_active_fd >= 0)
if (m->udev_seat_monitor)
if (m->udev_device_monitor)
if (m->udev_vcsa_monitor)
if (m->udev_button_monitor)
if (m->udev)
udev_unref(m->udev);
if (m->bus) {
dbus_connection_flush(m->bus);
dbus_connection_close(m->bus);
dbus_connection_unref(m->bus);
}
if (m->bus_fd >= 0)
if (m->epoll_fd >= 0)
if (m->reserve_vt_fd >= 0)
if (m->idle_action_fd >= 0)
strv_free(m->kill_only_users);
free(m->action_job);
free(m);
}
int manager_enumerate_devices(Manager *m) {
struct udev_enumerate *e;
int r;
assert(m);
/* Loads devices from udev and creates seats for them as
* necessary */
e = udev_enumerate_new(m->udev);
if (!e) {
r = -ENOMEM;
goto finish;
}
r = udev_enumerate_add_match_tag(e, "master-of-seat");
if (r < 0)
goto finish;
r = udev_enumerate_scan_devices(e);
if (r < 0)
goto finish;
struct udev_device *d;
int k;
if (!d) {
r = -ENOMEM;
goto finish;
}
k = manager_process_seat_device(m, d);
if (k < 0)
r = k;
}
if (e)
return r;
}
int manager_enumerate_buttons(Manager *m) {
struct udev_enumerate *e;
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) {
r = -ENOMEM;
goto finish;
}
r = udev_enumerate_add_match_subsystem(e, "input");
if (r < 0)
goto finish;
r = udev_enumerate_add_match_tag(e, "power-switch");
if (r < 0)
goto finish;
r = udev_enumerate_scan_devices(e);
if (r < 0)
goto finish;
struct udev_device *d;
int k;
if (!d) {
r = -ENOMEM;
goto finish;
}
k = manager_process_button_device(m, d);
if (k < 0)
r = k;
}
if (e)
return r;
}
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;
return -errno;
}
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;
return -errno;
}
int k;
if (!dirent_is_file(de))
continue;
if (k < 0) {
r = k;
}
}
return r;
}
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;
return -errno;
}
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;
}
int manager_enumerate_sessions(Manager *m) {
int r = 0;
assert(m);
/* Read in session data stored on disk */
if (!d) {
return 0;
return -errno;
}
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;
}
int manager_enumerate_inhibitors(Manager *m) {
int r = 0;
assert(m);
if (!d) {
return 0;
return -errno;
}
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;
}
int manager_dispatch_seat_udev(Manager *m) {
struct udev_device *d;
int r;
assert(m);
if (!d)
return -ENOMEM;
r = manager_process_seat_device(m, d);
return r;
}
static int manager_dispatch_device_udev(Manager *m) {
struct udev_device *d;
int r;
assert(m);
if (!d)
return -ENOMEM;
r = manager_process_seat_device(m, d);
return r;
}
int manager_dispatch_vcsa_udev(Manager *m) {
struct udev_device *d;
int r = 0;
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. */
r = seat_preallocate_vts(m->seat0);
return r;
}
int manager_dispatch_button_udev(Manager *m) {
struct udev_device *d;
int r;
assert(m);
if (!d)
return -ENOMEM;
r = manager_process_button_device(m, d);
return r;
}
int manager_dispatch_console(Manager *m) {
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 */
log_warning("Failed to pin reserved VT: %m");
return -errno;
}
return 0;
}
Session *s;
Inhibitor *i;
Button *b;
assert_se(m);
if (s) {
session_stop(s);
return;
}
if (i) {
inhibitor_stop(i);
inhibitor_free(i);
return;
}
if (b) {
button_process(b);
return;
}
assert_not_reached("Got event for unknown fd");
}
static int manager_connect_bus(Manager *m) {
int r;
struct epoll_event ev = {
};
assert(m);
if (!m->bus) {
r = -ECONNREFUSED;
goto fail;
}
if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/login1", &bus_manager_vtable, m) ||
!dbus_connection_register_fallback(m->bus, "/org/freedesktop/login1/session", &bus_session_vtable, m) ||
r = log_oom();
goto fail;
}
"type='signal',"
"member='NameOwnerChanged',"
&error);
if (dbus_error_is_set(&error)) {
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='JobRemoved',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='UnitRemoved',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.DBus.Properties',"
"member='PropertiesChanged'",
&error);
if (dbus_error_is_set(&error)) {
}
"type='signal',"
"sender='org.freedesktop.systemd1',"
"interface='org.freedesktop.systemd1.Manager',"
"member='Reloading',"
"path='/org/freedesktop/systemd1'",
&error);
if (dbus_error_is_set(&error)) {
}
m->bus,
"org.freedesktop.systemd1",
"/org/freedesktop/systemd1",
"org.freedesktop.systemd1.Manager",
"Subscribe",
NULL,
&error,
if (r < 0) {
}
if (dbus_error_is_set(&error)) {
r = -EIO;
goto fail;
}
if (r != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
log_error("Failed to acquire name.");
r = -EEXIST;
goto fail;
}
if (m->bus_fd < 0) {
r = m->bus_fd;
goto fail;
}
goto fail;
return 0;
fail:
return r;
}
static int manager_connect_console(Manager *m) {
struct epoll_event ev = {
.events = 0,
};
assert(m);
assert(m->console_active_fd < 0);
/* On certain architectures (S390 and Xen, and containers),
it. */
m->console_active_fd = -1;
return 0;
}
if (m->console_active_fd < 0) {
return 0;
return -errno;
}
return -errno;
return 0;
}
static int manager_connect_udev(Manager *m) {
int r;
struct epoll_event ev = {
};
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;
return -errno;
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;
return -errno;
/* 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) {
if (!m->udev_button_monitor)
return -ENOMEM;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return -errno;
}
/* 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;
return -errno;
}
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;
}
}
while ((user = m->user_gc_queue)) {
user->in_gc_queue = false;
}
}
}
int manager_dispatch_idle_action(Manager *m) {
struct dual_timestamp since;
struct itimerspec its = {};
int r;
usec_t n;
assert(m);
if (m->idle_action == HANDLE_IGNORE ||
m->idle_action_usec <= 0) {
r = 0;
goto finish;
}
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. */
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;
}
timespec_store(&its.it_value, MAX(since.monotonic, m->idle_action_not_before_usec) + m->idle_action_usec);
}
if (m->idle_action_fd < 0) {
struct epoll_event ev = {
};
if (m->idle_action_fd < 0) {
log_error("Failed to create idle action timer: %m");
r = -errno;
goto finish;
}
log_error("Failed to add idle action timer to epoll: %m");
r = -errno;
goto finish;
}
}
log_error("Failed to reset timerfd: %m");
r = -errno;
goto finish;
}
return 0;
if (m->idle_action_fd >= 0) {
m->idle_action_fd = -1;
}
return r;
}
int manager_startup(Manager *m) {
int r;
Iterator i;
assert(m);
if (m->epoll_fd < 0)
return -errno;
/* 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 r;
/* Connect to the bus */
r = manager_connect_bus(m);
if (r < 0)
return r;
/* Instantiate magic seat 0 */
if (r < 0)
return r;
/* Deserialize state */
r = manager_enumerate_devices(m);
if (r < 0)
r = manager_enumerate_seats(m);
if (r < 0)
r = manager_enumerate_users(m);
if (r < 0)
r = manager_enumerate_sessions(m);
if (r < 0)
r = manager_enumerate_inhibitors(m);
if (r < 0)
r = manager_enumerate_buttons(m);
if (r < 0)
/* Remove stale objects before we start them */
manager_gc(m, false);
/* Reserve the special reserved VT */
/* And start everything */
return 0;
}
static int manager_recheck_buttons(Manager *m) {
Iterator i;
Button *b;
int r = 0;
assert(m);
HASHMAP_FOREACH(b, m->buttons, i) {
int q;
q = button_recheck(b);
if (q > 0)
return 1;
if (q < 0)
r = q;
}
return r;
}
int manager_run(Manager *m) {
assert(m);
for (;;) {
struct epoll_event event;
int n;
int msec = -1;
manager_gc(m, true);
if (manager_dispatch_delayed(m) > 0)
continue;
if (manager_recheck_buttons(m) > 0)
continue;
continue;
manager_gc(m, true);
if (m->action_what != 0 && !m->action_job) {
usec_t x, y;
x = now(CLOCK_MONOTONIC);
y = m->action_timestamp + m->inhibit_delay_max;
msec = x >= y ? 0 : (int) ((y - x) / USEC_PER_MSEC);
}
if (n < 0) {
continue;
log_error("epoll() failed: %m");
return -errno;
}
if (n == 0)
continue;
case FD_SEAT_UDEV:
break;
case FD_DEVICE_UDEV:
break;
case FD_VCSA_UDEV:
break;
case FD_BUTTON_UDEV:
break;
case FD_CONSOLE:
break;
case FD_IDLE_ACTION:
break;
case FD_BUS:
bus_loop_dispatch(m->bus_fd);
break;
default:
}
}
return 0;
}
static int manager_parse_config_file(Manager *m) {
static const char fn[] = "/etc/systemd/logind.conf";
int r;
assert(m);
if (!f) {
return 0;
return -errno;
}
(void*) logind_gperf_lookup, false, false, m);
if (r < 0)
return r;
}
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) {
goto finish;
}
sd_notify(false,
"READY=1\n"
"STATUS=Processing requests...");
r = manager_run(m);
sd_notify(false,
"STATUS=Shutting down...");
if (m)
manager_free(m);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}