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=4e0211a7a04758c12c292064020c476972b81509;hp=dc0fe85ee22a3fb189253b1c2f044348bf453e2a;hb=5b12334d35eadf1f45cc3d631fd1a2e72ffaea0a;hpb=fecffe5d0a1bd66d80e5a8728ff8a89673be0df7 diff --git a/src/shared/cgroup-util.c b/src/shared/cgroup-util.c index dc0fe85ee..4e0211a7a 100644 --- a/src/shared/cgroup-util.c +++ b/src/shared/cgroup-util.c @@ -39,6 +39,7 @@ #include "unit-name.h" #include "fileio.h" #include "special.h" +#include "mkdir.h" int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) { _cleanup_free_ char *fs = NULL; @@ -278,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; @@ -618,6 +588,46 @@ int cg_delete(const char *controller, const char *path) { return r == -ENOENT ? 0 : r; } +int cg_create(const char *controller, const char *path) { + _cleanup_free_ char *fs = NULL; + int r; + + r = cg_get_path_and_check(controller, path, NULL, &fs); + if (r < 0) + return r; + + r = mkdir_parents(fs, 0755); + if (r < 0) + return r; + + if (mkdir(fs, 0755) < 0) { + + if (errno == EEXIST) + return 0; + + return -errno; + } + + return 1; +} + +int cg_create_and_attach(const char *controller, const char *path, pid_t pid) { + int r, q; + + assert(pid >= 0); + + r = cg_create(controller, path); + if (r < 0) + return r; + + q = cg_attach(controller, path, pid); + if (q < 0) + return q; + + /* This does not remove the cgroup on failure */ + return r; +} + int cg_attach(const char *controller, const char *path, pid_t pid) { _cleanup_free_ char *fs = NULL; char c[DECIMAL_STR_MAX(pid_t) + 2]; @@ -900,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; @@ -1003,19 +1000,28 @@ int cg_split_spec(const char *spec, char **controller, char **path) { return -EINVAL; } - u = strdup(e+1); - if (!u) { - free(t); - return -ENOMEM; - } - if (!path_is_safe(u) || - !path_is_absolute(u)) { - free(t); - free(u); - return -EINVAL; - } + if (streq(e+1, "")) { + u = strdup("/"); + if (!u) { + free(t); + return -ENOMEM; + } + } else { + u = strdup(e+1); + if (!u) { + free(t); + return -ENOMEM; + } + + if (!path_is_safe(u) || + !path_is_absolute(u)) { + free(t); + free(u); + return -EINVAL; + } - path_kill_slashes(u); + path_kill_slashes(u); + } if (controller) *controller = t; @@ -1030,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; @@ -1103,43 +1082,6 @@ 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); - - if (streq(p, "systemd")) { - free(*f); - continue; - } - - if (!cg_controller_is_valid(p, true)) { - log_warning("Controller %s is not valid, removing from controllers list.", p); - free(*f); - continue; - } - - r = check_hierarchy(p); - if (r < 0) { - log_debug("Controller %s is not available, removing from controllers list.", p); - free(*f); - continue; - } - - *(t++) = *f; - } - - *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; @@ -1479,35 +1421,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;