X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fshared%2Fcgroup-util.c;h=2c2ffc589879ead8b7f04c2cf31bcff833d428cb;hb=2cfbd749af308bdbe56edcfed7f3eea0fc2b93d2;hp=8a4eddab7a27e63fa76636d41b78c979b005c79c;hpb=1434ae6fd49f8377b0ddbd4c675736e0d3226ea6;p=elogind.git diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index 8a4eddab7..2c2ffc589 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -279,37 +279,6 @@ int cg_kill_recursive(const char *controller, const char *path, int sig, bool si return ret; } -int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) { - unsigned i; - - assert(path); - - /* This safely kills all processes; first it sends a SIGTERM, - * then checks 8 times after 200ms whether the group is now - * empty, then kills everything that is left with SIGKILL and - * finally checks 5 times after 200ms each whether the group - * is finally empty. */ - - for (i = 0; i < 15; i++) { - int sig, r; - - if (i <= 0) - sig = SIGTERM; - else if (i == 9) - sig = SIGKILL; - else - sig = 0; - - r = cg_kill_recursive(controller, path, sig, true, true, rem, NULL); - if (r <= 0) - return r; - - usleep(200 * USEC_PER_MSEC); - } - - return 0; -} - int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) { bool done = false; _cleanup_set_free_ Set *s = NULL; @@ -941,19 +910,6 @@ int cg_is_empty(const char *controller, const char *path, bool ignore_self) { return !found; } -int cg_is_empty_by_spec(const char *spec, bool ignore_self) { - _cleanup_free_ char *controller = NULL, *path = NULL; - int r; - - assert(spec); - - r = cg_split_spec(spec, &controller, &path); - if (r < 0) - return r; - - return cg_is_empty(controller, path, ignore_self); -} - int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) { _cleanup_closedir_ DIR *d = NULL; char *fn; @@ -1080,33 +1036,6 @@ int cg_split_spec(const char *spec, char **controller, char **path) { return 0; } -int cg_join_spec(const char *controller, const char *path, char **spec) { - char *s; - - assert(path); - - if (!controller) - controller = "systemd"; - else { - if (!cg_controller_is_valid(controller, true)) - return -EINVAL; - - controller = normalize_controller(controller); - } - - if (!path_is_absolute(path)) - return -EINVAL; - - s = strjoin(controller, ":", path, NULL); - if (!s) - return -ENOMEM; - - path_kill_slashes(s + strlen(controller) + 1); - - *spec = s; - return 0; -} - int cg_mangle_path(const char *path, char **result) { _cleanup_free_ char *c = NULL, *p = NULL; char *t; @@ -1153,79 +1082,42 @@ int cg_get_root_path(char **path) { return 0; } -char **cg_shorten_controllers(char **controllers) { - char **f, **t; - - if (!controllers) - return controllers; - - for (f = controllers, t = controllers; *f; f++) { - const char *p; - int r; - - p = normalize_controller(*f); +int cg_pid_get_path_shifted(pid_t pid, const char *root, char **cgroup) { + _cleanup_free_ char *cg_root = NULL; + char *cg_process, *p; + int r; - if (streq(p, "systemd")) { - free(*f); - continue; - } + assert(pid >= 0); + assert(cgroup); - if (!cg_controller_is_valid(p, true)) { - log_warning("Controller %s is not valid, removing from controllers list.", p); - free(*f); - continue; - } + if (!root) { + /* If the root was specified let's use that, otherwise + * let's determine it from PID 1 */ - r = check_hierarchy(p); - if (r < 0) { - log_debug("Controller %s is not available, removing from controllers list.", p); - free(*f); - continue; - } + r = cg_get_root_path(&cg_root); + if (r < 0) + return r; - *(t++) = *f; + root = cg_root; } - *t = NULL; - return strv_uniq(controllers); -} - -int cg_pid_get_path_shifted(pid_t pid, char **root, char **cgroup) { - _cleanup_free_ char *cg_root = NULL; - char *cg_process, *p; - int r; - - r = cg_get_root_path(&cg_root); - if (r < 0) - return r; - r = cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, pid, &cg_process); if (r < 0) return r; - p = path_startswith(cg_process, cg_root); - if (p) - p--; - else - p = cg_process; + p = path_startswith(cg_process, root); + if (p) { + char *c; - if (cgroup) { - char* c; + c = strdup(p - 1); + free(cg_process); - c = strdup(p); - if (!c) { - free(cg_process); + if (!c) return -ENOMEM; - } *cgroup = c; - } - - if (root) { - cg_process[p-cg_process] = 0; - *root = cg_process; } else - free(cg_process); + *cgroup = cg_process; return 0; } @@ -1445,36 +1337,41 @@ int cg_pid_get_session(pid_t pid, char **session) { int cg_path_get_owner_uid(const char *path, uid_t *uid) { _cleanup_free_ char *slice = NULL; - const char *e; + const char *start, *end; char *s; + uid_t u; int r; assert(path); - assert(uid); r = cg_path_get_slice(path, &slice); if (r < 0) return r; - e = startswith(slice, "user-"); - if (!e) + start = startswith(slice, "user-"); + if (!start) return -ENOENT; - if (!endswith(slice, ".slice")) + end = endswith(slice, ".slice"); + if (!end) return -ENOENT; - s = strndupa(e, strlen(e) - 6); + s = strndupa(start, end - start); if (!s) - return -ENOMEM; + return -ENOENT; + + if (parse_uid(s, &u) < 0) + return -EIO; + + if (uid) + *uid = u; - return parse_uid(s, uid); + return 0; } int cg_pid_get_owner_uid(pid_t pid, uid_t *uid) { _cleanup_free_ char *cgroup = NULL; int r; - assert(uid); - r = cg_pid_get_path_shifted(pid, NULL, &cgroup); if (r < 0) return r; @@ -1529,35 +1426,6 @@ int cg_pid_get_slice(pid_t pid, char **slice) { return cg_path_get_slice(cgroup, slice); } -int cg_controller_from_attr(const char *attr, char **controller) { - const char *dot; - char *c; - - assert(attr); - assert(controller); - - if (!filename_is_safe(attr)) - return -EINVAL; - - dot = strchr(attr, '.'); - if (!dot) { - *controller = NULL; - return 0; - } - - c = strndup(attr, dot - attr); - if (!c) - return -ENOMEM; - - if (!cg_controller_is_valid(c, false)) { - free(c); - return -EINVAL; - } - - *controller = c; - return 1; -} - char *cg_escape(const char *p) { bool need_prefix = false;