@@ -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:
@@ -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));
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(-)