From: Michael Tremer Date: Sat, 27 Mar 2021 16:38:23 +0000 (+0000) Subject: cgroups: Catch errors when writes fail X-Git-Tag: 0.9.28~1285^2~457 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ac149196fd73fc1f763cd27c82a279ed73c36cf2;p=pakfire.git cgroups: Catch errors when writes fail Signed-off-by: Michael Tremer --- diff --git a/src/libpakfire/cgroup.c b/src/libpakfire/cgroup.c index db1c9e423..84ab8eba1 100644 --- a/src/libpakfire/cgroup.c +++ b/src/libpakfire/cgroup.c @@ -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);