cgroup-util.c revision 934277fe6a26ff2a4da37059c70d84ab6a700781
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <dirent.h>
#include <ftw.h>
#include "cgroup-util.h"
#include "log.h"
#include "set.h"
#include "macro.h"
#include "util.h"
#include "path-util.h"
#include "strv.h"
#include "unit-name.h"
#include "fileio.h"
#include "special.h"
#include "mkdir.h"
FILE *f;
int r;
if (r < 0)
return r;
if (!f)
return -errno;
*_f = f;
return 0;
}
unsigned long ul;
/* Note that the cgroup.procs might contain duplicates! See
* cgroups.txt for details. */
assert(f);
errno = 0;
if (feof(f))
return 0;
}
if (ul <= 0)
return -EIO;
return 1;
}
int r;
DIR *d;
/* This is not recursive! */
if (r < 0)
return r;
if (!d)
return -errno;
*_d = d;
return 0;
}
assert(d);
char *b;
continue;
continue;
if (!b)
return -ENOMEM;
*fn = b;
return 1;
}
return 0;
}
_cleanup_free_ char *p = NULL;
int r;
if (r < 0)
return r;
r = rmdir(p);
return -errno;
return 0;
}
int cg_kill(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, Set *s) {
bool done = false;
int r, ret = 0;
/* This goes through the tasks list and kills them all. This
* is repeated until no further processes are added to the
* tasks list, to properly handle forking processes */
if (!s) {
if (!s)
return -ENOMEM;
}
do {
done = true;
if (r < 0) {
return r;
return ret;
}
while ((r = cg_read_pid(f, &pid)) > 0) {
continue;
continue;
/* If we haven't killed this process yet, kill
* it */
} else {
if (ret == 0)
ret = 1;
}
done = false;
if (r < 0) {
if (ret >= 0)
return r;
return ret;
}
}
if (r < 0) {
if (ret >= 0)
return r;
return ret;
}
/* To avoid racing against processes which fork
* quicker than we can kill them we repeat this until
* no new pids need to be killed. */
} while (!done);
return ret;
}
int cg_kill_recursive(const char *controller, const char *path, int sig, bool sigcont, bool ignore_self, bool rem, Set *s) {
int r, ret = 0;
char *fn;
if (!s) {
if (!s)
return -ENOMEM;
}
if (r < 0) {
return r;
return ret;
}
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;
if (!p)
return -ENOMEM;
if (ret >= 0 && r != 0)
ret = r;
}
if (ret >= 0 && r < 0)
ret = r;
if (rem) {
return r;
}
return ret;
}
int cg_migrate(const char *cfrom, const char *pfrom, const char *cto, const char *pto, bool ignore_self) {
bool done = false;
int r, ret = 0;
if (!s)
return -ENOMEM;
do {
done = true;
if (r < 0) {
return r;
return ret;
}
while ((r = cg_read_pid(f, &pid)) > 0) {
/* This might do weird stuff if we aren't a
* single-threaded program. However, we
* luckily know we are not */
continue;
continue;
if (r < 0) {
ret = r;
} else if (ret == 0)
ret = 1;
done = false;
if (r < 0) {
if (ret >= 0)
return r;
return ret;
}
}
if (r < 0) {
if (ret >= 0)
return r;
return ret;
}
} while (!done);
return ret;
}
int cg_migrate_recursive(
const char *cfrom,
const char *pfrom,
const char *cto,
const char *pto,
bool ignore_self,
bool rem) {
int r, ret = 0;
char *fn;
if (r < 0) {
return r;
return ret;
}
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;
if (!p) {
if (ret >= 0)
return -ENOMEM;
return ret;
}
if (r != 0 && ret >= 0)
ret = r;
}
if (r < 0 && ret >= 0)
ret = r;
if (rem) {
return r;
}
return ret;
}
const char *cfrom,
const char *pfrom,
const char *cto,
const char *pto,
bool ignore_self,
bool rem) {
int r;
if (r < 0) {
/* This didn't work? Then let's try all prefixes of the destination */
if (r >= 0)
break;
}
}
return 0;
}
static const char *normalize_controller(const char *controller) {
return "systemd";
return controller + 5;
else
return controller;
}
char *t = NULL;
if (!isempty(controller)) {
else
} else {
else
return -EINVAL;
}
if (!t)
return -ENOMEM;
*fs = path_kill_slashes(t);
return 0;
}
const char *p;
static thread_local bool good = false;
return -EINVAL;
if (_unlikely_(!good)) {
int r;
r = path_is_mount_point("/sys/fs/cgroup", false);
if (r <= 0)
return r < 0 ? r : -ENOENT;
/* Cache this to save a few stat()s */
good = true;
}
}
static int check_hierarchy(const char *p) {
const char *cc;
assert(p);
if (!filename_is_valid(p))
return 0;
/* Check if this controller actually really exists */
return -errno;
return 0;
}
int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
const char *p;
int r;
if (!cg_controller_is_valid(controller, true))
return -EINVAL;
/* Normalize the controller syntax */
/* Check if this controller actually really exists */
r = check_hierarchy(p);
if (r < 0)
return r;
}
return 0;
return 0;
return 0;
}
int r = 0;
if (r < 0)
return r;
errno = 0;
if (delete_root) {
return -errno;
}
return r;
}
int r;
if (r < 0)
return r;
return r == -ENOENT ? 0 : r;
}
int r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
return -errno;
}
return 1;
}
int r, q;
if (r < 0)
return r;
if (q < 0)
return q;
/* This does not remove the cgroup on failure */
return r;
}
int r;
if (r < 0)
return r;
if (pid == 0)
return write_string_file_no_create(fs, c);
}
int r;
if (r < 0) {
/* This didn't work? Then let's try all prefixes of
* the destination */
if (r >= 0)
break;
}
}
return 0;
}
int cg_set_group_access(
const char *controller,
const char *path,
int r;
if (mode != MODE_INVALID)
mode &= 0777;
if (r < 0)
return r;
}
int cg_set_task_access(
const char *controller,
const char *path,
int r;
return 0;
if (mode != MODE_INVALID)
mode &= 0666;
if (r < 0)
return r;
if (r < 0)
return r;
/* Compatibility, Always keep values for "tasks" in sync with
* "cgroup.procs" */
if (r < 0)
return r;
}
const char *fs;
if (controller) {
if (!cg_controller_is_valid(controller, true))
return -EINVAL;
} else
if (!f)
char *l, *p, *e;
size_t k;
bool found = false;
if (!l)
continue;
l++;
e = strchr(l, ':');
if (!e)
continue;
*e = 0;
found = true;
break;
}
if (k == 5 + cs &&
found = true;
break;
}
}
if (!found)
continue;
p = strdup(e + 1);
if (!p)
return -ENOMEM;
*path = p;
return 0;
}
return -ENOENT;
}
char *sc;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (sc[0] == 0) {
if (r < 0)
return r;
return -EEXIST;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 1;
}
return -EIO;
return 0;
}
int cg_uninstall_release_agent(const char *controller) {
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
if (r < 0)
return r;
return 0;
}
bool found = false;
int r;
if (r < 0)
return r == -ENOENT ? 1 : r;
while ((r = cg_read_pid(f, &pid)) > 0) {
continue;
found = true;
break;
}
if (r < 0)
return r;
return !found;
}
char *fn;
int r;
if (r <= 0)
return r;
if (r < 0)
return r == -ENOENT ? 1 : r;
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;
if (!p)
return -ENOMEM;
if (r <= 0)
return r;
}
if (r < 0)
return r;
return 1;
}
const char *e;
_cleanup_free_ char *v = NULL;
if (*spec == '/') {
if (!path_is_safe(spec))
return -EINVAL;
if (path) {
if (!t)
return -ENOMEM;
*path = path_kill_slashes(t);
}
if (controller)
*controller = NULL;
return 0;
}
if (!e) {
if (!cg_controller_is_valid(spec, true))
return -EINVAL;
if (controller) {
if (!t)
return -ENOMEM;
*controller = t;
}
if (path)
return 0;
}
if (!v)
return -ENOMEM;
t = strdup(normalize_controller(v));
if (!t)
return -ENOMEM;
if (!cg_controller_is_valid(t, true)) {
free(t);
return -EINVAL;
}
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;
}
}
if (controller)
*controller = t;
else
free(t);
if (path)
*path = u;
else
free(u);
return 0;
}
char *t;
int r;
/* First, check if it already is a filesystem path */
if (!t)
return -ENOMEM;
*result = path_kill_slashes(t);
return 0;
}
/* Otherwise, treat it as cg spec */
r = cg_split_spec(path, &c, &p);
if (r < 0)
return r;
}
int cg_get_root_path(char **path) {
char *p, *e;
int r;
if (r < 0)
return r;
if (e)
*e = 0;
*path = p;
return 0;
}
char *p;
int r;
if (!root) {
/* If the root was specified let's use that, otherwise
* let's determine it from PID 1 */
r = cg_get_root_path(&rt);
if (r < 0)
return r;
}
if (p)
*shifted = p - 1;
else
return 0;
}
const char *c;
int r;
if (r < 0)
return r;
if (r < 0)
return r;
if (c == raw) {
} else {
char *n;
n = strdup(c);
if (!n)
return -ENOMEM;
*cgroup = n;
}
return 0;
}
char *e, *c, *s;
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, "/");
return p;
p += n;
}
}
const char *e;
e = skip_slices(path);
return cg_path_decode_unit(e, unit);
}
int r;
if (r < 0)
return r;
}
/**
* 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;
}
const char *e, *t;
/* 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 r;
if (r < 0)
return r;
}
int r;
r = cg_path_get_unit(path, &u);
if (r < 0)
return r;
if (!sl)
return -ENOMEM;
}
int r;
if (r < 0)
return r;
}
const char *e, *n, *x, *y;
char *s;
/* Skip slices, if there are any */
e = skip_slices(path);
n = strchrnul(e, '/');
if (e == n)
return -ENOENT;
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)
return -ENOENT;
if (session) {
char *r;
r = strndup(x, y - x);
if (!r)
return -ENOMEM;
*session = r;
}
return 0;
}
int r;
if (r < 0)
return r;
}
char *s;
uid_t u;
int r;
if (r < 0)
return r;
if (!start)
return -ENOENT;
if (!end)
return -ENOENT;
if (!s)
return -ENOENT;
if (parse_uid(s, &u) < 0)
return -EIO;
if (uid)
*uid = u;
return 0;
}
int r;
if (r < 0)
return r;
}
int cg_path_get_slice(const char *p, char **slice) {
const char *e = NULL;
size_t m = 0;
assert(p);
for (;;) {
size_t n;
p += strspn(p, "/");
n = strcspn(p, "/");
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 r;
if (r < 0)
return r;
}
char *cg_escape(const char *p) {
bool need_prefix = false;
/* This implements very minimal escaping for names to be used
* as file names in the cgroup tree: any name which might
* conflict with a kernel name or is prefixed with '_' is
* prefixed with a '_'. That way, when reading cgroup names it
* is sufficient to remove a single prefixing underscore if
* there is one. */
/* The return value of this function (unlike cg_unescape())
* needs free()! */
if (p[0] == 0 ||
p[0] == '_' ||
p[0] == '.' ||
streq(p, "notify_on_release") ||
streq(p, "release_agent") ||
streq(p, "tasks"))
need_prefix = true;
else {
const char *dot;
if (dot) {
need_prefix = true;
else {
char *n;
if (check_hierarchy(n) >= 0)
need_prefix = true;
}
}
}
if (need_prefix)
return strappend("_", p);
else
return strdup(p);
}
char *cg_unescape(const char *p) {
assert(p);
/* The return value of this function (unlike cg_escape())
* doesn't need free()! */
if (p[0] == '_')
return (char*) p+1;
return (char*) p;
}
#define CONTROLLER_VALID \
"_"
bool cg_controller_is_valid(const char *p, bool allow_named) {
const char *t, *s;
if (!p)
return false;
if (allow_named) {
s = startswith(p, "name=");
if (s)
p = s;
}
if (*p == 0 || *p == '_')
return false;
for (t = p; *t; t++)
if (!strchr(CONTROLLER_VALID, *t))
return false;
if (t - p > FILENAME_MAX)
return false;
return true;
}
const char *dash;
return -EINVAL;
return -EINVAL;
p = unit_name_to_prefix(unit);
if (!p)
return -ENOMEM;
while (dash) {
char n[dash - p + sizeof(".slice")];
if (!unit_name_is_valid(n, TEMPLATE_INVALID))
return -EINVAL;
if (!escaped)
return -ENOMEM;
return -ENOMEM;
}
if (!e)
return -ENOMEM;
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;
if (r < 0)
return r;
return write_string_file_no_create(p, value);
}
_cleanup_free_ char *p = NULL;
int r;
if (r < 0)
return r;
return read_one_line_file(p, ret);
}
static const char mask_names[] =
"cpu\0"
"cpuacct\0"
"blkio\0"
"memory\0"
"devices\0";
int cg_create_everywhere(CGroupControllerMask supported, CGroupControllerMask mask, const char *path) {
const char *n;
int r;
/* This one will create a cgroup in our private tree, but also
* duplicate it in the trees specified in mask, and remove it
* in all others */
/* First create the cgroup in our own hierarchy. */
if (r < 0)
return r;
/* Then, do the same in the other hierarchies */
NULSTR_FOREACH(n, mask_names) {
bit <<= 1;
}
return 0;
}
int cg_attach_everywhere(CGroupControllerMask supported, const char *path, pid_t pid, cg_migrate_callback_t path_callback, void *userdata) {
const char *n;
int r;
if (r < 0)
return r;
NULSTR_FOREACH(n, mask_names) {
const char *p = NULL;
if (path_callback)
if (!p)
p = path;
}
bit <<= 1;
}
return 0;
}
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;
int q;
if (q < 0)
r = q;
}
return r;
}
int cg_migrate_everywhere(CGroupControllerMask supported, const char *from, const char *to, cg_migrate_callback_t to_callback, void *userdata) {
const char *n;
int r;
r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, from, SYSTEMD_CGROUP_CONTROLLER, to, false, true);
if (r < 0)
return r;
}
NULSTR_FOREACH(n, mask_names) {
const char *p = NULL;
if (to_callback)
if (!p)
p = to;
}
bit <<= 1;
}
return 0;
}
const char *n;
int r;
if (r < 0)
return r;
NULSTR_FOREACH(n, mask_names) {
bit <<= 1;
}
return 0;
}
const char *n;
NULSTR_FOREACH(n, mask_names) {
if (check_hierarchy(n) >= 0)
bit <<= 1;
}
return mask;
}
int r;
if (!f) {
return 0;
return -errno;
}
/* Ignore the header line */
for (;;) {
char *controller;
int enabled = 0;
errno = 0;
if (feof(f))
break;
return -errno;
return -EBADMSG;
}
if (!enabled) {
continue;
}
if (!filename_is_valid(controller)) {
return -EBADMSG;
}
if (r < 0)
return r;
}
return 0;
}