xen-devel.lists.xenproject.org archive mirror
 help / color / mirror / Atom feed
From: Ian Jackson <ian.jackson@eu.citrix.com>
To: xen-devel@lists.xensource.com
Cc: Ian Jackson <ian.jackson@eu.citrix.com>
Subject: [PATCH 06/11] libxl: introduce libxl_fd_set_nonblock, rationalise _cloexec
Date: Thu, 26 Jan 2012 17:20:52 +0000	[thread overview]
Message-ID: <1327598457-28261-7-git-send-email-ian.jackson@eu.citrix.com> (raw)
In-Reply-To: <1327598457-28261-1-git-send-email-ian.jackson@eu.citrix.com>

We want a function for setting fds to nonblocking, so introduce one.

This is a very similar requirement to that for libxl_fd_set_cloexec,
so make it common with that.

While we're at it, fix a few deficiences that make this latter
function less desirable than it could be:
 * Change the return from 0/-1 (like a syscall) to a libxl error code
 * Take a boolean parameter for turning the flag on and off
 * Log on error (and so, take a ctx for this purpose)

Change callers of libxl_fd_set_cloexec to notice errors.  (Although,
such errors are highly unlikely.)

Signed-off-by: Ian Jackson <ian.jackson@eu.citrix.com>
Acked-by: Ian Campbell <ian.campbell@citrix.com>
---
 tools/libxl/libxl.c      |   33 ++++++++++++++++++++++++++-------
 tools/libxl/libxl.h      |    7 ++++++-
 tools/libxl/libxl_qmp.c  |    3 ++-
 tools/libxl/xl_cmdimpl.c |    3 ++-
 4 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 59d5114..0094541 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -3656,19 +3656,38 @@ int libxl_cpupool_movedomain(libxl_ctx *ctx, uint32_t poolid, uint32_t domid)
     return 0;
 }
 
-int libxl_fd_set_cloexec(int fd)
+static int fd_set_flags(libxl_ctx *ctx, int fd,
+                        int fcntlgetop, int fcntlsetop, const char *fl,
+                        int flagmask, int set_p)
 {
-    int flags = 0;
+    int flags, r;
 
-    if ((flags = fcntl(fd, F_GETFD)) == -1) {
-        flags = 0;
+    flags = fcntl(fd, fcntlgetop);
+    if (flags == -1) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "fcntl(,F_GET%s) failed",fl);
+        return ERROR_FAIL;
     }
-    if ((flags & FD_CLOEXEC)) {
-        return 0;
+
+    if (set_p)
+        flags |= flagmask;
+    else
+        flags &= ~flagmask;
+
+    r = fcntl(fd, fcntlsetop, flags);
+    if (r == -1) {
+        LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "fcntl(,F_SET%s) failed",fl);
+        return ERROR_FAIL;
     }
-    return fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
+
+    return 0;
 }
 
+int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec)
+  { return fd_set_flags(ctx,fd, F_GETFD,F_SETFD,"FD", FD_CLOEXEC, cloexec); }
+
+int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock)
+  { return fd_set_flags(ctx,fd, F_GETFL,F_SETFL,"FL", O_NONBLOCK, nonblock); }
+
 /*
  * Local variables:
  * mode: C
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 4d3391f..e32881b 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -613,7 +613,12 @@ const char *libxl_run_dir_path(void);
 const char *libxl_xenpaging_dir_path(void);
 
 /* misc */
-int libxl_fd_set_cloexec(int fd);
+
+/* Each of these sets or clears the flag according to whether the
+ * 2nd parameter is nonzero.  On failure, they log, and
+ * return ERROR_FAIL, but also leave errno valid. */
+int libxl_fd_set_cloexec(libxl_ctx *ctx, int fd, int cloexec);
+int libxl_fd_set_nonblock(libxl_ctx *ctx, int fd, int nonblock);
 
 #include <libxl_event.h>
 
diff --git a/tools/libxl/libxl_qmp.c b/tools/libxl/libxl_qmp.c
index 6d401b7..f125b17 100644
--- a/tools/libxl/libxl_qmp.c
+++ b/tools/libxl/libxl_qmp.c
@@ -325,7 +325,8 @@ static int qmp_open(libxl__qmp_handler *qmp, const char *qmp_socket_path,
     if (fcntl(qmp->qmp_fd, F_SETFL, flags | O_NONBLOCK) == -1) {
         return -1;
     }
-    libxl_fd_set_cloexec(qmp->qmp_fd);
+    ret = libxl_fd_set_cloexec(qmp->ctx, qmp->qmp_fd, 1);
+    if (ret) return -1;
 
     memset(&qmp->addr, 0, sizeof (&qmp->addr));
     qmp->addr.sun_family = AF_UNIX;
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index 71126de..92bb275 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -1509,7 +1509,8 @@ static int create_domain(struct domain_create *dom_info)
             restore_fd = migrate_fd;
         } else {
             restore_fd = open(restore_file, O_RDONLY);
-            libxl_fd_set_cloexec(restore_fd);
+            rc = libxl_fd_set_cloexec(ctx, restore_fd, 1);
+            if (rc) return rc;
         }
 
         CHK_ERRNO( libxl_read_exactly(ctx, restore_fd, &hdr,
-- 
1.7.2.5

  parent reply	other threads:[~2012-01-26 17:20 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-01-26 17:20 [PATCH v8 00/11] libxl: New event API Ian Jackson
2012-01-26 17:20 ` [PATCH 01/11] .gitignore/.hgignore: New names for ioemu dirs, seabios Ian Jackson
2012-01-27 10:39   ` Ian Campbell
2012-01-26 17:20 ` [PATCH 02/11] xl: fix a couple of memory leaks Ian Jackson
2012-01-27 10:30   ` Ian Campbell
2012-01-26 17:20 ` [PATCH 03/11] libxl: New API for providing OS events to libxl Ian Jackson
2012-01-26 17:20 ` [PATCH 04/11] ocaml, libxl: support "private" fields Ian Jackson
2012-01-27 15:22   ` Ian Campbell
2012-01-26 17:20 ` [PATCH 05/11] libxl: New event generation API Ian Jackson
2012-01-26 17:20 ` Ian Jackson [this message]
2012-01-26 17:20 ` [PATCH 07/11] libxl: Permit multithreaded event waiting Ian Jackson
2012-01-26 17:20 ` [PATCH 08/11] libxl: Asynchronous/long-running operation infrastructure Ian Jackson
2012-02-15 12:25   ` Roger Pau Monné
2012-02-15 12:51     ` Ian Jackson
2012-02-15 13:06       ` Roger Pau Monné
2012-02-17 16:12         ` Ian Jackson
2012-02-17 18:43           ` [PATCH 0/3] libxl: Permit immediate asynchronous completion Ian Jackson
2012-02-17 18:47             ` Ian Jackson
2012-02-20 12:20               ` Roger Pau Monné
2012-02-20 13:44                 ` Ian Jackson
2012-02-17 18:43           ` [PATCH 1/3] libxl: ao: allow immediate completion Ian Jackson
2012-02-20 10:26             ` Ian Campbell
2012-02-20 13:32               ` Ian Jackson
2012-02-17 18:43           ` [PATCH 2/3] libxl: fix hang due to libxl__initiate_device_remove Ian Jackson
2012-02-17 18:43           ` [PATCH 3/3] libxl: Fix eventloop_iteration over-locking Ian Jackson
2012-02-20 10:30             ` Ian Campbell
2012-01-26 17:20 ` [PATCH 09/11] libxl: New convenience macro CONTAINER_OF Ian Jackson
2012-01-27 15:22   ` Ian Campbell
2012-01-26 17:20 ` [PATCH 10/11] libxl: Introduce libxl__ev_devstate Ian Jackson
2012-01-26 17:20 ` [PATCH 11/11] libxl: Convert to asynchronous: device removal Ian Jackson
2012-01-27 15:25 ` [PATCH v8 00/11] libxl: New event API Ian Campbell
2012-01-27 17:07   ` Ian Jackson

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=1327598457-28261-7-git-send-email-ian.jackson@eu.citrix.com \
    --to=ian.jackson@eu.citrix.com \
    --cc=xen-devel@lists.xensource.com \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).