]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/fdset: add detailed debug logging to fdset_new_fill()
authorAshishKumar Mishra <ashishkumar.mishra@bmwtechworks.in>
Wed, 21 Jan 2026 08:43:29 +0000 (14:13 +0530)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 21 Jan 2026 11:11:23 +0000 (20:11 +0900)
Currently, when fdset_new_fill() fails to open /proc/self/fd or
encounters an error while processing individual file descriptors
(such as fcntl or fstat failures), it returns a silent error code.

For debugging rarely reproducible failures it becomes difficult to
know the exact cause of failure
This commit updates the function to use log_debug_errno() for all
error paths and hence  provides better visibility into why FD collection
failed, including the path of the problematic FD (via fd_get_path)
and its inode type.

src/shared/fdset.c

index 832e7fda6079355a0bfddcfefdfef20bf38a7837..f340f41b0e4e4a5bb1c508537f5a2f41d2fcbed7 100644 (file)
@@ -8,6 +8,7 @@
 #include "alloc-util.h"
 #include "async.h"
 #include "dirent-util.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "fdset.h"
 #include "log.h"
@@ -179,9 +180,10 @@ int fdset_new_fill(
         d = opendir("/proc/self/fd");
         if (!d) {
                 if (errno == ENOENT && proc_mounted() == 0)
-                        return -ENOSYS;
+                        return log_debug_errno(SYNTHETIC_ERRNO(ENOSYS),
+                                               "Failed to open /proc/self/fd/, /proc/ is not mounted.");
 
-                return -errno;
+                return log_debug_errno(errno, "Failed to open /proc/self/fd/: %m ");
         }
 
         s = fdset_new();
@@ -210,9 +212,14 @@ int fdset_new_fill(
                          * been passed in can be collected and fds which have been created locally can be
                          * ignored, under the assumption that only the latter have O_CLOEXEC set. */
 
-                        fl = fcntl(fd, F_GETFD);
-                        if (fl < 0)
-                                return -errno;
+                        fl = RET_NERRNO(fcntl(fd, F_GETFD));
+                        if (fl < 0) {
+                                _cleanup_free_ char *path = NULL;
+                                (void) fd_get_path(fd, &path);
+                                return log_debug_errno(fl,
+                                                       "Failed to get flag of fd=%d (%s): %m ",
+                                                       fd, strna(path));
+                        }
 
                         if (FLAGS_SET(fl, FD_CLOEXEC) != !!filter_cloexec)
                                 continue;
@@ -221,13 +228,23 @@ int fdset_new_fill(
                 /* We need to set CLOEXEC manually only if we're collecting non-CLOEXEC fds. */
                 if (filter_cloexec <= 0) {
                         r = fd_cloexec(fd, true);
-                        if (r < 0)
-                                return r;
+                        if (r < 0) {
+                                _cleanup_free_ char *path = NULL;
+                                (void) fd_get_path(fd, &path);
+                                return log_debug_errno(r,
+                                                       "Failed to set CLOEXEC flag fd=%d (%s): %m ",
+                                                       fd, strna(path));
+                        }
                 }
 
                 r = fdset_put(s, fd);
-                if (r < 0)
-                        return r;
+                if (r < 0) {
+                        _cleanup_free_ char *path = NULL;
+                        (void) fd_get_path(fd, &path);
+                        return log_debug_errno(r,
+                                               "Failed to put fd=%d (%s) into fdset: %m ",
+                                               fd, strna(path));
+                }
         }
 
         *ret = TAKE_PTR(s);