]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/xen.git/commitdiff
tools/libs/*: Use O_CLOEXEC on Linux and FreeBSD
authorIan Campbell <ian.campbell@citrix.com>
Wed, 2 Dec 2015 16:21:41 +0000 (16:21 +0000)
committerIan Campbell <ian.campbell@citrix.com>
Fri, 22 Jan 2016 12:24:22 +0000 (12:24 +0000)
In some cases this replaces an FD_CLOEXEC dance, in others it is new.

Linux has had O_CLOEXEC since 2.6.23 (October 2007), so we can rely on
it from Xen 4.7 I think. Some libc headers may still lack the
definition, so we take care of that if need be by defining to 0 (on
the premise that such an old glibc might barf on O_CLOEXEC even if the
kernel may or may not be so old).

All stable versions of FreeBSD support O_CLOEXEC (10.2, 9.3 and 8.4),
and we assume the libc there does too.

Remove various comments about having to take responsibility for this
(since really it is just hygiene, politeness, not a requirement) and
the reasons for using O_CLOEXEC seem pretty straightforward.

Backends for other OSes are untouched.

Signed-off-by: Ian Campbell <ian.campbell@citrix.com>
Acked-by: Roger Pau Monné <roger.pau@citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Cc: Roger.Pau@citrix.com
Cc: jbeulich@suse.com
tools/libs/call/freebsd.c
tools/libs/call/linux.c
tools/libs/evtchn/freebsd.c
tools/libs/evtchn/linux.c
tools/libs/foreignmemory/freebsd.c
tools/libs/foreignmemory/linux.c
tools/libs/gnttab/linux.c

index 2413966452670358102f3c629e58333ee84ac46b..b3cbccd774344cf1aea6435e271ca015ba6dbef3 100644 (file)
 
 int osdep_xencall_open(xencall_handle *xcall)
 {
-    int flags, saved_errno;
-    int fd = open(PRIVCMD_DEV, O_RDWR);
+    int saved_errno;
+    int fd = open(PRIVCMD_DEV, O_RDWR|O_CLOEXEC);
+
+    /*
+     * This file descriptor is opaque to the caller, thus we are
+     * polite and try and ensure it doesn't propagate (ie leak)
+     * outside the process, by using O_CLOEXEC.
+     */
 
     if ( fd == -1 )
     {
@@ -45,26 +51,6 @@ int osdep_xencall_open(xencall_handle *xcall)
         return -1;
     }
 
-    /*
-     * Although we return the file handle as the 'xc handle' the API
-     * does not specify / guarentee that this integer is in fact
-     * a file handle. Thus we must take responsiblity to ensure
-     * it doesn't propagate (ie leak) outside the process.
-     */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     xcall->fd = fd;
     return 0;
 
index 651f380cb13c5862aed9546bcb9434cb962eef50..e8e03111ab187016ffdbfcd498daec05f7c24f82 100644 (file)
 
 #include "private.h"
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_xencall_open(xencall_handle *xcall)
 {
-    int flags, saved_errno;
-    int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
+    int fd;
+
+    /*
+     * Prefer the newer interface.
+     */
+    fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
 
     if ( fd == -1 && ( errno == ENOENT || errno == ENXIO || errno == ENODEV ))
     {
         /* Fallback to /proc/xen/privcmd */
-        fd = open("/proc/xen/privcmd", O_RDWR);
+        fd = open("/proc/xen/privcmd", O_RDWR|O_CLOEXEC);
     }
 
     if ( fd == -1 )
@@ -43,32 +51,8 @@ int osdep_xencall_open(xencall_handle *xcall)
         return -1;
     }
 
-    /* Although we return the file handle as the 'xc handle' the API
-       does not specify / guarentee that this integer is in fact
-       a file handle. Thus we must take responsiblity to ensure
-       it doesn't propagate (ie leak) outside the process */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     xcall->fd = fd;
     return 0;
-
- error:
-    saved_errno = errno;
-    close(fd);
-    errno = saved_errno;
-    return -1;
 }
 
 int osdep_xencall_close(xencall_handle *xcall)
index 6479f7c173092d09071228837dd22d598d7711fa..ddf221dd8b85015db90fe410e334eb9c9d8a8b32 100644 (file)
@@ -32,7 +32,7 @@
 
 int osdep_evtchn_open(xenevtchn_handle *xce)
 {
-    int fd = open(EVTCHN_DEV, O_RDWR);
+    int fd = open(EVTCHN_DEV, O_RDWR|O_CLOEXEC);
     if ( fd == -1 )
         return -1;
     xce->fd = fd;
index 76cf0acf883ba2e1716f22783194f4932e11d819..0a3c6e12c6cb6f9a5f5dc618d7bb00818ad182d4 100644 (file)
 
 #include "private.h"
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_evtchn_open(xenevtchn_handle *xce)
 {
-    int fd = open("/dev/xen/evtchn", O_RDWR);
+    int fd = open("/dev/xen/evtchn", O_RDWR|O_CLOEXEC);
     if ( fd == -1 )
         return -1;
     xce->fd = fd;
index 38138dcfed3959942b9aba068988e5c7fbb1801e..7bf393993d2e3dbe8e2cb32b98b41542342064bc 100644 (file)
@@ -33,8 +33,8 @@
 
 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
 {
-    int flags, saved_errno;
-    int fd = open(PRIVCMD_DEV, O_RDWR);
+    int saved_errno;
+    int fd = open(PRIVCMD_DEV, O_RDWR|O_CLOEXEC);
 
     if ( fd == -1 )
     {
@@ -43,26 +43,6 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }
 
-    /*
-     * Although we return the file handle as the 'xc handle' the API
-     * does not specify / guarentee that this integer is in fact
-     * a file handle. Thus we must take responsiblity to ensure
-     * it doesn't propagate (ie leak) outside the process.
-     */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     fmem->fd = fd;
     return 0;
 
index 32b6defa1b4bcceb53f7854637db79ba05aefa4e..423c7441bc87959fc9f87ef166162efe4ce451b8 100644 (file)
 
 #define ROUNDUP(_x,_w) (((unsigned long)(_x)+(1UL<<(_w))-1) & ~((1UL<<(_w))-1))
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
 {
-    int flags, saved_errno;
-    int fd = open("/dev/xen/privcmd", O_RDWR); /* prefer this newer interface */
+    int fd;
+
+    /* prefer this newer interface */
+    fd = open("/dev/xen/privcmd", O_RDWR|O_CLOEXEC);
 
     if ( fd == -1 && ( errno == ENOENT || errno == ENXIO || errno == ENODEV ))
     {
         /* Fallback to /proc/xen/privcmd */
-        fd = open("/proc/xen/privcmd", O_RDWR);
+        fd = open("/proc/xen/privcmd", O_RDWR|O_CLOEXEC);
     }
 
     if ( fd == -1 )
@@ -47,32 +53,8 @@ int osdep_xenforeignmemory_open(xenforeignmemory_handle *fmem)
         return -1;
     }
 
-    /* Although we return the file handle as the 'xc handle' the API
-       does not specify / guarentee that this integer is in fact
-       a file handle. Thus we must take responsiblity to ensure
-       it doesn't propagate (ie leak) outside the process */
-    if ( (flags = fcntl(fd, F_GETFD)) < 0 )
-    {
-        PERROR("Could not get file handle flags");
-        goto error;
-    }
-
-    flags |= FD_CLOEXEC;
-
-    if ( fcntl(fd, F_SETFD, flags) < 0 )
-    {
-        PERROR("Could not set file handle flags");
-        goto error;
-    }
-
     fmem->fd = fd;
     return 0;
-
- error:
-    saved_errno = errno;
-    close(fd);
-    errno = saved_errno;
-    return -1;
 }
 
 int osdep_xenforeignmemory_close(xenforeignmemory_handle *fmem)
index be04295304fce0732e46c12200a05538c90fdc1a..7b0fba46e61ee40a8b0f0962b2e47e4f688dc007 100644 (file)
 #define PAGE_SIZE            (1UL << PAGE_SHIFT)
 #define PAGE_MASK            (~(PAGE_SIZE-1))
 
+#ifndef O_CLOEXEC
+#define O_CLOEXEC 0
+#endif
+
 int osdep_gnttab_open(xengnttab_handle *xgt)
 {
-    int fd = open(DEVXEN "gntdev", O_RDWR);
+    int fd = open(DEVXEN "gntdev", O_RDWR|O_CLOEXEC);
     if ( fd == -1 )
         return -1;
     xgt->fd = fd;