]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
nspawn-mount: rework get_controllers() a bit
authorLennart Poettering <lennart@poettering.net>
Fri, 17 Nov 2017 12:35:56 +0000 (13:35 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 21 Nov 2017 10:54:08 +0000 (11:54 +0100)
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.

src/nspawn/nspawn-mount.c

index 69707cb7f988ae109520df1f0c3cdad599afef72..3273a7cfc436e5e124439213f658418c0f62d518 100644 (file)
@@ -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");