cgroup-util.c revision b59e246565337d6c6967059dd15a52a15277433e
/*-*- 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 "strv.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;
}
bool done = false;
Set *s;
int r, ret = 0;
return -ENOMEM;
do {
done = true;
ret = r;
goto finish;
}
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;
ret = r;
} else if (ret == 0)
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;
} while (!done);
set_free(s);
if (f)
fclose(f);
return ret;
}
int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self, bool rem) {
int r, ret = 0;
char *fn;
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);
return ret;
}
static const char *normalize_controller(const char *controller) {
return "systemd";
return controller + 5;
else
return controller;
}
char *t;
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;
}
}
int cg_get_path_and_check(const char *controller, const char *path, const char *suffix, char **fs) {
const char *p;
char *cc;
if (isempty(controller))
return -EINVAL;
/* Check if this controller actually really exists */
return -errno;
}
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;
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) {
return -ENOMEM;
*path = t;
}
if (controller)
*controller = NULL;
return 0;
}
return -EINVAL;
if (controller) {
return -ENOMEM;
*controller = t;
}
if (path)
return 0;
}
if (e[1] != '/' ||
e == spec ||
return -EINVAL;
if (controller)
return -ENOMEM;
if (path)
if (!(u = strdup(e+1))) {
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++) {
char *cc;
free(*f);
continue;
}
log_debug("Controller %s is not available, removing from controllers list.", *f);
free(*f);
continue;
}
*(t++) = *f;
}
*t = NULL;
return controllers;
}