From 6f2f5118f1ba06b7fd93a0ce6661530ea1f7e7ee Mon Sep 17 00:00:00 2001 From: Sven Eden Date: Wed, 14 Jun 2017 17:39:24 +0200 Subject: [PATCH] Prep v231: Move elogind specific code in login/logind.c to login/elogind.c --- src/login/elogind.c | 311 +++++++++++++++++++++++++++++++++++++++ src/login/elogind.h | 43 ++++++ src/login/logind.c | 140 +++--------------- src/login/logind.conf.in | 2 +- src/login/logind.h | 15 +- 5 files changed, 385 insertions(+), 126 deletions(-) create mode 100644 src/login/elogind.c create mode 100644 src/login/elogind.h diff --git a/src/login/elogind.c b/src/login/elogind.c new file mode 100644 index 000000000..e76654b60 --- /dev/null +++ b/src/login/elogind.c @@ -0,0 +1,311 @@ +/*** + This file is part of elogind. + + Copyright 2017 Sven Eden + + elogind is free software; you can redistribute it and/or modify it + 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. + + elogind 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 elogind; If not, see . +***/ + + +#include "bus-util.h" +#include "cgroup.h" +#include "elogind.h" +#include "fd-util.h" +#include "mount-setup.h" +#include "socket-util.h" +#include "string-util.h" +#include "strv.h" +#include "umask-util.h" + + +#define CGROUPS_AGENT_RCVBUF_SIZE (8*1024*1024) + + +static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) { + _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; + Manager *m = userdata; + const char *cgroup; + uid_t sender_uid; + int r; + + assert(message); + assert(m); + + /* only accept org.freedesktop.elogind.Agent from UID=0 */ + r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); + if (r < 0) + return r; + + r = sd_bus_creds_get_euid(creds, &sender_uid); + if (r < 0 || sender_uid != 0) + return 0; + + /* parse 'cgroup-empty' notification */ + r = sd_bus_message_read(message, "s", &cgroup); + if (r < 0) { + bus_log_parse_error(r); + return 0; + } + + manager_notify_cgroup_empty(m, cgroup); + + return 0; +} + +/// Add-On for manager_connect_bus() +void elogind_bus_setup_system(Manager* m) { + int r; + + assert(m); + assert(m->bus); + + /* if we are a user instance we get the Released message via the system bus */ + if (MANAGER_IS_USER(m)) { + r = sd_bus_add_match( + m->bus, + NULL, + "type='signal'," + "interface='org.freedesktop.elogind.Agent'," + "member='Released'," + "path='/org/freedesktop/elogind/agent'", + signal_agent_released, m); + if (r < 0) + log_warning_errno(r, "Failed to register Released match on system bus: %m"); + } + + log_debug("Successfully connected to system bus."); +} + +static int bus_forward_agent_released(Manager *m, const char *path) { + int r; + + assert(m); + assert(path); + + if (!MANAGER_IS_SYSTEM(m)) + return 0; + + if (!m->bus) + return 0; + + /* If we are running a system instance we forward the agent message on the system bus, so that the user + * instances get notified about this, too */ + + r = sd_bus_emit_signal(m->bus, + "/org/freedesktop/elogind/agent", + "org.freedesktop.elogind.Agent", + "Released", + "s", path); + if (r < 0) + return log_warning_errno(r, "Failed to propagate agent release message: %m"); + + return 1; +} + +static int manager_dispatch_cgroups_agent_fd(sd_event_source *source, int fd, uint32_t revents, void *userdata) { + Manager *m = userdata; + char buf[PATH_MAX+1]; + ssize_t n; + + n = recv(fd, buf, sizeof(buf), 0); + if (n < 0) + return log_error_errno(errno, "Failed to read cgroups agent message: %m"); + if (n == 0) { + log_error("Got zero-length cgroups agent message, ignoring."); + return 0; + } + if ((size_t) n >= sizeof(buf)) { + log_error("Got overly long cgroups agent message, ignoring."); + return 0; + } + + if (memchr(buf, 0, n)) { + log_error("Got cgroups agent message with embedded NUL byte, ignoring."); + return 0; + } + buf[n] = 0; + + manager_notify_cgroup_empty(m, buf); + bus_forward_agent_released(m, buf); + + return 0; +} + +/// Add-On for manager_connect_bus() +int elogind_setup_cgroups_agent(Manager *m) { + + static const union sockaddr_union sa = { + .un.sun_family = AF_UNIX, + .un.sun_path = "/run/systemd/cgroups-agent", + }; + int r; + + /* This creates a listening socket we receive cgroups agent messages on. We do not use D-Bus for delivering + * these messages from the cgroups agent binary to PID 1, as the cgroups agent binary is very short-living, and + * each instance of it needs a new D-Bus connection. Since D-Bus connections are SOCK_STREAM/AF_UNIX, on + * overloaded systems the backlog of the D-Bus socket becomes relevant, as not more than the configured number + * of D-Bus connections may be queued until the kernel will start dropping further incoming connections, + * possibly resulting in lost cgroups agent messages. To avoid this, we'll use a private SOCK_DGRAM/AF_UNIX + * socket, where no backlog is relevant as communication may take place without an actual connect() cycle, and + * we thus won't lose messages. + * + * Note that PID 1 will forward the agent message to system bus, so that the user systemd instance may listen + * to it. The system instance hence listens on this special socket, but the user instances listen on the system + * bus for these messages. */ + + if (m->test_run) + return 0; + + if (!MANAGER_IS_SYSTEM(m)) + return 0; + + if (cg_unified() > 0) /* We don't need this anymore on the unified hierarchy */ + return 0; + + if (m->cgroups_agent_fd < 0) { + _cleanup_close_ int fd = -1; + + /* First free all secondary fields */ + m->cgroups_agent_event_source = sd_event_source_unref(m->cgroups_agent_event_source); + + fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0); + if (fd < 0) + return log_error_errno(errno, "Failed to allocate cgroups agent socket: %m"); + + fd_inc_rcvbuf(fd, CGROUPS_AGENT_RCVBUF_SIZE); + + (void) unlink(sa.un.sun_path); + + /* Only allow root to connect to this socket */ + RUN_WITH_UMASK(0077) + r = bind(fd, &sa.sa, SOCKADDR_UN_LEN(sa.un)); + if (r < 0) + return log_error_errno(errno, "bind(%s) failed: %m", sa.un.sun_path); + + m->cgroups_agent_fd = fd; + fd = -1; + } + + if (!m->cgroups_agent_event_source) { + r = sd_event_add_io(m->event, &m->cgroups_agent_event_source, m->cgroups_agent_fd, EPOLLIN, manager_dispatch_cgroups_agent_fd, m); + if (r < 0) + return log_error_errno(r, "Failed to allocate cgroups agent event source: %m"); + + /* Process cgroups notifications early, but after having processed service notification messages or + * SIGCHLD signals, so that a cgroup running empty is always just the last safety net of notification, + * and we collected the metadata the notification and SIGCHLD stuff offers first. Also see handling of + * cgroup inotify for the unified cgroup stuff. */ + r = sd_event_source_set_priority(m->cgroups_agent_event_source, SD_EVENT_PRIORITY_NORMAL-5); + if (r < 0) + return log_error_errno(r, "Failed to set priority of cgroups agent event source: %m"); + + (void) sd_event_source_set_description(m->cgroups_agent_event_source, "manager-cgroups-agent"); + } + + return 0; +} + +/// Add-On for manager_free() +void elogind_manager_free(Manager* m) { + + manager_shutdown_cgroup(m, true); + + sd_event_source_unref(m->cgroups_agent_event_source); + + safe_close(m->cgroups_agent_fd); + + strv_free(m->suspend_mode); + strv_free(m->suspend_state); + strv_free(m->hibernate_mode); + strv_free(m->hibernate_state); + strv_free(m->hybrid_sleep_mode); + strv_free(m->hybrid_sleep_state); +} + +/// Add-On for manager_new() +int elogind_manager_new(Manager* m) { + int r = 0; + + m->cgroups_agent_fd = -1; + m->pin_cgroupfs_fd = -1; + m->test_run = false; + + /* Init sleep modes and states */ + m->suspend_mode = NULL; + m->suspend_state = NULL; + m->hibernate_mode = NULL; + m->hibernate_state = NULL; + m->hybrid_sleep_mode = NULL; + m->hybrid_sleep_state = NULL; + + /* If elogind should be its own controller, mount its cgroup */ + if (streq(SYSTEMD_CGROUP_CONTROLLER, "name=elogind")) { + m->is_system = true; + r = mount_setup(true); + } else + m->is_system = false; + + /* Make cgroups */ + if (r > -1) + r = manager_setup_cgroup(m); + + return r; +} + +/// Add-On for manager_reset_config() +void elogind_manager_reset_config(Manager* m) { + +#ifdef ENABLE_DEBUG_ELOGIND + int dbg_cnt; +#endif // ENABLE_DEBUG_ELOGIND + + /* Set default Sleep config if not already set by logind.conf */ + if (!m->suspend_state) + m->suspend_state = strv_new("mem", "standby", "freeze", NULL); + if (!m->hibernate_mode) + m->hibernate_mode = strv_new("platform", "shutdown", NULL); + if (!m->hibernate_state) + m->hibernate_state = strv_new("disk", NULL); + if (!m->hybrid_sleep_mode) + m->hybrid_sleep_mode = strv_new("suspend", "platform", "shutdown", NULL); + if (!m->hybrid_sleep_state) + m->hybrid_sleep_state = strv_new("disk", NULL); + +#ifdef ENABLE_DEBUG_ELOGIND + dbg_cnt = -1; + while (m->suspend_mode && m->suspend_mode[++dbg_cnt]) + log_debug_elogind("suspend_mode[%d] = %s", + dbg_cnt, m->suspend_mode[dbg_cnt]); + dbg_cnt = -1; + while (m->suspend_state[++dbg_cnt]) + log_debug_elogind("suspend_state[%d] = %s", + dbg_cnt, m->suspend_state[dbg_cnt]); + dbg_cnt = -1; + while (m->hibernate_mode[++dbg_cnt]) + log_debug_elogind("hibernate_mode[%d] = %s", + dbg_cnt, m->hibernate_mode[dbg_cnt]); + dbg_cnt = -1; + while (m->hibernate_state[++dbg_cnt]) + log_debug_elogind("hibernate_state[%d] = %s", + dbg_cnt, m->hibernate_state[dbg_cnt]); + dbg_cnt = -1; + while (m->hybrid_sleep_mode[++dbg_cnt]) + log_debug_elogind("hybrid_sleep_mode[%d] = %s", + dbg_cnt, m->hybrid_sleep_mode[dbg_cnt]); + dbg_cnt = -1; + while (m->hybrid_sleep_state[++dbg_cnt]) + log_debug_elogind("hybrid_sleep_state[%d] = %s", + dbg_cnt, m->hybrid_sleep_state[dbg_cnt]); +#endif // ENABLE_DEBUG_ELOGIND +} diff --git a/src/login/elogind.h b/src/login/elogind.h new file mode 100644 index 000000000..155654b04 --- /dev/null +++ b/src/login/elogind.h @@ -0,0 +1,43 @@ +#pragma once +#ifndef ELOGIND_SRC_LOGIN_ELOGIN_H_INCLUDED +#define ELOGIND_SRC_LOGIN_ELOGIN_H_INCLUDED + +/*** + This file is part of elogind. + + Copyright 2017 Sven Eden + + elogind is free software; you can redistribute it and/or modify it + 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. + + elogind 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 elogind; If not, see . +***/ + + +#include "logind.h" +#include "sd-bus.h" + + +/// Add-Ons for manager_connect_bus() +void elogind_bus_setup_system(Manager* m); +int elogind_setup_cgroups_agent(Manager *m); + +/// Add-On for manager_free() +void elogind_manager_free(Manager* m); + +/// Add-On for manager_new() +int elogind_manager_new(Manager* m); + +/// Add-On for manager_reset_config() +void elogind_manager_reset_config(Manager* m); + + +#endif // ELOGIND_SRC_LOGIN_ELOGIN_H_INCLUDED diff --git a/src/login/logind.c b/src/login/logind.c index b97698100..e0f391f41 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -43,20 +43,16 @@ #include "strv.h" #include "udev-util.h" -/// additional includes elogind needs +/// Additional includes needed by elogind #include "cgroup.h" // From src/core/ +#include "elogind.h" #include "label.h" -#include "mount-setup.h" // From src/core #include "musl_missing.h" static void manager_free(Manager *m); static void manager_reset_config(Manager *m) { -#ifdef ENABLE_DEBUG_ELOGIND - int dbg_cnt; -#endif // ENABLE_DEBUG_ELOGIND - #if 0 /// elogind does not support autospawning of vts m->n_autovts = 6; m->reserve_vt = 6; @@ -88,44 +84,9 @@ static void manager_reset_config(Manager *m) { m->kill_only_users = strv_free(m->kill_only_users); m->kill_exclude_users = strv_free(m->kill_exclude_users); - /* Set default Sleep config if not already set by logind.conf */ - if (!m->suspend_state) - m->suspend_state = strv_new("mem", "standby", "freeze", NULL); - if (!m->hibernate_mode) - m->hibernate_mode = strv_new("platform", "shutdown", NULL); - if (!m->hibernate_state) - m->hibernate_state = strv_new("disk", NULL); - if (!m->hybrid_sleep_mode) - m->hybrid_sleep_mode = strv_new("suspend", "platform", "shutdown", NULL); - if (!m->hybrid_sleep_state) - m->hybrid_sleep_state = strv_new("disk", NULL); - -#ifdef ENABLE_DEBUG_ELOGIND - dbg_cnt = -1; - while (m->suspend_mode && m->suspend_mode[++dbg_cnt]) - log_debug_elogind("suspend_mode[%d] = %s", - dbg_cnt, m->suspend_mode[dbg_cnt]); - dbg_cnt = -1; - while (m->suspend_state[++dbg_cnt]) - log_debug_elogind("suspend_state[%d] = %s", - dbg_cnt, m->suspend_state[dbg_cnt]); - dbg_cnt = -1; - while (m->hibernate_mode[++dbg_cnt]) - log_debug_elogind("hibernate_mode[%d] = %s", - dbg_cnt, m->hibernate_mode[dbg_cnt]); - dbg_cnt = -1; - while (m->hibernate_state[++dbg_cnt]) - log_debug_elogind("hibernate_state[%d] = %s", - dbg_cnt, m->hibernate_state[dbg_cnt]); - dbg_cnt = -1; - while (m->hybrid_sleep_mode[++dbg_cnt]) - log_debug_elogind("hybrid_sleep_mode[%d] = %s", - dbg_cnt, m->hybrid_sleep_mode[dbg_cnt]); - dbg_cnt = -1; - while (m->hybrid_sleep_state[++dbg_cnt]) - log_debug_elogind("hybrid_sleep_state[%d] = %s", - dbg_cnt, m->hybrid_sleep_state[dbg_cnt]); -#endif // ENABLE_DEBUG_ELOGIND +#if 1 /// elogind needs an Add-On for sleep configuration + elogind_manager_reset_config(m); +#endif // 1 } static Manager *manager_new(void) { @@ -136,7 +97,6 @@ static Manager *manager_new(void) { if (!m) return NULL; - m->pin_cgroupfs_fd = -1; m->console_active_fd = -1; #if 0 /// UNNEEDED by elogind m->reserve_vt_fd = -1; @@ -154,29 +114,14 @@ static Manager *manager_new(void) { m->user_units = hashmap_new(&string_hash_ops); m->session_units = hashmap_new(&string_hash_ops); - m->test_run = false; - if (!m->devices || !m->seats || !m->sessions || !m->users || !m->inhibitors || !m->buttons || !m->user_units || !m->session_units) goto fail; - /* If elogind should be its own controller, mount its cgroup */ - if (streq(SYSTEMD_CGROUP_CONTROLLER, "name=elogind")) { - r = mount_setup(true); - if (r < 0) - goto fail; - } - - /* Make cgroups */ - r = manager_setup_cgroup(m); +#if 1 /// elogind needs some more data + r = elogind_manager_new(m); if (r < 0) goto fail; - - m->suspend_mode = NULL; - m->suspend_state = NULL; - m->hibernate_mode = NULL; - m->hibernate_state = NULL; - m->hybrid_sleep_mode = NULL; - m->hybrid_sleep_state = NULL; +#endif // 1 m->udev = udev_new(); if (!m->udev) @@ -269,7 +214,9 @@ static void manager_free(Manager *m) { safe_close(m->reserve_vt_fd); #endif // 0 - manager_shutdown_cgroup(m, true); +#if 1 /// elogind has to free its own data + elogind_manager_free(m); +#endif // 1 strv_free(m->kill_only_users); strv_free(m->kill_exclude_users); @@ -277,14 +224,6 @@ static void manager_free(Manager *m) { free(m->scheduled_shutdown_type); free(m->scheduled_shutdown_tty); free(m->wall_message); - - strv_free(m->suspend_mode); - strv_free(m->suspend_state); - strv_free(m->hibernate_mode); - strv_free(m->hibernate_state); - strv_free(m->hybrid_sleep_mode); - strv_free(m->hybrid_sleep_state); - #if 0 /// UNNEEDED by elogind free(m->action_job); #endif // 0 @@ -687,46 +626,6 @@ static int manager_reserve_vt(Manager *m) { } #endif // 0 -static int signal_agent_released(sd_bus_message *message, void *userdata, sd_bus_error *error) { - _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; - Manager *m = userdata; - const char *cgroup; - Session *s; - uid_t sender_uid; - int r; - - assert(message); - assert(m); - - /* only accept org.freedesktop.systemd1.Agent from UID=0 */ - r = sd_bus_query_sender_creds(message, SD_BUS_CREDS_EUID, &creds); - if (r < 0) - return r; - - r = sd_bus_creds_get_euid(creds, &sender_uid); - if (r < 0 || sender_uid != 0) - return 0; - - /* parse 'cgroup-empty' notification */ - r = sd_bus_message_read(message, "s", &cgroup); - if (r < 0) { - bus_log_parse_error(r); - return 0; - } - - s = hashmap_get(m->sessions, cgroup); - - if (!s) { - log_warning("Session not found: %s", cgroup); - return 0; - } - - session_finalize(s); - session_free(s); - - return 0; -} - static int manager_connect_bus(Manager *m) { _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; int r; @@ -742,16 +641,6 @@ static int manager_connect_bus(Manager *m) { if (r < 0) return log_error_errno(r, "Failed to add manager object vtable: %m"); - /* elogind relies on signals from its release agent */ - r = sd_bus_add_match( - m->bus, - NULL, - "type='signal'," - "interface='org.freedesktop.elogind.Agent'," - "member='Released'," - "path='/org/freedesktop/elogind/agent'", - signal_agent_released, 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"); @@ -842,7 +731,12 @@ static int manager_connect_bus(Manager *m) { if (r < 0) return log_error_errno(r, "Failed to attach bus to event loop: %m"); - return 0; +#if 1 /// elogind has to setup its release agent + elogind_bus_setup_system(m); + r = elogind_setup_cgroups_agent(m); +#endif // 1 + + return r; } static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo *si, void *data) { diff --git a/src/login/logind.conf.in b/src/login/logind.conf.in index fc497f25f..27b05fe8a 100644 --- a/src/login/logind.conf.in +++ b/src/login/logind.conf.in @@ -32,7 +32,7 @@ #RemoveIPC=yes #InhibitorsMax=8192 #SessionsMax=8192 -#UserTasksMax=12288 +#UserTasksMax=33% [Sleep] #SuspendState=mem standby freeze diff --git a/src/login/logind.h b/src/login/logind.h index 41b48ef74..9be29ab28 100644 --- a/src/login/logind.h +++ b/src/login/logind.h @@ -29,7 +29,6 @@ #include "sd-bus.h" #include "sd-event.h" -#include "cgroup-util.h" #include "hashmap.h" #include "list.h" #include "set.h" @@ -40,7 +39,14 @@ typedef struct Manager Manager; #include "logind-button.h" #include "logind-device.h" #include "logind-inhibit.h" -#include "logind-sleep.h" + +/// Additional includes needed by elogind +#include "cgroup-util.h" + +#if 1 /// elogind has to ident itself +#define MANAGER_IS_SYSTEM(m) ((m)->is_system) +#define MANAGER_IS_USER(m) (!((m)->is_system)) +#endif // 1 struct Manager { sd_event *event; @@ -78,8 +84,13 @@ struct Manager { * file system */ int pin_cgroupfs_fd; + /* fd for handling cgroup socket if elogind is its own cgroups manager */ + int cgroups_agent_fd; + sd_event_source *cgroups_agent_event_source; + /* Flags */ bool test_run:1; + bool is_system:1; /* true if elogind is its own cgroups manager */ /* Data specific to the cgroup subsystem */ CGroupMask cgroup_supported; -- 2.30.2