]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
sysctl: use raw file descriptor in sysctl_write (#7753)
authorMike Gilbert <floppymaster@gmail.com>
Sat, 30 Dec 2017 11:16:49 +0000 (06:16 -0500)
committerLennart Poettering <lennart@poettering.net>
Sat, 30 Dec 2017 11:16:49 +0000 (12:16 +0100)
The kernel returns specific error codes which may be lost if we use the
libc buffered io functions.

Fixes: https://github.com/systemd/systemd/issues/7744
src/shared/sysctl-util.c

index 189580e3ed29d900a591fbf6912ed56c9c26553a..0bc81aaa567837a2f030d26c28c2f126dff49215 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
+#include <errno.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <string.h>
+#include <unistd.h>
 
+#include "fd-util.h"
 #include "fileio.h"
 #include "log.h"
 #include "macro.h"
@@ -53,6 +57,7 @@ char *sysctl_normalize(char *s) {
 
 int sysctl_write(const char *property, const char *value) {
         char *p;
+        _cleanup_close_ int fd = -1;
 
         assert(property);
         assert(value);
@@ -60,7 +65,17 @@ int sysctl_write(const char *property, const char *value) {
         log_debug("Setting '%s' to '%s'", property, value);
 
         p = strjoina("/proc/sys/", property);
-        return write_string_file(p, value, WRITE_STRING_FILE_DISABLE_BUFFER);
+        fd = open(p, O_WRONLY|O_CLOEXEC);
+        if (fd < 0)
+                return -errno;
+
+        if (!endswith(value, "\n"))
+                value = strjoina(value, "\n");
+
+        if (write(fd, value, strlen(value)) < 0)
+                return -errno;
+
+        return 0;
 }
 
 int sysctl_read(const char *property, char **content) {