[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH v2 1/7] virCommand: Introduce virCommandDoAsyncIO



Currently, if we want to feed stdin, or catch stdout or stderr of a
virCommand we have to use virCommandRun(). When using virCommandRunAsync()
we have to register FD handles by hand. This may lead to code duplication.
Hence, introduce an internal API, which does this automatically within
virCommandRunAsync(). The intended usage looks like this:

    virCommandPtr cmd = virCommandNew*(...);
    char *buf = NULL;

    ...

    virCommandSetOutputBuffer(cmd, &buf);
    virCommandDoAsyncIO(cmd);

    if (virCommandRunAsync(cmd, NULL) < 0)
        goto cleanup;

    ...

    if (virCommandWait(cmd, NULL) < 0)
        goto cleanup;

    /* @buf now contains @cmd's stdout */
    VIR_DEBUG("STDOUT: %s", NULLSTR(buf));

    ...

cleanup:
    VIR_FREE(buf);
    virCommandFree(cmd);

Note, that both stdout and stderr buffers may change until virCommandWait()
returns.
---
 src/libvirt_private.syms |   1 +
 src/util/vircommand.c    | 279 +++++++++++++++++++++++++++++++++++++++++++++--
 src/util/vircommand.h    |   1 +
 3 files changed, 273 insertions(+), 8 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index c589236..99e20c0 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -143,6 +143,7 @@ virCommandAddEnvString;
 virCommandAllowCap;
 virCommandClearCaps;
 virCommandDaemonize;
+virCommandDoAsyncIO;
 virCommandExec;
 virCommandFree;
 virCommandHandshakeNotify;
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 8566d1a..117fc07 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -47,11 +47,12 @@
 
 /* Flags for virExecWithHook */
 enum {
-    VIR_EXEC_NONE   = 0,
-    VIR_EXEC_NONBLOCK = (1 << 0),
-    VIR_EXEC_DAEMON = (1 << 1),
+    VIR_EXEC_NONE       = 0,
+    VIR_EXEC_NONBLOCK   = (1 << 0),
+    VIR_EXEC_DAEMON     = (1 << 1),
     VIR_EXEC_CLEAR_CAPS = (1 << 2),
-    VIR_EXEC_RUN_SYNC = (1 << 3),
+    VIR_EXEC_RUN_SYNC   = (1 << 3),
+    VIR_EXEC_ASYNC_IO   = (1 << 4),
 };
 
 struct _virCommand {
@@ -84,6 +85,11 @@ struct _virCommand {
     int *outfdptr;
     int *errfdptr;
 
+    size_t inbufOffset;
+    int inWatch;
+    int outWatch;
+    int errWatch;
+
     bool handshake;
     int handshakeWait[2];
     int handshakeNotify[2];
@@ -779,6 +785,7 @@ virCommandNewArgs(const char *const*args)
     cmd->handshakeNotify[1] = -1;
 
     cmd->infd = cmd->outfd = cmd->errfd = -1;
+    cmd->inWatch = cmd->outWatch = cmd->errWatch = -1;
     cmd->pid = -1;
 
     virCommandAddArgSet(cmd, args);
@@ -2122,6 +2129,173 @@ virCommandHook(void *data)
 }
 
 
+static void
+virCommandHandleReadWrite(int watch, int fd, int events, void *opaque)
+{
+    virCommandPtr cmd = (virCommandPtr) opaque;
+    char ***bufptr = NULL;
+    char buf[1024];
+    ssize_t nread;
+    size_t len = 0;
+    int *watchPtr = NULL;
+    bool eof = false;
+    int tmpfd, *fdptr = NULL, **fdptrptr = NULL;
+
+    VIR_DEBUG("watch=%d fd=%d events=%d", watch, fd, events);
+    errno = 0;
+
+    if (watch == cmd->inWatch) {
+        watchPtr = &cmd->inWatch;
+        fdptr  = &cmd->infd;
+
+        if (events & VIR_EVENT_HANDLE_WRITABLE) {
+            len = strlen(cmd->inbuf);
+
+            while (true) {
+                nread = write(fd, cmd->inbuf + cmd->inbufOffset,
+                              len - cmd->inbufOffset);
+                if (nread < 0) {
+                    if (errno != EAGAIN && errno != EINTR) {
+                        virReportSystemError(errno,
+                                             _("Unable to write command's "
+                                               "input to FD %d"),
+                                             fd);
+                        eof = true;
+                    }
+                    break;
+                }
+
+                if (nread == 0) {
+                    eof = true;
+                    break;
+                }
+
+                cmd->inbufOffset += nread;
+                if (cmd->inbufOffset == len) {
+                    tmpfd = cmd->infd;
+                    if (VIR_CLOSE(cmd->infd) < 0)
+                        VIR_DEBUG("ignoring failed close on fd %d", tmpfd);
+                    eof = true;
+                    break;
+                }
+            }
+
+        }
+    } else {
+        if (watch == cmd->outWatch) {
+            watchPtr = &cmd->outWatch;
+            bufptr = &cmd->outbuf;
+            fdptr = &cmd->outfd;
+            fdptrptr = &cmd->outfdptr;
+        } else {
+            watchPtr = &cmd->errWatch;
+            bufptr = &cmd->errbuf;
+            fdptr = &cmd->outfd;
+            fdptrptr = &cmd->outfdptr;
+        }
+
+        if (bufptr && *bufptr && events & VIR_EVENT_HANDLE_READABLE) {
+            if (**bufptr)
+                len = strlen(**bufptr);
+
+            while (true) {
+                nread = read(fd, buf, sizeof(buf));
+                if (nread < 0) {
+                    if (errno != EAGAIN && errno != EINTR) {
+                        virReportSystemError(errno,
+                                             _("unable to read command's "
+                                               "output from FD %d"),
+                                             fd);
+                        eof = true;
+                    }
+                    break;
+                }
+
+                if (nread == 0) {
+                    eof = true;
+                    break;
+                }
+
+                if (VIR_REALLOC_N(**bufptr, len + nread + 1) < 0) {
+                    virReportOOMError();
+                    break;
+                }
+
+                memcpy(**bufptr + len, buf, nread);
+                (**bufptr)[len + nread] = '\0';
+            }
+
+        }
+    }
+
+    if (eof || (events & VIR_EVENT_HANDLE_HANGUP) ||
+        (events & VIR_EVENT_HANDLE_ERROR)) {
+        *watchPtr = -1;
+        /* Reset any capturing, in case caller runs
+         * this identical command again */
+        tmpfd = *fdptr;
+        if (VIR_CLOSE(*fdptr) < 0)
+            VIR_DEBUG("ignoring failed close on fd %d", tmpfd);
+        if (bufptr)
+            *bufptr = NULL;
+        if (fdptrptr)
+            *fdptrptr = NULL;
+        virEventRemoveHandle(watch);
+    }
+}
+
+
+static int
+virCommandRegisterEventLoop(virCommandPtr cmd)
+{
+    int ret = -1;
+
+    if (cmd->inbuf &&
+        (cmd->inWatch = virEventAddHandle(cmd->infd,
+                                          VIR_EVENT_HANDLE_WRITABLE |
+                                          VIR_EVENT_HANDLE_HANGUP |
+                                          VIR_EVENT_HANDLE_ERROR,
+                                          virCommandHandleReadWrite,
+                                          cmd, NULL)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to register infd %d in the event loop"),
+                       cmd->infd);
+        goto cleanup;
+    }
+
+    if (cmd->outbuf && cmd->outfdptr == &cmd->outfd &&
+        (cmd->outWatch = virEventAddHandle(cmd->outfd,
+                                           VIR_EVENT_HANDLE_READABLE |
+                                           VIR_EVENT_HANDLE_HANGUP |
+                                           VIR_EVENT_HANDLE_ERROR,
+                                           virCommandHandleReadWrite,
+                                           cmd, NULL)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to register outfd %d in the event loop"),
+                       cmd->outfd);
+        goto cleanup;
+    }
+
+    if (cmd->errbuf && cmd->errfdptr == &cmd->errfd &&
+        (cmd->errWatch = virEventAddHandle(cmd->errfd,
+                                           VIR_EVENT_HANDLE_READABLE |
+                                           VIR_EVENT_HANDLE_HANGUP |
+                                           VIR_EVENT_HANDLE_ERROR,
+                                           virCommandHandleReadWrite,
+                                           cmd, NULL)) < 0) {
+        virReportError(VIR_ERR_INTERNAL_ERROR,
+                       _("Unable to register errfd %d in the event loop"),
+                       cmd->errfd);
+        goto cleanup;
+    }
+
+    ret = 0;
+
+cleanup:
+    return ret;
+}
+
+
 /**
  * virCommandRunAsync:
  * @cmd: command to start
@@ -2149,6 +2323,7 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
     char *str;
     int i;
     bool synchronous = false;
+    int infd[2] = {-1, -1};
 
     if (!cmd || cmd->has_error == ENOMEM) {
         virReportOOMError();
@@ -2163,10 +2338,23 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
     synchronous = cmd->flags & VIR_EXEC_RUN_SYNC;
     cmd->flags &= ~VIR_EXEC_RUN_SYNC;
 
-    /* Buffer management can only be requested via virCommandRun.  */
-    if ((cmd->inbuf && cmd->infd == -1) ||
-        (cmd->outbuf && cmd->outfdptr != &cmd->outfd) ||
-        (cmd->errbuf && cmd->errfdptr != &cmd->errfd)) {
+    /* Buffer management can only be requested via virCommandRun, unless help
+     * from the event loop has been requested via virCommandDoAsyncIO. */
+    if (cmd->flags & VIR_EXEC_ASYNC_IO) {
+        /* If we have an input buffer, we need
+         * a pipe to feed the data to the child */
+        if (cmd->inbuf && cmd->infd == -1) {
+            if (pipe2(infd, O_CLOEXEC) < 0) {
+                virReportSystemError(errno, "%s",
+                                     _("unable to open pipe"));
+                cmd->has_error = -1;
+                return -1;
+            }
+            cmd->infd = infd[0];
+        }
+    } else if ((cmd->inbuf && cmd->infd == -1) ||
+         (cmd->outbuf && cmd->outfdptr != &cmd->outfd) ||
+         (cmd->errbuf && cmd->errfdptr != &cmd->errfd)) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
                        _("cannot mix string I/O with asynchronous command"));
         return -1;
@@ -2228,6 +2416,16 @@ virCommandRunAsync(virCommandPtr cmd, pid_t *pid)
     else
         cmd->reap = true;
 
+    if (ret == 0 && cmd->flags & VIR_EXEC_ASYNC_IO) {
+        cmd->flags &= ~VIR_EXEC_ASYNC_IO;
+        if (cmd->inbuf && cmd->infd != -1) {
+            /* close the read end of infd and replace it with the write end */
+            VIR_FORCE_CLOSE(cmd->infd);
+            cmd->infd = infd[1];
+        }
+        ret = virCommandRegisterEventLoop(cmd);
+    }
+
     return ret;
 }
 
@@ -2248,6 +2446,7 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
 {
     int ret;
     int status = 0;
+    const int events = VIR_EVENT_HANDLE_READABLE | VIR_EVENT_HANDLE_HANGUP;
 
     if (!cmd ||cmd->has_error == ENOMEM) {
         virReportOOMError();
@@ -2272,6 +2471,24 @@ virCommandWait(virCommandPtr cmd, int *exitstatus)
      * guarantee that virProcessWait only fails due to failure to wait,
      * and repeat the exitstatus check code ourselves.  */
     ret = virProcessWait(cmd->pid, exitstatus ? exitstatus : &status);
+
+    if (cmd->inWatch != -1) {
+        virEventRemoveHandle(cmd->inWatch);
+        cmd->inWatch = -1;
+    }
+
+    if (cmd->outWatch != -1) {
+        virEventRemoveHandle(cmd->outWatch);
+        virCommandHandleReadWrite(cmd->outWatch, cmd->outfd, events, cmd);
+        cmd->outWatch = -1;
+    }
+
+    if (cmd->errWatch != -1) {
+        virEventRemoveHandle(cmd->errWatch);
+        virCommandHandleReadWrite(cmd->errWatch, cmd->errfd, events, cmd);
+        cmd->errWatch = -1;
+    }
+
     if (ret == 0) {
         cmd->pid = -1;
         cmd->reap = false;
@@ -2521,3 +2738,49 @@ virCommandFree(virCommandPtr cmd)
 
     VIR_FREE(cmd);
 }
+
+/**
+ * virCommandDoAsyncIO:
+ * @cmd: command to do async IO on
+ *
+ * This requests asynchronous string IO on @cmd. It is useful in
+ * combination with virCommandRunAsync():
+ *
+ *      virCommandPtr cmd = virCommandNew*(...);
+ *      char *buf = NULL;
+ *
+ *      ...
+ *
+ *      virCommandSetOutputBuffer(cmd, &buf);
+ *      virCommandDoAsyncIO(cmd);
+ *
+ *      if (virCommandRunAsync(cmd, NULL) < 0)
+ *          goto cleanup;
+ *
+ *      ...
+ *
+ *      if (virCommandWait(cmd, NULL) < 0)
+ *          goto cleanup;
+ *
+ *      // @buf now contains @cmd's stdout
+ *      VIR_DEBUG("STDOUT: %s", NULLSTR(buf));
+ *
+ *      ...
+ *
+ *  cleanup:
+ *      VIR_FREE(buf);
+ *      virCommandFree(cmd);
+ *
+ * The libvirt's event loop is used for handling stdios of @cmd.
+ * Since current implementation uses strlen to determine length
+ * of data to be written to @cmd's stdin, don't pass any binary
+ * data.
+ */
+void
+virCommandDoAsyncIO(virCommandPtr cmd)
+{
+   if (!cmd || cmd->has_error)
+       return;
+
+   cmd->flags |= VIR_EXEC_ASYNC_IO | VIR_EXEC_NONBLOCK;
+}
diff --git a/src/util/vircommand.h b/src/util/vircommand.h
index 9b7117d..c1a2e24 100644
--- a/src/util/vircommand.h
+++ b/src/util/vircommand.h
@@ -163,4 +163,5 @@ void virCommandAbort(virCommandPtr cmd);
 
 void virCommandFree(virCommandPtr cmd);
 
+void virCommandDoAsyncIO(virCommandPtr cmd);
 #endif /* __VIR_COMMAND_H__ */
-- 
1.8.0.2


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]