diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | Makefile.am | 24 | ||||
-rw-r--r-- | src/dbus-loop.c | 263 | ||||
-rw-r--r-- | src/dbus-loop.h | 30 | ||||
-rw-r--r-- | src/logind-device.c | 85 | ||||
-rw-r--r-- | src/logind-seat.c | 245 | ||||
-rw-r--r-- | src/logind-session.c | 472 | ||||
-rw-r--r-- | src/logind-user.c | 420 | ||||
-rw-r--r-- | src/logind.c | 886 | ||||
-rw-r--r-- | src/logind.h | 235 |
10 files changed, 2660 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore index 5669d740ba..c2820b03e2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +systemd-logind systemd-hostnamed systemd-binfmt systemd-getty-generator diff --git a/Makefile.am b/Makefile.am index 7ca0ae858f..79ccbbe6e6 100644 --- a/Makefile.am +++ b/Makefile.am @@ -158,7 +158,8 @@ rootlibexec_PROGRAMS = \ systemd-ac-power \ systemd-detect-virt \ systemd-sysctl \ - systemd-hostnamed + systemd-hostnamed \ + systemd-logind if ENABLE_BINFMT rootlibexec_PROGRAMS += \ @@ -806,6 +807,27 @@ systemd_hostnamed_LDADD = \ libsystemd-daemon.la \ $(DBUS_LIBS) +systemd_logind_SOURCES = \ + src/logind.c \ + src/logind-device.c \ + src/logind-seat.c \ + src/logind-session.c \ + src/logind-user.c \ + src/dbus-common.c \ + src/dbus-loop.c \ + src/cgroup-util.c + +systemd_logind_CFLAGS = \ + $(AM_CFLAGS) \ + $(DBUS_CFLAGS) \ + $(UDEV_CFLAGS) + +systemd_logind_LDADD = \ + libsystemd-basic.la \ + libsystemd-daemon.la \ + $(DBUS_LIBS) \ + $(UDEV_LIBS) + systemd_shutdown_SOURCES = \ src/mount-setup.c \ src/umount.c \ diff --git a/src/dbus-loop.c b/src/dbus-loop.c new file mode 100644 index 0000000000..8eb1d171a7 --- /dev/null +++ b/src/dbus-loop.c @@ -0,0 +1,263 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <stdbool.h> +#include <assert.h> +#include <sys/epoll.h> +#include <string.h> +#include <errno.h> +#include <sys/timerfd.h> +#include <unistd.h> + +#include "dbus-loop.h" +#include "dbus-common.h" +#include "util.h" + +/* Minimal implementation of the dbus loop which integrates all dbus + * events into a single epoll fd which we can triviall integrate with + * other loops. Note that this is not used in the main systemd daemon + * since we run a more elaborate mainloop there. */ + +typedef struct EpollData { + int fd; + void *object; + bool is_timeout:1; + bool fd_is_dupped:1; +} EpollData; + +static dbus_bool_t add_watch(DBusWatch *watch, void *data) { + EpollData *e; + struct epoll_event ev; + + assert(watch); + + e = new0(EpollData, 1); + if (!e) + return FALSE; + + e->fd = dbus_watch_get_unix_fd(watch); + e->object = watch; + e->is_timeout = false; + + zero(ev); + ev.events = bus_flags_to_events(watch); + ev.data.ptr = e; + + if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) { + + if (errno != EEXIST) { + free(e); + return FALSE; + } + + /* Hmm, bloody D-Bus creates multiple watches on the + * same fd. epoll() does not like that. As a dirty + * hack we simply dup() the fd and hence get a second + * one we can safely add to the epoll(). */ + + e->fd = dup(e->fd); + if (e->fd < 0) { + free(e); + return FALSE; + } + + if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) { + close_nointr_nofail(e->fd); + free(e); + return FALSE; + } + + e->fd_is_dupped = true; + } + + dbus_watch_set_data(watch, e, NULL); + + return TRUE; +} + +static void remove_watch(DBusWatch *watch, void *data) { + EpollData *e; + + assert(watch); + + e = dbus_watch_get_data(watch); + if (!e) + return; + + assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_DEL, e->fd, NULL) >= 0); + + if (e->fd_is_dupped) + close_nointr_nofail(e->fd); + + free(e); +} + +static void toggle_watch(DBusWatch *watch, void *data) { + EpollData *e; + struct epoll_event ev; + + assert(watch); + + e = dbus_watch_get_data(watch); + if (!e) + return; + + zero(ev); + ev.events = bus_flags_to_events(watch); + ev.data.ptr = e; + + assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_MOD, e->fd, &ev) == 0); +} + +static int timeout_arm(EpollData *e) { + struct itimerspec its; + + assert(e); + assert(e->is_timeout); + + zero(its); + + if (dbus_timeout_get_enabled(e->object)) { + timespec_store(&its.it_value, dbus_timeout_get_interval(e->object) * USEC_PER_MSEC); + its.it_interval = its.it_value; + } + + if (timerfd_settime(e->fd, 0, &its, NULL) < 0) + return -errno; + + return 0; +} + +static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data) { + EpollData *e; + struct epoll_event ev; + + assert(timeout); + + e = new0(EpollData, 1); + if (!e) + return FALSE; + + e->fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK|TFD_CLOEXEC); + if (e->fd < 0) + goto fail; + + e->object = timeout; + e->is_timeout = true; + + if (timeout_arm(e) < 0) + goto fail; + + zero(ev); + ev.events = EPOLLIN; + ev.data.ptr = e; + + if (epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_ADD, e->fd, &ev) < 0) + goto fail; + + dbus_timeout_set_data(timeout, e, NULL); + + return TRUE; + +fail: + if (e->fd >= 0) + close_nointr_nofail(e->fd); + + free(e); + return FALSE; +} + +static void remove_timeout(DBusTimeout *timeout, void *data) { + EpollData *e; + + assert(timeout); + + e = dbus_timeout_get_data(timeout); + if (!e) + return; + + assert_se(epoll_ctl(PTR_TO_INT(data), EPOLL_CTL_DEL, e->fd, NULL) >= 0); + close_nointr_nofail(e->fd); + free(e); +} + +static void toggle_timeout(DBusTimeout *timeout, void *data) { + EpollData *e; + int r; + + assert(timeout); + + e = dbus_timeout_get_data(timeout); + if (!e) + return; + + r = timeout_arm(e); + if (r < 0) + log_error("Failed to rearm timer: %s", strerror(-r)); +} + +int bus_loop_open(DBusConnection *c) { + int fd; + + assert(c); + + fd = epoll_create1(EPOLL_CLOEXEC); + if (fd < 0) + return -errno; + + if (!dbus_connection_set_watch_functions(c, add_watch, remove_watch, toggle_watch, INT_TO_PTR(fd), NULL) || + !dbus_connection_set_timeout_functions(c, add_timeout, remove_timeout, toggle_timeout, INT_TO_PTR(fd), NULL)) { + close_nointr_nofail(fd); + return -ENOMEM; + } + + return fd; +} + +int bus_loop_dispatch(int fd) { + int n; + struct epoll_event event; + EpollData *d; + + assert(fd >= 0); + + zero(event); + + n = epoll_wait(fd, &event, 1, 0); + if (n < 0) + return errno == EAGAIN || errno == EINTR ? 0 : -errno; + + assert_se(d = event.data.ptr); + + if (d->is_timeout) { + DBusTimeout *t = d->object; + + if (dbus_timeout_get_enabled(t)) + dbus_timeout_handle(t); + } else { + DBusWatch *w = d->object; + + if (dbus_watch_get_enabled(w)) + dbus_watch_handle(w, bus_events_to_flags(event.events)); + } + + return 0; +} diff --git a/src/dbus-loop.h b/src/dbus-loop.h new file mode 100644 index 0000000000..0bbdfe5925 --- /dev/null +++ b/src/dbus-loop.h @@ -0,0 +1,30 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#ifndef foodbusloophfoo +#define foodbusloophfoo + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <dbus/dbus.h> + +int bus_loop_open(DBusConnection *c); +int bus_loop_dispatch(int fd); + +#endif diff --git a/src/logind-device.c b/src/logind-device.c new file mode 100644 index 0000000000..9084b5fb4e --- /dev/null +++ b/src/logind-device.c @@ -0,0 +1,85 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <assert.h> +#include <string.h> + +#include "logind.h" +#include "util.h" + +Device* device_new(Manager *m, const char *sysfs) { + Device *d; + + assert(m); + assert(sysfs); + + d = new0(Device, 1); + if (!d) + return NULL; + + d->sysfs = strdup(sysfs); + if (!d->sysfs) { + free(d); + return NULL; + } + + if (hashmap_put(m->devices, d->sysfs, d) < 0) { + free(d->sysfs); + free(d); + return NULL; + } + + d->manager = m; + dual_timestamp_get(&d->timestamp); + + return d; +} + +void device_free(Device *d) { + assert(d); + + device_detach(d); + + hashmap_remove(d->manager->devices, d->sysfs); + + free(d->sysfs); + free(d); +} + +void device_detach(Device *d) { + assert(d); + + if (d->seat) + LIST_REMOVE(Device, devices, d->seat->devices, d); + + d->seat = NULL; +} + +void device_attach(Device *d, Seat *s) { + assert(d); + assert(s); + + if (d->seat) + device_detach(d); + + LIST_PREPEND(Device, devices, d->seat->devices, d); + d->seat = s; +} diff --git a/src/logind-seat.c b/src/logind-seat.c new file mode 100644 index 0000000000..dcf1c7107b --- /dev/null +++ b/src/logind-seat.c @@ -0,0 +1,245 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <assert.h> +#include <errno.h> +#include <unistd.h> +#include <fcntl.h> +#include <sys/ioctl.h> +#include <linux/vt.h> + +#include "logind.h" +#include "util.h" + +Seat *seat_new(Manager *m, const char *id) { + Seat *s; + + assert(m); + assert(id); + + s = new0(Seat, 1); + if (!s) + return NULL; + + s->state_file = strappend("/run/systemd/seat/", id); + if (!s->state_file) { + free(s); + return NULL; + } + + s->id = file_name_from_path(s->state_file); + + if (hashmap_put(m->seats, s->id, s) < 0) { + free(s->id); + free(s); + return NULL; + } + + s->manager = m; + + return s; +} + +void seat_free(Seat *s) { + assert(s); + + while (s->sessions) + session_free(s->sessions); + + assert(!s->active); + + while (s->devices) + device_free(s->devices); + + hashmap_remove(s->manager->seats, s->id); + + free(s->state_file); + free(s); +} + +int seat_save(Seat *s) { + FILE *f; + int r; + + assert(s); + + r = safe_mkdir("/run/systemd/seat", 0755, 0, 0); + if (r < 0) + return r; + + f = fopen(s->state_file, "we"); + if (!f) + return -errno; + + fprintf(f, + "IS_VTCONSOLE=%i\n", + s->manager->vtconsole == s); + + if (s->active) { + assert(s->active->user); + + fprintf(f, + "ACTIVE=%s\n" + "ACTIVE_UID=%lu\n", + s->active->id, + (unsigned long) s->active->user->uid); + } + + if (s->sessions) { + Session *i; + fputs("OTHER_UIDS=", f); + + LIST_FOREACH(sessions_by_seat, i, s->sessions) { + assert(i->user); + + if (i == s->active) + continue; + + fprintf(f, + "%s%lu", + i == s->sessions ? "" : " ", + (unsigned long) i->user->uid); + } + } + + fflush(f); + if (ferror(f)) { + r = -errno; + unlink(s->state_file); + } + + fclose(f); + return r; +} + +int seat_load(Seat *s) { + assert(s); + + return 0; +} + +static int vt_allocate(int vtnr) { + int fd, r; + char *p; + + assert(vtnr >= 1); + + if (asprintf(&p, "/dev/tty%i", vtnr) < 0) + return -ENOMEM; + + fd = open_terminal(p, O_RDWR|O_NOCTTY|O_CLOEXEC); + free(p); + + r = fd < 0 ? -errno : 0; + + if (fd >= 0) + close_nointr_nofail(fd); + + return r; +} + +int seat_preallocate_vts(Seat *s) { + int i, r = 0; + + assert(s); + assert(s->manager); + + if (s->manager->n_autovts <= 0) + return 0; + + if (s->manager->vtconsole != s) + return 0; + + for (i = 1; i < s->manager->n_autovts; i++) { + int q; + + q = vt_allocate(i); + if (r >= 0 && q < 0) + r = q; + } + + return r; +} + +int seat_apply_acls(Seat *s) { + assert(s); + + + return 0; +} + +static int vt_is_busy(int vtnr) { + struct vt_stat vt_stat; + int r = 0, fd; + + assert(vtnr >= 1); + + fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY|O_CLOEXEC); + if (fd < 0) + return -errno; + + if (ioctl(fd, VT_GETSTATE, &vt_stat) < 0) + r = -errno; + else + r = !!(vt_stat.v_state & (1 << vtnr)); + + close_nointr_nofail(fd); + + return r; +} + +void seat_active_vt_changed(Seat *s, int vtnr) { + Session *i; + + assert(s); + assert(vtnr >= 1); + assert(s->manager->vtconsole == s); + + s->active = NULL; + + LIST_FOREACH(sessions_by_seat, i, s->sessions) + if (i->vtnr == vtnr) { + s->active = i; + break; + } + + seat_apply_acls(s); + + if (vt_is_busy(vtnr) == 0) + manager_spawn_autovt(s->manager, vtnr); +} + +int seat_stop(Seat *s) { + Session *session; + int r = 0; + + assert(s); + + LIST_FOREACH(sessions_by_seat, session, s->sessions) { + int k; + + k = session_stop(session); + if (k < 0) + r = k; + } + + return r; +} diff --git a/src/logind-session.c b/src/logind-session.c new file mode 100644 index 0000000000..9af99d054a --- /dev/null +++ b/src/logind-session.c @@ -0,0 +1,472 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <errno.h> +#include <string.h> +#include <unistd.h> + +#include "logind.h" +#include "strv.h" +#include "util.h" +#include "cgroup-util.h" + +Session* session_new(Manager *m, User *u, const char *id) { + Session *s; + + assert(m); + assert(id); + + s = new(Session, 1); + if (!s) + return NULL; + + s->state_file = strappend("/run/systemd/session/", id); + if (!s->state_file) { + free(s); + return NULL; + } + + s->id = file_name_from_path(s->state_file); + + if (hashmap_put(m->sessions, s->id, s) < 0) { + free(s->id); + free(s); + return NULL; + } + + s->manager = m; + s->pipe_fd = -1; + s->user = u; + + dual_timestamp_get(&s->timestamp); + + return s; +} + +void session_free(Session *s) { + assert(s); + + if (s->user) { + LIST_REMOVE(Session, sessions_by_user, s->user->sessions, s); + + if (s->user->display == s) + s->user->display = NULL; + } + + if (s->seat) + LIST_REMOVE(Session, sessions_by_seat, s->seat->sessions, s); + + free(s->cgroup_path); + strv_free(s->controllers); + + free(s->tty); + free(s->display); + free(s->remote_host); + + hashmap_remove(s->manager->sessions, s->id); + + free(s->state_file); + free(s); +} + +int session_save(Session *s) { + FILE *f; + int r = 0; + + assert(s); + + r = safe_mkdir("/run/systemd/session", 0755, 0, 0); + if (r < 0) + return r; + + f = fopen(s->state_file, "we"); + if (!f) + return -errno; + + assert(s->user); + + fprintf(f, + "# This is private data. Do not parse.\n" + "UID=%lu\n" + "USER=%s\n" + "ACTIVE=%i\n" + "REMOTE=%i\n" + "KILL_PROCESSES=%i\n", + (unsigned long) s->user->uid, + s->user->name, + session_is_active(s), + s->remote, + s->kill_processes); + + if (s->cgroup_path) + fprintf(f, + "CGROUP=%s\n", + s->cgroup_path); + + if (s->seat) + fprintf(f, + "SEAT=%s\n", + s->seat->id); + + if (s->tty) + fprintf(f, + "TTY=%s\n", + s->tty); + + if (s->display) + fprintf(f, + "DISPLAY=%s\n", + s->display); + + if (s->remote_host) + fprintf(f, + "REMOTE_HOST=%s\n", + s->remote_host); + + if (s->seat && s->seat->manager->vtconsole == s->seat) + fprintf(f, + "VTNR=%i\n", + s->vtnr); + + if (s->leader > 0) + fprintf(f, + "LEADER=%lu\n", + (unsigned long) s->leader); + + if (s->audit_id > 0) + fprintf(f, + "AUDIT=%llu\n", + (unsigned long long) s->audit_id); + + fflush(f); + if (ferror(f)) { + r = -errno; + unlink(s->state_file); + } + + fclose(f); + return r; +} + +int session_load(Session *s) { + assert(s); + + return 0; +} + +int session_activate(Session *s) { + int r; + + assert(s); + + if (s->vtnr < 0) + return -ENOTSUP; + + if (!s->seat) + return -ENOTSUP; + + if (s->seat->active == s) + return 0; + + assert(s->manager->vtconsole == s->seat); + + r = chvt(s->vtnr); + if (r < 0) + return r; + + s->seat->active = s; + + return seat_apply_acls(s->seat); +} + +bool x11_display_is_local(const char *display) { + assert(display); + + return + display[0] == ':' && + display[1] >= '0' && + display[1] <= '9'; +} + +static int session_link_x11_socket(Session *s) { + char *t, *f, *c; + size_t k; + + assert(s); + assert(s->user); + assert(s->user->runtime_path); + + if (s->user->display) + return 0; + + if (!s->display || !x11_display_is_local(s->display)) + return 0; + + k = strspn(s->display+1, "0123456789"); + f = new(char, sizeof("/tmp/.X11-unix/X") + k); + if (!f) { + log_error("Out of memory"); + return -ENOMEM; + } + + c = stpcpy(f, "/tmp/.X11-unix/X"); + memcpy(c, s->display+1, k); + c[k] = 0; + + if (access(f, F_OK) < 0) { + log_warning("Session %s has display %s with nonexisting socket %s.", s->id, s->display, f); + free(f); + return -ENOENT; + } + + t = strappend(s->user->runtime_path, "/display"); + if (!t) { + log_error("Out of memory"); + free(f); + return -ENOMEM; + } + + if (link(f, t) < 0) { + if (errno == EEXIST) { + unlink(t); + + if (link(f, t) >= 0) + goto done; + } + + if (symlink(f, t) < 0) { + + if (errno == EEXIST) { + unlink(t); + + if (symlink(f, t) >= 0) + goto done; + } + + log_error("Failed to link %s to %s: %m", f, t); + free(f); + free(t); + return -errno; + } + } + +done: + log_info("Linked %s to %s.", f, t); + free(f); + free(t); + + s->user->display = s; + + return 0; +} + +static int session_create_cgroup(Session *s) { + char **k; + char *p; + int r; + + assert(s); + assert(s->user); + assert(s->user->cgroup_path); + + if (!s->cgroup_path) { + if (asprintf(&p, "%s/%s", s->user->cgroup_path, s->id) < 0) { + log_error("Out of memory"); + return -ENOMEM; + } + } else + p = s->cgroup_path; + + if (s->leader > 0) + r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, p, s->leader); + else + r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p); + + if (r < 0) { + free(p); + s->cgroup_path = NULL; + log_error("Failed to create "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); + return r; + } + + s->cgroup_path = p; + + STRV_FOREACH(k, s->manager->controllers) { + if (s->leader > 0) + r = cg_create_and_attach(*k, p, s->leader); + else + r = cg_create(*k, p); + + if (r < 0) + log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r)); + } + + return 0; +} + +int session_start(Session *s) { + int r; + + assert(s); + assert(s->user); + + /* Create user first */ + r = user_start(s->user); + if (r < 0) + return r; + + /* Create cgroup */ + r = session_create_cgroup(s); + if (r < 0) + return r; + + /* Create X11 symlink */ + session_link_x11_socket(s); + return 0; +} + +static bool session_shall_kill(Session *s) { + assert(s); + + return s->kill_processes; +} + +static int session_kill_cgroup(Session *s) { + int r; + char **k; + + assert(s); + + if (!s->cgroup_path) + return 0; + + cg_trim(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false); + + if (session_shall_kill(s)) { + + r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true); + if (r < 0) + log_error("Failed to kill session cgroup: %s", strerror(-r)); + + } else { + r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, true); + if (r < 0) + log_error("Failed to check session cgroup: %s", strerror(-r)); + else if (r > 0) { + r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path); + if (r < 0) + log_error("Failed to delete session cgroup: %s", strerror(-r)); + } else + r = -EBUSY; + } + + STRV_FOREACH(k, s->user->manager->controllers) + cg_trim(*k, s->cgroup_path, true); + + free(s->cgroup_path); + s->cgroup_path = NULL; + + return r; +} + +static int session_unlink_x11_socket(Session *s) { + char *t; + int r; + + assert(s); + assert(s->user); + + if (s->user->display != s) + return 0; + + s->user->display = NULL; + + t = strappend(s->user->runtime_path, "/display"); + if (!t) { + log_error("Out of memory"); + return -ENOMEM; + } + + r = unlink(t); + free(t); + + return r < 0 ? -errno : 0; +} + +int session_stop(Session *s) { + int r = 0, k; + + assert(s); + + /* Kill cgroup */ + k = session_kill_cgroup(s); + if (k < 0) + r = k; + + /* Remove X11 symlink */ + session_unlink_x11_socket(s); + + return r; +} + +bool session_is_active(Session *s) { + assert(s); + + if (!s->seat) + return true; + + return s->seat->active == s; +} + +int session_check_gc(Session *s) { + int r; + + assert(s); + + if (s->pipe_fd >= 0) { + + r = pipe_eof(s->pipe_fd); + if (r < 0) + return r; + + if (r <= 0) + return 1; + } + + if (s->cgroup_path) { + + r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, s->cgroup_path, false); + if (r < 0) + return r; + + if (r <= 0) + return 1; + } + + return 0; +} + +static const char* const session_type_table[_SESSION_TYPE_MAX] = { + [SESSION_TERMINAL] = "terminal", + [SESSION_X11] = "x11" +}; + +DEFINE_STRING_TABLE_LOOKUP(session_type, SessionType); diff --git a/src/logind-user.c b/src/logind-user.c new file mode 100644 index 0000000000..1292eed3e2 --- /dev/null +++ b/src/logind-user.c @@ -0,0 +1,420 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <string.h> +#include <unistd.h> +#include <errno.h> + +#include "logind.h" +#include "util.h" +#include "cgroup-util.h" +#include "hashmap.h" +#include "strv.h" + +User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name) { + User *u; + + assert(m); + assert(name); + + u = new(User, 1); + if (!u) + return NULL; + + u->name = strdup(name); + if (!u->name) { + free(u); + return NULL; + } + + if (asprintf(&u->state_file, "/run/systemd/user/%lu", (unsigned long) uid) < 0) { + free(u->name); + free(u); + return NULL; + } + + if (hashmap_put(m->users, ULONG_TO_PTR((unsigned long) uid), u) < 0) { + free(u->state_file); + free(u->name); + free(u); + return NULL; + } + + u->manager = m; + u->uid = uid; + u->gid = gid; + + return u; +} + +void user_free(User *u) { + assert(u); + + while (u->sessions) + session_free(u->sessions); + + free(u->cgroup_path); + + free(u->service); + free(u->runtime_path); + + hashmap_remove(u->manager->users, ULONG_TO_PTR((unsigned long) u->uid)); + + free(u->name); + free(u->state_file); + free(u); +} + +int user_save(User *u) { + FILE *f; + int r; + + assert(u); + assert(u->state_file); + + r = safe_mkdir("/run/systemd/user", 0755, 0, 0); + if (r < 0) + return r; + + f = fopen(u->state_file, "we"); + if (!f) + return -errno; + + fprintf(f, + "NAME=%s\n" + "STATE=%s\n", + u->name, + user_state_to_string(user_get_state(u))); + + if (u->cgroup_path) + fprintf(f, + "CGROUP=%s\n", + u->cgroup_path); + + if (u->runtime_path) + fprintf(f, + "RUNTIME=%s\n", + u->runtime_path); + + if (u->service) + fprintf(f, + "SERVICE=%s\n", + u->service); + + if (u->display) + fprintf(f, + "DISPLAY=%s\n", + u->display->id); + + fflush(f); + if (ferror(f)) { + r = -errno; + unlink(u->state_file); + } + + fclose(f); + return r; +} + +int user_load(User *u) { + int r; + char *display = NULL; + Session *s; + + assert(u); + + r = parse_env_file(u->state_file, "r", + "CGROUP", &u->cgroup_path, + "RUNTIME", &u->runtime_path, + "SERVICE", &u->service, + "DISPLAY", &display, + NULL); + if (r < 0) { + free(display); + + if (r == -ENOENT) + return 0; + + log_error("Failed to read %s: %s", u->state_file, strerror(-r)); + return r; + } + + s = hashmap_get(u->manager->sessions, display); + free(display); + + if (s && s->display && x11_display_is_local(s->display)) + u->display = s; + + return r; +} + +static int user_mkdir_runtime_path(User *u) { + char *p; + int r; + + assert(u); + + r = safe_mkdir("/run/user", 0755, 0, 0); + if (r < 0) { + log_error("Failed to create /run/user: %s", strerror(-r)); + return r; + } + + if (!u->runtime_path) { + p = strappend("/run/user/", u->name); + + if (!p) { + log_error("Out of memory"); + return -ENOMEM; + } + } else + p = u->runtime_path; + + r = safe_mkdir(p, 0700, u->uid, u->gid); + if (r < 0) { + log_error("Failed to create runtime directory %s: %s", p, strerror(-r)); + free(p); + u->runtime_path = NULL; + return r; + } + + u->runtime_path = p; + return 0; +} + +static int user_create_cgroup(User *u) { + char **k; + char *p; + int r; + + assert(u); + + if (!u->cgroup_path) { + if (asprintf(&p, "%s/%s", u->manager->cgroup_path, u->name) < 0) { + log_error("Out of memory"); + return -ENOMEM; + } + } else + p = u->cgroup_path; + + r = cg_create(SYSTEMD_CGROUP_CONTROLLER, p); + if (r < 0) { + free(p); + u->cgroup_path = NULL; + log_error("Failed to create cgroup "SYSTEMD_CGROUP_CONTROLLER":%s: %s", p, strerror(-r)); + return r; + } + + u->cgroup_path = p; + + STRV_FOREACH(k, u->manager->controllers) { + r = cg_create(*k, p); + if (r < 0) + log_warning("Failed to create cgroup %s:%s: %s", *k, p, strerror(-r)); + } + + return 0; +} + +static int user_start_service(User *u) { + assert(u); + + return 0; +} + +int user_start(User *u) { + int r; + + assert(u); + + /* Make XDG_RUNTIME_DIR */ + r = user_mkdir_runtime_path(u); + if (r < 0) + return r; + + /* Create cgroup */ + r = user_create_cgroup(u); + if (r < 0) + return r; + + /* Spawn user systemd */ + r = user_start_service(u); + if (r < 0) + return r; + + dual_timestamp_get(&u->timestamp); + + return 0; +} + +static int user_stop_service(User *u) { + assert(u); + + if (!u->service) + return 0; + + return 0; +} + +static int user_shall_kill(User *u) { + assert(u); + + return u->manager->kill_user_processes; +} + +static int user_kill_cgroup(User *u) { + int r; + char **k; + + assert(u); + + if (!u->cgroup_path) + return 0; + + cg_trim(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); + + if (user_shall_kill(u)) { + + r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); + if (r < 0) + log_error("Failed to kill user cgroup: %s", strerror(-r)); + } else { + + r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, true); + if (r < 0) + log_error("Failed to check user cgroup: %s", strerror(-r)); + else if (r > 0) { + r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path); + if (r < 0) + log_error("Failed to delete user cgroup: %s", strerror(-r)); + } else + r = -EBUSY; + } + + STRV_FOREACH(k, u->manager->controllers) + cg_trim(*k, u->cgroup_path, true); + + free(u->cgroup_path); + u->cgroup_path = NULL; + + return r; +} + +static int user_remove_runtime_path(User *u) { + int r; + + assert(u); + + if (!u->runtime_path) + return 0; + + r = rm_rf(u->runtime_path, false, true); + if (r < 0) + log_error("Failed to remove runtime directory %s: %s", u->runtime_path, strerror(-r)); + + free(u->runtime_path); + u->runtime_path = NULL; + + return r; +} + +int user_stop(User *u) { + Session *s; + int r = 0, k; + assert(u); + + LIST_FOREACH(sessions_by_user, s, u->sessions) { + k = session_stop(s); + if (k < 0) + r = k; + } + + /* Kill systemd */ + k = user_stop_service(u); + if (k < 0) + r = k; + + /* Kill cgroup */ + k = user_kill_cgroup(u); + if (k < 0) + r = k; + + /* Kill XDG_RUNTIME_DIR */ + k = user_remove_runtime_path(u); + if (k < 0) + r = k; + + return r; +} + +int user_check_gc(User *u) { + int r; + char *p; + + assert(u); + + if (u->sessions) + return 1; + + if (asprintf(&p, "/var/lib/systemd/linger/%s", u->name) < 0) + return -ENOMEM; + + r = access(p, F_OK) >= 0; + free(p); + + if (r > 0) + return 1; + + if (u->cgroup_path) { + r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, false); + if (r < 0) + return r; + + if (r <= 0) + return 1; + } + + return 0; +} + +UserState user_get_state(User *u) { + Session *i; + + assert(u); + + if (!u->sessions) + return USER_LINGERING; + + LIST_FOREACH(sessions_by_user, i, u->sessions) + if (session_is_active(i)) + return USER_ACTIVE; + + return USER_ONLINE; +} + +static const char* const user_state_table[_USER_STATE_MAX] = { + [USER_OFFLINE] = "offline", + [USER_LINGERING] = "lingering", + [USER_ONLINE] = "online", + [USER_ACTIVE] = "active" +}; + +DEFINE_STRING_TABLE_LOOKUP(user_state, UserState); diff --git a/src/logind.c b/src/logind.c new file mode 100644 index 0000000000..0c26aaddbf --- /dev/null +++ b/src/logind.c @@ -0,0 +1,886 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU 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 <sys/epoll.h> + +#include "logind.h" +#include "dbus-common.h" +#include "dbus-loop.h" + +enum { + FD_UDEV, + FD_CONSOLE, + FD_BUS +}; + +Manager *manager_new(void) { + Manager *m; + + m = new0(Manager, 1); + if (!m) + return NULL; + + m->console_active_fd = -1; + m->bus_fd = -1; + m->udev_fd = -1; + m->epoll_fd = -1; + m->n_autovts = 6; + + m->devices = hashmap_new(string_hash_func, string_compare_func); + m->seats = hashmap_new(string_hash_func, string_compare_func); + m->sessions = hashmap_new(string_hash_func, string_compare_func); + m->users = hashmap_new(trivial_hash_func, trivial_compare_func); + + if (!m->devices || !m->seats || !m->sessions || !m->users) { + manager_free(m); + return NULL; + } + + m->udev = udev_new(); + 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) { + Session *session; + User *u; + Device *d; + Seat *s; + + assert(m); + + while ((session = hashmap_first(m->sessions))) + session_free(session); + + 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); + + hashmap_free(m->sessions); + hashmap_free(m->users); + hashmap_free(m->devices); + hashmap_free(m->seats); + + if (m->console_active_fd >= 0) + close_nointr_nofail(m->console_active_fd); + + if (m->udev_monitor) + udev_monitor_unref(m->udev_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) + close_nointr_nofail(m->bus_fd); + + if (m->epoll_fd >= 0) + close_nointr_nofail(m->epoll_fd); + + free(m->cgroup_path); + free(m); +} + +int manager_add_device(Manager *m, const char *sysfs, Device **_device) { + Device *d; + + assert(m); + assert(sysfs); + + d = hashmap_get(m->devices, sysfs); + if (d) { + if (_device) + *_device = d; + + return 0; + } + + d = device_new(m, sysfs); + if (!d) + return -ENOMEM; + + if (_device) + *_device = d; + + return 0; +} + +int manager_add_seat(Manager *m, const char *id, Seat **_seat) { + Seat *s; + + assert(m); + assert(id); + + s = hashmap_get(m->seats, id); + if (s) { + if (_seat) + *_seat = s; + + return 0; + } + + s = seat_new(m, id); + if (!s) + return -ENOMEM; + + if (_seat) + *_seat = s; + + return 0; +} + +int manager_add_session(Manager *m, User *u, const char *id, Session **_session) { + Session *s; + + assert(m); + assert(id); + + s = hashmap_get(m->sessions, id); + if (s) { + if (_session) + *_session = s; + + return 0; + } + + s = session_new(m, u, id); + if (!s) + return -ENOMEM; + + if (_session) + *_session = s; + + return 0; +} + +int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user) { + User *u; + int r; + + assert(m); + assert(name); + + u = hashmap_get(m->users, ULONG_TO_PTR((unsigned long) uid)); + if (u) { + if (_user) + *_user = u; + + return 0; + } + + u = user_new(m, uid, gid, name); + if (!u) + return -ENOMEM; + + r = user_start(u); + if (r < 0) { + user_stop(u); + user_free(u); + } else { + if (_user) + *_user = u; + } + + return r; +} + +int manager_add_user_by_name(Manager *m, const char *name, User **_user) { + struct passwd *p; + + assert(m); + assert(name); + + errno = 0; + p = getpwnam(name); + if (!p) + return errno ? -errno : -ENOENT; + + return manager_add_user(m, p->pw_uid, p->pw_gid, name, _user); +} + +int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) { + struct passwd *p; + + assert(m); + + errno = 0; + p = getpwuid(uid); + if (!p) + return errno ? -errno : -ENOENT; + + return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user); +} + +int manager_process_device(Manager *m, struct udev_device *d) { + const char *sn; + Seat *seat; + Device *device; + int r; + + assert(m); + + sn = udev_device_get_property_value(d, "SEAT"); + if (!sn) + sn = "seat0"; + + r = manager_add_device(m, udev_device_get_syspath(d), &device); + if (r < 0) + return r; + + r = manager_add_seat(m, sn, &seat); + if (r < 0) + return r; + + device_attach(device, seat); + return 0; +} + +int manager_enumerate_devices(Manager *m) { + struct udev_list_entry *item = NULL, *first = NULL; + struct udev_enumerate *e; + int r = -ENOMEM; + + assert(m); + + /* Loads devices from udev and creates seats for them as + * necessary */ + + e = udev_enumerate_new(m->udev); + if (!e) + return -ENOMEM; + + if (udev_enumerate_add_match_subsystem(e, "graphics") < 0) + goto finish; + + if (udev_enumerate_add_match_tag(e, "seat") < 0) + goto finish; + + if (udev_enumerate_scan_devices(e) < 0) + goto finish; + + first = udev_enumerate_get_list_entry(e); + udev_list_entry_foreach(item, first) { + struct udev_device *d; + int k; + + d = udev_device_new_from_syspath(m->udev, udev_list_entry_get_name(item)); + if (!d) + goto finish; + + k = manager_process_device(m, d); + udev_device_unref(d); + + if (k < 0) + r = k; + } + +finish: + if (e) + udev_enumerate_unref(e); + + return r; +} + +int manager_enumerate_seats(Manager *m) { + DIR *d; + struct dirent *de; + 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. */ + + d = opendir("/run/systemd/seats"); + if (!d) { + if (errno == ENOENT) + return 0; + + log_error("Failed to open /run/systemd/seats: %m"); + return -errno; + } + + while ((de = readdir(d))) { + Seat *s; + int k; + + if (!dirent_is_file(de)) + continue; + + s = hashmap_get(m->seats, de->d_name); + if (!s) { + unlinkat(dirfd(d), de->d_name, 0); + 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; + char *name; + DIR *d; + + r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_path, &d); + if (r < 0) { + if (r == -ENOENT) + return 0; + + log_error("Failed to open %s: %s", m->cgroup_path, strerror(-r)); + return r; + } + + while ((r = cg_read_subgroup(d, &name)) > 0) { + User *user; + + r = manager_add_user_by_name(m, name, &user); + free(name); + + if (r < 0) + break; + + if (user->cgroup_path) + continue; + + if (asprintf(&user->cgroup_path, "%s/%s", m->cgroup_path, name) < 0) { + r = -ENOMEM; + break; + } + } + + if (d) + closedir(d); + + return r; +} + +int manager_enumerate_users(Manager *m) { + DIR *d; + struct dirent *de; + int r; + + assert(m); + + r = manager_enumerate_users_from_cgroup(m); + + d = opendir("/run/systemd/users"); + if (!d) { + if (errno == ENOENT) + return 0; + + log_error("Failed to open /run/systemd/users: %m"); + return -errno; + } + + while ((de = readdir(d))) { + unsigned long ul; + User *u; + int k; + + if (!dirent_is_file(de)) + continue; + + k = safe_atolu(de->d_name, &ul); + if (k < 0) { + log_error("Failed to parse file name %s: %s", de->d_name, strerror(-k)); + continue; + } + + u = hashmap_get(m->users, ULONG_TO_PTR(ul)); + if (!u) { + unlinkat(dirfd(d), de->d_name, 0); + 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; + + k = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, &d); + if (k < 0) { + if (k == -ENOENT) + continue; + + log_error("Failed to open %s: %s", u->cgroup_path, strerror(-k)); + r = k; + continue; + } + + while ((k = cg_read_subgroup(d, &name)) > 0) { + Session *session; + + k = manager_add_session(m, u, name, &session); + free(name); + + if (k < 0) + break; + + if (session->cgroup_path) + continue; + + if (asprintf(&session->cgroup_path, "%s/%s", u->cgroup_path, name) < 0) { + k = -ENOMEM; + break; + } + } + + closedir(d); + + if (k < 0) + r = k; + } + + return r; +} + +int manager_enumerate_sessions(Manager *m) { + DIR *d; + struct dirent *de; + int r = 0; + + assert(m); + + r = manager_enumerate_sessions_from_cgroup(m); + + d = opendir("/run/systemd/sessions"); + if (!d) { + if (errno == ENOENT) + return 0; + + log_error("Failed to open /run/systemd/sessions: %m"); + return -errno; + } + + while ((de = readdir(d))) { + struct Session *s; + int k; + + if (!dirent_is_file(de)) + continue; + + s = hashmap_get(m->sessions, de->d_name); + if (!s) { + unlinkat(dirfd(d), de->d_name, 0); + continue; + } + + k = session_load(s); + if (k < 0) + r = k; + } + + closedir(d); + + return r; +} + +int manager_start_linger_users(Manager *m) { + DIR *d; + struct dirent *de; + int r = 0; + + d = opendir("/var/lib/systemd/linger"); + if (!d) { + if (errno == ENOENT) + return 0; + + log_error("Failed to open /var/lib/systemd/linger/: %m"); + return -errno; + } + + while ((de = readdir(d))) { + int k; + + if (!dirent_is_file(de)) + continue; + + k = manager_add_user_by_name(m, de->d_name, NULL); + if (k < 0) { + log_notice("Couldn't add lingering user %s: %s", de->d_name, strerror(-k)); + r = k; + } + } + + closedir(d); + + return r; +} + +int manager_dispatch_udev(Manager *m) { + struct udev_device *d; + int r; + + assert(m); + + d = udev_monitor_receive_device(m->udev_monitor); + if (!d) + return -ENOMEM; + + r = manager_process_device(m, d); + udev_device_unref(d); + + return r; +} + +int manager_dispatch_console(Manager *m) { + char t[64]; + ssize_t k; + int r, vtnr; + + assert(m); + + lseek(m->console_active_fd, SEEK_SET, 0); + + k = read(m->console_active_fd, t, sizeof(t)-1); + if (k <= 0) { + log_error("Failed to read current console: %s", k < 0 ? strerror(-errno) : "EOF"); + return k < 0 ? -errno : -EIO; + } + + t[k] = 0; + if (!startswith(t, "tty")) { + log_error("Hm, /sys/class/tty/tty0/active is badly formatted."); + return -EIO; + } + + r = safe_atoi(t+3, &vtnr); + if (r < 0) { + log_error("Failed to parse VT number %s", t+3); + return r; + } + + if (vtnr <= 0) { + log_error("VT number invalid: %s", t+3); + return -EIO; + } + + if (m->vtconsole) + seat_active_vt_changed(m->vtconsole, vtnr); + + return 0; +} + +int manager_spawn_autovt(Manager *m, int vtnr) { + assert(m); + + return 0; +} + +static DBusHandlerResult login_message_handler( + DBusConnection *connection, + DBusMessage *message, + void *userdata) { + + return DBUS_HANDLER_RESULT_HANDLED; +} + +static DBusHandlerResult login_message_filter( + DBusConnection *connection, + DBusMessage *message, + void *userdata) { + + return DBUS_HANDLER_RESULT_HANDLED; +} + +static int manager_connect_bus(Manager *m) { + const DBusObjectPathVTable login_vtable = { + .message_function = login_message_handler + }; + + DBusError error; + int r; + struct epoll_event ev; + + assert(m); + assert(!m->bus); + assert(m->bus_fd < 0); + + dbus_error_init(&error); + + m->bus = dbus_bus_get_private(DBUS_BUS_SYSTEM, &error); + if (!m->bus) { + log_error("Failed to get system D-Bus connection: %s", error.message); + r = -ECONNREFUSED; + goto fail; + } + + if (!dbus_connection_register_object_path(m->bus, "/org/freedesktop/login1", &login_vtable, NULL)) { + log_error("Not enough memory"); + r = -ENOMEM; + goto fail; + } + + if (!dbus_connection_add_filter(m->bus, login_message_filter, m, NULL)) { + log_error("Not enough memory"); + r = -ENOMEM; + goto fail; + } + + dbus_bus_add_match(m->bus, + "type='signal'," + "interface='org.freedesktop.systemd1.Agent'," + "member='Released'," + "path='/org/freedesktop/systemd1/agent'", + &error); + + if (dbus_error_is_set(&error)) { + log_error("Failed to register match: %s", error.message); + r = -EIO; + goto fail; + } + + if (dbus_bus_request_name(m->bus, "org.freedesktop.login1", DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) < 0) { + log_error("Failed to register name on bus: %s", error.message); + r = -EEXIST; + goto fail; + } + + m->bus_fd = bus_loop_open(m->bus); + if (m->bus_fd < 0) { + r = m->bus_fd; + goto fail; + } + + zero(ev); + ev.events = EPOLLIN; + ev.data.u32 = FD_BUS; + + if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->bus_fd, &ev) < 0) + goto fail; + + return 0; + +fail: + dbus_error_free(&error); + + return r; +} + +static int manager_connect_console(Manager *m) { + struct epoll_event ev; + + assert(m); + assert(m->console_active_fd < 0); + + m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC); + if (m->console_active_fd < 0) { + log_error("Failed to open /sys/class/tty/tty0/active: %m"); + return -errno; + } + + zero(ev); + ev.events = EPOLLIN; + ev.data.u32 = FD_CONSOLE; + + if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->console_active_fd, &ev) < 0) + return -errno; + + return 0; +} + +static int manager_connect_udev(Manager *m) { + struct epoll_event ev; + + assert(m); + assert(!m->udev_monitor); + + m->udev_monitor = udev_monitor_new_from_netlink(m->udev, "udev"); + if (!m->udev_monitor) + return -ENOMEM; + + udev_monitor_filter_add_match_tag(m->udev_monitor, "seat"); + + if (udev_monitor_filter_add_match_subsystem_devtype(m->udev_monitor, "graphics", NULL) < 0) + return -ENOMEM; + + if (udev_monitor_enable_receiving(m->udev_monitor) < 0) + return -ENOMEM; + + m->udev_fd = udev_monitor_get_fd(m->udev_monitor); + + zero(ev); + ev.events = EPOLLIN; + ev.data.u32 = FD_UDEV; + + if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->udev_fd, &ev) < 0) + return -errno; + + return 0; +} + +int manager_startup(Manager *m) { + int r; + + assert(m); + assert(m->epoll_fd <= 0); + + m->epoll_fd = epoll_create1(EPOLL_CLOEXEC); + if (m->epoll_fd < 0) + return -errno; + + /* Connect to udev */ + r = manager_connect_udev(m); + if (r < 0) + return r; + + /* Connect to console */ + r = manager_connect_console(m); + if (r < 0) + return r; + + /* Connect to the bus */ + r = manager_connect_bus(m); + if (r < 0) + return r; + + /* Deserialize state */ + manager_enumerate_devices(m); + manager_enumerate_seats(m); + manager_enumerate_users(m); + manager_enumerate_sessions(m); + + /* Spawn lingering users */ + manager_start_linger_users(m); + + return 0; +} + +int manager_run(Manager *m) { + assert(m); + + for (;;) { + struct epoll_event event; + int n; + + if (dbus_connection_dispatch(m->bus) != DBUS_DISPATCH_COMPLETE) + continue; + + n = epoll_wait(m->epoll_fd, &event, 1, -1); + if (n < 0) { + log_error("epoll() failed: %m"); + return -errno; + } + + switch (event.data.u32) { + + case FD_UDEV: + manager_dispatch_udev(m); + break; + + case FD_CONSOLE: + manager_dispatch_console(m); + break; + + case FD_BUS: + bus_loop_dispatch(m->bus_fd); + break; + } + } + + return 0; +} + +int main(int argc, char *argv[]) { + Manager *m = NULL; + int r = 0; + + log_set_target(LOG_TARGET_AUTO); + log_parse_environment(); + log_open(); + + if (argc != 1) { + log_error("This program takes no arguments."); + r = -EINVAL; + goto finish; + } + + umask(0022); + + m = manager_new(); + if (!m) { + log_error("Out of memory"); + r = -ENOMEM; + goto finish; + } + + r = manager_startup(m); + if (r < 0) { + log_error("Failed to fully start up daemon: %s", strerror(-r)); + goto finish; + } + + r = manager_run(m); + +finish: + if (m) + manager_free(m); + + return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS; +} diff --git a/src/logind.h b/src/logind.h new file mode 100644 index 0000000000..5e69a71b02 --- /dev/null +++ b/src/logind.h @@ -0,0 +1,235 @@ +/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ + +#ifndef foologindhfoo +#define foologindhfoo + +/*** + This file is part of systemd. + + Copyright 2011 Lennart Poettering + + systemd is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 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 + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with systemd; If not, see <http://www.gnu.org/licenses/>. +***/ + +#include <stdbool.h> +#include <inttypes.h> +#include <dbus/dbus.h> +#include <libudev.h> + +#include "util.h" +#include "list.h" +#include "hashmap.h" +#include "cgroup-util.h" + +/* TODO: + * + * recreate VTs when disallocated + * udev rules + * spawn user systemd + * non-local X11 server + * udev-acl + * reboot/shutdown halt management + * PAM rewrite + */ + +typedef struct Manager Manager; +typedef struct Device Device; +typedef struct Seat Seat; +typedef struct Session Session; +typedef struct User User; + +struct Device { + Manager *manager; + + char *sysfs; + Seat *seat; + + dual_timestamp timestamp; + + LIST_FIELDS(struct Device, devices); +}; + +struct Seat { + Manager *manager; + char *id; + + char *state_file; + + LIST_HEAD(Device, devices); + + Session *active; + LIST_HEAD(Session, sessions); +}; + +typedef enum SessionType { + SESSION_TERMINAL, + SESSION_X11, + _SESSION_TYPE_MAX, + _SESSION_TYPE_INVALID = -1 +} SessionType; + +struct Session { + Manager *manager; + + char *id; + SessionType type; + + char *state_file; + + User *user; + + dual_timestamp timestamp; + + char *tty; + char *display; + + bool remote; + char *remote_host; + + int vtnr; + Seat *seat; + + pid_t leader; + uint64_t audit_id; + + int pipe_fd; + + char *cgroup_path; + char **controllers, **reset_controllers; + + bool kill_processes; + + LIST_FIELDS(Session, sessions_by_user); + LIST_FIELDS(Session, sessions_by_seat); +}; + +typedef enum UserState { + USER_OFFLINE, + USER_LINGERING, + USER_ONLINE, + USER_ACTIVE, + _USER_STATE_MAX, + _USER_STATE_INVALID = -1 +} UserState; + +struct User { + Manager *manager; + + uid_t uid; + gid_t gid; + char *name; + + char *state_file; + char *runtime_path; + char *service; + char *cgroup_path; + + Session *display; + + dual_timestamp timestamp; + + LIST_HEAD(Session, sessions); +}; + +struct Manager { + DBusConnection *bus; + + Hashmap *devices; + Hashmap *seats; + Hashmap *sessions; + Hashmap *users; + + struct udev *udev; + struct udev_monitor *udev_monitor; + + int udev_fd; + int console_active_fd; + int bus_fd; + int epoll_fd; + + int n_autovts; + + Seat *vtconsole; + + char *cgroup_path; + char **controllers, **reset_controllers; + + char **kill_only_users, **kill_exlude_users; + + bool kill_user_processes; +}; + +Device* device_new(Manager *m, const char *sysfs); +void device_free(Device *d); +void device_attach(Device *d, Seat *s); +void device_detach(Device *d); + +Seat *seat_new(Manager *m, const char *id); +void seat_free(Seat *s); +int seat_preallocate_vts(Seat *s); +void seat_active_vt_changed(Seat *s, int vtnr); +int seat_apply_acls(Seat *s); +int seat_stop(Seat *s); +int seat_save(Seat *s); +int seat_load(Seat *s); + +Session *session_new(Manager *m, User *u, const char *id); +void session_free(Session *s); +int session_activate(Session *s); +bool session_is_active(Session *s); +int session_check_gc(Session *s); +int session_start(Session *s); +int session_stop(Session *s); +int session_save(Session *s); +int session_load(Session *s); + +User* user_new(Manager *m, uid_t uid, gid_t gid, const char *name); +void user_free(User *u); +int user_start(User *u); +int user_stop(User *u); +int user_check_gc(User *u); +UserState user_get_state(User *u); +int user_save(User *u); +int user_load(User *u); + +Manager *manager_new(void); +void manager_free(Manager *m); +int manager_add_device(Manager *m, const char *sysfs, Device **_device); +int manager_add_seat(Manager *m, const char *id, Seat **_seat); +int manager_add_session(Manager *m, User *u, const char *id, Session **_session); +int manager_add_user(Manager *m, uid_t uid, gid_t gid, const char *name, User **_user); +int manager_add_user_by_name(Manager *m, const char *name, User **_user); +int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user); +int manager_process_device(Manager *m, struct udev_device *d); +int manager_dispatch_udev(Manager *m); +int manager_dispatch_console(Manager *m); +int manager_enumerate_devices(Manager *m); +int manager_enumerate_seats(Manager *m); +int manager_enumerate_sessions(Manager *m); +int manager_enumerate_users(Manager *m); +int manager_start_one_linger_user(Manager *m, const char *user); +int manager_start_linger_users(Manager *m); +int manager_startup(Manager *m); +int manager_run(Manager *m); +int manager_spawn_autovt(Manager *m, int vtnr); + +const char* session_type_to_string(SessionType t); +SessionType session_type_from_string(const char *s); + +const char* user_state_to_string(UserState s); +UserState user_state_from_string(const char *s); + +bool x11_display_is_local(const char *display); + +#endif |