cgroup-util.c revision ef1673d16907726d83bdff2e57b5261997a85020
/*-*- 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"
char *fs;
int r;
FILE *f;
if (r < 0)
return r;
if (!f)
return -errno;
*_f = f;
return 0;
}
char *fs;
int r;
FILE *f;
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. */
errno = 0;
if (feof(f))
return 0;
}
if (ul <= 0)
return -EIO;
return 1;
}
char *fs;
int r;
DIR *d;
/* This is not recursive! */
if (r < 0)
return r;
if (!d)
return -errno;
*_d = d;
return 0;
}
assert(d);
errno = 0;
char *b;
continue;
continue;
return -ENOMEM;
*fn = b;
return 1;
}
if (errno)
return -errno;
return 0;
}
char *p;
int r;
if (r < 0)
return r;
if (honour_sticky) {
char *tasks;
/* If the sticky bit is set don't remove the directory */
if (!tasks) {
free(p);
return -ENOMEM;
}
r = file_is_priv_sticky(tasks);
if (r > 0) {
free(p);
return 0;
}
}
r = rmdir(p);
free(p);
}
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)
return -ENOMEM;
do {
done = true;
ret = r;
goto finish;
}
while ((r = cg_read_pid(f, &pid)) > 0) {
continue;
continue;
/* If we haven't killed this process yet, kill
* it */
} else if (ret == 0) {
if (sigcont)
ret = 1;
}
done = false;
if (ret >= 0)
ret = r;
goto finish;
}
}
if (r < 0) {
if (ret >= 0)
ret = r;
goto finish;
}
fclose(f);
f = NULL;
/* 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);
if (allocated_set)
if (f)
fclose(f);
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)
return -ENOMEM;
ret = r;
goto finish;
}
while ((r = cg_read_subgroup(d, &fn)) > 0) {
char *p = NULL;
if (r < 0) {
if (ret >= 0)
goto finish;
}
free(p);
if (r != 0 && ret >= 0)
ret = r;
}
if (r < 0 && ret >= 0)
ret = r;
if (rem)
if (ret >= 0 &&
r != -ENOENT &&
r != -EBUSY)
ret = r;
}
if (d)
closedir(d);
if (allocated_set)
return ret;
}
unsigned i;
/* 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)
else if (i == 9)
else
sig = 0;
return r;
}
return 0;
}
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) {
ret = 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)
ret = r;
return ret;
}
}
if (r < 0) {
if (ret >= 0)
ret = r;
return ret;
}
fclose(f);
f = NULL;
} 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) {
ret = r;
return ret;
}
while ((r = cg_read_subgroup(d, &fn)) > 0) {
_cleanup_free_ char *p = NULL;
if (!p) {
if (ret >= 0)
return ret;
}
if (r != 0 && ret >= 0)
ret = r;
}
if (r < 0 && ret >= 0)
ret = r;
if (rem) {
return r;
}
return ret;
}
static const char *normalize_controller(const char *controller) {
return "systemd";
return controller + 5;
else
return controller;
}
char *t = NULL;
if (!(controller || path))
return -EINVAL;
if (controller) {
else if (path)
else if (suffix)
else
} else {
else if (path)
}
if (!t)
return -ENOMEM;
*fs = t;
return 0;
}
const char *p;
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(const char *p) {
char *cc;
assert(p);
/* 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 (isempty(controller))
return -EINVAL;
/* Normalize the controller syntax */
/* Check if this controller actually really exists */
r = check(p);
if (r < 0)
return r;
}
char *p;
bool is_sticky;
return 0;
return 0;
if (!p) {
return 1;
}
is_sticky = file_is_priv_sticky(p) > 0;
free(p);
if (is_sticky)
return 0;
return 0;
}
char *fs;
int r = 0;
if (r < 0)
return r;
errno = 0;
if (delete_root) {
bool is_sticky;
char *p;
if (!p) {
return -ENOMEM;
}
is_sticky = file_is_priv_sticky(p) > 0;
free(p);
if (!is_sticky)
if (r == 0)
r = -errno;
}
}
return r;
}
char *parent;
int r;
return r;
return r == -ENOENT ? 0 : r;
}
char *fs;
int r;
char c[32];
if (r < 0)
return r;
if (pid == 0)
char_array_0(c);
r = write_one_line_file(fs, c);
return r;
}
int cg_set_group_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid) {
char *fs;
int r;
mode &= 0777;
if (r < 0)
return r;
return r;
}
int cg_set_task_access(const char *controller, const char *path, mode_t mode, uid_t uid, gid_t gid, int sticky) {
char *fs;
int r;
return 0;
mode &= 0666;
if (r < 0)
return r;
/* Both mode and sticky param are passed */
/* Only one param is passed, hence read the current
* mode from the file itself */
if (r < 0) {
return -errno;
}
/* No mode set, we just shall set the sticky bit */
else
/* Only mode set, leave sticky bit untouched */
}
return r;
}
int r;
char *p = NULL;
FILE *f;
char *fs;
if (pid == 0)
return -ENOMEM;
if (!f)
while (!feof(f)) {
char *l;
errno = 0;
if (feof(f))
break;
goto finish;
}
continue;
l++;
continue;
if (l[cs] != ':')
continue;
r = -ENOMEM;
goto finish;
}
*path = p;
r = 0;
goto finish;
}
r = -ENOENT;
fclose(f);
return r;
}
int r;
return r;
goto finish;
if (sc[0] == 0) {
r = -ENOMEM;
goto finish;
}
goto finish;
r = -EEXIST;
goto finish;
}
goto finish;
goto finish;
goto finish;
r = 1;
r = -EIO;
goto finish;
} else
r = 0;
return r;
}
int r;
bool found = false;
if (r < 0)
return r == -ENOENT ? 1 : r;
while ((r = cg_read_pid(f, &pid)) > 0) {
continue;
found = true;
break;
}
fclose(f);
if (r < 0)
return r;
return !found;
}
int r;
if (r < 0)
return r;
}
int r;
char *fn;
if (r <= 0)
return r;
if (r < 0)
return r == -ENOENT ? 1 : r;
while ((r = cg_read_subgroup(d, &fn)) > 0) {
char *p = NULL;
if (r < 0) {
r = -ENOMEM;
goto finish;
}
free(p);
if (r <= 0)
goto finish;
}
if (r >= 0)
r = 1;
if (d)
closedir(d);
return r;
}
const char *e;
if (*spec == '/') {
if (path) {
if (!t)
return -ENOMEM;
*path = t;
}
if (controller)
*controller = NULL;
return 0;
}
if (!e) {
return -EINVAL;
if (controller) {
if (!t)
return -ENOMEM;
*controller = t;
}
if (path)
return 0;
}
return -EINVAL;
if (controller) {
if (!t)
return -ENOMEM;
}
if (path) {
u = strdup(e+1);
if (!u) {
free(t);
return -ENOMEM;
}
}
if (controller)
*controller = t;
if (path)
*path = u;
return 0;
}
if (!path_is_absolute(path) ||
controller[0] == 0 ||
return -EINVAL;
return -ENOMEM;
return 0;
}
char *t, *c, *p;
int r;
/* First check if it already is a filesystem path */
if (!t)
return -ENOMEM;
*result = t;
return 0;
}
/* Otherwise treat it as cg spec */
r = cg_split_spec(path, &c, &p);
if (r < 0)
return r;
free(c);
free(p);
return r;
}
int cg_get_user_path(char **path) {
char *root, *p;
/* Figure out the place to put user cgroups below. We use the
* same as PID 1 has but with the "/system" suffix replaced by
* "/user" */
p = strdup("/user");
else {
root[0] = 0;
}
if (!p)
return -ENOMEM;
*path = p;
return 0;
}
char **cg_shorten_controllers(char **controllers) {
char **f, **t;
if (!controllers)
return controllers;
for (f = controllers, t = controllers; *f; f++) {
int r;
const char *p;
free(*f);
continue;
}
p = normalize_controller(*f);
r = check(p);
if (r < 0) {
log_debug("Controller %s is not available, removing from controllers list.", *f);
free(*f);
continue;
}
*(t++) = *f;
}
*t = NULL;
return controllers;
}
char *cg_process, *cg_init, *p;
int r;
if (pid == 0)
if (r < 0)
return r;
if (r < 0) {
return r;
}
cg_init[0] = 0;
else
p = cg_process;
if (cgroup) {
char* c;
c = strdup(p);
if (!c) {
return -ENOMEM;
}
*cgroup = c;
}
if (root) {
cg_process[p-cg_process] = 0;
*root = cg_process;
} else
return 0;
}
static int instance_unit_from_cgroup(char **cgroup){
char *at;
char *i, *s;
/* This is a templated service */
if(!i)
return -EIO;
if (!s)
return -ENOMEM;
i = strdup(i + 1);
if (!i) {
free(s);
return -ENOMEM;
}
free(i);
free(s);
}
return 0;
}
int r;
char *b, *p;
size_t k;
r = instance_unit_from_cgroup(&cgroup);
if (r < 0)
return r;
k = strlen(p);
b = strndup(p, k);
if (!b)
return -ENOMEM;
r = unit_name_is_valid(b, true);
if (!r) {
free(b);
return -ENOENT;
}
*unit = b;
return 0;
}
int r;
char *cgroup;
if (r < 0)
return r;
return -ENOENT;
}
if (r < 0){
return r;
}
return 0;
}
int r;
char *cgroup;
if (r < 0)
return r;
return -ENOENT;
}
if (r < 0) {
return r;
}
return 0;
}