From: Lennart Poettering Date: Fri, 17 Nov 2017 12:35:56 +0000 (+0100) Subject: nspawn-mount: rework get_controllers() a bit X-Git-Tag: v236~152^2~10 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=d7c9693a3e792a43d27f78ec5ccf37e4738c7902;p=thirdparty%2Fsystemd.git nspawn-mount: rework get_controllers() a bit Let's rename get_controllers() → get_process_controllers(), in order to underline the difference to cg_kernel_controllers(). After all, one returns the controllers available to the process, the other the controllers enabled in the kernel at all). Let's also update the code to use read_line() and set_put_strdup() to shorten the code a bit, and make it more robust. --- diff --git a/src/nspawn/nspawn-mount.c b/src/nspawn/nspawn-mount.c index 69707cb7f98..3273a7cfc43 100644 --- a/src/nspawn/nspawn-mount.c +++ b/src/nspawn/nspawn-mount.c @@ -867,19 +867,30 @@ int mount_custom( /* Retrieve existing subsystems. This function is called in a new cgroup * namespace. */ -static int get_controllers(Set *subsystems) { +static int get_process_controllers(Set **ret) { + _cleanup_set_free_free_ Set *controllers = NULL; _cleanup_fclose_ FILE *f = NULL; - char line[LINE_MAX]; + int r; + + assert(ret); - assert(subsystems); + controllers = set_new(&string_hash_ops); + if (!controllers) + return -ENOMEM; f = fopen("/proc/self/cgroup", "re"); if (!f) return errno == ENOENT ? -ESRCH : -errno; - FOREACH_LINE(line, f, return -errno) { - int r; - char *e, *l, *p; + for (;;) { + _cleanup_free_ char *line = NULL; + char *e, *l; + + r = read_line(f, LONG_LINE_MAX, &line); + if (r < 0) + return r; + if (r == 0) + break; l = strchr(line, ':'); if (!l) @@ -895,15 +906,14 @@ static int get_controllers(Set *subsystems) { if (STR_IN_SET(l, "", "name=systemd", "name=unified")) continue; - p = strdup(l); - if (!p) - return -ENOMEM; - - r = set_consume(subsystems, p); + r = set_put_strdup(controllers, l); if (r < 0) return r; } + *ret = controllers; + controllers = NULL; + return 0; } @@ -999,11 +1009,7 @@ static int mount_legacy_cgns_supported( if (r > 0) goto skip_controllers; - controllers = set_new(&string_hash_ops); - if (!controllers) - return log_oom(); - - r = get_controllers(controllers); + r = get_process_controllers(&controllers); if (r < 0) return log_error_errno(r, "Failed to determine cgroup controllers: %m");