X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Flogin%2Flogind.c;h=8d4f7333c950d7045ce8c4813458113bb6bed8c4;hb=36140842612803d71fe771ce03f3dee7732284f0;hp=a54195cebb6d578ab343302090bad343630e6430;hpb=3eff4208ffecedd778fec260f0d4b18e94dab443;p=elogind.git diff --git a/src/login/logind.c b/src/login/logind.c index a54195ceb..8d4f7333c 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -6,16 +6,16 @@ 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 + 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 - General Public License for more details. + Lesser General Public License for more details. - You should have received a copy of the GNU General Public License + You should have received a copy of the GNU Lesser General Public License along with systemd; If not, see . ***/ @@ -50,15 +50,20 @@ Manager *manager_new(void) { m->udev_vcsa_fd = -1; m->epoll_fd = -1; m->n_autovts = 6; + m->inhibit_delay_max = 5 * USEC_PER_SEC; 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); + m->inhibitors = hashmap_new(string_hash_func, string_compare_func); + m->cgroups = hashmap_new(string_hash_func, string_compare_func); - m->fifo_fds = hashmap_new(trivial_hash_func, trivial_compare_func); + m->session_fds = hashmap_new(trivial_hash_func, trivial_compare_func); + m->inhibitor_fds = hashmap_new(trivial_hash_func, trivial_compare_func); - if (!m->devices || !m->seats || !m->sessions || !m->users || !m->cgroups || !m->fifo_fds) { + if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || + !m->cgroups || !m->session_fds || !m->inhibitor_fds) { manager_free(m); return NULL; } @@ -89,6 +94,7 @@ void manager_free(Manager *m) { User *u; Device *d; Seat *s; + Inhibitor *i; assert(m); @@ -104,12 +110,18 @@ void manager_free(Manager *m) { while ((s = hashmap_first(m->seats))) seat_free(s); - hashmap_free(m->sessions); - hashmap_free(m->users); + while ((i = hashmap_first(m->inhibitors))) + inhibitor_free(i); + hashmap_free(m->devices); hashmap_free(m->seats); + hashmap_free(m->sessions); + hashmap_free(m->users); + hashmap_free(m->inhibitors); + hashmap_free(m->cgroups); - hashmap_free(m->fifo_fds); + hashmap_free(m->session_fds); + hashmap_free(m->inhibitor_fds); if (m->console_active_fd >= 0) close_nointr_nofail(m->console_active_fd); @@ -268,6 +280,30 @@ int manager_add_user_by_uid(Manager *m, uid_t uid, User **_user) { return manager_add_user(m, uid, p->pw_gid, p->pw_name, _user); } +int manager_add_inhibitor(Manager *m, const char* id, Inhibitor **_inhibitor) { + Inhibitor *i; + + assert(m); + assert(id); + + i = hashmap_get(m->inhibitors, id); + if (i) { + if (_inhibitor) + *_inhibitor = i; + + return 0; + } + + i = inhibitor_new(m, id); + if (!i) + return -ENOMEM; + + if (_inhibitor) + *_inhibitor = i; + + return 0; +} + int manager_process_seat_device(Manager *m, struct udev_device *d) { Device *device; int r; @@ -412,10 +448,9 @@ int manager_enumerate_seats(Manager *m) { } static int manager_enumerate_users_from_cgroup(Manager *m) { - int r = 0; + int r = 0, k; char *name; DIR *d; - int k; r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_path, &d); if (r < 0) { @@ -641,6 +676,46 @@ int manager_enumerate_sessions(Manager *m) { return r; } +int manager_enumerate_inhibitors(Manager *m) { + DIR *d; + struct dirent *de; + int r = 0; + + assert(m); + + d = opendir("/run/systemd/inhibit"); + if (!d) { + if (errno == ENOENT) + return 0; + + log_error("Failed to open /run/systemd/inhibit: %m"); + return -errno; + } + + while ((de = readdir(d))) { + int k; + Inhibitor *i; + + if (!dirent_is_file(de)) + continue; + + k = manager_add_inhibitor(m, de->d_name, &i); + if (k < 0) { + log_notice("Couldn't add inhibitor %s: %s", de->d_name, strerror(-k)); + 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; @@ -853,15 +928,28 @@ void manager_cgroup_notify_empty(Manager *m, const char *cgroup) { static void manager_pipe_notify_eof(Manager *m, int fd) { Session *s; + Inhibitor *i; assert_se(m); assert_se(fd >= 0); - assert_se(s = hashmap_get(m->fifo_fds, INT_TO_PTR(fd + 1))); - assert(s->fifo_fd == fd); - session_remove_fifo(s); + s = hashmap_get(m->session_fds, INT_TO_PTR(fd + 1)); + if (s) { + assert(s->fifo_fd == fd); + session_remove_fifo(s); + session_stop(s); + return; + } - session_stop(s); + i = hashmap_get(m->inhibitor_fds, INT_TO_PTR(fd + 1)); + if (i) { + assert(i->fifo_fd == fd); + inhibitor_stop(i); + inhibitor_free(i); + return; + } + + assert_not_reached("Got EOF on unknown pipe"); } static int manager_connect_bus(Manager *m) { @@ -945,14 +1033,16 @@ static int manager_connect_console(Manager *m) { assert(m); assert(m->console_active_fd < 0); + /* On certain architectures (S390 and Xen, and containers), + /dev/tty0 does not exist, so don't fail if we can't open + it. */ + if (access("/dev/tty0", F_OK) < 0) { + m->console_active_fd = -1; + return 0; + } + m->console_active_fd = open("/sys/class/tty/tty0/active", O_RDONLY|O_NOCTTY|O_CLOEXEC); if (m->console_active_fd < 0) { - - /* On certain architectures (S390 and Xen), /dev/tty0 - does not exist, so don't fail if we can't open it.*/ - if (errno == ENOENT) - return 0; - log_error("Failed to open /sys/class/tty/tty0/active: %m"); return -errno; } @@ -1068,12 +1158,14 @@ void manager_gc(Manager *m, bool drop_not_started) { int manager_get_idle_hint(Manager *m, dual_timestamp *t) { Session *s; - bool idle_hint = true; + bool idle_hint; dual_timestamp ts = { 0, 0 }; Iterator i; assert(m); + idle_hint = !manager_is_inhibited(m, INHIBIT_IDLE, INHIBIT_BLOCK, t); + HASHMAP_FOREACH(s, m->sessions, i) { dual_timestamp k; int ih; @@ -1108,11 +1200,15 @@ int manager_startup(Manager *m) { Seat *seat; Session *session; User *user; + Inhibitor *inhibitor; Iterator i; assert(m); assert(m->epoll_fd <= 0); + cg_shorten_controllers(m->reset_controllers); + cg_shorten_controllers(m->controllers); + m->epoll_fd = epoll_create1(EPOLL_CLOEXEC); if (m->epoll_fd < 0) return -errno; @@ -1142,6 +1238,7 @@ int manager_startup(Manager *m) { manager_enumerate_seats(m); manager_enumerate_users(m); manager_enumerate_sessions(m); + manager_enumerate_inhibitors(m); /* Remove stale objects before we start them */ manager_gc(m, false); @@ -1156,6 +1253,9 @@ int manager_startup(Manager *m) { HASHMAP_FOREACH(session, m->sessions, i) session_start(session); + HASHMAP_FOREACH(inhibitor, m->inhibitors, i) + inhibitor_start(inhibitor); + return 0; } @@ -1165,15 +1265,28 @@ int manager_run(Manager *m) { for (;;) { struct epoll_event event; int n; + int msec = -1; manager_gc(m, true); + if (manager_dispatch_delayed(m) > 0) + continue; + if (dbus_connection_dispatch(m->bus) != DBUS_DISPATCH_COMPLETE) continue; manager_gc(m, true); - n = epoll_wait(m->epoll_fd, &event, 1, -1); + 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); + } + + n = epoll_wait(m->epoll_fd, &event, 1, msec); if (n < 0) { if (errno == EINTR || errno == EAGAIN) continue; @@ -1182,6 +1295,9 @@ int manager_run(Manager *m) { return -errno; } + if (n == 0) + continue; + switch (event.data.u32) { case FD_SEAT_UDEV: