X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fcgroup-util.c;h=b286243c3629cb165ed3c8c353f50b5387e80f8e;hb=d896ac2d2fbce41a0b11a0618a685adeaf18b8fe;hp=ce951174fb636df5ed9ad0c4d04d06347c15a870;hpb=6b1c671c3ab4a44ec18f3059e45f197fbe8502f3;p=elogind.git diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index ce951174f..b286243c3 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -440,9 +440,7 @@ static const char *normalize_controller(const char *controller) { assert(controller); - if (streq(controller, SYSTEMD_CGROUP_CONTROLLER)) - return "systemd"; - else if (startswith(controller, "name=")) + if (startswith(controller, "name=")) return controller + 5; else return controller; @@ -482,13 +480,13 @@ int cg_get_path(const char *controller, const char *path, const char *suffix, ch assert(fs); - if (controller && !cg_controller_is_valid(controller, true)) + if (controller && !cg_controller_is_valid(controller)) return -EINVAL; if (_unlikely_(!good)) { int r; - r = path_is_mount_point("/sys/fs/cgroup", false); + r = path_is_mount_point("/sys/fs/cgroup", 0); if (r < 0) return r; if (r == 0) @@ -525,7 +523,7 @@ int cg_get_path_and_check(const char *controller, const char *path, const char * assert(fs); - if (!cg_controller_is_valid(controller, true)) + if (!cg_controller_is_valid(controller)) return -EINVAL; /* Normalize the controller syntax */ @@ -741,7 +739,7 @@ int cg_pid_get_path(const char *controller, pid_t pid, char **path) { assert(pid >= 0); if (controller) { - if (!cg_controller_is_valid(controller, true)) + if (!cg_controller_is_valid(controller)) return -EINVAL; controller = normalize_controller(controller); @@ -970,7 +968,7 @@ int cg_split_spec(const char *spec, char **controller, char **path) { e = strchr(spec, ':'); if (!e) { - if (!cg_controller_is_valid(spec, true)) + if (!cg_controller_is_valid(spec)) return -EINVAL; if (controller) { @@ -993,7 +991,7 @@ int cg_split_spec(const char *spec, char **controller, char **path) { t = strdup(normalize_controller(v)); if (!t) return -ENOMEM; - if (!cg_controller_is_valid(t, true)) { + if (!cg_controller_is_valid(t)) { free(t); return -EINVAL; } @@ -1153,7 +1151,7 @@ int cg_path_decode_unit(const char *cgroup, char **unit){ c = strndupa(cgroup, n); c = cg_unescape(c); - if (!unit_name_is_valid(c, TEMPLATE_INVALID)) + if (!unit_name_is_valid(c, UNIT_NAME_PLAIN|UNIT_NAME_INSTANCE)) return -ENXIO; s = strdup(c); @@ -1180,7 +1178,7 @@ static bool valid_slice_name(const char *p, size_t n) { c = cg_unescape(buf); - return unit_name_is_valid(c, TEMPLATE_INVALID); + return unit_name_is_valid(c, UNIT_NAME_PLAIN); } return false; @@ -1316,45 +1314,37 @@ static const char *skip_user_manager(const char *p) { return NULL; } -int cg_path_get_user_unit(const char *path, char **ret) { +static const char *skip_user_prefix(const char *path) { const char *e, *t; - char *unit; - int r; assert(path); - assert(ret); - - /* 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 user manager... */ + /* Skip the user manager, if it's in the path now... */ t = skip_user_manager(e); + if (t) + return t; - /* Alternatively skip the user session... */ - if (!t) - t = skip_session(e); - if (!t) - return -ENXIO; + /* Alternatively skip the user session if it is in the path... */ + return skip_session(e); +} - /* ... and skip more slices if there are any */ - e = skip_slices(t); +int cg_path_get_user_unit(const char *path, char **ret) { + const char *t; - r = cg_path_decode_unit(e, &unit); - if (r < 0) - return r; + assert(path); + assert(ret); - /* We skipped over the slices, don't accept any now */ - if (endswith(unit, ".slice")) { - free(unit); + t = skip_user_prefix(path); + if (!t) return -ENXIO; - } - *ret = unit; - return 0; + /* And from here on it looks pretty much the same as for a + * system unit, hence let's use the same parser from here + * on. */ + return cg_path_get_unit(t, ret); } int cg_pid_get_user_unit(pid_t pid, char **unit) { @@ -1486,6 +1476,9 @@ int cg_path_get_slice(const char *p, char **slice) { assert(p); assert(slice); + /* Finds the right-most slice unit from the beginning, but + * stops before we come to the first non-slice unit. */ + for (;;) { size_t n; @@ -1526,6 +1519,33 @@ int cg_pid_get_slice(pid_t pid, char **slice) { return cg_path_get_slice(cgroup, slice); } +int cg_path_get_user_slice(const char *p, char **slice) { + const char *t; + assert(p); + assert(slice); + + t = skip_user_prefix(p); + if (!t) + return -ENXIO; + + /* And now it looks pretty much the same as for a system + * slice, so let's just use the same parser from here on. */ + return cg_path_get_slice(t, slice); +} + +int cg_pid_get_user_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_user_slice(cgroup, slice); +} + char *cg_escape(const char *p) { bool need_prefix = false; @@ -1587,17 +1607,15 @@ char *cg_unescape(const char *p) { DIGITS LETTERS \ "_" -bool cg_controller_is_valid(const char *p, bool allow_named) { +bool cg_controller_is_valid(const char *p) { const char *t, *s; if (!p) return false; - if (allow_named) { - s = startswith(p, "name="); - if (s) - p = s; - } + s = startswith(p, "name="); + if (s) + p = s; if (*p == 0 || *p == '_') return false; @@ -1615,28 +1633,41 @@ bool cg_controller_is_valid(const char *p, bool allow_named) { int cg_slice_to_path(const char *unit, char **ret) { _cleanup_free_ char *p = NULL, *s = NULL, *e = NULL; const char *dash; + int r; assert(unit); assert(ret); - if (!unit_name_is_valid(unit, TEMPLATE_INVALID)) + if (streq(unit, "-.slice")) { + char *x; + + x = strdup(""); + if (!x) + return -ENOMEM; + *ret = x; + return 0; + } + + if (!unit_name_is_valid(unit, UNIT_NAME_PLAIN)) return -EINVAL; if (!endswith(unit, ".slice")) return -EINVAL; - p = unit_name_to_prefix(unit); - if (!p) - return -ENOMEM; + r = unit_name_to_prefix(unit, &p); + if (r < 0) + return r; dash = strchr(p, '-'); while (dash) { _cleanup_free_ char *escaped = NULL; char n[dash - p + sizeof(".slice")]; - strcpy(stpncpy(n, p, dash - p), ".slice"); + if (isempty(dash + 1)) + return -EINVAL; - if (!unit_name_is_valid(n, TEMPLATE_INVALID)) + strcpy(stpncpy(n, p, dash - p), ".slice"); + if (!unit_name_is_valid(n, UNIT_NAME_PLAIN)) return -EINVAL; escaped = cg_escape(n);