aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <pbonzini@redhat.com>2009-08-12 14:17:35 +0200
committerAnthony Liguori <aliguori@us.ibm.com>2009-08-24 08:02:55 -0500
commit7f79dd281c834013f32cff0f5d98135b87cf470e (patch)
tree59f523f058469c14511af3c5d22fd6ec15022ded
parenta25a0ef51e642130017e19f3e41af1b794aa75c1 (diff)
unify popen/fopen qemu wrappers
While reading Chris's code for fd migration I noticed the duplication between QEMUFilePopen and QEMUFileStdio. This fixes it, and makes qemu_fopen more similar qemu_popen. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Anthony Liguori <aliguori@us.ibm.com>
-rw-r--r--hw/hw.h2
-rw-r--r--migration-exec.c4
-rw-r--r--savevm.c99
3 files changed, 53 insertions, 52 deletions
diff --git a/hw/hw.h b/hw/hw.h
index 1e5783d973..322f077079 100644
--- a/hw/hw.h
+++ b/hw/hw.h
@@ -52,7 +52,7 @@ QEMUFile *qemu_fopen(const char *filename, const char *mode);
QEMUFile *qemu_fopen_socket(int fd);
QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
-int qemu_popen_fd(QEMUFile *f);
+int qemu_stdio_fd(QEMUFile *f);
void qemu_fflush(QEMUFile *f);
int qemu_fclose(QEMUFile *f);
void qemu_put_buffer(QEMUFile *f, const uint8_t *buf, int size);
diff --git a/migration-exec.c b/migration-exec.c
index ef4620f79e..b45c83385a 100644
--- a/migration-exec.c
+++ b/migration-exec.c
@@ -114,7 +114,7 @@ static void exec_accept_incoming_migration(void *opaque)
qemu_announce_self();
dprintf("successfully loaded vm state\n");
/* we've successfully migrated, close the fd */
- qemu_set_fd_handler2(qemu_popen_fd(f), NULL, NULL, NULL, NULL);
+ qemu_set_fd_handler2(qemu_stdio_fd(f), NULL, NULL, NULL, NULL);
if (autostart)
vm_start();
@@ -133,7 +133,7 @@ int exec_start_incoming_migration(const char *command)
return -errno;
}
- qemu_set_fd_handler2(qemu_popen_fd(f), NULL,
+ qemu_set_fd_handler2(qemu_stdio_fd(f), NULL,
exec_accept_incoming_migration, NULL,
(void *)(unsigned long)f);
diff --git a/savevm.c b/savevm.c
index 570377fdb7..975e7ab46f 100644
--- a/savevm.c
+++ b/savevm.c
@@ -172,11 +172,11 @@ struct QEMUFile {
int has_error;
};
-typedef struct QEMUFilePopen
+typedef struct QEMUFileStdio
{
- FILE *popen_file;
+ FILE *stdio_file;
QEMUFile *file;
-} QEMUFilePopen;
+} QEMUFileStdio;
typedef struct QEMUFileSocket
{
@@ -206,16 +206,16 @@ static int socket_close(void *opaque)
return 0;
}
-static int popen_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
+static int stdio_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
{
- QEMUFilePopen *s = opaque;
- return fwrite(buf, 1, size, s->popen_file);
+ QEMUFileStdio *s = opaque;
+ return fwrite(buf, 1, size, s->stdio_file);
}
-static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
+static int stdio_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
{
- QEMUFilePopen *s = opaque;
- FILE *fp = s->popen_file;
+ QEMUFileStdio *s = opaque;
+ FILE *fp = s->stdio_file;
int bytes;
do {
@@ -225,31 +225,39 @@ static int popen_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
return bytes;
}
-static int popen_close(void *opaque)
+static int stdio_pclose(void *opaque)
+{
+ QEMUFileStdio *s = opaque;
+ pclose(s->stdio_file);
+ qemu_free(s);
+ return 0;
+}
+
+static int stdio_fclose(void *opaque)
{
- QEMUFilePopen *s = opaque;
- pclose(s->popen_file);
+ QEMUFileStdio *s = opaque;
+ fclose(s->stdio_file);
qemu_free(s);
return 0;
}
-QEMUFile *qemu_popen(FILE *popen_file, const char *mode)
+QEMUFile *qemu_popen(FILE *stdio_file, const char *mode)
{
- QEMUFilePopen *s;
+ QEMUFileStdio *s;
- if (popen_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
+ if (stdio_file == NULL || mode == NULL || (mode[0] != 'r' && mode[0] != 'w') || mode[1] != 0) {
fprintf(stderr, "qemu_popen: Argument validity check failed\n");
return NULL;
}
- s = qemu_mallocz(sizeof(QEMUFilePopen));
+ s = qemu_mallocz(sizeof(QEMUFileStdio));
- s->popen_file = popen_file;
+ s->stdio_file = stdio_file;
if(mode[0] == 'r') {
- s->file = qemu_fopen_ops(s, NULL, popen_get_buffer, popen_close, NULL, NULL);
+ s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_pclose, NULL, NULL);
} else {
- s->file = qemu_fopen_ops(s, popen_put_buffer, NULL, popen_close, NULL, NULL);
+ s->file = qemu_fopen_ops(s, stdio_put_buffer, NULL, stdio_pclose, NULL, NULL);
}
return s->file;
}
@@ -266,13 +274,13 @@ QEMUFile *qemu_popen_cmd(const char *command, const char *mode)
return qemu_popen(popen_file, mode);
}
-int qemu_popen_fd(QEMUFile *f)
+int qemu_stdio_fd(QEMUFile *f)
{
- QEMUFilePopen *p;
+ QEMUFileStdio *p;
int fd;
- p = (QEMUFilePopen *)f->opaque;
- fd = fileno(p->popen_file);
+ p = (QEMUFileStdio *)f->opaque;
+ fd = fileno(p->stdio_file);
return fd;
}
@@ -286,53 +294,46 @@ QEMUFile *qemu_fopen_socket(int fd)
return s->file;
}
-typedef struct QEMUFileStdio
-{
- FILE *outfile;
-} QEMUFileStdio;
-
static int file_put_buffer(void *opaque, const uint8_t *buf,
int64_t pos, int size)
{
QEMUFileStdio *s = opaque;
- fseek(s->outfile, pos, SEEK_SET);
- fwrite(buf, 1, size, s->outfile);
+ fseek(s->stdio_file, pos, SEEK_SET);
+ fwrite(buf, 1, size, s->stdio_file);
return size;
}
static int file_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
{
QEMUFileStdio *s = opaque;
- fseek(s->outfile, pos, SEEK_SET);
- return fread(buf, 1, size, s->outfile);
-}
-
-static int file_close(void *opaque)
-{
- QEMUFileStdio *s = opaque;
- fclose(s->outfile);
- qemu_free(s);
- return 0;
+ fseek(s->stdio_file, pos, SEEK_SET);
+ return fread(buf, 1, size, s->stdio_file);
}
QEMUFile *qemu_fopen(const char *filename, const char *mode)
{
QEMUFileStdio *s;
+ if (mode == NULL ||
+ (mode[0] != 'r' && mode[0] != 'w') ||
+ mode[1] != 'b' || mode[2] != 0) {
+ fprintf(stderr, "qemu_fdopen: Argument validity check failed\n");
+ return NULL;
+ }
+
s = qemu_mallocz(sizeof(QEMUFileStdio));
- s->outfile = fopen(filename, mode);
- if (!s->outfile)
+ s->stdio_file = fopen(filename, mode);
+ if (!s->stdio_file)
goto fail;
- if (!strcmp(mode, "wb"))
- return qemu_fopen_ops(s, file_put_buffer, NULL, file_close, NULL, NULL);
- else if (!strcmp(mode, "rb"))
- return qemu_fopen_ops(s, NULL, file_get_buffer, file_close, NULL, NULL);
-
+ if(mode[0] == 'w') {
+ s->file = qemu_fopen_ops(s, file_put_buffer, NULL, stdio_fclose, NULL, NULL);
+ } else {
+ s->file = qemu_fopen_ops(s, NULL, file_get_buffer, stdio_fclose, NULL, NULL);
+ }
+ return s->file;
fail:
- if (s->outfile)
- fclose(s->outfile);
qemu_free(s);
return NULL;
}