]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
Add fdopen_unlocked() wrapper
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Thu, 4 Apr 2019 09:27:08 +0000 (11:27 +0200)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 12 Apr 2019 09:44:57 +0000 (11:44 +0200)
coccinelle/fopen-unlocked.cocci
src/basic/fileio.c
src/basic/fileio.h
src/basic/tmpfile-util.c
src/portable/portable.c

index e6f2bc5681fd4172c8a3dbd43d653cdf7e77067e..bbd70a6338dabb5fd41a467c7ebc1777c2059c03 100644 (file)
@@ -49,3 +49,15 @@ expression f, g, path, p;
   if (r < 0)
     return ...;
 - (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+@@
+expression f, fd, options;
+@@
+- f = fdopen(fd, options);
++ r = fdopen_unlocked(fd, options, &f);
++ if (r < 0) {
+- if (!f) {
+        ...
+-       return -errno;
++       return r;
+  }
+- (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
index 2318f407b871acbc497e43817a9ee9ebf6d76eb3..af22ec9110624ed7d157ede1f16e1f60d800142a 100644 (file)
@@ -42,6 +42,19 @@ int fopen_unlocked(const char *path, const char *options, FILE **ret) {
         return 0;
 }
 
+int fdopen_unlocked(int fd, const char *options, FILE **ret) {
+        assert(ret);
+
+        FILE *f = fdopen(fd, options);
+        if (!f)
+                return -errno;
+
+        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+
+        *ret = f;
+        return 0;
+}
+
 int write_string_stream_ts(
                 FILE *f,
                 const char *line,
@@ -167,14 +180,11 @@ int write_string_file_ts(
                         goto fail;
                 }
 
-                f = fdopen(fd, "w");
-                if (!f) {
-                        r = -errno;
+                r = fdopen_unlocked(fd, "w", &f);
+                if (r < 0) {
                         safe_close(fd);
                         goto fail;
                 }
-
-                (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
         }
 
         if (flags & WRITE_STRING_FILE_DISABLE_BUFFER)
index bee33534392944690318679561be2bc08e389420..7903b57c80eb7a4c022be77a333c78ceb1ee4d39 100644 (file)
@@ -34,6 +34,7 @@ typedef enum {
 } ReadFullFileFlags;
 
 int fopen_unlocked(const char *path, const char *options, FILE **ret);
+int fdopen_unlocked(int fd, const char *options, FILE **ret);
 
 int write_string_stream_ts(FILE *f, const char *line, WriteStringFileFlags flags, struct timespec *ts);
 static inline int write_string_stream(FILE *f, const char *line, WriteStringFileFlags flags) {
index 260443a1d66ef5f85ef9ef6a2cc45468a8faf01a..3ed91520b98ca4910724ff14732cf0d1d206bc89 100644 (file)
@@ -6,6 +6,7 @@
 
 #include "alloc-util.h"
 #include "fd-util.h"
+#include "fileio.h"
 #include "fs-util.h"
 #include "hexdecoct.h"
 #include "macro.h"
@@ -42,16 +43,14 @@ int fopen_temporary(const char *path, FILE **_f, char **_temp_path) {
         /* This assumes that returned FILE object is short-lived and used within the same single-threaded
          * context and never shared externally, hence locking is not necessary. */
 
-        f = fdopen(fd, "w");
-        if (!f) {
-                unlink_noerrno(t);
+        r = fdopen_unlocked(fd, "w", &f);
+        if (r < 0) {
+                unlink(t);
                 free(t);
                 safe_close(fd);
-                return -errno;
+                return r;
         }
 
-        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
-
         *_f = f;
         *_temp_path = t;
 
index 9b6cc21d2c174e25a6e515e548d455833dc6dd0d..4aa879801f830e58e6e1d5656f1663cbb635d2ca 100644 (file)
@@ -1091,12 +1091,10 @@ static int test_chroot_dropin(
                 return log_debug_errno(errno, "Failed to open %s/%s: %m", where, p);
         }
 
-        f = fdopen(fd, "r");
-        if (!f)
-                return log_debug_errno(errno, "Failed to convert file handle: %m");
-        fd = -1;
-
-        (void) __fsetlocking(f, FSETLOCKING_BYCALLER);
+        r = fdopen_unlocked(fd, "r", &f);
+        if (r < 0)
+                return log_debug_errno(r, "Failed to convert file handle: %m");
+        TAKE_FD(fd);
 
         r = read_line(f, LONG_LINE_MAX, &line);
         if (r < 0)