]> git.ipfire.org Git - pakfire.git/commitdiff
cgroup: Add helper functions to write to file
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Mar 2021 14:38:50 +0000 (14:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Mar 2021 14:38:50 +0000 (14:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/cgroup.c

index 1cd55e5743a68c0a267f48c9d443e38ff80a0ebb..3bc5368673abd5c113f7a5e2c3d0581f88f5c12a 100644 (file)
@@ -149,27 +149,38 @@ static FILE* pakfire_cgroup_fopen(Pakfire pakfire,
        return f;
 }
 
-static int pakfire_cgroup_enable_controller(Pakfire pakfire,
-               const char* group, const char* controller) {
+static int pakfire_cgroup_fprintf(Pakfire pakfire,
+               const char* group, const char* file, const char* format, ...) {
        int r;
+       va_list args;
 
-       FILE* f = pakfire_cgroup_fopen(pakfire, group, "cgroup.subtree_control", "w");
+       FILE* f = pakfire_cgroup_fopen(pakfire, group, file, "w");
        if (!f)
                return 1;
 
-RETRY:
+       // Write to file
+       va_start(args, format);
+       r = vfprintf(f, format, args);
+       va_end(args);
+
+       fclose(f);
+
+       return r;
+}
+
+static int pakfire_cgroup_enable_controller(Pakfire pakfire,
+               const char* group, const char* controller) {
        // Enable controller
-       r = fprintf(f, "+%s", controller);
+       int r = pakfire_cgroup_fprintf(pakfire, group, "cgroup.subtree_control",
+               "+%s", controller);
 
        // fprintf might set errno when there was a problem, although the write itself was ok
        if (errno) {
-               r = 1;
-
                // The parent group does not seem to have this controller enabled
                if (errno == ENOENT) {
                        char* parent = pakfire_cgroup_parent_name(group);
                        if (!parent)
-                               goto ERROR;
+                               return 1;
 
                        // Try to enable this on the parent level
                        r = pakfire_cgroup_enable_controller(pakfire, parent, controller);
@@ -177,20 +188,14 @@ RETRY:
 
                        // If this failed, we fail
                        if (r)
-                               goto ERROR;
+                               return r;
 
                        // Otherwise we try again
-                       goto RETRY;
+                       return pakfire_cgroup_enable_controller(pakfire, group, controller);
                }
        }
 
-       // Success
-       r = 0;
-
-ERROR:
-       fclose(f);
-
-       return r;
+       return 0;
 }
 
 static int pakfire_cgroup_enable_controllers(Pakfire pakfire,
@@ -245,7 +250,6 @@ int pakfire_cgroup_create(Pakfire pakfire, const char* group) {
 
        // Enable default controllers
        r = pakfire_cgroup_enable_controllers(pakfire, group, cgroup_controllers);
-       printf("r = %d\n", r);
        if (r) {
                pakfire_cgroup_destroy(pakfire, group);
                return r;