]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fd-util: add new fd_cloexec_many() helper
authorLennart Poettering <lennart@poettering.net>
Fri, 4 Nov 2022 17:20:19 +0000 (18:20 +0100)
committerLennart Poettering <lennart@poettering.net>
Fri, 4 Nov 2022 17:46:42 +0000 (18:46 +0100)
src/basic/fd-util.c
src/basic/fd-util.h

index 6ed04449bf08900319d6bfe651bbb22c3abfc9f5..66bb7569bbb39831666ab65eff2be4629472832c 100644 (file)
@@ -174,6 +174,25 @@ int fd_cloexec(int fd, bool cloexec) {
         return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
 }
 
+int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec) {
+        int ret = 0, r;
+
+        assert(n_fds == 0 || fds);
+
+        for (size_t i = 0; i < n_fds; i++) {
+                if (fds[i] < 0) /* Skip gracefully over already invalidated fds */
+                        continue;
+
+                r = fd_cloexec(fds[i], cloexec);
+                if (r < 0 && ret >= 0) /* Continue going, but return first error */
+                        ret = r;
+                else
+                        ret = 1; /* report if we did anything */
+        }
+
+        return ret;
+}
+
 _pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
         assert(n_fdset == 0 || fdset);
 
index d9896e27e8d5c0fc2876f3a4548485ab9b621416..29c7d86f27ddbb6b0f17ae1426cff33825fb3499 100644 (file)
@@ -56,6 +56,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
 
 int fd_nonblock(int fd, bool nonblock);
 int fd_cloexec(int fd, bool cloexec);
+int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
 
 int get_max_fd(void);