Commit 35d2e7ec authored by Lennart Poettering's avatar Lennart Poettering
Browse files

cgroup: reimplement the last bit of libcgroup functionality natively

parent dbd821ac
......@@ -285,14 +285,12 @@ libsystemd_core_la_SOURCES = \
libsystemd_core_la_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS) \
$(UDEV_CFLAGS) \
$(CGROUP_CFLAGS)
$(UDEV_CFLAGS)
libsystemd_core_la_LIBADD = \
libsystemd-basic.la \
$(DBUS_LIBS) \
$(UDEV_LIBS) \
$(CGROUP_LIBS) \
$(LIBWRAP_LIBS) \
$(PAM_LIBS)
......@@ -396,8 +394,7 @@ systemd_SOURCES = \
systemd_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS) \
$(UDEV_CFLAGS) \
$(CGROUP_CFLAGS)
$(UDEV_CFLAGS)
systemd_LDADD = \
libsystemd-core.la
......@@ -439,12 +436,10 @@ test_cgroup_SOURCES = \
src/cgroup-util.c
test_cgroup_CFLAGS = \
$(AM_CFLAGS) \
$(CGROUP_CFLAGS)
$(AM_CFLAGS)
test_cgroup_LDADD = \
libsystemd-basic.la \
$(CGROUP_LIBS)
libsystemd-basic.la
systemd_logger_SOURCES = \
src/logger.c \
......@@ -488,13 +483,11 @@ systemctl_SOURCES = \
systemctl_CFLAGS = \
$(AM_CFLAGS) \
$(DBUS_CFLAGS) \
$(CGROUP_CLAGS)
$(DBUS_CFLAGS)
systemctl_LDADD = \
libsystemd-basic.la \
$(DBUS_LIBS) \
$(CGROUP_LIBS)
$(DBUS_LIBS)
systemd_notify_SOURCES = \
src/notify.c \
......@@ -523,12 +516,10 @@ systemd_cgls_SOURCES = \
src/cgroup-util.c
systemd_cgls_CFLAGS = \
$(AM_CFLAGS) \
$(CGROUP_CLAGS)
$(AM_CFLAGS)
systemd_cgls_LDADD = \
libsystemd-basic.la \
$(CGROUP_LIBS)
libsystemd-basic.la
systemadm_SOURCES = \
src/systemadm.vala \
......@@ -559,8 +550,7 @@ pam_systemd_la_SOURCES = \
src/sd-daemon.c
pam_systemd_la_CFLAGS = \
$(AM_CFLAGS) \
$(CGROUP_CFLAGS) \
$(AM_CFLAGS)
-fvisibility=hidden
pam_systemd_la_LDFLAGS = \
......@@ -572,8 +562,7 @@ pam_systemd_la_LDFLAGS = \
pam_systemd_la_LIBADD = \
libsystemd-basic.la \
$(PAM_LIBS) \
$(CGROUP_LIBS)
$(PAM_LIBS)
SED_PROCESS = \
$(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
......
......@@ -109,10 +109,6 @@ PKG_CHECK_MODULES(DBUSGLIB, [ dbus-glib-1 ])
AC_SUBST(DBUSGLIB_CFLAGS)
AC_SUBST(DBUSGLIB_LIBS)
PKG_CHECK_MODULES(CGROUP, [ libcgroup >= 0.36 ])
AC_SUBST(CGROUP_CFLAGS)
AC_SUBST(CGROUP_LIBS)
AC_ARG_ENABLE([tcpwrap],
AS_HELP_STRING([--disable-tcpwrap],[Disable optional TCP wrappers support]),
[case "${enableval}" in
......
......@@ -39,20 +39,6 @@ static int compare(const void *a, const void *b) {
return 0;
}
static char *get_cgroup_path(const char *name) {
if (!name)
return strdup("/cgroup/systemd");
if (startswith(name, SYSTEMD_CGROUP_CONTROLLER ":"))
name += sizeof(SYSTEMD_CGROUP_CONTROLLER);
if (path_startswith(name, "/cgroup"))
return strdup(name);
return strappend("/cgroup/systemd/", name);
}
static unsigned ilog10(unsigned long ul) {
int n = 0;
......@@ -64,7 +50,7 @@ static unsigned ilog10(unsigned long ul) {
return n;
}
static int show_cgroup_full(const char *path, const char *prefix, unsigned n_columns, bool more) {
static int show_cgroup_one_by_path(const char *path, const char *prefix, unsigned n_columns, bool more) {
char *fn;
FILE *f;
size_t n = 0, n_allocated = 0;
......@@ -79,8 +65,8 @@ static int show_cgroup_full(const char *path, const char *prefix, unsigned n_col
if (!prefix)
prefix = "";
if (!(p = get_cgroup_path(path)))
return -ENOMEM;
if ((r = cg_fix_path(path, &p)) < 0)
return r;
r = asprintf(&fn, "%s/cgroup.procs", p);
free(p);
......@@ -171,15 +157,10 @@ finish:
return r;
}
int show_cgroup(const char *path, const char *prefix, unsigned n_columns) {
return show_cgroup_full(path, prefix, n_columns, false);
}
int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_columns) {
int show_cgroup_by_path(const char *path, const char *prefix, unsigned n_columns) {
DIR *d;
char *last = NULL;
char *p1 = NULL, *p2 = NULL, *fn = NULL;
struct dirent *de;
char *p1 = NULL, *p2 = NULL, *fn = NULL, *gn = NULL;
bool shown_pids = false;
int r;
......@@ -189,24 +170,18 @@ int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_colum
if (!prefix)
prefix = "";
if (!(fn = get_cgroup_path(path)))
return -ENOMEM;
if ((r = cg_fix_path(path, &fn)) < 0)
return r;
if (!(d = opendir(fn))) {
free(fn);
return -errno;
}
while ((de = readdir(d))) {
if (de->d_type != DT_DIR)
continue;
if (ignore_file(de->d_name))
continue;
while ((r = cg_read_subgroup(d, &gn)) > 0) {
if (!shown_pids) {
show_cgroup_full(path, prefix, n_columns, true);
show_cgroup_one_by_path(path, prefix, n_columns, true);
shown_pids = true;
}
......@@ -219,20 +194,26 @@ int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_colum
goto finish;
}
show_cgroup_recursive(last, p1, n_columns-2);
show_cgroup_by_path(last, p1, n_columns-2);
free(last);
last = NULL;
}
if (asprintf(&last, "%s/%s", strempty(path), de->d_name) < 0) {
r = asprintf(&last, "%s/%s", fn, gn);
free(gn);
if (r < 0) {
r = -ENOMEM;
goto finish;
}
}
if (r < 0)
goto finish;
if (!shown_pids)
show_cgroup_full(path, prefix, n_columns, !!last);
show_cgroup_one_by_path(path, prefix, n_columns, !!last);
if (last) {
printf("%s\342\224\224 %s\n", prefix, file_name_from_path(last));
......@@ -243,7 +224,7 @@ int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_colum
goto finish;
}
show_cgroup_recursive(last, p2, n_columns-2);
show_cgroup_by_path(last, p2, n_columns-2);
}
r = 0;
......@@ -258,3 +239,19 @@ finish:
return r;
}
int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned n_columns) {
char *p;
int r;
assert(controller);
assert(path);
if ((r = cg_get_path(controller, path, NULL, &p)) < 0)
return r;
r = show_cgroup_by_path(p, prefix, n_columns);
free(p);
return r;
}
......@@ -22,7 +22,7 @@
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
int show_cgroup(const char *path, const char *prefix, unsigned columns);
int show_cgroup_recursive(const char *path, const char *prefix, unsigned columns);
int show_cgroup_by_path(const char *path, const char *prefix, unsigned columns);
int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned columns);
#endif
......@@ -24,8 +24,7 @@
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <libcgroup.h>
#include <dirent.h>
#include "cgroup-util.h"
#include "log.h"
......@@ -33,54 +32,6 @@
#include "macro.h"
#include "util.h"
/*
Currently, the only remaining functionality from libcgroup we call
here is:
- cgroup_walk_tree_begin()/cgroup_walk_tree_next()
- cgroup_delete_cgroup_ext()
*/
int cg_translate_error(int error, int _errno) {
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;
}
static struct cgroup* cg_new(const char *controller, const char *path) {
struct cgroup *cgroup;
assert(path);
assert(controller);
if (!(cgroup = cgroup_new_cgroup(path)))
return NULL;
if (!cgroup_add_controller(cgroup, controller)) {
cgroup_free(&cgroup);
return NULL;
}
return cgroup;
}
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
char *fs;
int r;
......@@ -147,11 +98,77 @@ int cg_read_pid(FILE *f, pid_t *_pid) {
return 1;
}
int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d) {
char *fs;
int r;
DIR *d;
assert(controller);
assert(path);
assert(_d);
/* This is not recursive! */
if ((r = cg_get_path(controller, path, NULL, &fs)) < 0)
return r;
d = opendir(fs);
free(fs);
if (!d)
return -errno;
*_d = d;
return 0;
}
int cg_read_subgroup(DIR *d, char **fn) {
struct dirent *de;
assert(d);
errno = 0;
while ((de = readdir(d))) {
char *b;
if (de->d_type != DT_DIR)
continue;
if (streq(de->d_name, ".") ||
streq(de->d_name, ".."))
continue;
if (!(b = strdup(de->d_name)))
return -ENOMEM;
*fn = b;
return 1;
}
if (errno)
return -errno;
return 0;
}
int cg_rmdir(const char *controller, const char *path) {
char *p;
int r;
if ((r = cg_get_path(controller, path, NULL, &p)) < 0)
return r;
r = rmdir(p);
free(p);
return r < 0 ? -errno : 0;
}
int cg_kill(const char *controller, const char *path, int sig, bool ignore_self) {
bool killed = false, done = false;
bool done = false;
Set *s;
pid_t my_pid;
int r, ret = 0;
pid_t my_pid;
FILE *f = NULL;
assert(controller);
......@@ -171,8 +188,12 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
pid_t pid;
done = true;
if ((r = cg_enumerate_processes(controller, path, &f)) < 0)
if ((r = cg_enumerate_processes(controller, path, &f)) < 0) {
if (ret >= 0)
ret = r;
goto finish;
}
while ((r = cg_read_pid(f, &pid)) > 0) {
......@@ -185,15 +206,26 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
/* If we haven't killed this process yet, kill
* it */
if (kill(pid, sig) < 0 && errno != ESRCH) {
if (ret == 0)
if (ret >= 0)
ret = -errno;
}
} else if (ret == 0)
ret = 1;
killed = true;
done = false;
if ((r = set_put(s, LONG_TO_PTR(pid))) < 0)
break;
if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
if (ret >= 0)
ret = r;
goto finish;
}
}
if (r < 0) {
if (ret >= 0)
ret = r;
goto finish;
}
fclose(f);
......@@ -203,7 +235,7 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
* quicker than we can kill them we repeat this until
* no new pids need to be killed. */
} while (!done && r >= 0);
} while (!done);
finish:
set_free(s);
......@@ -211,69 +243,64 @@ finish:
if (f)
fclose(f);
if (r < 0)
return r;
if (ret < 0)
return ret;
return !!killed;
return ret;
}
int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self) {
struct cgroup_file_info info;
int level = 0, r, ret = 0;
void *iterator = NULL;
bool killed = false;
int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self, bool rem) {
int r, ret = 0;
DIR *d = NULL;
char *fn;
assert(path);
assert(controller);
assert(sig >= 0);
zero(info);
ret = cg_kill(controller, path, sig, ignore_self);
r = cgroup_walk_tree_begin(controller, path, 0, &iterator, &info, &level);
while (r == 0) {
int k;
char *p;
if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
if (ret >= 0)
ret = r;
if (info.type != CGROUP_FILE_TYPE_DIR)
goto next;
goto finish;
}
if (asprintf(&p, "%s/%s", path, info.path) < 0) {
ret = -ENOMEM;
break;
}
while ((r = cg_read_subgroup(d, &fn)) > 0) {
char *p = NULL;
k = cg_kill(controller, p, sig, ignore_self);
free(p);
r = asprintf(&p, "%s/%s", path, fn);
free(fn);
if (r < 0) {
if (ret >= 0)
ret = -ENOMEM;
if (k < 0) {
if (ret == 0)
ret = k;
} else if (k > 0)
killed = true;
goto finish;
}
next:
r = cg_kill_recursive(controller, p, sig, ignore_self, rem);
free(p);
r = cgroup_walk_tree_next(0, &iterator, &info, level);
if (r != 0 && ret >= 0)
ret = r;
}
if (ret == 0) {
if (r == 0 || r == ECGEOF)
ret = !!killed;
else if (r == ECGOTHER && errno == ENOENT)
ret = -ESRCH;
else
ret = cg_translate_error(r, errno);
}
if (r < 0 && ret >= 0)
ret = r;
if (rem)
if ((r = cg_rmdir(controller, path)) < 0) {
if (ret >= 0)
ret = r;
}
assert_se(cgroup_walk_tree_end(&iterator) == 0);
finish:
if (d)
closedir(d);
return ret;
}
int cg_kill_recursive_and_wait(const char *controller, const char *path) {
int cg_kill_recursive_and_wait(const char *controller, const char *path, bool rem) {
unsigned i;
assert(path);
......@@ -294,7 +321,7 @@ int cg_kill_recursive_and_wait(const char *controller, const char *path) {
else
sig = 0;
if ((r = cg_kill_recursive(controller, path, sig, true)) <= 0)
if ((r = cg_kill_recursive(controller, path, sig, true, rem)) <= 0)
return r;
usleep(50 * USEC_PER_MSEC);
......@@ -304,7 +331,8 @@ int cg_kill_recursive_and_wait(const char *controller, const char *path) {
}
int cg_migrate(const char *controller, const char *from, const char *to, bool ignore_self) {
bool migrated = false, done = false;
bool done = false;
Set *s;
int r, ret = 0;
pid_t my_pid;
FILE *f = NULL;
......@@ -313,96 +341,119 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
assert(from);
assert(to);
if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
return -ENOMEM;
my_pid = getpid();
do {
pid_t pid;
done = true;
if ((r = cg_enumerate_tasks(controller, from, &f)) < 0)
if ((r = cg_enumerate_tasks(controller, from, &f)) < 0) {
if (ret >= 0)
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 */
if (pid == my_pid && ignore_self)
continue;
if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
continue;
if ((r = cg_attach(controller, to, pid)) < 0) {
if (ret == 0)
ret = -r;
}
if (ret >= 0)
ret = r;
} else if (ret == 0)
ret = 1;
migrated = true;
done = false;
if ((r = set_put(s,