From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id 245B75A0262; Tue, 20 Aug 2024 23:58:53 +0200 (CEST) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH v2 2/2] util: Provide own version of close_range(), and no-op fallback Date: Tue, 20 Aug 2024 23:58:53 +0200 Message-ID: <20240820215853.2166370-3-sbrivio@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240820215853.2166370-1-sbrivio@redhat.com> References: <20240820215853.2166370-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: EE6QAQ7KLGQSTWFMRADMRJST4M5RBA5R X-Message-ID-Hash: EE6QAQ7KLGQSTWFMRADMRJST4M5RBA5R X-MailFrom: sbrivio@passt.top X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: David Gibson , lemmi@nerd2nerd.org X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: musl, as of 1.2.5, and glibc < 2.34 don't ship a (trivial) close_range() implementation. This will probably be added to musl soon, by the way: https://www.openwall.com/lists/musl/2024/08/01/9 Add a weakly-aliased implementation, if it's supported by the kernel. If it's not supported (< 5.9), use a no-op fallback. Looping over 2^31 file descriptors calling close() on them is probably not a good idea. Reported-by: lemmi Signed-off-by: Stefano Brivio --- util.h | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/util.h b/util.h index cb4d181..9c95dcd 100644 --- a/util.h +++ b/util.h @@ -14,6 +14,9 @@ #include #include #include +#include +#include +#include #include "log.h" @@ -160,6 +163,25 @@ struct ctx; /* cppcheck-suppress funcArgNamesDifferent */ __attribute__ ((weak)) int ffsl(long int i) { return __builtin_ffsl(i); } + +#ifdef CLOSE_RANGE_UNSHARE /* Linux kernel >= 5.9 */ +/* glibc < 2.34 and musl as of 1.2.5 need these */ +#ifndef SYS_close_range +#define SYS_close_range 436 +#endif +__attribute__ ((weak)) +/* cppcheck-suppress funcArgNamesDifferent */ +int close_range(unsigned int first, unsigned int last, int flags) { + return syscall(SYS_close_range, first, last, flags); +} +#else +/* No reasonable fallback option */ +/* cppcheck-suppress funcArgNamesDifferent */ +int close_range(unsigned int first, unsigned int last, int flags) { + return 0; +} +#endif + int sock_l4_sa(const struct ctx *c, enum epoll_type type, const void *sa, socklen_t sl, const char *ifname, bool v6only, uint32_t data); -- 2.43.0