logind.c revision 669241a076108e0483d7d8475beaa506106d077e
/*-*- 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 <pwd.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"
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->n_autovts = 6;
m->handle_lid_switch = HANDLE_OFF;
!m->user_cgroups || !m->session_cgroups ||
manager_free(m);
return NULL;
}
if (!m->reset_controllers || !m->kill_exclude_users) {
manager_free(m);
return NULL;
}
if (!m->udev) {
manager_free(m);
return NULL;
}
if (cg_get_user_path(&m->cgroup_path) < 0) {
manager_free(m);
return NULL;
}
return m;
}
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_cgroups);
hashmap_free(m->session_fds);
hashmap_free(m->button_fds);
if (m->console_active_fd >= 0)
if (m->udev_seat_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)
strv_free(m->controllers);
strv_free(m->kill_only_users);
free(m->cgroup_path);
free(m);
}
Device *d;
assert(m);
if (d) {
if (_device)
*_device = d;
return 0;
}
d = device_new(m, sysfs);
if (!d)
return -ENOMEM;
if (_device)
*_device = d;
return 0;
}
Seat *s;
assert(m);
if (s) {
if (_seat)
*_seat = s;
return 0;
}
if (!s)
return -ENOMEM;
if (_seat)
*_seat = s;
return 0;
}
Session *s;
assert(m);
if (s) {
if (_session)
*_session = s;
return 0;
}
s = session_new(m, u, id);
if (!s)
return -ENOMEM;
if (_session)
*_session = s;
return 0;
}
User *u;
assert(m);
if (u) {
if (_user)
*_user = u;
return 0;
}
if (!u)
return -ENOMEM;
if (_user)
*_user = u;
return 0;
}
int r;
assert(m);
if (r < 0)
return r;
}
struct passwd *p;
assert(m);
errno = 0;
if (!p)
}
Inhibitor *i;
assert(m);
if (i) {
if (_inhibitor)
*_inhibitor = i;
return 0;
}
i = inhibitor_new(m, id);
if (!i)
return -ENOMEM;
if (_inhibitor)
*_inhibitor = i;
return 0;
}
Button *b;
assert(m);
if (b) {
if (_button)
*_button = b;
return 0;
}
b = button_new(m, name);
if (!b)
return -ENOMEM;
if (_button)
*_button = b;
return 0;
}
int r;
assert(m);
if (!device)
return 0;
} else {
const char *sn;
sn = "seat0";
if (!seat_name_is_valid(sn)) {
return 0;
}
if (r < 0)
return r;
if (r < 0) {
return r;
}
}
return 0;
}
Button *b;
int r;
assert(m);
if (!b)
return 0;
button_free(b);
} else {
const char *sn;
r = manager_add_button(m, udev_device_get_sysname(d), &b);
if (r < 0)
return r;
sn = "seat0";
button_set_seat(b, sn);
button_open(b);
}
return 0;
}
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_subsystem(e, "graphics");
if (r < 0)
goto finish;
r = udev_enumerate_add_match_tag(e, "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_OFF &&
m->handle_sleep_key == HANDLE_OFF &&
m->handle_lid_switch == HANDLE_OFF)
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) {
DIR *d;
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;
}
closedir(d);
return r;
}
static int manager_enumerate_users_from_cgroup(Manager *m) {
int r = 0, k;
char *name;
DIR *d;
if (r < 0) {
if (r == -ENOENT)
return 0;
return r;
}
while ((k = cg_read_subgroup(d, &name)) > 0) {
if (k < 0) {
r = k;
continue;
}
if (!user->cgroup_path)
r = -ENOMEM;
break;
}
}
if (r >= 0 && k < 0)
r = k;
closedir(d);
return r;
}
static int manager_enumerate_linger_users(Manager *m) {
DIR *d;
int r = 0;
if (!d) {
return 0;
return -errno;
}
int k;
if (!dirent_is_file(de))
continue;
if (k < 0) {
r = k;
}
}
closedir(d);
return r;
}
int manager_enumerate_users(Manager *m) {
DIR *d;
int r, k;
assert(m);
/* First, enumerate user cgroups */
/* Second, add lingering users on top */
k = manager_enumerate_linger_users(m);
if (k < 0)
r = k;
/* Third, read in user data stored on disk */
if (!d) {
return 0;
return -errno;
}
User *u;
if (!dirent_is_file(de))
continue;
if (k < 0) {
continue;
}
if (!u) {
continue;
}
k = user_load(u);
if (k < 0)
r = k;
}
closedir(d);
return r;
}
static int manager_enumerate_sessions_from_cgroup(Manager *m) {
User *u;
Iterator i;
int r = 0;
HASHMAP_FOREACH(u, m->users, i) {
DIR *d;
char *name;
int k;
if (!u->cgroup_path)
continue;
if (k < 0) {
if (k == -ENOENT)
continue;
r = k;
continue;
}
while ((k = cg_read_subgroup(d, &name)) > 0) {
continue;
if (k < 0) {
break;
}
if (!session->cgroup_path)
k = -ENOMEM;
break;
}
}
closedir(d);
if (k < 0)
r = k;
}
return r;
}
int manager_enumerate_sessions(Manager *m) {
DIR *d;
int r = 0;
assert(m);
/* First enumerate session cgroups */
/* Second, read in session data stored on disk */
if (!d) {
return 0;
return -errno;
}
struct Session *s;
int k;
if (!dirent_is_file(de))
continue;
if (!s) {
continue;
}
k = session_load(s);
if (k < 0)
r = k;
}
closedir(d);
return r;
}
int manager_enumerate_inhibitors(Manager *m) {
DIR *d;
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;
}
closedir(d);
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;
}
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->vtconsole);
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);
if (m->vtconsole)
return 0;
}
static int vt_is_busy(int vtnr) {
int r = 0, fd;
* we'd open the latter we'd open the foreground tty which
* we avoid this. Since tty1 is special and needs to be an
* explicitly loaded getty or DM this is safe. */
if (fd < 0)
return -errno;
r = -errno;
else
return r;
}
int r;
const char *mode = "fail";
assert(m);
return 0;
r = vt_is_busy(vtnr);
if (r < 0)
return r;
else if (r > 0)
return -EBUSY;
message = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit");
if (!message) {
log_error("Could not allocate message.");
r = -ENOMEM;
goto finish;
}
log_error("Could not allocate service name.");
r = -ENOMEM;
goto finish;
}
log_error("Could not attach target and flag information to message.");
r = -ENOMEM;
goto finish;
}
if (!reply) {
goto finish;
}
r = 0;
if (message)
if (reply)
return r;
}
Session *s;
char *p;
assert(m);
if (s) {
*session = s;
return 1;
}
if (!p) {
log_error("Out of memory.");
return -ENOMEM;
}
for (;;) {
char *e;
e = strrchr(p, '/');
if (!e || e == p) {
free(p);
return 0;
}
*e = 0;
s = hashmap_get(m->session_cgroups, p);
if (s) {
free(p);
*session = s;
return 1;
}
}
}
User *u;
char *p;
assert(m);
if (u) {
*user = u;
return 1;
}
if (!p) {
log_error("Out of memory.");
return -ENOMEM;
}
for (;;) {
char *e;
e = strrchr(p, '/');
if (!e || e == p) {
free(p);
return 0;
}
*e = 0;
u = hashmap_get(m->user_cgroups, p);
if (u) {
free(p);
*user = u;
return 1;
}
}
}
char *p;
int r;
assert(m);
if (r < 0)
return r;
r = manager_get_session_by_cgroup(m, p, session);
free(p);
return r;
}
Session *s;
User *u;
int r;
r = manager_get_session_by_cgroup(m, cgroup, &s);
if (r > 0)
r = manager_get_user_by_cgroup(m, cgroup, &u);
if (r > 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) ||
log_error("Out of memory.");
r = -ENOMEM;
goto fail;
}
"type='signal',"
"interface='org.freedesktop.systemd1.Agent',"
"member='Released',"
"path='/org/freedesktop/systemd1/agent'",
&error);
if (dbus_error_is_set(&error)) {
r = -EIO;
goto fail;
}
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;
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) {
struct epoll_event ev;
int r;
assert(m);
assert(!m->udev_seat_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;
if (r < 0)
return r;
return -errno;
/* Don't watch keys if nobody cares */
if (m->handle_power_key != HANDLE_OFF ||
m->handle_sleep_key != HANDLE_OFF ||
m->handle_lid_switch != HANDLE_OFF) {
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;
}
}
}
Session *s;
bool idle_hint;
dual_timestamp ts = { 0, 0 };
Iterator i;
assert(m);
HASHMAP_FOREACH(s, m->sessions, i) {
int ih;
ih = session_get_idle_hint(s, &k);
if (ih < 0)
return ih;
if (!ih) {
if (!idle_hint) {
ts = k;
} else {
idle_hint = false;
ts = k;
}
} else if (idle_hint) {
ts = k;
}
}
if (t)
*t = ts;
return idle_hint;
}
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 */
/* Remove stale objects before we start them */
manager_gc(m, false);
/* And start everything */
return 0;
}
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;
continue;
manager_gc(m, true);
if (m->delayed_unit) {
usec_t x, y;
x = now(CLOCK_MONOTONIC);
y = m->delayed_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_VCSA_UDEV:
break;
case FD_BUTTON_UDEV:
break;
case FD_CONSOLE:
break;
case FD_BUS:
bus_loop_dispatch(m->bus_fd);
break;
default:
}
}
return 0;
}
static int manager_parse_config_file(Manager *m) {
FILE *f;
const char *fn;
int r;
assert(m);
fn = "/etc/systemd/logind.conf";
if (!f) {
return 0;
return -errno;
}
if (r < 0)
fclose(f);
return r;
}
int r;
log_open();
umask(0022);
if (argc != 1) {
log_error("This program takes no arguments.");
r = -EINVAL;
goto finish;
}
m = manager_new();
if (!m) {
log_error("Out of memory.");
r = -ENOMEM;
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;
}