From: Andrew Cooper <andrew.cooper3@citrix.com>
To: Xen-devel <xen-devel@lists.xenproject.org>
Cc: "Andrew Cooper" <andrew.cooper3@citrix.com>,
"Anthony PERARD" <anthony@xenproject.org>,
"Juergen Gross" <jgross@suse.com>,
"Roger Pau Monné" <roger.pau@citrix.com>,
"Frediano Ziglio" <frediano.ziglio@cloud.com>,
"Oleksii Kurochko" <oleksii.kurochko@gmail.com>
Subject: [PATCH for-4.19 3/3] tools/libxs: Fix CLOEXEC handling in xs_fileno()
Date: Fri, 28 Jun 2024 15:31:16 +0100 [thread overview]
Message-ID: <20240628143116.1044976-4-andrew.cooper3@citrix.com> (raw)
In-Reply-To: <20240628143116.1044976-1-andrew.cooper3@citrix.com>
xs_fileno() opens a pipe on first use to communicate between the watch thread
and the main thread. Nothing ever sets CLOEXEC on the file descriptors.
Check for the availability of the pipe2() function with configure. Despite
starting life as Linux-only, FreeBSD and NetBSD have gained it.
When pipe2() isn't available, try our best with pipe() and set_cloexec().
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
---
CC: Anthony PERARD <anthony@xenproject.org>
CC: Juergen Gross <jgross@suse.com>
CC: Roger Pau Monné <roger.pau@citrix.com>
CC: Frediano Ziglio <frediano.ziglio@cloud.com>
CC: Oleksii Kurochko <oleksii.kurochko@gmail.com>
---
tools/config.h.in | 3 +++
tools/configure | 12 ++++++++++++
tools/configure.ac | 2 ++
tools/libs/store/xs.c | 16 +++++++++++++++-
4 files changed, 32 insertions(+), 1 deletion(-)
diff --git a/tools/config.h.in b/tools/config.h.in
index 0bb2fe08a143..50ad60fcb091 100644
--- a/tools/config.h.in
+++ b/tools/config.h.in
@@ -39,6 +39,9 @@
/* Define to 1 if you have the <memory.h> header file. */
#undef HAVE_MEMORY_H
+/* Define to 1 if you have the `pipe2' function. */
+#undef HAVE_PIPE2
+
/* pygrub enabled */
#undef HAVE_PYGRUB
diff --git a/tools/configure b/tools/configure
index 459bfb56520e..a6b43bfc6064 100755
--- a/tools/configure
+++ b/tools/configure
@@ -9751,6 +9751,18 @@ if test "$ax_found" = "0"; then :
fi
+for ac_func in pipe2
+do :
+ ac_fn_c_check_func "$LINENO" "pipe2" "ac_cv_func_pipe2"
+if test "x$ac_cv_func_pipe2" = xyes; then :
+ cat >>confdefs.h <<_ACEOF
+#define HAVE_PIPE2 1
+_ACEOF
+
+fi
+done
+
+
cat >confcache <<\_ACEOF
# This file is a shell script that caches the results of configure
# tests run on this system so they can be shared between configure
diff --git a/tools/configure.ac b/tools/configure.ac
index 851887080c5e..ac0fdc4314c4 100644
--- a/tools/configure.ac
+++ b/tools/configure.ac
@@ -543,4 +543,6 @@ AS_IF([test "x$pvshim" = "xy"], [
AX_FIND_HEADER([INCLUDE_ENDIAN_H], [endian.h sys/endian.h])
+AC_CHECK_FUNCS([pipe2])
+
AC_OUTPUT()
diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c
index 11a766c50887..27bd20933efd 100644
--- a/tools/libs/store/xs.c
+++ b/tools/libs/store/xs.c
@@ -190,13 +190,27 @@ static bool set_cloexec(int fd)
return fcntl(fd, flags | FD_CLOEXEC) >= 0;
}
+static int pipe_cloexec(int fds[2])
+{
+#if HAVE_PIPE2
+ return pipe2(fds, O_CLOEXEC);
+#else
+ if (pipe(fds) < 0)
+ return -1;
+ /* Best effort to set CLOEXEC. Racy. */
+ set_cloexec(fds[0]);
+ set_cloexec(fds[1]);
+ return 0;
+#endif
+}
+
int xs_fileno(struct xs_handle *h)
{
char c = 0;
mutex_lock(&h->watch_mutex);
- if ((h->watch_pipe[0] == -1) && (pipe(h->watch_pipe) != -1)) {
+ if ((h->watch_pipe[0] == -1) && (pipe_cloexec(h->watch_pipe) != -1)) {
/* Kick things off if the watch list is already non-empty. */
if (!XEN_TAILQ_EMPTY(&h->watch_list))
while (write(h->watch_pipe[1], &c, 1) != 1)
--
2.39.2
next prev parent reply other threads:[~2024-06-28 14:31 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-28 14:31 [PATCH for-4.19(?) 0/3] tools/libxs: More CLOEXEC fixes Andrew Cooper
2024-06-28 14:31 ` [PATCH for-4.19 1/3] tools/libxs: Fix CLOEXEC handling in get_dev() Andrew Cooper
2024-06-28 14:31 ` [PATCH for-4.19 2/3] tools/libxs: Fix CLOEXEC handling in get_socket() Andrew Cooper
2024-06-28 14:31 ` Andrew Cooper [this message]
2024-07-01 9:03 ` [PATCH for-4.19(?) 0/3] tools/libxs: More CLOEXEC fixes Jürgen Groß
2024-07-01 9:28 ` Anthony PERARD
2024-07-01 15:06 ` Oleksii
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240628143116.1044976-4-andrew.cooper3@citrix.com \
--to=andrew.cooper3@citrix.com \
--cc=anthony@xenproject.org \
--cc=frediano.ziglio@cloud.com \
--cc=jgross@suse.com \
--cc=oleksii.kurochko@gmail.com \
--cc=roger.pau@citrix.com \
--cc=xen-devel@lists.xenproject.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.