qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Michael Roth <mdroth@linux.vnet.ibm.com>
To: qemu-devel@nongnu.org
Cc: bazulay@redhat.com, aliguori@us.ibm.com,
	mdroth@linux.vnet.ibm.com, agl@us.ibm.com
Subject: [Qemu-devel] [PATCH 1/2] guest agent: add guest-file-open-pipe
Date: Tue,  6 Dec 2011 08:34:07 -0600	[thread overview]
Message-ID: <1323182048-12134-2-git-send-email-mdroth@linux.vnet.ibm.com> (raw)
In-Reply-To: <1323182048-12134-1-git-send-email-mdroth@linux.vnet.ibm.com>

Creates a FIFO pair that can be used with existing file read/write
interfaces to communicate with processes spawned via the forthcoming
guest-file-exec interface.

Signed-off-by: Michael Roth <mdroth@linux.vnet.ibm.com>
---
 qapi-schema-guest.json     |   24 ++++++-
 qga/guest-agent-commands.c |  179 +++++++++++++++++++++++++++++++++++++------
 2 files changed, 177 insertions(+), 26 deletions(-)

diff --git a/qapi-schema-guest.json b/qapi-schema-guest.json
index fde5971..4c9f063 100644
--- a/qapi-schema-guest.json
+++ b/qapi-schema-guest.json
@@ -80,18 +80,40 @@
   'returns': 'int' }
 
 ##
+# @guest-file-open-pipe
+#
+# Open a pipe to in the guest to associated with a qga-spawned processes
+# for communication.
+#
+# Returns: Guest file handle on success, as per guest-file-open. This
+# handle is useable with the same interfaces as a handle returned by
+# guest-file-open.
+#
+# Since: 1.0.50
+##
+{ 'command': 'guest-file-open-pipe',
+  'returns': 'int' }
+
+##
 # @guest-file-close:
 #
 # Close an open file in the guest
 #
 # @handle: filehandle returned by guest-file-open
+# @pipe-end: #optional GuestFilePipeEnd value ("rw"/"w"/"r") to specify
+# which end of the pipe to close. Please note that closing the write
+# side of a pipe will block until the read side is closed. If you've
+# passed the read-side of the pipe to a qga-spawned process, make sure
+# the process as exited before attempting to close the write side.
 #
 # Returns: Nothing on success.
 #
 # Since: 0.15.0
 ##
+{ 'enum': 'GuestFilePipeEnd',
+  'data': [ 'r', 'w', 'rw' ] }
 { 'command': 'guest-file-close',
-  'data': { 'handle': 'int' } }
+  'data': { 'handle': 'int', '*pipe-end': 'GuestFilePipeEnd' } }
 
 ##
 # @guest-file-read:
diff --git a/qga/guest-agent-commands.c b/qga/guest-agent-commands.c
index 6da9904..ae77ee4 100644
--- a/qga/guest-agent-commands.c
+++ b/qga/guest-agent-commands.c
@@ -44,6 +44,34 @@ static void slog(const char *fmt, ...)
     va_end(ap);
 }
 
+static void toggle_flags(int fd, long flags, bool set, Error **err)
+{
+    int ret, old_flags;
+
+    old_flags = fcntl(fd, F_GETFL);
+    if (old_flags == -1) {
+        error_set(err, QERR_QGA_COMMAND_FAILED,
+                  "failed to fetch filehandle flags");
+        return;
+    }
+    ret = fcntl(fd, F_SETFL, set ? old_flags | flags : old_flags & ~flags);
+    if (ret == -1) {
+        error_set(err, QERR_QGA_COMMAND_FAILED,
+                  "failed to set filehandle flags");
+        return;
+    }
+}
+
+static void ftoggle_flags(FILE *fh, long flags, bool set, Error **err)
+{
+    int fd;
+    if (!fh || (fd = fileno(fh)) == -1) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, "invalid filehandle");
+        return;
+    }
+    toggle_flags(fd, flags, set, err);
+}
+
 int64_t qmp_guest_sync(int64_t id, Error **errp)
 {
     return id;
@@ -102,7 +130,14 @@ void qmp_guest_shutdown(bool has_mode, const char *mode, Error **err)
 
 typedef struct GuestFileHandle {
     uint64_t id;
-    FILE *fh;
+    bool is_pipe;
+    union {
+        FILE *fh;
+        struct {
+            FILE *in;
+            FILE *out;
+        } pipe;
+    } stream;
     QTAILQ_ENTRY(GuestFileHandle) next;
 } GuestFileHandle;
 
@@ -110,14 +145,31 @@ static struct {
     QTAILQ_HEAD(, GuestFileHandle) filehandles;
 } guest_file_state;
 
-static void guest_file_handle_add(FILE *fh)
+static uint64_t guest_file_handle_add(FILE *fh)
 {
     GuestFileHandle *gfh;
 
     gfh = g_malloc0(sizeof(GuestFileHandle));
     gfh->id = fileno(fh);
-    gfh->fh = fh;
+    gfh->is_pipe = false;
+    gfh->stream.fh = fh;
+
+    QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
+    return gfh->id;
+}
+
+static uint64_t guest_file_handle_add_pipe(FILE *in, FILE *out)
+{
+    GuestFileHandle *gfh;
+
+    gfh = g_malloc0(sizeof(GuestFileHandle));
+    gfh->id = fileno(in);
+    gfh->is_pipe = true;
+    gfh->stream.pipe.in = in;
+    gfh->stream.pipe.out = out;
+
     QTAILQ_INSERT_TAIL(&guest_file_state.filehandles, gfh, next);
+    return gfh->id;
 }
 
 static GuestFileHandle *guest_file_handle_find(int64_t id)
@@ -137,7 +189,6 @@ static GuestFileHandle *guest_file_handle_find(int64_t id)
 int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, Error **err)
 {
     FILE *fh;
-    int fd;
     int64_t ret = -1;
 
     if (!has_mode) {
@@ -153,39 +204,112 @@ int64_t qmp_guest_file_open(const char *path, bool has_mode, const char *mode, E
     /* set fd non-blocking to avoid common use cases (like reading from a
      * named pipe) from hanging the agent
      */
-    fd = fileno(fh);
-    ret = fcntl(fd, F_GETFL);
-    ret = fcntl(fd, F_SETFL, ret | O_NONBLOCK);
-    if (ret == -1) {
-        error_set(err, QERR_QGA_COMMAND_FAILED, "fcntl() failed");
+    ftoggle_flags(fh, O_NONBLOCK, true, err);
+    if (error_is_set(err)) {
         fclose(fh);
         return -1;
     }
 
-    guest_file_handle_add(fh);
-    slog("guest-file-open, handle: %d", fd);
-    return fd;
+    ret = guest_file_handle_add(fh);
+    slog("guest-file-open, handle: %ld", ret);
+    return ret;
 }
 
-void qmp_guest_file_close(int64_t handle, Error **err)
+int64_t qmp_guest_file_open_pipe(Error **err)
+{
+    FILE *fh[2];
+    int fd[2], i;
+    int64_t ret = -1;
+
+    slog("guest-file-open-pipe called");
+
+    ret = pipe(fd);
+    if (ret == -1) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, "pipe() failed");
+        return -1;
+    }
+    for (i = 0; i < 2; i++) {
+        toggle_flags(fd[i], O_NONBLOCK, true, err);
+        if (error_is_set(err)) {
+            close(fd[i]);
+            return -1;
+        }
+    }
+
+    fh[0] = fdopen(fd[0], "r");
+    if (!fh[0]) {
+        error_set(err, QERR_OPEN_FILE_FAILED, "pipe (read-side)");
+        return -1;
+    }
+    fh[1] = fdopen(fd[1], "w");
+    if (!fh[1]) {
+        fclose(fh[0]);
+        error_set(err, QERR_OPEN_FILE_FAILED, "pipe (write-side)");
+        return -1;
+    }
+    ret = guest_file_handle_add_pipe(fh[1], fh[0]);
+    slog("guest-file-open-pipe, handle: %ld", ret);
+    return ret;
+}
+
+void qmp_guest_file_close(int64_t handle, bool has_pipe_end,
+                          GuestFilePipeEnd pipe_end, Error **err)
 {
     GuestFileHandle *gfh = guest_file_handle_find(handle);
     int ret;
+    bool remove = false;
 
-    slog("guest-file-close called, handle: %ld", handle);
+    slog("guest-file-close called, handle: %ld, pipe: %d, pipe_end: %d", handle,
+         has_pipe_end, pipe_end);
     if (!gfh) {
         error_set(err, QERR_FD_NOT_FOUND, "handle");
         return;
     }
 
-    ret = fclose(gfh->fh);
-    if (ret == -1) {
-        error_set(err, QERR_QGA_COMMAND_FAILED, "fclose() failed");
-        return;
+    if (gfh->is_pipe) {
+        if (!has_pipe_end) {
+            pipe_end = GUEST_FILE_PIPE_END_RW;
+        }
+        if ((pipe_end == GUEST_FILE_PIPE_END_RW ||
+             pipe_end == GUEST_FILE_PIPE_END_R) &&
+            gfh->stream.pipe.out) {
+            g_debug("closing pipe (read-side)...");
+            ret = fclose(gfh->stream.pipe.out);
+            g_debug("done closing pipe.");
+            if (ret == -1) {
+                error_set(err, QERR_INVALID_PARAMETER, "pipe (read-side)");
+                goto out_err;
+            }
+            gfh->stream.pipe.out = NULL;
+        }
+        if ((pipe_end == GUEST_FILE_PIPE_END_RW ||
+             pipe_end == GUEST_FILE_PIPE_END_W) &&
+            gfh->stream.pipe.in) {
+            g_debug("closing pipe (write-side)...");
+            ret = fclose(gfh->stream.pipe.in);
+            g_debug("done closing pipe.");
+            if (ret == -1) {
+                error_set(err, QERR_INVALID_PARAMETER, "pipe (write-side)");
+                goto out_err;
+            }
+            gfh->stream.pipe.in = NULL;
+        }
+        remove = !gfh->stream.pipe.in && !gfh->stream.pipe.out;
+    } else {
+        ret = fclose(gfh->stream.fh);
+        if (ret == -1) {
+            error_set(err, QERR_INVALID_PARAMETER, "filehandle");
+            goto out_err;
+        }
+        gfh->stream.fh = NULL;
+        remove = true;
     }
 
-    QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
-    g_free(gfh);
+out_err:
+    if (remove) {
+        QTAILQ_REMOVE(&guest_file_state.filehandles, gfh, next);
+        g_free(gfh);
+    }
 }
 
 struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
@@ -209,10 +333,10 @@ struct GuestFileRead *qmp_guest_file_read(int64_t handle, bool has_count,
         return NULL;
     }
 
-    fh = gfh->fh;
+    fh = gfh->is_pipe ? gfh->stream.pipe.out : gfh->stream.fh;
     buf = g_malloc0(count+1);
     read_count = fread(buf, 1, count, fh);
-    if (ferror(fh)) {
+    if (0 && read_count == 0 && !feof(fh) && ferror(fh)) {
         slog("guest-file-read failed, handle: %ld", handle);
         error_set(err, QERR_QGA_COMMAND_FAILED, "fread() failed");
     } else {
@@ -245,7 +369,7 @@ GuestFileWrite *qmp_guest_file_write(int64_t handle, const char *buf_b64,
         return NULL;
     }
 
-    fh = gfh->fh;
+    fh = gfh->is_pipe ? gfh->stream.pipe.in : gfh->stream.fh;
     buf = g_base64_decode(buf_b64, &buf_len);
 
     if (!has_count) {
@@ -284,7 +408,12 @@ struct GuestFileSeek *qmp_guest_file_seek(int64_t handle, int64_t offset,
         return NULL;
     }
 
-    fh = gfh->fh;
+    if (gfh->is_pipe) {
+        error_set(err, QERR_QGA_COMMAND_FAILED, "cannot seek on a pipe");
+        return NULL;
+    }
+
+    fh = gfh->stream.fh;
     ret = fseek(fh, offset, whence);
     if (ret == -1) {
         error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
@@ -309,7 +438,7 @@ void qmp_guest_file_flush(int64_t handle, Error **err)
         return;
     }
 
-    fh = gfh->fh;
+    fh = gfh->is_pipe ? gfh->stream.pipe.in : gfh->stream.fh;
     ret = fflush(fh);
     if (ret == EOF) {
         error_set(err, QERR_QGA_COMMAND_FAILED, strerror(errno));
-- 
1.7.4.1

  reply	other threads:[~2011-12-06 14:34 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-12-06 14:34 [Qemu-devel] [PATCH 0/2] [RFC] qemu-ga: add support for guest command execution Michael Roth
2011-12-06 14:34 ` Michael Roth [this message]
2011-12-06 14:34 ` [Qemu-devel] [PATCH 2/2] guest agent: add guest-exec and guest-exec-status interfaces Michael Roth
2011-12-06 14:44 ` [Qemu-devel] [PATCH 0/2] [RFC] qemu-ga: add support for guest command execution Daniel P. Berrange
2011-12-06 16:43   ` Michael Roth
2011-12-18 12:56     ` Alon Levy

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=1323182048-12134-2-git-send-email-mdroth@linux.vnet.ibm.com \
    --to=mdroth@linux.vnet.ibm.com \
    --cc=agl@us.ibm.com \
    --cc=aliguori@us.ibm.com \
    --cc=bazulay@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /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).