]> git.ipfire.org Git - pakfire.git/commitdiff
cgroups: Catch errors when writes fail
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Mar 2021 16:38:23 +0000 (16:38 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 27 Mar 2021 16:38:23 +0000 (16:38 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/libpakfire/cgroup.c

index db1c9e423232a8f76f124dd112a851752064a4e7..84ab8eba1ccd12d55e03b35fc1f038b078103b9f 100644 (file)
@@ -151,6 +151,8 @@ static FILE* pakfire_cgroup_fopen(Pakfire pakfire,
 
 static int pakfire_cgroup_fprintf(Pakfire pakfire,
                const char* group, const char* file, const char* format, ...) {
+       char buffer[64];
+       size_t length;
        int r;
        va_list args;
 
@@ -158,11 +160,15 @@ static int pakfire_cgroup_fprintf(Pakfire pakfire,
        if (!f)
                return 1;
 
-       // Write to file
+       // Format what we have to write
        va_start(args, format);
-       r = vfprintf(f, format, args);
+       length = vsnprintf(buffer, sizeof(buffer) - 1, format, args);
        va_end(args);
 
+       // Use write(2) instead of fprintf/fwrite because we want to know
+       // if the operation was successful.
+       r = write(fileno(f), buffer, length);
+
        fclose(f);
 
        return r;
@@ -175,7 +181,7 @@ static int pakfire_cgroup_enable_controller(Pakfire pakfire,
                "+%s", controller);
 
        // fprintf might set errno when there was a problem, although the write itself was ok
-       if (errno) {
+       if (r < 0) {
                // The parent group does not seem to have this controller enabled
                if (errno == ENOENT) {
                        char* parent = pakfire_cgroup_parent_name(group);