|
|
ac3a84 |
From 60fa029fe83af62f27bf833dc86c0aeeb76b412b Mon Sep 17 00:00:00 2001
|
|
|
ac3a84 |
From: Lennart Poettering <lennart@poettering.net>
|
|
|
ac3a84 |
Date: Fri, 4 Nov 2022 18:19:29 +0100
|
|
|
ac3a84 |
Subject: [PATCH] fd-util: make fd_in_set() (and thus close_all_fds()) handle
|
|
|
ac3a84 |
invalidated fds in the array
|
|
|
ac3a84 |
|
|
|
ac3a84 |
let's handle gracefully if fds in the specified array are already
|
|
|
ac3a84 |
invalidated (i.e. negative). This is handy when putting together arrays
|
|
|
ac3a84 |
on the fly.
|
|
|
ac3a84 |
|
|
|
ac3a84 |
(cherry picked from commit d11c14a9817f6561a30d96d8faea126a4c811af8)
|
|
|
ac3a84 |
|
|
|
ac3a84 |
Related: #2138081
|
|
|
ac3a84 |
---
|
|
|
ac3a84 |
src/basic/fd-util.c | 10 +++++++++-
|
|
|
ac3a84 |
1 file changed, 9 insertions(+), 1 deletion(-)
|
|
|
ac3a84 |
|
|
|
ac3a84 |
diff --git a/src/basic/fd-util.c b/src/basic/fd-util.c
|
|
|
ac3a84 |
index cee20a9a81..6ed04449bf 100644
|
|
|
ac3a84 |
--- a/src/basic/fd-util.c
|
|
|
ac3a84 |
+++ b/src/basic/fd-util.c
|
|
|
ac3a84 |
@@ -177,9 +177,13 @@ int fd_cloexec(int fd, bool cloexec) {
|
|
|
ac3a84 |
_pure_ static bool fd_in_set(int fd, const int fdset[], size_t n_fdset) {
|
|
|
ac3a84 |
assert(n_fdset == 0 || fdset);
|
|
|
ac3a84 |
|
|
|
ac3a84 |
- for (size_t i = 0; i < n_fdset; i++)
|
|
|
ac3a84 |
+ for (size_t i = 0; i < n_fdset; i++) {
|
|
|
ac3a84 |
+ if (fdset[i] < 0)
|
|
|
ac3a84 |
+ continue;
|
|
|
ac3a84 |
+
|
|
|
ac3a84 |
if (fdset[i] == fd)
|
|
|
ac3a84 |
return true;
|
|
|
ac3a84 |
+ }
|
|
|
ac3a84 |
|
|
|
ac3a84 |
return false;
|
|
|
ac3a84 |
}
|
|
|
ac3a84 |
@@ -252,6 +256,10 @@ static int close_all_fds_special_case(const int except[], size_t n_except) {
|
|
|
ac3a84 |
if (!have_close_range)
|
|
|
ac3a84 |
return 0;
|
|
|
ac3a84 |
|
|
|
ac3a84 |
+ if (n_except == 1 && except[0] < 0) /* Minor optimization: if we only got one fd, and it's invalid,
|
|
|
ac3a84 |
+ * we got none */
|
|
|
ac3a84 |
+ n_except = 0;
|
|
|
ac3a84 |
+
|
|
|
ac3a84 |
switch (n_except) {
|
|
|
ac3a84 |
|
|
|
ac3a84 |
case 0:
|