cgroup.c revision e99e38bbdcca3fe5956823bdb3d38544ccf93221
/*-*- Mode: C; c-basic-offset: 8 -*-*/
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 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
General Public License for more details.
You should have received a copy of the GNU General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <errno.h>
#include <assert.h>
#include <unistd.h>
#include <signal.h>
#include "cgroup.h"
#include "log.h"
switch (error) {
case ECGROUPNOTCOMPILED:
case ECGROUPNOTMOUNTED:
case ECGROUPNOTEXIST:
case ECGROUPNOTCREATED:
return -ENOENT;
case ECGINVAL:
return -EINVAL;
case ECGROUPNOTALLOWED:
return -EPERM;
case ECGOTHER:
return -_errno;
}
return -EIO;
}
int cgroup_bonding_realize(CGroupBonding *b) {
int r;
assert(b);
assert(b->controller);
if (b->cgroup)
return 0;
return -ENOMEM;
r = -ENOMEM;
goto fail;
}
if (b->inherit)
r = cgroup_create_cgroup_from_parent(b->cgroup, true);
else
r = cgroup_create_cgroup(b->cgroup, true);
if (r != 0) {
r = translate_error(r, errno);
goto fail;
}
return 0;
fail:
cgroup_free(&b->cgroup);
return r;
}
CGroupBonding *b;
int r;
if ((r = cgroup_bonding_realize(b)) < 0)
return r;
}
return 0;
}
void cgroup_bonding_free(CGroupBonding *b) {
assert(b);
if (b->unit) {
CGroupBonding *f;
if (f)
else
}
if (b->cgroup) {
cgroup_delete_cgroup_ext(b->cgroup, true);
cgroup_free(&b->cgroup);
}
free(b->controller);
free(b);
}
CGroupBonding *b, *n;
}
int r;
assert(b);
if (pid == 0)
if (!b->cgroup)
return -ENOENT;
return translate_error(r, errno);
return 0;
}
CGroupBonding *b;
int r;
if ((r = cgroup_bonding_install(b, pid)) < 0)
return r;
}
return 0;
}
int r;
Set *s;
bool done;
bool killed = false;
assert(b);
if (!b->only_us)
return -EAGAIN;
return -ENOMEM;
do {
void *iterator;
done = true;
if (r == ECGEOF) {
r = 0;
goto kill_done;
} else {
r = translate_error(r, errno);
break;
}
}
for (;;) {
/* If we haven't killed this process
* yet, kill it */
r = -errno;
break;
}
killed = true;
done = false;
break;
}
if (r == ECGEOF)
r = 0;
else
r = translate_error(r, errno);
break;
}
}
/* 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 && r >= 0);
set_free(s);
if (r < 0)
return r;
}
CGroupBonding *b;
int r = -EAGAIN;
if ((r = cgroup_bonding_kill(b, sig)) < 0) {
continue;
return r;
}
return 0;
}
return r;
}
/* Returns 1 if the group is empty, 0 if it is not, -EAGAIN if we
* cannot know */
int cgroup_bonding_is_empty(CGroupBonding *b) {
void *iterator;
int r;
assert(b);
if (r == 0 || r == ECGEOF)
/* Hmm, no PID in this group? Then it is definitely empty */
if (r == ECGEOF)
return 1;
/* Some error? Let's return it */
if (r != 0)
return translate_error(r, errno);
/* It's not empty, and we are the only user, then it is
* definitely not empty */
if (b->only_us)
return 0;
/* There are PIDs in the group but we aren't the only users,
* hence we cannot say */
return -EAGAIN;
}
CGroupBonding *b;
int r;
if ((r = cgroup_bonding_is_empty(b)) < 0) {
/* If this returned -EAGAIN, then we don't know if the
* group is empty, so let's see if another group can
* tell us */
if (r != -EAGAIN)
return r;
} else
return r;
}
return -EAGAIN;
}
char *p, *c, *sc;
int r;
assert(m);
return -ENOMEM;
if ((r = read_one_line_file(p, &c)) < 0) {
free(p);
return r;
}
if (sc[0] == 0) {
free(p);
free(c);
return r;
}
free(p);
free(c);
return -EEXIST;
}
free(c);
free(p);
return -ENOMEM;
if ((r = read_one_line_file(p, &c)) < 0) {
free(p);
return r;
}
if ((r = write_one_line_file(p, "1\n")) < 0) {
free(p);
free(c);
return r;
}
free(p);
free(c);
return -EIO;
}
free(p);
free(c);
return 0;
}
static int create_hierarchy_cgroup(Manager *m) {
int r;
assert(m);
return -ENOMEM;
r = -ENOMEM;
goto finish;
}
if ((r = cgroup_create_cgroup(cg, true)) != 0) {
r = translate_error(r, errno);
goto finish;
}
if ((r = cgroup_attach_task(cg)) != 0) {
r = translate_error(r, errno);
goto finish;
}
r = 0;
cgroup_free(&cg);
return r;
}
int manager_setup_cgroup(Manager *m) {
int r;
char suffix[32];
assert(m);
if ((r = cgroup_init()) != 0) {
return translate_error(r, errno);
}
free(m->cgroup_controller);
return -ENOMEM;
return translate_error(r, errno);
return translate_error(r, errno);
}
free(m->cgroup_hierarchy);
/* We probably got reexecuted and can continue to use our root cgroup */
m->cgroup_hierarchy = cp;
else {
/* We need a new root cgroup */
m->cgroup_hierarchy = NULL;
if (r < 0) {
return -ENOMEM;
}
}
log_debug("Using cgroup controller <%s>, hierarchy mounted at <%s>, using root group <%s>.",
mp,
m->cgroup_hierarchy);
if ((r = install_release_agent(m, mp)) < 0)
else
log_debug("Installed release agent, or already installed.");
if ((r = create_hierarchy_cgroup(m)) < 0)
else
log_debug("Created root group.");
return r;
}
int r;
assert(m);
if (!m->cgroup_hierarchy)
return 0;
return -ENOMEM;
r = -ENOMEM;
goto finish;
}
/* Often enough we won't be able to delete the cgroup we
* ourselves are in, hence ignore all errors here */
if (delete)
r = 0;
cgroup_free(&cg);
return r;
}
CGroupBonding *l, *b;
assert(m);
return 0;
LIST_FOREACH(by_path, b, l) {
int t;
if (!b->unit)
continue;
if ((t = cgroup_bonding_is_empty_list(b)) < 0) {
/* If we don't know, we don't know */
if (t != -EAGAIN)
continue;
}
if (t > 0)
}
return 0;
}
CGroupBonding *b;
return b;
return NULL;
}
char *cgroup_bonding_to_string(CGroupBonding *b) {
char *r;
assert(b);
return NULL;
return r;
}