cgroup: reimplement the last bit of libcgroup functionality natively
This commit is contained in:
parent
dbd821acb4
commit
35d2e7ec19
31
Makefile.am
31
Makefile.am
|
@ -285,14 +285,12 @@ libsystemd_core_la_SOURCES = \
|
||||||
libsystemd_core_la_CFLAGS = \
|
libsystemd_core_la_CFLAGS = \
|
||||||
$(AM_CFLAGS) \
|
$(AM_CFLAGS) \
|
||||||
$(DBUS_CFLAGS) \
|
$(DBUS_CFLAGS) \
|
||||||
$(UDEV_CFLAGS) \
|
$(UDEV_CFLAGS)
|
||||||
$(CGROUP_CFLAGS)
|
|
||||||
|
|
||||||
libsystemd_core_la_LIBADD = \
|
libsystemd_core_la_LIBADD = \
|
||||||
libsystemd-basic.la \
|
libsystemd-basic.la \
|
||||||
$(DBUS_LIBS) \
|
$(DBUS_LIBS) \
|
||||||
$(UDEV_LIBS) \
|
$(UDEV_LIBS) \
|
||||||
$(CGROUP_LIBS) \
|
|
||||||
$(LIBWRAP_LIBS) \
|
$(LIBWRAP_LIBS) \
|
||||||
$(PAM_LIBS)
|
$(PAM_LIBS)
|
||||||
|
|
||||||
|
@ -396,8 +394,7 @@ systemd_SOURCES = \
|
||||||
systemd_CFLAGS = \
|
systemd_CFLAGS = \
|
||||||
$(AM_CFLAGS) \
|
$(AM_CFLAGS) \
|
||||||
$(DBUS_CFLAGS) \
|
$(DBUS_CFLAGS) \
|
||||||
$(UDEV_CFLAGS) \
|
$(UDEV_CFLAGS)
|
||||||
$(CGROUP_CFLAGS)
|
|
||||||
|
|
||||||
systemd_LDADD = \
|
systemd_LDADD = \
|
||||||
libsystemd-core.la
|
libsystemd-core.la
|
||||||
|
@ -439,12 +436,10 @@ test_cgroup_SOURCES = \
|
||||||
src/cgroup-util.c
|
src/cgroup-util.c
|
||||||
|
|
||||||
test_cgroup_CFLAGS = \
|
test_cgroup_CFLAGS = \
|
||||||
$(AM_CFLAGS) \
|
$(AM_CFLAGS)
|
||||||
$(CGROUP_CFLAGS)
|
|
||||||
|
|
||||||
test_cgroup_LDADD = \
|
test_cgroup_LDADD = \
|
||||||
libsystemd-basic.la \
|
libsystemd-basic.la
|
||||||
$(CGROUP_LIBS)
|
|
||||||
|
|
||||||
systemd_logger_SOURCES = \
|
systemd_logger_SOURCES = \
|
||||||
src/logger.c \
|
src/logger.c \
|
||||||
|
@ -488,13 +483,11 @@ systemctl_SOURCES = \
|
||||||
|
|
||||||
systemctl_CFLAGS = \
|
systemctl_CFLAGS = \
|
||||||
$(AM_CFLAGS) \
|
$(AM_CFLAGS) \
|
||||||
$(DBUS_CFLAGS) \
|
$(DBUS_CFLAGS)
|
||||||
$(CGROUP_CLAGS)
|
|
||||||
|
|
||||||
systemctl_LDADD = \
|
systemctl_LDADD = \
|
||||||
libsystemd-basic.la \
|
libsystemd-basic.la \
|
||||||
$(DBUS_LIBS) \
|
$(DBUS_LIBS)
|
||||||
$(CGROUP_LIBS)
|
|
||||||
|
|
||||||
systemd_notify_SOURCES = \
|
systemd_notify_SOURCES = \
|
||||||
src/notify.c \
|
src/notify.c \
|
||||||
|
@ -523,12 +516,10 @@ systemd_cgls_SOURCES = \
|
||||||
src/cgroup-util.c
|
src/cgroup-util.c
|
||||||
|
|
||||||
systemd_cgls_CFLAGS = \
|
systemd_cgls_CFLAGS = \
|
||||||
$(AM_CFLAGS) \
|
$(AM_CFLAGS)
|
||||||
$(CGROUP_CLAGS)
|
|
||||||
|
|
||||||
systemd_cgls_LDADD = \
|
systemd_cgls_LDADD = \
|
||||||
libsystemd-basic.la \
|
libsystemd-basic.la
|
||||||
$(CGROUP_LIBS)
|
|
||||||
|
|
||||||
systemadm_SOURCES = \
|
systemadm_SOURCES = \
|
||||||
src/systemadm.vala \
|
src/systemadm.vala \
|
||||||
|
@ -559,8 +550,7 @@ pam_systemd_la_SOURCES = \
|
||||||
src/sd-daemon.c
|
src/sd-daemon.c
|
||||||
|
|
||||||
pam_systemd_la_CFLAGS = \
|
pam_systemd_la_CFLAGS = \
|
||||||
$(AM_CFLAGS) \
|
$(AM_CFLAGS)
|
||||||
$(CGROUP_CFLAGS) \
|
|
||||||
-fvisibility=hidden
|
-fvisibility=hidden
|
||||||
|
|
||||||
pam_systemd_la_LDFLAGS = \
|
pam_systemd_la_LDFLAGS = \
|
||||||
|
@ -572,8 +562,7 @@ pam_systemd_la_LDFLAGS = \
|
||||||
|
|
||||||
pam_systemd_la_LIBADD = \
|
pam_systemd_la_LIBADD = \
|
||||||
libsystemd-basic.la \
|
libsystemd-basic.la \
|
||||||
$(PAM_LIBS) \
|
$(PAM_LIBS)
|
||||||
$(CGROUP_LIBS)
|
|
||||||
|
|
||||||
SED_PROCESS = \
|
SED_PROCESS = \
|
||||||
$(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
|
$(AM_V_GEN)$(MKDIR_P) $(dir $@) && \
|
||||||
|
|
|
@ -109,10 +109,6 @@ PKG_CHECK_MODULES(DBUSGLIB, [ dbus-glib-1 ])
|
||||||
AC_SUBST(DBUSGLIB_CFLAGS)
|
AC_SUBST(DBUSGLIB_CFLAGS)
|
||||||
AC_SUBST(DBUSGLIB_LIBS)
|
AC_SUBST(DBUSGLIB_LIBS)
|
||||||
|
|
||||||
PKG_CHECK_MODULES(CGROUP, [ libcgroup >= 0.36 ])
|
|
||||||
AC_SUBST(CGROUP_CFLAGS)
|
|
||||||
AC_SUBST(CGROUP_LIBS)
|
|
||||||
|
|
||||||
AC_ARG_ENABLE([tcpwrap],
|
AC_ARG_ENABLE([tcpwrap],
|
||||||
AS_HELP_STRING([--disable-tcpwrap],[Disable optional TCP wrappers support]),
|
AS_HELP_STRING([--disable-tcpwrap],[Disable optional TCP wrappers support]),
|
||||||
[case "${enableval}" in
|
[case "${enableval}" in
|
||||||
|
|
|
@ -39,20 +39,6 @@ static int compare(const void *a, const void *b) {
|
||||||
return 0;
|
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) {
|
static unsigned ilog10(unsigned long ul) {
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
|
@ -64,7 +50,7 @@ static unsigned ilog10(unsigned long ul) {
|
||||||
return n;
|
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;
|
char *fn;
|
||||||
FILE *f;
|
FILE *f;
|
||||||
size_t n = 0, n_allocated = 0;
|
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)
|
if (!prefix)
|
||||||
prefix = "";
|
prefix = "";
|
||||||
|
|
||||||
if (!(p = get_cgroup_path(path)))
|
if ((r = cg_fix_path(path, &p)) < 0)
|
||||||
return -ENOMEM;
|
return r;
|
||||||
|
|
||||||
r = asprintf(&fn, "%s/cgroup.procs", p);
|
r = asprintf(&fn, "%s/cgroup.procs", p);
|
||||||
free(p);
|
free(p);
|
||||||
|
@ -171,15 +157,10 @@ finish:
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int show_cgroup(const char *path, const char *prefix, unsigned n_columns) {
|
int show_cgroup_by_path(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) {
|
|
||||||
DIR *d;
|
DIR *d;
|
||||||
char *last = NULL;
|
char *last = NULL;
|
||||||
char *p1 = NULL, *p2 = NULL, *fn = NULL;
|
char *p1 = NULL, *p2 = NULL, *fn = NULL, *gn = NULL;
|
||||||
struct dirent *de;
|
|
||||||
bool shown_pids = false;
|
bool shown_pids = false;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
@ -189,24 +170,18 @@ int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_colum
|
||||||
if (!prefix)
|
if (!prefix)
|
||||||
prefix = "";
|
prefix = "";
|
||||||
|
|
||||||
if (!(fn = get_cgroup_path(path)))
|
if ((r = cg_fix_path(path, &fn)) < 0)
|
||||||
return -ENOMEM;
|
return r;
|
||||||
|
|
||||||
if (!(d = opendir(fn))) {
|
if (!(d = opendir(fn))) {
|
||||||
free(fn);
|
free(fn);
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
while ((de = readdir(d))) {
|
while ((r = cg_read_subgroup(d, &gn)) > 0) {
|
||||||
|
|
||||||
if (de->d_type != DT_DIR)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (ignore_file(de->d_name))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
if (!shown_pids) {
|
if (!shown_pids) {
|
||||||
show_cgroup_full(path, prefix, n_columns, true);
|
show_cgroup_one_by_path(path, prefix, n_columns, true);
|
||||||
shown_pids = true;
|
shown_pids = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -219,20 +194,26 @@ int show_cgroup_recursive(const char *path, const char *prefix, unsigned n_colum
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
show_cgroup_recursive(last, p1, n_columns-2);
|
show_cgroup_by_path(last, p1, n_columns-2);
|
||||||
|
|
||||||
free(last);
|
free(last);
|
||||||
last = NULL;
|
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;
|
r = -ENOMEM;
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (r < 0)
|
||||||
|
goto finish;
|
||||||
|
|
||||||
if (!shown_pids)
|
if (!shown_pids)
|
||||||
show_cgroup_full(path, prefix, n_columns, !!last);
|
show_cgroup_one_by_path(path, prefix, n_columns, !!last);
|
||||||
|
|
||||||
if (last) {
|
if (last) {
|
||||||
printf("%s\342\224\224 %s\n", prefix, file_name_from_path(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;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
show_cgroup_recursive(last, p2, n_columns-2);
|
show_cgroup_by_path(last, p2, n_columns-2);
|
||||||
}
|
}
|
||||||
|
|
||||||
r = 0;
|
r = 0;
|
||||||
|
@ -258,3 +239,19 @@ finish:
|
||||||
|
|
||||||
return r;
|
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/>.
|
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_by_path(const char *path, const char *prefix, unsigned columns);
|
||||||
int show_cgroup_recursive(const char *path, const char *prefix, unsigned columns);
|
int show_cgroup(const char *controller, const char *path, const char *prefix, unsigned columns);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -24,8 +24,7 @@
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <dirent.h>
|
||||||
#include <libcgroup.h>
|
|
||||||
|
|
||||||
#include "cgroup-util.h"
|
#include "cgroup-util.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
@ -33,54 +32,6 @@
|
||||||
#include "macro.h"
|
#include "macro.h"
|
||||||
#include "util.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) {
|
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f) {
|
||||||
char *fs;
|
char *fs;
|
||||||
int r;
|
int r;
|
||||||
|
@ -147,11 +98,77 @@ int cg_read_pid(FILE *f, pid_t *_pid) {
|
||||||
return 1;
|
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) {
|
int cg_kill(const char *controller, const char *path, int sig, bool ignore_self) {
|
||||||
bool killed = false, done = false;
|
bool done = false;
|
||||||
Set *s;
|
Set *s;
|
||||||
pid_t my_pid;
|
|
||||||
int r, ret = 0;
|
int r, ret = 0;
|
||||||
|
pid_t my_pid;
|
||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
|
|
||||||
assert(controller);
|
assert(controller);
|
||||||
|
@ -171,8 +188,12 @@ int cg_kill(const char *controller, const char *path, int sig, bool ignore_self)
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
done = true;
|
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;
|
goto finish;
|
||||||
|
}
|
||||||
|
|
||||||
while ((r = cg_read_pid(f, &pid)) > 0) {
|
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
|
/* If we haven't killed this process yet, kill
|
||||||
* it */
|
* it */
|
||||||
if (kill(pid, sig) < 0 && errno != ESRCH) {
|
if (kill(pid, sig) < 0 && errno != ESRCH) {
|
||||||
if (ret == 0)
|
if (ret >= 0)
|
||||||
ret = -errno;
|
ret = -errno;
|
||||||
}
|
} else if (ret == 0)
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
killed = true;
|
|
||||||
done = false;
|
done = false;
|
||||||
|
|
||||||
if ((r = set_put(s, LONG_TO_PTR(pid))) < 0)
|
if ((r = set_put(s, LONG_TO_PTR(pid))) < 0) {
|
||||||
break;
|
if (ret >= 0)
|
||||||
|
ret = r;
|
||||||
|
|
||||||
|
goto finish;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (r < 0) {
|
||||||
|
if (ret >= 0)
|
||||||
|
ret = r;
|
||||||
|
|
||||||
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
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
|
* quicker than we can kill them we repeat this until
|
||||||
* no new pids need to be killed. */
|
* no new pids need to be killed. */
|
||||||
|
|
||||||
} while (!done && r >= 0);
|
} while (!done);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
set_free(s);
|
set_free(s);
|
||||||
|
@ -211,69 +243,64 @@ finish:
|
||||||
if (f)
|
if (f)
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
if (r < 0)
|
return ret;
|
||||||
return r;
|
|
||||||
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
return !!killed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self) {
|
int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self, bool rem) {
|
||||||
struct cgroup_file_info info;
|
int r, ret = 0;
|
||||||
int level = 0, r, ret = 0;
|
DIR *d = NULL;
|
||||||
void *iterator = NULL;
|
char *fn;
|
||||||
bool killed = false;
|
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
assert(controller);
|
assert(controller);
|
||||||
assert(sig >= 0);
|
assert(sig >= 0);
|
||||||
|
|
||||||
zero(info);
|
ret = cg_kill(controller, path, sig, ignore_self);
|
||||||
|
|
||||||
r = cgroup_walk_tree_begin(controller, path, 0, &iterator, &info, &level);
|
if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0) {
|
||||||
while (r == 0) {
|
if (ret >= 0)
|
||||||
int k;
|
ret = r;
|
||||||
char *p;
|
|
||||||
|
|
||||||
if (info.type != CGROUP_FILE_TYPE_DIR)
|
goto finish;
|
||||||
goto next;
|
}
|
||||||
|
|
||||||
if (asprintf(&p, "%s/%s", path, info.path) < 0) {
|
while ((r = cg_read_subgroup(d, &fn)) > 0) {
|
||||||
ret = -ENOMEM;
|
char *p = NULL;
|
||||||
break;
|
|
||||||
|
r = asprintf(&p, "%s/%s", path, fn);
|
||||||
|
free(fn);
|
||||||
|
|
||||||
|
if (r < 0) {
|
||||||
|
if (ret >= 0)
|
||||||
|
ret = -ENOMEM;
|
||||||
|
|
||||||
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
k = cg_kill(controller, p, sig, ignore_self);
|
r = cg_kill_recursive(controller, p, sig, ignore_self, rem);
|
||||||
free(p);
|
free(p);
|
||||||
|
|
||||||
if (k < 0) {
|
if (r != 0 && ret >= 0)
|
||||||
if (ret == 0)
|
ret = r;
|
||||||
ret = k;
|
|
||||||
} else if (k > 0)
|
|
||||||
killed = true;
|
|
||||||
|
|
||||||
next:
|
|
||||||
|
|
||||||
r = cgroup_walk_tree_next(0, &iterator, &info, level);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (r < 0 && ret >= 0)
|
||||||
if (r == 0 || r == ECGEOF)
|
ret = r;
|
||||||
ret = !!killed;
|
|
||||||
else if (r == ECGOTHER && errno == ENOENT)
|
|
||||||
ret = -ESRCH;
|
|
||||||
else
|
|
||||||
ret = cg_translate_error(r, errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_se(cgroup_walk_tree_end(&iterator) == 0);
|
if (rem)
|
||||||
|
if ((r = cg_rmdir(controller, path)) < 0) {
|
||||||
|
if (ret >= 0)
|
||||||
|
ret = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
finish:
|
||||||
|
if (d)
|
||||||
|
closedir(d);
|
||||||
|
|
||||||
return ret;
|
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;
|
unsigned i;
|
||||||
|
|
||||||
assert(path);
|
assert(path);
|
||||||
|
@ -294,7 +321,7 @@ int cg_kill_recursive_and_wait(const char *controller, const char *path) {
|
||||||
else
|
else
|
||||||
sig = 0;
|
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;
|
return r;
|
||||||
|
|
||||||
usleep(50 * USEC_PER_MSEC);
|
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) {
|
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;
|
int r, ret = 0;
|
||||||
pid_t my_pid;
|
pid_t my_pid;
|
||||||
FILE *f = NULL;
|
FILE *f = NULL;
|
||||||
|
@ -313,96 +341,119 @@ int cg_migrate(const char *controller, const char *from, const char *to, bool ig
|
||||||
assert(from);
|
assert(from);
|
||||||
assert(to);
|
assert(to);
|
||||||
|
|
||||||
|
if (!(s = set_new(trivial_hash_func, trivial_compare_func)))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
my_pid = getpid();
|
my_pid = getpid();
|
||||||
|
|
||||||
do {
|
do {
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
done = true;
|
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;
|
goto finish;
|
||||||
|
}
|
||||||
|
|
||||||
while ((r = cg_read_pid(f, &pid)) > 0) {
|
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)
|
if (pid == my_pid && ignore_self)
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
if ((r = cg_attach(controller, to, pid)) < 0) {
|
if (set_get(s, LONG_TO_PTR(pid)) == LONG_TO_PTR(pid))
|
||||||
if (ret == 0)
|
continue;
|
||||||
ret = -r;
|
|
||||||
}
|
if ((r = cg_attach(controller, to, pid)) < 0) {
|
||||||
|
if (ret >= 0)
|
||||||
|
ret = r;
|
||||||
|
} else if (ret == 0)
|
||||||
|
ret = 1;
|
||||||
|
|
||||||
migrated = true;
|
|
||||||
done = false;
|
done = false;
|
||||||
|
|
||||||
|
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);
|
fclose(f);
|
||||||
f = NULL;
|
f = NULL;
|
||||||
|
|
||||||
} while (!done && r >= 0);
|
} while (!done);
|
||||||
|
|
||||||
finish:
|
finish:
|
||||||
|
set_free(s);
|
||||||
|
|
||||||
if (f)
|
if (f)
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
if (r < 0)
|
return ret;
|
||||||
return r;
|
|
||||||
|
|
||||||
if (ret < 0)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
return !!migrated;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self) {
|
int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self, bool rem) {
|
||||||
struct cgroup_file_info info;
|
int r, ret = 0;
|
||||||
int level = 0, r, ret = 0;
|
DIR *d = NULL;
|
||||||
void *iterator = NULL;
|
char *fn;
|
||||||
bool migrated = false;
|
|
||||||
|
|
||||||
assert(controller);
|
assert(controller);
|
||||||
assert(from);
|
assert(from);
|
||||||
assert(to);
|
assert(to);
|
||||||
|
|
||||||
zero(info);
|
ret = cg_migrate(controller, from, to, ignore_self);
|
||||||
|
|
||||||
r = cgroup_walk_tree_begin(controller, from, 0, &iterator, &info, &level);
|
if ((r = cg_enumerate_subgroups(controller, from, &d)) < 0) {
|
||||||
while (r == 0) {
|
if (ret >= 0)
|
||||||
int k;
|
ret = r;
|
||||||
char *p;
|
goto finish;
|
||||||
|
}
|
||||||
|
|
||||||
if (info.type != CGROUP_FILE_TYPE_DIR)
|
while ((r = cg_read_subgroup(d, &fn)) > 0) {
|
||||||
goto next;
|
char *p = NULL;
|
||||||
|
|
||||||
if (asprintf(&p, "%s/%s", from, info.path) < 0) {
|
r = asprintf(&p, "%s/%s", from, fn);
|
||||||
ret = -ENOMEM;
|
free(fn);
|
||||||
break;
|
|
||||||
|
if (r < 0) {
|
||||||
|
if (ret >= 0)
|
||||||
|
ret = -ENOMEM;
|
||||||
|
|
||||||
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
k = cg_migrate(controller, p, to, ignore_self);
|
r = cg_migrate_recursive(controller, p, to, ignore_self, rem);
|
||||||
free(p);
|
free(p);
|
||||||
|
|
||||||
if (k < 0) {
|
if (r != 0 && ret >= 0)
|
||||||
if (ret == 0)
|
ret = r;
|
||||||
ret = k;
|
|
||||||
} else if (k > 0)
|
|
||||||
migrated = true;
|
|
||||||
|
|
||||||
next:
|
|
||||||
r = cgroup_walk_tree_next(0, &iterator, &info, level);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (r < 0 && ret >= 0)
|
||||||
if (r == 0 || r == ECGEOF)
|
ret = r;
|
||||||
r = !!migrated;
|
|
||||||
else if (r == ECGOTHER && errno == ENOENT)
|
|
||||||
r = -ESRCH;
|
|
||||||
else
|
|
||||||
r = cg_translate_error(r, errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_se(cgroup_walk_tree_end(&iterator) == 0);
|
if (rem)
|
||||||
|
if ((r = cg_rmdir(controller, from)) < 0) {
|
||||||
|
if (ret >= 0)
|
||||||
|
ret = r;
|
||||||
|
}
|
||||||
|
|
||||||
|
finish:
|
||||||
|
if (d)
|
||||||
|
closedir(d);
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
@ -470,24 +521,17 @@ int cg_trim(const char *controller, const char *path, bool delete_root) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int cg_delete(const char *controller, const char *path) {
|
int cg_delete(const char *controller, const char *path) {
|
||||||
struct cgroup *cg;
|
char *parent;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(controller);
|
assert(controller);
|
||||||
assert(path);
|
assert(path);
|
||||||
|
|
||||||
if (!(cg = cg_new(controller, path)))
|
if ((r = parent_of_path(path, &parent)) < 0)
|
||||||
return -ENOMEM;
|
return r;
|
||||||
|
|
||||||
if ((r = cgroup_delete_cgroup_ext(cg, CGFLAG_DELETE_RECURSIVE|CGFLAG_DELETE_IGNORE_MIGRATION)) != 0) {
|
r = cg_migrate_recursive(controller, path, parent, false, true);
|
||||||
r = cg_translate_error(r, errno);
|
free(parent);
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
r = 0;
|
|
||||||
|
|
||||||
finish:
|
|
||||||
cgroup_free(&cg);
|
|
||||||
|
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
@ -739,63 +783,154 @@ int cg_is_empty(const char *controller, const char *path, bool ignore_self) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
|
int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self) {
|
||||||
struct cgroup_file_info info;
|
int r;
|
||||||
int level = 0, r, ret = 0;
|
DIR *d = NULL;
|
||||||
void *iterator = NULL;
|
char *fn;
|
||||||
bool empty = true;
|
|
||||||
|
|
||||||
assert(controller);
|
assert(controller);
|
||||||
assert(path);
|
assert(path);
|
||||||
|
|
||||||
zero(info);
|
if ((r = cg_is_empty(controller, path, ignore_self)) <= 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
r = cgroup_walk_tree_begin(controller, path, 0, &iterator, &info, &level);
|
if ((r = cg_enumerate_subgroups(controller, path, &d)) < 0)
|
||||||
while (r == 0) {
|
return r;
|
||||||
int k;
|
|
||||||
char *p;
|
|
||||||
|
|
||||||
if (info.type != CGROUP_FILE_TYPE_DIR)
|
while ((r = cg_read_subgroup(d, &fn)) > 0) {
|
||||||
goto next;
|
char *p = NULL;
|
||||||
|
|
||||||
if (asprintf(&p, "%s/%s", path, info.path) < 0) {
|
r = asprintf(&p, "%s/%s", path, fn);
|
||||||
ret = -ENOMEM;
|
free(fn);
|
||||||
break;
|
|
||||||
|
if (r < 0) {
|
||||||
|
r = -ENOMEM;
|
||||||
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
k = cg_is_empty(controller, p, ignore_self);
|
r = cg_is_empty_recursive(controller, p, ignore_self);
|
||||||
free(p);
|
free(p);
|
||||||
|
|
||||||
if (k < 0) {
|
if (r <= 0)
|
||||||
ret = k;
|
goto finish;
|
||||||
break;
|
|
||||||
} else if (k == 0) {
|
|
||||||
empty = false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
next:
|
|
||||||
r = cgroup_walk_tree_next(0, &iterator, &info, level);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ret == 0) {
|
if (r >= 0)
|
||||||
if (r == 0 || r == ECGEOF)
|
r = 1;
|
||||||
ret = !!empty;
|
|
||||||
else if (r == ECGOTHER && errno == ENOENT)
|
|
||||||
ret = -ESRCH;
|
|
||||||
else
|
|
||||||
ret = cg_translate_error(r, errno);
|
|
||||||
}
|
|
||||||
|
|
||||||
assert_se(cgroup_walk_tree_end(&iterator) == 0);
|
finish:
|
||||||
|
|
||||||
return ret;
|
if (d)
|
||||||
|
closedir(d);
|
||||||
|
|
||||||
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
int cg_init(void) {
|
int cg_split_spec(const char *spec, char **controller, char **path) {
|
||||||
int r;
|
const char *e;
|
||||||
|
char *t = NULL, *u = NULL;
|
||||||
|
|
||||||
if ((r = cgroup_init()) != 0)
|
assert(spec);
|
||||||
return cg_translate_error(r, errno);
|
assert(controller || path);
|
||||||
|
|
||||||
|
if (*spec == '/') {
|
||||||
|
|
||||||
|
if (path) {
|
||||||
|
if (!(t = strdup(spec)))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
*path = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (controller)
|
||||||
|
*controller = NULL;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(e = strchr(spec, ':'))) {
|
||||||
|
|
||||||
|
if (strchr(spec, '/') || spec[0] == 0)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (controller) {
|
||||||
|
if (!(t = strdup(spec)))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
*controller = t;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (path)
|
||||||
|
*path = NULL;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (e[1] != '/' ||
|
||||||
|
e == spec ||
|
||||||
|
memchr(spec, '/', e-spec))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (controller)
|
||||||
|
if (!(t = strndup(spec, e-spec)))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
if (path)
|
||||||
|
if (!(u = strdup(e+1))) {
|
||||||
|
free(t);
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (controller)
|
||||||
|
*controller = t;
|
||||||
|
|
||||||
|
if (path)
|
||||||
|
*path = u;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int cg_join_spec(const char *controller, const char *path, char **spec) {
|
||||||
|
assert(controller);
|
||||||
|
assert(path);
|
||||||
|
|
||||||
|
if (!path_is_absolute(path) ||
|
||||||
|
controller[0] == 0 ||
|
||||||
|
strchr(controller, ':') ||
|
||||||
|
strchr(controller, '/'))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (asprintf(spec, "%s:%s", controller, path) < 0)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int cg_fix_path(const char *path, char **result) {
|
||||||
|
char *t, *c, *p;
|
||||||
|
int r;
|
||||||
|
|
||||||
|
assert(path);
|
||||||
|
assert(result);
|
||||||
|
|
||||||
|
/* First check if it already is a filesystem path */
|
||||||
|
if (path_is_absolute(path) &&
|
||||||
|
path_startswith(path, "/cgroup") &&
|
||||||
|
access(path, F_OK) >= 0) {
|
||||||
|
|
||||||
|
if (!(t = strdup(path)))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
*result = t;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Otherwise treat it as cg spec */
|
||||||
|
if ((r = cg_split_spec(path, &c, &p)) < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
|
r = cg_get_path(c ? c : SYSTEMD_CGROUP_CONTROLLER, p ? p : "/", NULL, result);
|
||||||
|
free(c);
|
||||||
|
free(p);
|
||||||
|
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
|
@ -24,29 +24,36 @@
|
||||||
|
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
#include <dirent.h>
|
||||||
|
|
||||||
#include "set.h"
|
#include "set.h"
|
||||||
|
|
||||||
#define SYSTEMD_CGROUP_CONTROLLER "name=systemd"
|
#define SYSTEMD_CGROUP_CONTROLLER "name=systemd"
|
||||||
|
|
||||||
int cg_translate_error(int error, int _errno);
|
|
||||||
|
|
||||||
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f);
|
int cg_enumerate_processes(const char *controller, const char *path, FILE **_f);
|
||||||
int cg_enumerate_tasks(const char *controller, const char *path, FILE **_f);
|
int cg_enumerate_tasks(const char *controller, const char *path, FILE **_f);
|
||||||
|
|
||||||
int cg_read_pid(FILE *f, pid_t *_pid);
|
int cg_read_pid(FILE *f, pid_t *_pid);
|
||||||
|
|
||||||
|
int cg_enumerate_subgroups(const char *controller, const char *path, DIR **_d);
|
||||||
|
int cg_read_subgroup(DIR *d, char **fn);
|
||||||
|
|
||||||
int cg_kill(const char *controller, const char *path, int sig, bool ignore_self);
|
int cg_kill(const char *controller, const char *path, int sig, bool ignore_self);
|
||||||
int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self);
|
int cg_kill_recursive(const char *controller, const char *path, int sig, bool ignore_self, bool remove);
|
||||||
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 remove);
|
||||||
|
|
||||||
int cg_migrate(const char *controller, const char *from, const char *to, bool ignore_self);
|
int cg_migrate(const char *controller, const char *from, const char *to, bool ignore_self);
|
||||||
int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self);
|
int cg_migrate_recursive(const char *controller, const char *from, const char *to, bool ignore_self, bool remove);
|
||||||
|
|
||||||
|
int cg_split_spec(const char *spec, char **controller, char **path);
|
||||||
|
int cg_join_spec(const char *controller, const char *path, char **spec);
|
||||||
|
int cg_fix_path(const char *path, char **result);
|
||||||
|
|
||||||
int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs);
|
int cg_get_path(const char *controller, const char *path, const char *suffix, char **fs);
|
||||||
int cg_get_by_pid(const char *controller, pid_t pid, char **path);
|
int cg_get_by_pid(const char *controller, pid_t pid, char **path);
|
||||||
|
|
||||||
int cg_trim(const char *controller, const char *path, bool delete_root);
|
int cg_trim(const char *controller, const char *path, bool delete_root);
|
||||||
|
|
||||||
|
int cg_rmdir(const char *controller, const char *path);
|
||||||
int cg_delete(const char *controller, const char *path);
|
int cg_delete(const char *controller, const char *path);
|
||||||
|
|
||||||
int cg_create(const char *controller, const char *path);
|
int cg_create(const char *controller, const char *path);
|
||||||
|
@ -61,6 +68,4 @@ int cg_install_release_agent(const char *controller, const char *agent);
|
||||||
int cg_is_empty(const char *controller, const char *path, bool ignore_self);
|
int cg_is_empty(const char *controller, const char *path, bool ignore_self);
|
||||||
int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self);
|
int cg_is_empty_recursive(const char *controller, const char *path, bool ignore_self);
|
||||||
|
|
||||||
int cg_init(void);
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
18
src/cgroup.c
18
src/cgroup.c
|
@ -149,7 +149,7 @@ int cgroup_bonding_kill(CGroupBonding *b, int sig) {
|
||||||
|
|
||||||
assert(b->realized);
|
assert(b->realized);
|
||||||
|
|
||||||
return cg_kill_recursive(b->controller, b->path, sig, true);
|
return cg_kill_recursive(b->controller, b->path, sig, true, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
|
int cgroup_bonding_kill_list(CGroupBonding *first, int sig) {
|
||||||
|
@ -215,13 +215,7 @@ int manager_setup_cgroup(Manager *m) {
|
||||||
|
|
||||||
assert(m);
|
assert(m);
|
||||||
|
|
||||||
/* 1. Initialize libcg */
|
/* 1. Determine hierarchy */
|
||||||
if ((r = cg_init()) < 0) {
|
|
||||||
log_error("Failed to initialize libcg: %s", strerror(-r));
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 2. Determine hierarchy */
|
|
||||||
if ((r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 0, ¤t)) < 0)
|
if ((r = cg_get_by_pid(SYSTEMD_CGROUP_CONTROLLER, 0, ¤t)) < 0)
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
|
@ -243,13 +237,13 @@ int manager_setup_cgroup(Manager *m) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 3. Show data */
|
/* 2. Show data */
|
||||||
if ((r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_hierarchy, NULL, &path)) < 0)
|
if ((r = cg_get_path(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_hierarchy, NULL, &path)) < 0)
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path);
|
log_debug("Using cgroup controller " SYSTEMD_CGROUP_CONTROLLER ". File system hierarchy is at %s.", path);
|
||||||
|
|
||||||
/* 4. Install agent */
|
/* 3. Install agent */
|
||||||
if ((r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, CGROUP_AGENT_PATH)) < 0)
|
if ((r = cg_install_release_agent(SYSTEMD_CGROUP_CONTROLLER, CGROUP_AGENT_PATH)) < 0)
|
||||||
log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
|
log_warning("Failed to install release agent, ignoring: %s", strerror(-r));
|
||||||
else if (r > 0)
|
else if (r > 0)
|
||||||
|
@ -257,13 +251,13 @@ int manager_setup_cgroup(Manager *m) {
|
||||||
else
|
else
|
||||||
log_debug("Release agent already installed.");
|
log_debug("Release agent already installed.");
|
||||||
|
|
||||||
/* 5. Realize the group */
|
/* 4. Realize the group */
|
||||||
if ((r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_hierarchy, 0)) < 0) {
|
if ((r = cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, m->cgroup_hierarchy, 0)) < 0) {
|
||||||
log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
|
log_error("Failed to create root cgroup hierarchy: %s", strerror(-r));
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 6. And pin it, so that it cannot be unmounted */
|
/* 5. And pin it, so that it cannot be unmounted */
|
||||||
if (m->pin_cgroupfs_fd >= 0)
|
if (m->pin_cgroupfs_fd >= 0)
|
||||||
close_nointr_nofail(m->pin_cgroupfs_fd);
|
close_nointr_nofail(m->pin_cgroupfs_fd);
|
||||||
|
|
||||||
|
|
|
@ -258,12 +258,6 @@ _public_ PAM_EXTERN int pam_sm_open_session(
|
||||||
if (sd_booted() <= 0)
|
if (sd_booted() <= 0)
|
||||||
return PAM_SUCCESS;
|
return PAM_SUCCESS;
|
||||||
|
|
||||||
if ((r = cg_init()) < 0) {
|
|
||||||
pam_syslog(handle, LOG_ERR, "libcgroup initialization failed: %s", strerror(-r));
|
|
||||||
r = PAM_SESSION_ERR;
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
|
if ((r = get_user_data(handle, &username, &pw)) != PAM_SUCCESS)
|
||||||
goto finish;
|
goto finish;
|
||||||
|
|
||||||
|
@ -354,44 +348,29 @@ finish:
|
||||||
}
|
}
|
||||||
|
|
||||||
static int session_remains(pam_handle_t *handle, const char *user_path) {
|
static int session_remains(pam_handle_t *handle, const char *user_path) {
|
||||||
struct cgroup_file_info info;
|
int r;
|
||||||
int level = 0, r;
|
|
||||||
void *iterator = NULL;
|
|
||||||
bool remains = false;
|
bool remains = false;
|
||||||
|
DIR *d;
|
||||||
|
char *subgroup;
|
||||||
|
|
||||||
zero(info);
|
if ((r = cg_enumerate_subgroups(SYSTEMD_CGROUP_CONTROLLER, user_path, &d)) < 0)
|
||||||
|
return r;
|
||||||
|
|
||||||
r = cgroup_walk_tree_begin(SYSTEMD_CGROUP_CONTROLLER, user_path, 0, &iterator, &info, &level);
|
while ((r = cg_read_subgroup(d, &subgroup)) > 0) {
|
||||||
while (r == 0) {
|
|
||||||
|
|
||||||
if (info.type != CGROUP_FILE_TYPE_DIR)
|
remains = !streq(subgroup, "no-session");
|
||||||
goto next;
|
free(subgroup);
|
||||||
|
|
||||||
if (streq(info.path, ""))
|
if (remains)
|
||||||
goto next;
|
break;
|
||||||
|
|
||||||
if (streq(info.path, "no-session"))
|
|
||||||
goto next;
|
|
||||||
|
|
||||||
remains = true;
|
|
||||||
break;
|
|
||||||
|
|
||||||
next:
|
|
||||||
|
|
||||||
r = cgroup_walk_tree_next(0, &iterator, &info, level);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
closedir(d);
|
||||||
|
|
||||||
if (remains)
|
if (r < 0)
|
||||||
r = 1;
|
return r;
|
||||||
else if (r == 0 || r == ECGEOF)
|
|
||||||
r = 0;
|
|
||||||
else
|
|
||||||
r = cg_translate_error(r, errno);
|
|
||||||
|
|
||||||
assert_se(cgroup_walk_tree_end(&iterator) == 0);
|
return !!remains;
|
||||||
|
|
||||||
return r;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_public_ PAM_EXTERN int pam_sm_close_session(
|
_public_ PAM_EXTERN int pam_sm_close_session(
|
||||||
|
@ -425,6 +404,10 @@ _public_ PAM_EXTERN int pam_sm_close_session(
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* We are probably still in some session/no-session dir. Move ourselves out of the way as first step */
|
||||||
|
if ((r = cg_attach(SYSTEMD_CGROUP_CONTROLLER, "/user", 0)) < 0)
|
||||||
|
pam_syslog(handle, LOG_ERR, "Failed to move us away: %s", strerror(-r));
|
||||||
|
|
||||||
if (asprintf(&user_path, "/user/%s", username) < 0) {
|
if (asprintf(&user_path, "/user/%s", username) < 0) {
|
||||||
r = PAM_BUF_ERR;
|
r = PAM_BUF_ERR;
|
||||||
goto finish;
|
goto finish;
|
||||||
|
@ -439,28 +422,19 @@ _public_ PAM_EXTERN int pam_sm_close_session(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (kill_session) {
|
if (kill_session) {
|
||||||
/* Kill processes in session cgroup */
|
/* Kill processes in session cgroup, and delete it */
|
||||||
if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path)) < 0)
|
if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, session_path, true)) < 0)
|
||||||
pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
|
pam_syslog(handle, LOG_ERR, "Failed to kill session cgroup: %s", strerror(-r));
|
||||||
|
} else {
|
||||||
} else {
|
|
||||||
/* Migrate processes from session to
|
/* Migrate processes from session to
|
||||||
* no-session cgroup. First, try to create the
|
* no-session cgroup. First, try to create the
|
||||||
* no-session group in case it doesn't exist
|
* no-session group in case it doesn't exist
|
||||||
* yet. */
|
* yet. Also, delete the session group. */
|
||||||
create_user_group(handle, nosession_path, pw, 0);
|
create_user_group(handle, nosession_path, pw, 0);
|
||||||
|
|
||||||
if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false)) < 0)
|
if ((r = cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, session_path, nosession_path, false, true)) < 0)
|
||||||
pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
|
pam_syslog(handle, LOG_ERR, "Failed to migrate session cgroup: %s", strerror(-r));
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Delete session cgroup */
|
|
||||||
if (r < 0)
|
|
||||||
pam_syslog(handle, LOG_INFO, "Couldn't empty session cgroup, not deleting.");
|
|
||||||
else {
|
|
||||||
if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, session_path)) < 0)
|
|
||||||
pam_syslog(handle, LOG_ERR, "Failed to delete session cgroup: %s", strerror(-r));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* GC user tree */
|
/* GC user tree */
|
||||||
|
@ -473,25 +447,26 @@ _public_ PAM_EXTERN int pam_sm_close_session(
|
||||||
if (kill_user && r == 0) {
|
if (kill_user && r == 0) {
|
||||||
|
|
||||||
/* Kill no-session cgroup */
|
/* Kill no-session cgroup */
|
||||||
if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
|
if ((r = cg_kill_recursive_and_wait(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
|
||||||
pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
|
pam_syslog(handle, LOG_ERR, "Failed to kill user cgroup: %s", strerror(-r));
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
|
if ((r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, user_path, true)) < 0)
|
||||||
pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
|
pam_syslog(handle, LOG_ERR, "Failed to check user cgroup: %s", strerror(-r));
|
||||||
|
|
||||||
/* If we managed to kill somebody, don't cleanup the cgroup. */
|
/* Remove user cgroup */
|
||||||
if (r == 0)
|
if (r > 0) {
|
||||||
|
if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
|
||||||
|
pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
|
||||||
|
|
||||||
|
/* If we managed to find somebody, don't cleanup the cgroup. */
|
||||||
|
} else if (r == 0)
|
||||||
r = -EBUSY;
|
r = -EBUSY;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (r >= 0) {
|
if (r >= 0) {
|
||||||
const char *runtime_dir;
|
const char *runtime_dir;
|
||||||
|
|
||||||
/* Remove user cgroup */
|
|
||||||
if ((r = cg_delete(SYSTEMD_CGROUP_CONTROLLER, user_path)) < 0)
|
|
||||||
pam_syslog(handle, LOG_ERR, "Failed to delete user cgroup: %s", strerror(-r));
|
|
||||||
|
|
||||||
/* This will migrate us to the /user cgroup. */
|
/* This will migrate us to the /user cgroup. */
|
||||||
|
|
||||||
if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
|
if ((runtime_dir = pam_getenv(handle, "XDG_RUNTIME_DIR")))
|
||||||
|
|
|
@ -1064,7 +1064,6 @@ typedef struct UnitStatusInfo {
|
||||||
|
|
||||||
static void print_status_info(UnitStatusInfo *i) {
|
static void print_status_info(UnitStatusInfo *i) {
|
||||||
ExecStatusInfo *p;
|
ExecStatusInfo *p;
|
||||||
int r;
|
|
||||||
|
|
||||||
assert(i);
|
assert(i);
|
||||||
|
|
||||||
|
@ -1195,10 +1194,7 @@ static void print_status_info(UnitStatusInfo *i) {
|
||||||
else
|
else
|
||||||
c = 0;
|
c = 0;
|
||||||
|
|
||||||
if ((r = cg_init()) < 0)
|
show_cgroup_by_path(i->default_control_group, "\t\t ", c);
|
||||||
log_error("Failed to initialize libcg: %s", strerror(-r));
|
|
||||||
else
|
|
||||||
show_cgroup_recursive(i->default_control_group, "\t\t ", c);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -83,11 +83,6 @@ int main(int argc, char *argv[]) {
|
||||||
goto finish;
|
goto finish;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cg_init() < 0) {
|
|
||||||
log_error("Failed to initialize libcg: %s", strerror(-r));
|
|
||||||
goto finish;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (optind < argc) {
|
if (optind < argc) {
|
||||||
unsigned i;
|
unsigned i;
|
||||||
|
|
||||||
|
@ -95,7 +90,7 @@ int main(int argc, char *argv[]) {
|
||||||
int q;
|
int q;
|
||||||
printf("%s:\n", argv[i]);
|
printf("%s:\n", argv[i]);
|
||||||
|
|
||||||
if ((q = show_cgroup_recursive(argv[i], NULL, 0)) < 0)
|
if ((q = show_cgroup_by_path(argv[i], NULL, 0)) < 0)
|
||||||
r = q;
|
r = q;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -109,9 +104,9 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
if (path_startswith(p, "/cgroup")) {
|
if (path_startswith(p, "/cgroup")) {
|
||||||
printf("Working Directory %s:\n", p);
|
printf("Working Directory %s:\n", p);
|
||||||
r = show_cgroup_recursive(p, NULL, 0);
|
r = show_cgroup_by_path(p, NULL, 0);
|
||||||
} else
|
} else
|
||||||
r = show_cgroup_recursive(NULL, NULL, 0);
|
r = show_cgroup(SYSTEMD_CGROUP_CONTROLLER, "/", NULL, 0);
|
||||||
|
|
||||||
free(p);
|
free(p);
|
||||||
}
|
}
|
||||||
|
|
|
@ -28,8 +28,7 @@
|
||||||
|
|
||||||
int main(int argc, char*argv[]) {
|
int main(int argc, char*argv[]) {
|
||||||
char *path;
|
char *path;
|
||||||
|
char *c, *p;
|
||||||
assert_se(cg_init() >= 0);
|
|
||||||
|
|
||||||
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
||||||
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
||||||
|
@ -62,21 +61,44 @@ int main(int argc, char*argv[]) {
|
||||||
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) > 0);
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) > 0);
|
||||||
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) == 0);
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) == 0);
|
||||||
|
|
||||||
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false) == 0);
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false, false) == 0);
|
||||||
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false) > 0);
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false, false) > 0);
|
||||||
|
|
||||||
assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", "/test-a", false) == 0);
|
assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", "/test-a", false, false) > 0);
|
||||||
|
|
||||||
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) == 0);
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", false) == 0);
|
||||||
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) > 0);
|
assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", false) > 0);
|
||||||
|
|
||||||
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false) > 0);
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-a", 0, false, false) > 0);
|
||||||
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false) == 0);
|
assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, "/test-b", 0, false, false) == 0);
|
||||||
|
|
||||||
cg_trim(SYSTEMD_CGROUP_CONTROLLER, "/", false);
|
cg_trim(SYSTEMD_CGROUP_CONTROLLER, "/", false);
|
||||||
|
|
||||||
assert_se(cg_delete(SYSTEMD_CGROUP_CONTROLLER, "/test-b") < 0);
|
assert_se(cg_delete(SYSTEMD_CGROUP_CONTROLLER, "/test-b") < 0);
|
||||||
assert_se(cg_delete(SYSTEMD_CGROUP_CONTROLLER, "/test-a") == 0);
|
assert_se(cg_delete(SYSTEMD_CGROUP_CONTROLLER, "/test-a") >= 0);
|
||||||
|
|
||||||
|
assert_se(cg_split_spec("foobar:/", &c, &p) == 0);
|
||||||
|
assert(streq(c, "foobar"));
|
||||||
|
assert(streq(p, "/"));
|
||||||
|
free(c);
|
||||||
|
free(p);
|
||||||
|
|
||||||
|
assert_se(cg_split_spec("foobar:", &c, &p) < 0);
|
||||||
|
assert_se(cg_split_spec("foobar:asdfd", &c, &p) < 0);
|
||||||
|
assert_se(cg_split_spec(":///", &c, &p) < 0);
|
||||||
|
assert_se(cg_split_spec(":", &c, &p) < 0);
|
||||||
|
assert_se(cg_split_spec("", &c, &p) < 0);
|
||||||
|
assert_se(cg_split_spec("fo/obar:/", &c, &p) < 0);
|
||||||
|
|
||||||
|
assert_se(cg_split_spec("/", &c, &p) >= 0);
|
||||||
|
assert(c == NULL);
|
||||||
|
assert(streq(p, "/"));
|
||||||
|
free(p);
|
||||||
|
|
||||||
|
assert_se(cg_split_spec("foo", &c, &p) >= 0);
|
||||||
|
assert(streq(c, "foo"));
|
||||||
|
assert(p == NULL);
|
||||||
|
free(c);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
54
src/unit.c
54
src/unit.c
|
@ -1511,12 +1511,9 @@ char *unit_dbus_path(Unit *u) {
|
||||||
if (!(e = bus_path_escape(u->meta.id)))
|
if (!(e = bus_path_escape(u->meta.id)))
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if (asprintf(&p, "/org/freedesktop/systemd1/unit/%s", e) < 0) {
|
p = strappend("/org/freedesktop/systemd1/unit/", e);
|
||||||
free(e);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
free(e);
|
free(e);
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1526,8 +1523,13 @@ int unit_add_cgroup(Unit *u, CGroupBonding *b) {
|
||||||
|
|
||||||
assert(u);
|
assert(u);
|
||||||
assert(b);
|
assert(b);
|
||||||
|
|
||||||
assert(b->path);
|
assert(b->path);
|
||||||
|
|
||||||
|
if (!b->controller)
|
||||||
|
if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
/* Ensure this hasn't been added yet */
|
/* Ensure this hasn't been added yet */
|
||||||
assert(!b->unit);
|
assert(!b->unit);
|
||||||
|
|
||||||
|
@ -1566,7 +1568,6 @@ static char *default_cgroup_path(Unit *u) {
|
||||||
}
|
}
|
||||||
|
|
||||||
int unit_add_cgroup_from_text(Unit *u, const char *name) {
|
int unit_add_cgroup_from_text(Unit *u, const char *name) {
|
||||||
size_t n;
|
|
||||||
char *controller = NULL, *path = NULL;
|
char *controller = NULL, *path = NULL;
|
||||||
CGroupBonding *b = NULL;
|
CGroupBonding *b = NULL;
|
||||||
int r;
|
int r;
|
||||||
|
@ -1574,38 +1575,20 @@ int unit_add_cgroup_from_text(Unit *u, const char *name) {
|
||||||
assert(u);
|
assert(u);
|
||||||
assert(name);
|
assert(name);
|
||||||
|
|
||||||
/* Detect controller name */
|
if ((r = cg_split_spec(name, &controller, &path)) < 0)
|
||||||
n = strcspn(name, ":");
|
return r;
|
||||||
|
|
||||||
if (name[n] == 0 ||
|
if (!path)
|
||||||
(name[n] == ':' && name[n+1] == 0)) {
|
path = default_cgroup_path(u);
|
||||||
|
|
||||||
/* Only controller name, no path? */
|
if (!controller)
|
||||||
|
|
||||||
if (!(path = default_cgroup_path(u)))
|
|
||||||
return -ENOMEM;
|
|
||||||
|
|
||||||
} else {
|
|
||||||
const char *p;
|
|
||||||
|
|
||||||
/* Controller name, and path. */
|
|
||||||
p = name+n+1;
|
|
||||||
|
|
||||||
if (!path_is_absolute(p))
|
|
||||||
return -EINVAL;
|
|
||||||
|
|
||||||
if (!(path = strdup(p)))
|
|
||||||
return -ENOMEM;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (n > 0)
|
|
||||||
controller = strndup(name, n);
|
|
||||||
else
|
|
||||||
controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
|
controller = strdup(SYSTEMD_CGROUP_CONTROLLER);
|
||||||
|
|
||||||
if (!controller) {
|
if (!path || !controller) {
|
||||||
r = -ENOMEM;
|
free(path);
|
||||||
goto fail;
|
free(controller);
|
||||||
|
|
||||||
|
return -ENOMEM;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
|
if (cgroup_bonding_find_list(u->meta.cgroup_bondings, controller)) {
|
||||||
|
@ -1650,9 +1633,6 @@ int unit_add_default_cgroup(Unit *u) {
|
||||||
if (!(b = new0(CGroupBonding, 1)))
|
if (!(b = new0(CGroupBonding, 1)))
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
if (!(b->controller = strdup(SYSTEMD_CGROUP_CONTROLLER)))
|
|
||||||
goto fail;
|
|
||||||
|
|
||||||
if (!(b->path = default_cgroup_path(u)))
|
if (!(b->path = default_cgroup_path(u)))
|
||||||
goto fail;
|
goto fail;
|
||||||
|
|
||||||
|
|
77
src/util.c
77
src/util.c
|
@ -609,8 +609,22 @@ int get_process_cmdline(pid_t pid, size_t max_length, char **line) {
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
|
||||||
if (r[0] == 0)
|
/* Kernel threads have no argv[] */
|
||||||
return get_process_name(pid, line);
|
if (r[0] == 0) {
|
||||||
|
char *t;
|
||||||
|
int h;
|
||||||
|
|
||||||
|
free(r);
|
||||||
|
|
||||||
|
if ((h = get_process_name(pid, &t)) < 0)
|
||||||
|
return h;
|
||||||
|
|
||||||
|
h = asprintf(&r, "[%s]", t);
|
||||||
|
free(t);
|
||||||
|
|
||||||
|
if (h < 0)
|
||||||
|
return -ENOMEM;
|
||||||
|
}
|
||||||
|
|
||||||
*line = r;
|
*line = r;
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -698,6 +712,48 @@ int readlink_and_make_absolute(const char *p, char **r) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int parent_of_path(const char *path, char **_r) {
|
||||||
|
const char *e, *a = NULL, *b = NULL, *p;
|
||||||
|
char *r;
|
||||||
|
bool slash = false;
|
||||||
|
|
||||||
|
assert(path);
|
||||||
|
assert(_r);
|
||||||
|
|
||||||
|
if (!*path)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
for (e = path; *e; e++) {
|
||||||
|
|
||||||
|
if (!slash && *e == '/') {
|
||||||
|
a = b;
|
||||||
|
b = e;
|
||||||
|
slash = true;
|
||||||
|
} else if (slash && *e != '/')
|
||||||
|
slash = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (*(e-1) == '/')
|
||||||
|
p = a;
|
||||||
|
else
|
||||||
|
p = b;
|
||||||
|
|
||||||
|
if (!p)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (p == path)
|
||||||
|
r = strdup("/");
|
||||||
|
else
|
||||||
|
r = strndup(path, p-path);
|
||||||
|
|
||||||
|
if (!r)
|
||||||
|
return -ENOMEM;
|
||||||
|
|
||||||
|
*_r = r;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
char *file_name_from_path(const char *p) {
|
char *file_name_from_path(const char *p) {
|
||||||
char *r;
|
char *r;
|
||||||
|
|
||||||
|
@ -2201,25 +2257,24 @@ ssize_t loop_write(int fd, const void *buf, size_t nbytes, bool do_poll) {
|
||||||
|
|
||||||
int path_is_mount_point(const char *t) {
|
int path_is_mount_point(const char *t) {
|
||||||
struct stat a, b;
|
struct stat a, b;
|
||||||
char *copy;
|
char *parent;
|
||||||
|
int r;
|
||||||
|
|
||||||
if (lstat(t, &a) < 0) {
|
if (lstat(t, &a) < 0) {
|
||||||
|
|
||||||
if (errno == ENOENT)
|
if (errno == ENOENT)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(copy = strdup(t)))
|
if ((r = parent_of_path(t, &parent)) < 0)
|
||||||
return -ENOMEM;
|
return r;
|
||||||
|
|
||||||
if (lstat(dirname(copy), &b) < 0) {
|
r = lstat(parent, &b);
|
||||||
free(copy);
|
free(parent);
|
||||||
|
|
||||||
|
if (r < 0)
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
|
||||||
|
|
||||||
free(copy);
|
|
||||||
|
|
||||||
return a.st_dev != b.st_dev;
|
return a.st_dev != b.st_dev;
|
||||||
}
|
}
|
||||||
|
|
|
@ -212,6 +212,8 @@ int safe_mkdir(const char *path, mode_t mode, uid_t uid, gid_t gid);
|
||||||
int mkdir_parents(const char *path, mode_t mode);
|
int mkdir_parents(const char *path, mode_t mode);
|
||||||
int mkdir_p(const char *path, mode_t mode);
|
int mkdir_p(const char *path, mode_t mode);
|
||||||
|
|
||||||
|
int parent_of_path(const char *path, char **parent);
|
||||||
|
|
||||||
int rmdir_parents(const char *path, const char *stop);
|
int rmdir_parents(const char *path, const char *stop);
|
||||||
|
|
||||||
int get_process_name(pid_t pid, char **name);
|
int get_process_name(pid_t pid, char **name);
|
||||||
|
|
Loading…
Reference in a new issue