X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fshared%2Fcgroup-util.c;h=c746d606d9ff648c29cbcd530d579b5e87acd547;hp=9692a07f56484a2071321fb9371e66cca79543be;hb=01c94c5d0aff09b4c0e429d483c8eeba40017071;hpb=89f7c8465cd1ab37347dd0c15920bce31e8225df diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 9692a07f5..c746d606d 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -30,13 +30,11 @@ #include #include "cgroup-util.h" -#include "log.h" #include "set.h" #include "macro.h" #include "util.h" +#include "formats-util.h" #include "path-util.h" -#include "strv.h" -#include "unit-name.h" #include "fileio.h" #include "special.h" #include "mkdir.h" @@ -161,7 +159,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool sigcont, boo * tasks list, to properly handle forking processes */ if (!s) { - s = allocated_set = set_new(trivial_hash_func, trivial_compare_func); + s = allocated_set = set_new(NULL); if (!s) return -ENOMEM; } @@ -195,7 +193,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool sigcont, boo if (ret >= 0 && errno != ESRCH) ret = -errno; } else { - if (sigcont) + if (sigcont && sig != SIGKILL) kill(pid, SIGCONT); if (ret == 0) @@ -239,7 +237,7 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool si assert(sig >= 0); if (!s) { - s = allocated_set = set_new(trivial_hash_func, trivial_compare_func); + s = allocated_set = set_new(NULL); if (!s) return -ENOMEM; } @@ -290,7 +288,7 @@ int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char assert(cto); assert(pto); - s = set_new(trivial_hash_func, trivial_compare_func); + s = set_new(NULL); if (!s) return -ENOMEM; @@ -441,7 +439,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 @@ -472,9 +470,7 @@ static int join_path(const char *controller, const char *path, const char *suffi if (!t) return -ENOMEM; - path_kill_slashes(t); - - *fs = t; + *fs = path_kill_slashes(t); return 0; } @@ -491,8 +487,10 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch int r; r = path_is_mount_point("/sys/fs/cgroup", false); - if (r <= 0) - return r < 0 ? r : -ENOENT; + if (r < 0) + return r; + if (r == 0) + return -ENOENT; /* Cache this to save a few stat()s */ good = true; @@ -504,14 +502,16 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch } static int check_hierarchy(const char *p) { - char *cc; + const char *cc; assert(p); + if (!filename_is_valid(p)) + return 0; + /* Check if this controller actually really exists */ - cc = alloca(sizeof("/sys/fs/cgroup/") + strlen(p)); - strcpy(stpcpy(cc, "/sys/fs/cgroup/"), p); - if (access(cc, F_OK) < 0) + cc = strjoina("/sys/fs/cgroup/", p); + if (laccess(cc, F_OK) < 0) return -errno; return 0; @@ -643,9 +643,9 @@ int cg_attach(const char *controller, const char *path, pid_t pid) { if (pid == 0) pid = getpid(); - snprintf(c, sizeof(c), "%lu\n", (unsigned long) pid); + snprintf(c, sizeof(c), PID_FMT"\n", pid); - return write_string_file(fs, c); + return write_string_file_no_create(fs, c); } int cg_attach_fallback(const char *controller, const char *path, pid_t pid) { @@ -684,7 +684,7 @@ int cg_set_group_access( assert(path); - if (mode != (mode_t) -1) + if (mode != MODE_INVALID) mode &= 0777; r = cg_get_path(controller, path, NULL, &fs); @@ -706,10 +706,10 @@ int cg_set_task_access( assert(path); - if (mode == (mode_t) -1 && uid == (uid_t) -1 && gid == (gid_t) -1) + if (mode == MODE_INVALID && uid == UID_INVALID && gid == GID_INVALID) return 0; - if (mode != (mode_t) -1) + if (mode != MODE_INVALID) mode &= 0666; r = cg_get_path(controller, path, "cgroup.procs", &fs); @@ -755,9 +755,9 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) { cs = strlen(controller); FOREACH_LINE(line, f, return -errno) { - char *l, *p, *w, *e; + char *l, *p, *e; size_t k; - char *state; + const char *word, *state; bool found = false; truncate_nl(line); @@ -773,16 +773,16 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) { *e = 0; - FOREACH_WORD_SEPARATOR(w, k, l, ",", state) { + FOREACH_WORD_SEPARATOR(word, k, l, ",", state) { - if (k == cs && memcmp(w, controller, cs) == 0) { + if (k == cs && memcmp(word, controller, cs) == 0) { found = true; break; } if (k == 5 + cs && - memcmp(w, "name=", 5) == 0 && - memcmp(w+5, controller, cs) == 0) { + memcmp(word, "name=", 5) == 0 && + memcmp(word+5, controller, cs) == 0) { found = true; break; } @@ -819,7 +819,7 @@ int cg_install_release_agent(const char *controller, const char *agent) { sc = strstrip(contents); if (sc[0] == 0) { - r = write_string_file(fs, agent); + r = write_string_file_no_create(fs, agent); if (r < 0) return r; } else if (!streq(sc, agent)) @@ -839,7 +839,7 @@ int cg_install_release_agent(const char *controller, const char *agent) { sc = strstrip(contents); if (streq(sc, "0")) { - r = write_string_file(fs, "1"); + r = write_string_file_no_create(fs, "1"); if (r < 0) return r; @@ -860,7 +860,7 @@ int cg_uninstall_release_agent(const char *controller) { if (r < 0) return r; - r = write_string_file(fs, "0"); + r = write_string_file_no_create(fs, "0"); if (r < 0) return r; @@ -871,7 +871,7 @@ int cg_uninstall_release_agent(const char *controller) { if (r < 0) return r; - r = write_string_file(fs, ""); + r = write_string_file_no_create(fs, ""); if (r < 0) return r; @@ -957,8 +957,7 @@ int cg_split_spec(const char *spec, char **controller, char **path) { if (!t) return -ENOMEM; - path_kill_slashes(t); - *path = t; + *path = path_kill_slashes(t); } if (controller) @@ -1041,19 +1040,18 @@ int cg_mangle_path(const char *path, char **result) { assert(path); assert(result); - /* First check if it already is a filesystem path */ + /* First, check if it already is a filesystem path */ if (path_startswith(path, "/sys/fs/cgroup")) { t = strdup(path); if (!t) return -ENOMEM; - path_kill_slashes(t); - *result = t; + *result = path_kill_slashes(t); return 0; } - /* Otherwise treat it as cg spec */ + /* Otherwise, treat it as cg spec */ r = cg_split_spec(path, &c, &p); if (r < 0) return r; @@ -1062,21 +1060,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) { @@ -1139,186 +1125,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... */ - t = skip_session(e); - if (t) - /* ... and skip more slices if there's one */ - e = skip_slices(t); - else { - /* ... or require a user manager unit to be there */ - e = skip_user_manager(e); - if (!e) - return -ENOENT; - } - - 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; - char *s; - size_t l; + 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; @@ -1326,20 +1142,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; - if (!endswith(x, ".scope")) - return -ENOENT; - - l = strlen(x); - if (l <= 6) + if (!s[0]) return -ENOENT; if (session) { char *r; - r = strndup(x, l - 6); + r = strdup(s); if (!r) return -ENOMEM; @@ -1360,97 +1169,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; @@ -1537,57 +1255,18 @@ 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; +int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) { + _cleanup_free_ char *p = NULL; + int r; - *ret = s; - s = NULL; + r = cg_get_path(controller, path, attribute, &p); + if (r < 0) + return r; - return 0; + return write_string_file_no_create(p, value); } -int cg_set_attribute(const char *controller, const char *path, const char *attribute, const char *value) { +int cg_get_attribute(const char *controller, const char *path, const char *attribute, char **ret) { _cleanup_free_ char *p = NULL; int r; @@ -1595,7 +1274,7 @@ int cg_set_attribute(const char *controller, const char *path, const char *attri if (r < 0) return r; - return write_string_file(p, value); + return read_one_line_file(p, ret); } static const char mask_names[] = @@ -1632,7 +1311,7 @@ int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask ma return 0; } -int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid) { +int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) { CGroupControllerMask bit = 1; const char *n; int r; @@ -1642,8 +1321,18 @@ int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t return r; NULSTR_FOREACH(n, mask_names) { - if (supported & bit) + + if (supported & bit) { + const char *p = NULL; + + if (path_callback) + p = path_callback(bit, userdata); + + if (!p) + p = path; + cg_attach_fallback(n, path, pid); + } bit <<= 1; } @@ -1651,7 +1340,7 @@ int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t return 0; } -int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids) { +int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, Set* pids, cg_migrate_callback_t path_callback, void *userdata) { Iterator i; void *pidp; int r = 0; @@ -1660,7 +1349,7 @@ int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, pid_t pid = PTR_TO_LONG(pidp); int q; - q = cg_attach_everywhere(supported, path, pid); + q = cg_attach_everywhere(supported, path, pid, path_callback, userdata); if (q < 0) r = q; } @@ -1668,7 +1357,7 @@ int cg_attach_many_everywhere(CGroupControllerMask supported, const char *path, return r; } -int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to) { +int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) { CGroupControllerMask bit = 1; const char *n; int r; @@ -1680,8 +1369,17 @@ int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, cons } NULSTR_FOREACH(n, mask_names) { - if (supported & bit) - cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, to, false, false); + if (supported & bit) { + const char *p = NULL; + + if (to_callback) + p = to_callback(bit, userdata); + + if (!p) + p = to; + + cg_migrate_recursive_fallback(SYSTEMD_CGROUP_CONTROLLER, to, n, p, false, false); + } bit <<= 1; } @@ -1721,3 +1419,54 @@ CGroupControllerMask cg_mask_supported(void) { return mask; } + +int cg_kernel_controllers(Set *controllers) { + _cleanup_fclose_ FILE *f = NULL; + char buf[LINE_MAX]; + int r; + + assert(controllers); + + f = fopen("/proc/cgroups", "re"); + if (!f) { + if (errno == ENOENT) + return 0; + return -errno; + } + + /* Ignore the header line */ + (void) fgets(buf, sizeof(buf), f); + + for (;;) { + char *controller; + int enabled = 0; + + errno = 0; + if (fscanf(f, "%ms %*i %*i %i", &controller, &enabled) != 2) { + + if (feof(f)) + break; + + if (ferror(f) && errno) + return -errno; + + return -EBADMSG; + } + + if (!enabled) { + free(controller); + continue; + } + + if (!filename_is_valid(controller)) { + free(controller); + return -EBADMSG; + } + + r = set_consume(controllers, controller); + if (r < 0) + return r; + } + + return 0; +}