]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: tweak error handling in fd_reopen()
authorLennart Poettering <lennart@poettering.net>
Fri, 22 Oct 2021 12:03:46 +0000 (14:03 +0200)
committerLennart Poettering <lennart@poettering.net>
Fri, 22 Oct 2021 12:06:08 +0000 (14:06 +0200)
If we know that /proc/ works, then ENOENT when reopening an fd means the
fd didn't exist. Let's return the correct error code for that, i.e.
EBADF.

src/basic/fd-util.c

index cf6742c2b319503b3be825c7af94e503f5a95b3e..27f651600ecb29b9d2d6c998a40448ad06fcbd20 100644 (file)
@@ -645,7 +645,7 @@ finish:
 }
 
 int fd_reopen(int fd, int flags) {
-        int new_fd;
+        int new_fd, r;
 
         /* Reopens the specified fd with new flags. This is useful for convert an O_PATH fd into a regular one, or to
          * turn O_RDWR fds into O_RDONLY fds.
@@ -669,10 +669,13 @@ int fd_reopen(int fd, int flags) {
                 if (errno != ENOENT)
                         return -errno;
 
-                if (proc_mounted() == 0)
+                r = proc_mounted();
+                if (r == 0)
                         return -ENOSYS; /* if we have no /proc/, the concept is not implementable */
 
-                return -ENOENT;
+                return r > 0 ? -EBADF : -ENOENT; /* If /proc/ is definitely around then this means the fd is
+                                                  * not valid, otherwise let's propagate the original
+                                                  * error */
         }
 
         return new_fd;