]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
basic/group-util: optimize alloca use
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 10 Mar 2021 09:00:04 +0000 (10:00 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 11 Mar 2021 13:43:16 +0000 (14:43 +0100)
Follow-up for 0fa7b50053.

src/basic/cgroup-util.c
src/test/test-string-util.c

index 82743480eec9cdc9ac2e0e6ce0b510441230e710..527043a2b39048bd11a470e8301460e304691a22 100644 (file)
@@ -533,14 +533,12 @@ static int controller_is_v1_accessible(const char *root, const char *controller)
         assert(controller);
 
         dn = controller_to_dirname(controller);
-        cpath = strjoina("/sys/fs/cgroup/", dn);
-        if (root)
-                /* Also check that:
-                 * - possible subcgroup is created at root,
-                 * - we can modify the hierarchy.
-                 * "Leak" cpath on stack */
-                cpath = strjoina(cpath, root, "/cgroup.procs");
 
+        /* If root if specified, we check that:
+         * - possible subcgroup is created at root,
+         * - we can modify the hierarchy. */
+
+        cpath = strjoina("/sys/fs/cgroup/", dn, root, root ? "/cgroup.procs" : NULL);
         if (laccess(cpath, root ? W_OK : F_OK) < 0)
                 return -errno;
 
index fec9577d1f1760dfae628bd4e7a7cce66b0d676e..15ea24ae4bec2f80de03ee5beb2568cc8f85fdb6 100644 (file)
@@ -345,6 +345,12 @@ static void test_strjoina(void) {
 
         actual = strjoina("foo", NULL, "bar");
         assert_se(streq(actual, "foo"));
+
+        actual = strjoina("/sys/fs/cgroup/", "dn", "/a/b/c", "/cgroup.procs");
+        assert_se(streq(actual, "/sys/fs/cgroup/dn/a/b/c/cgroup.procs"));
+
+        actual = strjoina("/sys/fs/cgroup/", "dn", NULL, NULL);
+        assert_se(streq(actual, "/sys/fs/cgroup/dn"));
 }
 
 static void test_strjoin(void) {