diff options
Diffstat (limited to '0047-tools-libxs-Fix-CLOEXEC-handling-in-get_dev.patch')
-rw-r--r-- | 0047-tools-libxs-Fix-CLOEXEC-handling-in-get_dev.patch | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/0047-tools-libxs-Fix-CLOEXEC-handling-in-get_dev.patch b/0047-tools-libxs-Fix-CLOEXEC-handling-in-get_dev.patch new file mode 100644 index 0000000..aabae58 --- /dev/null +++ b/0047-tools-libxs-Fix-CLOEXEC-handling-in-get_dev.patch @@ -0,0 +1,95 @@ +From d1b3bbb46402af77089906a97c413c14ed1740d2 Mon Sep 17 00:00:00 2001 +From: Andrew Cooper <andrew.cooper3@citrix.com> +Date: Thu, 4 Jul 2024 14:13:10 +0200 +Subject: [PATCH 47/56] tools/libxs: Fix CLOEXEC handling in get_dev() + +Move the O_CLOEXEC compatibility outside of an #ifdef USE_PTHREAD block. + +Introduce set_cloexec() to wrap fcntl() setting FD_CLOEXEC. It will be reused +for other CLOEXEC fixes too. + +Use set_cloexec() when O_CLOEXEC isn't available as a best-effort fallback. + +Fixes: f4f2f3402b2f ("tools/libxs: Open /dev/xen/xenbus fds as O_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: bf7c1464706adfa903f1e7d59383d042c3a88e39 +master date: 2024-07-02 10:51:06 +0100 +--- + tools/libs/store/xs.c | 38 ++++++++++++++++++++++++++++++++------ + 1 file changed, 32 insertions(+), 6 deletions(-) + +diff --git a/tools/libs/store/xs.c b/tools/libs/store/xs.c +index 1498515073..037e79d98b 100644 +--- a/tools/libs/store/xs.c ++++ b/tools/libs/store/xs.c +@@ -40,6 +40,10 @@ + #include <xentoolcore_internal.h> + #include <xen_list.h> + ++#ifndef O_CLOEXEC ++#define O_CLOEXEC 0 ++#endif ++ + struct xs_stored_msg { + XEN_TAILQ_ENTRY(struct xs_stored_msg) list; + struct xsd_sockmsg hdr; +@@ -54,10 +58,6 @@ struct xs_stored_msg { + #include <dlfcn.h> + #endif + +-#ifndef O_CLOEXEC +-#define O_CLOEXEC 0 +-#endif +- + struct xs_handle { + /* Communications channel to xenstore daemon. */ + int fd; +@@ -176,6 +176,16 @@ static bool setnonblock(int fd, int nonblock) { + return true; + } + ++static bool set_cloexec(int fd) ++{ ++ int flags = fcntl(fd, F_GETFL); ++ ++ if (flags < 0) ++ return false; ++ ++ return fcntl(fd, flags | FD_CLOEXEC) >= 0; ++} ++ + int xs_fileno(struct xs_handle *h) + { + char c = 0; +@@ -230,8 +240,24 @@ error: + + static int get_dev(const char *connect_to) + { +- /* We cannot open read-only because requests are writes */ +- return open(connect_to, O_RDWR | O_CLOEXEC); ++ int fd, saved_errno; ++ ++ fd = open(connect_to, O_RDWR | O_CLOEXEC); ++ if (fd < 0) ++ return -1; ++ ++ /* Compat for non-O_CLOEXEC environments. Racy. */ ++ if (!O_CLOEXEC && !set_cloexec(fd)) ++ goto error; ++ ++ return fd; ++ ++error: ++ saved_errno = errno; ++ close(fd); ++ errno = saved_errno; ++ ++ return -1; + } + + static int all_restrict_cb(Xentoolcore__Active_Handle *ah, domid_t domid) { +-- +2.45.2 + |