#include <xentoolcore_internal.h>
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
struct xs_stored_msg {
struct list_head list;
struct xsd_sockmsg hdr;
#include <dlfcn.h>
#endif
-#ifndef O_CLOEXEC
-#define O_CLOEXEC 0
-#endif
-
struct xs_handle {
/* Communications channel to xenstore daemon. */
int fd;
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;
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) {