]> xenbits.xensource.com Git - xen.git/commitdiff
tools/libxs: Fix CLOEXEC handling in xs_fileno()
authorAndrew Cooper <andrew.cooper3@citrix.com>
Thu, 4 Jul 2024 12:22:15 +0000 (14:22 +0200)
committerJan Beulich <jbeulich@suse.com>
Thu, 4 Jul 2024 12:22:15 +0000 (14:22 +0200)
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>
Reviewed-by: Juergen Gross <jgross@suse.com>
Acked-by: Anthony PERARD <anthony.perard@vates.tech>
master commit: a2ff677852f0ce05fa335e8e5682bf2ae0c916ee
master date: 2024-07-02 10:52:59 +0100

tools/config.h.in
tools/configure
tools/configure.ac
tools/libs/store/xs.c

index 3071cb3998cef692bdfed935dee52d3c27767666..9d839f8af1e09b8ef558b24016d016e978d5a6a8 100644 (file)
@@ -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
+
 /* Qemu traditional enabled */
 #undef HAVE_QEMU_TRADITIONAL
 
index 6f0b0bb82bcb63a85bfbf7a4f2b875ab4cfacf6c..402364f4c4e3fe3f589626f94c5d6289c35d980e 100755 (executable)
@@ -9699,6 +9699,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
index 31bdef3d70791f33384d011d669172b3d2e27c98..c1ea1e32ca58d9910d30ba9a461a0941dc6500a2 100644 (file)
@@ -518,4 +518,6 @@ AS_IF([test "x$pvshim" = "xy"], [
 
 AX_FIND_HEADER([INCLUDE_ENDIAN_H], [endian.h sys/endian.h])
 
+AC_CHECK_FUNCS([pipe2])
+
 AC_OUTPUT()
index 26542f13dd24be497b4f8753d806fe1149363747..3f723bff5ac40ecedf29e08d3bc21a30f8087bc0 100644 (file)
@@ -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 (!list_empty(&h->watch_list))
                        while (write(h->watch_pipe[1], &c, 1) != 1)