cgroup-util.c revision 8b0849e9710d721c5d0b775aaf0fd662eefa1449
/*-*- 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 "set.h"
#include "macro.h"
#include "util.h"
#include "formats-util.h"
#include "process-util.h"
#include "path-util.h"
#include "unit-name.h"
#include "fileio.h"
#include "special.h"
#include "mkdir.h"
#include "login-shared.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;
if (r == 0)
return -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 *c, *s;
size_t n;
if (n < 3)
return -ENXIO;
c = cg_unescape(c);
if (!unit_name_is_valid(c, TEMPLATE_INVALID))
return -ENXIO;
s = strdup(c);
if (!s)
return -ENOMEM;
*unit = s;
return 0;
}
static bool valid_slice_name(const char *p, size_t n) {
if (!p)
return false;
return false;
char buf[n+1], *c;
buf[n] = 0;
c = cg_unescape(buf);
return unit_name_is_valid(c, TEMPLATE_INVALID);
}
return false;
}
static const char *skip_slices(const char *p) {
assert(p);
/* Skips over all slice assignments */
for (;;) {
size_t n;
p += strspn(p, "/");
n = strcspn(p, "/");
if (!valid_slice_name(p, n))
return p;
p += n;
}
}
const char *e;
char *unit;
int r;
e = skip_slices(path);
r = cg_path_decode_unit(e, &unit);
if (r < 0)
return r;
/* We skipped over the slices, don't accept any now */
return -ENXIO;
}
return 0;
}
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;
if (isempty(p))
return NULL;
p += strspn(p, "/");
n = strcspn(p, "/");
if (n < strlen("session-x.scope"))
return NULL;
/* Note that session scopes never need unescaping,
* since they cannot conflict with the kernel's own
* names, hence we don't need to call cg_unescape()
* here. */
if (!session_id_valid(buf))
return false;
p += n;
p += strspn(p, "/");
return p;
}
return NULL;
}
/**
* Skip user@*.service, but require it to be there.
*/
static const char *skip_user_manager(const char *p) {
size_t n;
if (isempty(p))
return NULL;
p += strspn(p, "/");
n = strcspn(p, "/");
if (n < strlen("user@x.service"))
return NULL;
/* Note that user manager services never need unescaping,
* since they cannot conflict with the kernel's own
* names, hence we don't need to call cg_unescape()
* here. */
return NULL;
p += n;
p += strspn(p, "/");
return p;
}
return NULL;
}
const char *e, *t;
char *unit;
int r;
/* 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... */
t = skip_user_manager(e);
/* Alternatively skip the user session... */
if (!t)
t = skip_session(e);
if (!t)
return -ENXIO;
/* ... and skip more slices if there are any */
e = skip_slices(t);
r = cg_path_decode_unit(e, &unit);
if (r < 0)
return r;
/* We skipped over the slices, don't accept any now */
return -ENXIO;
}
return 0;
}
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;
}
int r;
if (r < 0)
return r;
if (!start)
return -ENXIO;
if (!end)
return -ENXIO;
*end = 0;
if (!session_id_valid(start))
return -ENXIO;
if (session) {
char *rr;
if (!rr)
return -ENOMEM;
}
return 0;
}
int r;
if (r < 0)
return r;
}
int r;
if (r < 0)
return r;
if (!start)
return -ENXIO;
if (!end)
return -ENXIO;
*end = 0;
return -ENXIO;
return 0;
}
int r;
if (r < 0)
return r;
}
int cg_path_get_slice(const char *p, char **slice) {
const char *e = NULL;
assert(p);
for (;;) {
size_t n;
p += strspn(p, "/");
n = strcspn(p, "/");
if (!valid_slice_name(p, n)) {
if (!e) {
char *s;
s = strdup("-.slice");
if (!s)
return -ENOMEM;
*slice = s;
return 0;
}
return cg_path_decode_unit(e, slice);
}
e = p;
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;
}