|
|
2aacef |
From b9223cd76c73fa5cf3ed19a2238980047bf13826 Mon Sep 17 00:00:00 2001
|
|
|
2aacef |
From: Lennart Poettering <lennart@poettering.net>
|
|
|
2aacef |
Date: Fri, 4 Nov 2022 18:20:19 +0100
|
|
|
2aacef |
Subject: [PATCH] fd-util: add new fd_cloexec_many() helper
|
|
|
2aacef |
|
|
|
2aacef |
(cherry picked from commit ed18c22c989495aab36512f03449222cfcf79aa7)
|
|
|
2aacef |
|
|
|
2aacef |
Related: #2138081
|
|
|
2aacef |
---
|
|
|
2aacef |
src/basic/fd-util.c | 19 +++++++++++++++++++
|
|
|
2aacef |
src/basic/fd-util.h | 1 +
|
|
|
2aacef |
2 files changed, 20 insertions(+)
|
|
|
2aacef |
|
|
|
2aacef |
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c
|
|
|
2aacef |
index 6ed04449bf..66bb7569bb 100644
|
|
|
2aacef |
--- a/src/basic/fd-util.c
|
|
|
2aacef |
+++ b/src/basic/fd-util.c
|
|
|
2aacef |
@@ -174,6 +174,25 @@ int fd_cloexec(int fd, bool cloexec) {
|
|
|
2aacef |
return RET_NERRNO(fcntl(fd, F_SETFD, nflags));
|
|
|
2aacef |
}
|
|
|
2aacef |
|
|
|
2aacef |
+int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec) {
|
|
|
2aacef |
+ int ret = 0, r;
|
|
|
2aacef |
+
|
|
|
2aacef |
+ assert(n_fds == 0 || fds);
|
|
|
2aacef |
+
|
|
|
2aacef |
+ for (size_t i = 0; i < n_fds; i++) {
|
|
|
2aacef |
+ if (fds[i] < 0) /* Skip gracefully over already invalidated fds */
|
|
|
2aacef |
+ continue;
|
|
|
2aacef |
+
|
|
|
2aacef |
+ r = fd_cloexec(fds[i], cloexec);
|
|
|
2aacef |
+ if (r < 0 && ret >= 0) /* Continue going, but return first error */
|
|
|
2aacef |
+ ret = r;
|
|
|
2aacef |
+ else
|
|
|
2aacef |
+ ret = 1; /* report if we did anything */
|
|
|
2aacef |
+ }
|
|
|
2aacef |
+
|
|
|
2aacef |
+ return ret;
|
|
|
2aacef |
+}
|
|
|
2aacef |
+
|
|
|
2aacef |
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
|
|
|
2aacef |
assert(n_fdset == 0 || fdset);
|
|
|
2aacef |
|
|
|
2aacef |
diff --git a/src/basic/fd-util.h b/src/basic/fd-util.h
|
|
|
2aacef |
index d9896e27e8..29c7d86f27 100644
|
|
|
2aacef |
--- a/src/basic/fd-util.h
|
|
|
2aacef |
+++ b/src/basic/fd-util.h
|
|
|
2aacef |
@@ -56,6 +56,7 @@ DEFINE_TRIVIAL_CLEANUP_FUNC_FULL(DIR*, closedir, NULL);
|
|
|
2aacef |
|
|
|
2aacef |
int fd_nonblock(int fd, bool nonblock);
|
|
|
2aacef |
int fd_cloexec(int fd, bool cloexec);
|
|
|
2aacef |
+int fd_cloexec_many(const int fds[], size_t n_fds, bool cloexec);
|
|
|
2aacef |
|
|
|
2aacef |
int get_max_fd(void);
|
|
|
2aacef |
|