From: Andy Wingo Date: Sun, 6 Mar 2016 15:02:31 +0000 (+0100) Subject: Classify processes from sessions into cgroups X-Git-Tag: v226.4~7 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=commitdiff_plain;h=162255186046fa7a5e82859b7dba5a7909ce6fc4 Classify processes from sessions into cgroups Create a private cgroup tree associated with no controllers, and use it to map PIDs to sessions. Since we use our own path structure, remove internal cgroup-related helpers that interpret the cgroup path structure to pull out users, slices, and scopes. --- diff --git a/README b/README index 928bd7d0c..80e1ab9dc 100644 --- a/README +++ b/README @@ -29,7 +29,7 @@ provide a subset of the interfaces of systemd 219. To contribute to elogind, fork the current source code from github: - https://github.com/andywingo/elogind + https://github.com/wingo/elogind Send a pull request for the changes you like. @@ -39,7 +39,7 @@ To chat about elogind: Finally, bug reports: - https://github.com/andywingo/elogind/issues + https://github.com/wingo/elogind/issues Why bother? ----------- @@ -77,21 +77,26 @@ of . Libelogind just implements login-related functionality. It also provides the sd-bus API. -Unlike systemd, whose logind arranges to run user sessions in cgroups -via RPC calls to systemd, in elogind there is no systemd so there are -no cgroups. This has a few implications: +Unlike systemd, whose logind arranges to manage resources for user +sessions via RPC calls to systemd, in elogind there is no systemd so +there is no global cgroup-based resource management. This has a few +implications: * Elogind does not create "slices" for users. Elogind will not record that users are associated with slices. - * Systemd's logind waits for all user jobs to stop before recording - that a user's session has gone away. Since we have no cgroups, - elogind just removes the session directly when pam_elogind.so - indicates the user has logged out. - * The /run/systemd/slices directory will always be empty. - * Support for lingering is not so great. + * Elogind does not have the concept of a "scope", internally, as + it's the same as a session. Any API that refers to scopes will + always return an error code. + +On the other hand, elogind does use a similar strategy to systemd in +that it places processes in a private cgroup for organizational +purposes, without installing any controllers (see +http://0pointer.de/blog/projects/cgroups-vs-cgroups.html). This +allows elogind to map arbitrary processes to sessions, even if the +process does the usual double-fork to be reparented to PID 1. Elogind does not manage virtual terminals. diff --git a/src/cgroups-agent/cgroups-agent.c b/src/cgroups-agent/cgroups-agent.c index 529e84303..3be4cff80 100644 --- a/src/cgroups-agent/cgroups-agent.c +++ b/src/cgroups-agent/cgroups-agent.c @@ -38,17 +38,13 @@ int main(int argc, char *argv[]) { log_parse_environment(); log_open(); - /* We send this event to the private D-Bus socket and then the - * system instance will forward this to the system bus. We do - * this to avoid an activation loop when we start dbus when we - * are called when the dbus service is shut down. */ - - r = bus_open_system_systemd(&bus); + /* Unlike in systemd where this has to use a private socket, + since logind doesn't associate control groups with services + and doesn't manage the dbus service, we can just use the + system bus. */ + r = sd_bus_open_system(&bus); if (r < 0) { - /* If we couldn't connect we assume this was triggered - * while systemd got restarted/transitioned from - * initrd to the system, so let's ignore this */ - log_debug_errno(r, "Failed to get D-Bus connection: %m"); + log_debug_errno(r, "Failed to open system bus: %m"); return EXIT_FAILURE; } @@ -58,7 +54,7 @@ int main(int argc, char *argv[]) { "Released", "s", argv[1]); if (r < 0) { - log_debug_errno(r, "Failed to send signal message on private connection: %m"); + log_debug_errno(r, "Failed to send signal message: %m"); return EXIT_FAILURE; } diff --git a/src/libelogind/sd-bus/bus-creds.c b/src/libelogind/sd-bus/bus-creds.c index cd1c5e232..aea379d66 100644 --- a/src/libelogind/sd-bus/bus-creds.c +++ b/src/libelogind/sd-bus/bus-creds.c @@ -358,8 +358,6 @@ _public_ int sd_bus_creds_get_cgroup(sd_bus_creds *c, const char **ret) { } _public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) { - int r; - assert_return(c, -EINVAL); assert_return(ret, -EINVAL); @@ -368,25 +366,14 @@ _public_ int sd_bus_creds_get_unit(sd_bus_creds *c, const char **ret) { assert(c->cgroup); - if (!c->unit) { - const char *shifted; - - r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); - if (r < 0) - return r; - - r = cg_path_get_unit(shifted, (char**) &c->unit); - if (r < 0) - return r; - } + if (!c->unit) + return -ESRCH; *ret = c->unit; return 0; } _public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) { - int r; - assert_return(c, -EINVAL); assert_return(ret, -EINVAL); @@ -395,25 +382,14 @@ _public_ int sd_bus_creds_get_user_unit(sd_bus_creds *c, const char **ret) { assert(c->cgroup); - if (!c->user_unit) { - const char *shifted; - - r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); - if (r < 0) - return r; - - r = cg_path_get_user_unit(shifted, (char**) &c->user_unit); - if (r < 0) - return r; - } + if (!c->user_unit) + return -ESRCH; *ret = c->user_unit; return 0; } _public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) { - int r; - assert_return(c, -EINVAL); assert_return(ret, -EINVAL); @@ -422,17 +398,8 @@ _public_ int sd_bus_creds_get_slice(sd_bus_creds *c, const char **ret) { assert(c->cgroup); - if (!c->slice) { - const char *shifted; - - r = cg_shift_path(c->cgroup, c->cgroup_root, &shifted); - if (r < 0) - return r; - - r = cg_path_get_slice(shifted, (char**) &c->slice); - if (r < 0) - return r; - } + if (!c->slice) + return -ESRCH; *ret = c->slice; return 0; @@ -481,7 +448,7 @@ _public_ int sd_bus_creds_get_owner_uid(sd_bus_creds *c, uid_t *uid) { if (r < 0) return r; - return cg_path_get_owner_uid(shifted, uid); + return -ESRCH; } _public_ int sd_bus_creds_get_cmdline(sd_bus_creds *c, char ***cmdline) { diff --git a/src/libelogind/sd-bus/sd-bus.c b/src/libelogind/sd-bus/sd-bus.c index 2cb1aafa2..35de5b051 100644 --- a/src/libelogind/sd-bus/sd-bus.c +++ b/src/libelogind/sd-bus/sd-bus.c @@ -1124,10 +1124,7 @@ _public_ int sd_bus_open(sd_bus **ret) { e = secure_getenv("DBUS_STARTER_ADDRESS"); if (!e) { - if (cg_pid_get_owner_uid(0, NULL) >= 0) - return sd_bus_open_user(ret); - else - return sd_bus_open_system(ret); + return sd_bus_open_system(ret); } r = sd_bus_new(&b); @@ -3327,10 +3324,7 @@ _public_ int sd_bus_default(sd_bus **ret) { /* Finally, if nothing is set use the cached connection for * the right scope */ - if (cg_pid_get_owner_uid(0, NULL) >= 0) - return sd_bus_default_user(ret); - else - return sd_bus_default_system(ret); + return sd_bus_default_system(ret); } _public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { diff --git a/src/libelogind/sd-login/sd-login.c b/src/libelogind/sd-login/sd-login.c index cc0677bdf..8e2b7c1dc 100644 --- a/src/libelogind/sd-login/sd-login.c +++ b/src/libelogind/sd-login/sd-login.c @@ -46,7 +46,7 @@ _public_ int sd_pid_get_unit(pid_t pid, char **unit) { assert_return(pid >= 0, -EINVAL); assert_return(unit, -EINVAL); - return cg_pid_get_unit(pid, unit); + return -ESRCH; } _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) { @@ -54,7 +54,7 @@ _public_ int sd_pid_get_user_unit(pid_t pid, char **unit) { assert_return(pid >= 0, -EINVAL); assert_return(unit, -EINVAL); - return cg_pid_get_user_unit(pid, unit); + return -ESRCH; } _public_ int sd_pid_get_machine_name(pid_t pid, char **name) { @@ -62,7 +62,7 @@ _public_ int sd_pid_get_machine_name(pid_t pid, char **name) { assert_return(pid >= 0, -EINVAL); assert_return(name, -EINVAL); - return cg_pid_get_machine_name(pid, name); + return -ESRCH; } _public_ int sd_pid_get_slice(pid_t pid, char **slice) { @@ -70,7 +70,7 @@ _public_ int sd_pid_get_slice(pid_t pid, char **slice) { assert_return(pid >= 0, -EINVAL); assert_return(slice, -EINVAL); - return cg_pid_get_slice(pid, slice); + return -ESRCH; } _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) { @@ -78,7 +78,7 @@ _public_ int sd_pid_get_owner_uid(pid_t pid, uid_t *uid) { assert_return(pid >= 0, -EINVAL); assert_return(uid, -EINVAL); - return cg_pid_get_owner_uid(pid, uid); + return -ESRCH; } _public_ int sd_peer_get_session(int fd, char **session) { @@ -106,7 +106,7 @@ _public_ int sd_peer_get_owner_uid(int fd, uid_t *uid) { if (r < 0) return r; - return cg_pid_get_owner_uid(ucred.pid, uid); + return -ESRCH; } _public_ int sd_peer_get_unit(int fd, char **unit) { @@ -120,7 +120,7 @@ _public_ int sd_peer_get_unit(int fd, char **unit) { if (r < 0) return r; - return cg_pid_get_unit(ucred.pid, unit); + return -ESRCH; } _public_ int sd_peer_get_user_unit(int fd, char **unit) { @@ -134,7 +134,7 @@ _public_ int sd_peer_get_user_unit(int fd, char **unit) { if (r < 0) return r; - return cg_pid_get_user_unit(ucred.pid, unit); + return -ESRCH; } _public_ int sd_peer_get_machine_name(int fd, char **machine) { @@ -148,7 +148,7 @@ _public_ int sd_peer_get_machine_name(int fd, char **machine) { if (r < 0) return r; - return cg_pid_get_machine_name(ucred.pid, machine); + return -ESRCH; } _public_ int sd_peer_get_slice(int fd, char **slice) { @@ -162,7 +162,7 @@ _public_ int sd_peer_get_slice(int fd, char **slice) { if (r < 0) return r; - return cg_pid_get_slice(ucred.pid, slice); + return -ESRCH; } static int file_of_uid(uid_t uid, char **p) { diff --git a/src/login/logind-core.c b/src/login/logind-core.c index 67b03623a..041593c5a 100644 --- a/src/login/logind-core.c +++ b/src/login/logind-core.c @@ -311,24 +311,41 @@ int manager_process_button_device(Manager *m, struct udev_device *d) { } int manager_get_session_by_pid(Manager *m, pid_t pid, Session **session) { + _cleanup_free_ char *session_name = NULL; + Session *s; + int r; + assert(m); assert(session); - /* Without cgroups, we have no way to map from pid to - session. */ - return 0; + if (pid < 1) + return -EINVAL; + + r = cg_pid_get_session(pid, &session_name); + if (r < 0) + return 0; + + s = hashmap_get(m->sessions, session_name); + if (!s) + return 0; + + *session = s; + return 1; } int manager_get_user_by_pid(Manager *m, pid_t pid, User **user) { + Session *s; + int r; + assert(m); assert(user); - if (pid < 1) - return -EINVAL; + r = manager_get_session_by_pid (m, pid, &s); + if (r <= 0) + return r; - /* Without cgroups, we have no way to map from pid to - user. */ - return 0; + *user = s->user; + return 1; } int manager_get_idle_hint(Manager *m, dual_timestamp *t) { diff --git a/src/login/logind-session.c b/src/login/logind-session.c index 109e3ef13..f03c2e565 100644 --- a/src/login/logind-session.c +++ b/src/login/logind-session.c @@ -36,6 +36,8 @@ #include "audit.h" #include "bus-util.h" #include "bus-error.h" +#include "cgroup-util.h" +#include "def.h" #include "logind-session.h" static void session_remove_fifo(Session *s); @@ -475,6 +477,25 @@ int session_activate(Session *s) { return 0; } +static int session_start_cgroup(Session *s) { + int r; + + assert(s); + assert(s->user); + assert(s->leader > 0); + + /* First, create our own group */ + r = cg_create(SYSTEMD_CGROUP_CONTROLLER, s->id); + if (r < 0) + return log_error_errno(r, "Failed to create cgroup %s: %m", s->id); + + r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, s->id, s->leader); + if (r < 0) + log_warning_errno(r, "Failed to attach PID %d to cgroup %s: %m", s->leader, s->id); + + return 0; +} + int session_start(Session *s) { int r; @@ -490,6 +511,10 @@ int session_start(Session *s) { if (r < 0) return r; + r = session_start_cgroup(s); + if (r < 0) + return r; + log_struct(s->class == SESSION_BACKGROUND ? LOG_DEBUG : LOG_INFO, LOG_MESSAGE_ID(SD_MESSAGE_SESSION_START), "SESSION_ID=%s", s->id, @@ -527,8 +552,23 @@ int session_start(Session *s) { return 0; } +static int session_stop_cgroup(Session *s, bool force) { + _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; + int r; + + assert(s); + + if (force || manager_shall_kill(s->manager, s->user->name)) { + r = session_kill(s, KILL_ALL, SIGTERM); + if (r < 0) + return r; + } + + return 0; +} + int session_stop(Session *s, bool force) { - int r = 0; + int r; assert(s); @@ -540,6 +580,9 @@ int session_stop(Session *s, bool force) { /* We are going down, don't care about FIFOs anymore */ session_remove_fifo(s); + /* Kill cgroup */ + r = session_stop_cgroup(s, force); + s->stopping = true; user_elect_display(s->user); @@ -609,7 +652,7 @@ int session_release(Session *s) { /* In systemd, session release is triggered by user jobs dying. In elogind we don't have that so go ahead and stop now. */ - session_stop(s, false); + return session_stop(s, false); } bool session_is_active(Session *s) { @@ -814,6 +857,9 @@ bool session_check_gc(Session *s, bool drop_not_started) { return true; } + if (cg_is_empty_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, false) > 0) + return true; + return false; } @@ -846,8 +892,23 @@ SessionState session_get_state(Session *s) { int session_kill(Session *s, KillWho who, int signo) { assert(s); - /* No way to kill the session without cgroups. */ - return -ESRCH; + if (who == KILL_LEADER) { + if (s->leader <= 0) + return -ESRCH; + + /* FIXME: verify that leader is in cgroup? */ + + if (kill(s->leader, signo) < 0) { + return log_error_errno(errno, "Failed to kill process leader %d for session %s: %m", s->leader, s->id); + } + return 0; + } else { + bool sigcont = false; + bool ignore_self = true; + bool rem = true; + return cg_kill_recursive (SYSTEMD_CGROUP_CONTROLLER, s->id, signo, + sigcont, ignore_self, rem, NULL); + } } static int session_open_vt(Session *s) { diff --git a/src/login/logind-user.c b/src/login/logind-user.c index 539c316ad..0aac3bcf5 100644 --- a/src/login/logind-user.c +++ b/src/login/logind-user.c @@ -562,10 +562,18 @@ UserState user_get_state(User *u) { } int user_kill(User *u, int signo) { + Session *s; + int res = 0; + assert(u); - /* FIXME: No way to kill a user without systemd. */ - return -ESRCH; + LIST_FOREACH(sessions_by_user, s, u->sessions) { + int r = session_kill(s, KILL_ALL, signo); + if (res == 0 && r < 0) + res = r; + } + + return res; } void user_elect_display(User *u) { diff --git a/src/login/logind.c b/src/login/logind.c index 49dae036c..8e8fe1915 100644 --- a/src/login/logind.c +++ b/src/login/logind.c @@ -542,6 +542,35 @@ static int manager_dispatch_console(sd_event_source *s, int fd, uint32_t revents return 0; } +static int signal_agent_released(sd_bus *bus, sd_bus_message *message, void *userdata, sd_bus_error *error) { + Manager *m = userdata; + Session *s; + const char *cgroup; + int r; + + assert(bus); + assert(message); + assert(m); + + 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_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; int r; @@ -557,6 +586,13 @@ static int manager_connect_bus(Manager *m) { if (r < 0) return log_error_errno(r, "Failed to add manager object vtable: %m"); + r = sd_bus_add_match(m->bus, NULL, + "type='signal'," + "interface='org.freedesktop.systemd1.Agent'," + "member='Released'," + "path='/org/freedesktop/systemd1/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"); diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 05c3125ae..99d06540a 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -34,7 +34,6 @@ #include "macro.h" #include "util.h" #include "path-util.h" -#include "unit-name.h" #include "fileio.h" #include "special.h" #include "mkdir.h" @@ -439,7 +438,7 @@ static const char *normalize_controller(const char *controller) { assert(controller); if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) - return "systemd"; + return "elogind"; else if (startswith(controller, "name=")) return controller + 5; else @@ -1058,21 +1057,9 @@ int cg_mangle_path(const char *path, char **result) { } int cg_get_root_path(char **path) { - char *p, *e; - int r; - assert(path); - r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, &p); - if (r < 0) - return r; - - e = endswith(p, "/" SPECIAL_SYSTEM_SLICE); - if (e) - *e = 0; - - *path = p; - return 0; + return cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, 1, path); } int cg_shift_path(const char *cgroup, const char *root, const char **shifted) { @@ -1135,183 +1122,16 @@ int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { return 0; } -int cg_path_decode_unit(const char *cgroup, char **unit){ - char *e, *c, *s; - - assert(cgroup); - assert(unit); - - e = strchrnul(cgroup, '/'); - c = strndupa(cgroup, e - cgroup); - c = cg_unescape(c); - - if (!unit_name_is_valid(c, TEMPLATE_INVALID)) - return -EINVAL; - - s = strdup(c); - if (!s) - return -ENOMEM; - - *unit = s; - return 0; -} - -static const char *skip_slices(const char *p) { - /* Skips over all slice assignments */ - - for (;;) { - size_t n; - - p += strspn(p, "/"); - - n = strcspn(p, "/"); - if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) - return p; - - p += n; - } -} - -int cg_path_get_unit(const char *path, char **unit) { - const char *e; - - assert(path); - assert(unit); - - e = skip_slices(path); - - return cg_path_decode_unit(e, unit); -} - -int cg_pid_get_unit(pid_t pid, char **unit) { - _cleanup_free_ char *cgroup = NULL; - int r; - - assert(unit); - - r = cg_pid_get_path_shifted(pid, NULL, &cgroup); - if (r < 0) - return r; - - return cg_path_get_unit(cgroup, unit); -} - -/** - * Skip session-*.scope, but require it to be there. - */ -static const char *skip_session(const char *p) { - size_t n; - - assert(p); - - p += strspn(p, "/"); - - n = strcspn(p, "/"); - if (n < strlen("session-x.scope") || memcmp(p, "session-", 8) != 0 || memcmp(p + n - 6, ".scope", 6) != 0) - return NULL; - - p += n; - p += strspn(p, "/"); - - return p; -} - -/** - * Skip user@*.service, but require it to be there. - */ -static const char *skip_user_manager(const char *p) { - size_t n; - - assert(p); - - p += strspn(p, "/"); - - n = strcspn(p, "/"); - if (n < strlen("user@x.service") || memcmp(p, "user@", 5) != 0 || memcmp(p + n - 8, ".service", 8) != 0) - return NULL; - - p += n; - p += strspn(p, "/"); - - return p; -} - -int cg_path_get_user_unit(const char *path, char **unit) { - const char *e, *t; - - assert(path); - assert(unit); - - /* We always have to parse the path from the beginning as unit - * cgroups might have arbitrary child cgroups and we shouldn't get - * confused by those */ - - /* Skip slices, if there are any */ - e = skip_slices(path); - - /* Skip the session scope or user manager... */ - t = skip_session(e); - if (!t) - t = skip_user_manager(e); - if (!t) - return -ENOENT; - - /* ... and skip more slices if there are any */ - e = skip_slices(t); - - return cg_path_decode_unit(e, unit); -} - -int cg_pid_get_user_unit(pid_t pid, char **unit) { - _cleanup_free_ char *cgroup = NULL; - int r; - - assert(unit); - - r = cg_pid_get_path_shifted(pid, NULL, &cgroup); - if (r < 0) - return r; - - return cg_path_get_user_unit(cgroup, unit); -} - -int cg_path_get_machine_name(const char *path, char **machine) { - _cleanup_free_ char *u = NULL, *sl = NULL; - int r; - - r = cg_path_get_unit(path, &u); - if (r < 0) - return r; - - sl = strjoin("/run/systemd/machines/unit:", u, NULL); - if (!sl) - return -ENOMEM; - - return readlink_malloc(sl, machine); -} - -int cg_pid_get_machine_name(pid_t pid, char **machine) { - _cleanup_free_ char *cgroup = NULL; - int r; - - assert(machine); - - r = cg_pid_get_path_shifted(pid, NULL, &cgroup); - if (r < 0) - return r; - - return cg_path_get_machine_name(cgroup, machine); -} - int cg_path_get_session(const char *path, char **session) { - const char *e, *n, *x, *y; - char *s; + const char *e, *n, *s; - assert(path); + /* Elogind uses a flat hierarchy, just "/SESSION". The only + wrinkle is that SESSION might be escaped. */ - /* Skip slices, if there are any */ - e = skip_slices(path); + assert(path); + assert(path[0] == '/'); + e = path + 1; n = strchrnul(e, '/'); if (e == n) return -ENOENT; @@ -1319,17 +1139,13 @@ int cg_path_get_session(const char *path, char **session) { s = strndupa(e, n - e); s = cg_unescape(s); - x = startswith(s, "session-"); - if (!x) - return -ENOENT; - y = endswith(x, ".scope"); - if (!y || x == y) + if (!s[0]) return -ENOENT; if (session) { char *r; - r = strndup(x, y - x); + r = strdup(s); if (!r) return -ENOMEM; @@ -1350,97 +1166,6 @@ int cg_pid_get_session(pid_t pid, char **session) { return cg_path_get_session(cgroup, session); } -int cg_path_get_owner_uid(const char *path, uid_t *uid) { - _cleanup_free_ char *slice = NULL; - const char *start, *end; - char *s; - uid_t u; - int r; - - assert(path); - - r = cg_path_get_slice(path, &slice); - if (r < 0) - return r; - - start = startswith(slice, "user-"); - if (!start) - return -ENOENT; - end = endswith(slice, ".slice"); - if (!end) - return -ENOENT; - - s = strndupa(start, end - start); - if (!s) - return -ENOENT; - - if (parse_uid(s, &u) < 0) - return -EIO; - - if (uid) - *uid = u; - - return 0; -} - -int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) { - _cleanup_free_ char *cgroup = NULL; - int r; - - r = cg_pid_get_path_shifted(pid, NULL, &cgroup); - if (r < 0) - return r; - - return cg_path_get_owner_uid(cgroup, uid); -} - -int cg_path_get_slice(const char *p, char **slice) { - const char *e = NULL; - size_t m = 0; - - assert(p); - assert(slice); - - for (;;) { - size_t n; - - p += strspn(p, "/"); - - n = strcspn(p, "/"); - if (n <= 6 || memcmp(p + n - 6, ".slice", 6) != 0) { - char *s; - - if (!e) - return -ENOENT; - - s = strndup(e, m); - if (!s) - return -ENOMEM; - - *slice = s; - return 0; - } - - e = p; - m = n; - - p += n; - } -} - -int cg_pid_get_slice(pid_t pid, char **slice) { - _cleanup_free_ char *cgroup = NULL; - int r; - - assert(slice); - - r = cg_pid_get_path_shifted(pid, NULL, &cgroup); - if (r < 0) - return r; - - return cg_path_get_slice(cgroup, slice); -} - char *cg_escape(const char *p) { bool need_prefix = false; @@ -1527,56 +1252,6 @@ bool cg_controller_is_valid(const char *p, bool allow_named) { return true; } -int cg_slice_to_path(const char *unit, char **ret) { - _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL; - const char *dash; - - assert(unit); - assert(ret); - - if (!unit_name_is_valid(unit, TEMPLATE_INVALID)) - return -EINVAL; - - if (!endswith(unit, ".slice")) - return -EINVAL; - - p = unit_name_to_prefix(unit); - if (!p) - return -ENOMEM; - - dash = strchr(p, '-'); - while (dash) { - _cleanup_free_ char *escaped = NULL; - char n[dash - p + sizeof(".slice")]; - - strcpy(stpncpy(n, p, dash - p), ".slice"); - - if (!unit_name_is_valid(n, TEMPLATE_INVALID)) - return -EINVAL; - - escaped = cg_escape(n); - if (!escaped) - return -ENOMEM; - - if (!strextend(&s, escaped, "/", NULL)) - return -ENOMEM; - - dash = strchr(dash+1, '-'); - } - - e = cg_escape(unit); - if (!e) - return -ENOMEM; - - if (!strextend(&s, e, NULL)) - return -ENOMEM; - - *ret = s; - s = NULL; - - return 0; -} - int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) { _cleanup_free_ char *p = NULL; int r; diff --git a/src/shared/cgroup-util.h b/src/shared/cgroup-util.h index 96a3d3baf..cc2aeeb97 100644 --- a/src/shared/cgroup-util.h +++ b/src/shared/cgroup-util.h @@ -99,31 +99,17 @@ int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_ int cg_get_root_path(char **path); int cg_path_get_session(const char *path, char **session); -int cg_path_get_owner_uid(const char *path, uid_t *uid); -int cg_path_get_unit(const char *path, char **unit); -int cg_path_get_user_unit(const char *path, char **unit); -int cg_path_get_machine_name(const char *path, char **machine); -int cg_path_get_slice(const char *path, char **slice); int cg_shift_path(const char *cgroup, const char *cached_root, const char **shifted); int cg_pid_get_path_shifted(pid_t pid, const char *cached_root, char **cgroup); int cg_pid_get_session(pid_t pid, char **session); -int cg_pid_get_owner_uid(pid_t pid, uid_t *uid); -int cg_pid_get_unit(pid_t pid, char **unit); -int cg_pid_get_user_unit(pid_t pid, char **unit); -int cg_pid_get_machine_name(pid_t pid, char **machine); -int cg_pid_get_slice(pid_t pid, char **slice); - -int cg_path_decode_unit(const char *cgroup, char **unit); char *cg_escape(const char *p); char *cg_unescape(const char *p) _pure_; bool cg_controller_is_valid(const char *p, bool allow_named); -int cg_slice_to_path(const char *unit, char **ret); - typedef const char* (*cg_migrate_callback_t)(CGroupControllerMask mask, void *userdata); int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path);