From 6f5035f2db6ab7b4beae46c44b95e6a8b6d956b5 Mon Sep 17 00:00:00 2001 From: Andy Wingo Date: Wed, 2 Mar 2016 20:50:35 +0100 Subject: Set up cgroups when logind starts This code is from systemd. --- src/login/logind.c | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/login/logind.h | 7 +++++++ src/shared/def.h | 2 +- 3 files changed, 65 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/login/logind.c b/src/login/logind.c index bc611df8e3..49dae036c4 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -28,6 +28,7 @@ #include "label.h" #include "sd-daemon.h" #include "strv.h" +#include "cgroup-util.h" #include "conf-parser.h" #include "bus-util.h" #include "bus-error.h" @@ -632,6 +633,57 @@ static int manager_vt_switch(sd_event_source *src, const struct signalfd_siginfo return 0; } +static int manager_setup_cgroup(Manager *m) { + _cleanup_free_ char *path = NULL; + int r; + + assert(m); + + /* 1. Determine hierarchy */ + free(m->cgroup_root); + m->cgroup_root = NULL; + + r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 0, &m->cgroup_root); + if (r < 0) + return log_error_errno(r, "Cannot determine cgroup we are running in: %m"); + + /* Make sure to store away the root value without trailing + * slash, even for the root dir, so that we can easily prepend + * it everywhere. */ + if (streq(m->cgroup_root, "/")) + m->cgroup_root[0] = 0; + + /* 2. Show data */ + r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, NULL, &path); + if (r < 0) + return log_error_errno(r, "Cannot find cgroup mount point: %m"); + + log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path); + + /* 3. Install agent */ + r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, SYSTEMD_CGROUP_AGENT_PATH); + if (r < 0) + log_warning_errno(r, "Failed to install release agent, ignoring: %m"); + else if (r > 0) + log_debug("Installed release agent."); + else + log_debug("Release agent already installed."); + + /* 4. Make sure we are in the root cgroup */ + r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_root, 0); + if (r < 0) + return log_error_errno(r, "Failed to create root cgroup hierarchy: %m"); + + /* 5. And pin it, so that it cannot be unmounted */ + safe_close(m->pin_cgroupfs_fd); + + m->pin_cgroupfs_fd = open(path, O_RDONLY|O_CLOEXEC|O_DIRECTORY|O_NOCTTY|O_NONBLOCK); + if (m->pin_cgroupfs_fd < 0) + return log_error_errno(errno, "Failed to open pin file: %m"); + + return 0; +} + static int manager_connect_console(Manager *m) { int r; @@ -891,6 +943,11 @@ int manager_startup(Manager *m) { assert(m); + /* Make cgroups */ + r = manager_setup_cgroup(m); + if (r < 0) + return r; + /* Connect to console */ r = manager_connect_console(m); if (r < 0) diff --git a/src/login/logind.h b/src/login/logind.h index 90c972df6c..8645e85723 100644 --- a/src/login/logind.h +++ b/src/login/logind.h @@ -63,6 +63,13 @@ struct Manager { sd_event_source *udev_vcsa_event_source; sd_event_source *udev_button_event_source; + /* Make sure the user cannot accidentally unmount our cgroup + * file system */ + int pin_cgroupfs_fd; + + /* Data specific to the cgroup subsystem */ + char *cgroup_root; + int console_active_fd; Seat *seat0; diff --git a/src/shared/def.h b/src/shared/def.h index a6046b4d3f..7958f18c1e 100644 --- a/src/shared/def.h +++ b/src/shared/def.h @@ -35,7 +35,7 @@ * the watchdog pings will keep the loop busy. */ #define DEFAULT_EXIT_USEC (30*USEC_PER_SEC) -#define SYSTEMD_CGROUP_CONTROLLER "name=systemd" +#define SYSTEMD_CGROUP_CONTROLLER "name=elogind" #define SIGNALS_CRASH_HANDLER SIGSEGV,SIGILL,SIGFPE,SIGBUS,SIGQUIT,SIGABRT #define SIGNALS_IGNORE SIGPIPE -- cgit v1.2.3-54-g00ecf