@@ -140,7 +140,7 @@ static void tcp_accept_incoming_migration(void *opaque)
goto out2;
}
- f = qemu_fopen_socket(c);
+ f = qemu_fopen_socket(c, "r");
if (f == NULL) {
fprintf(stderr, "could not qemu_fopen socket\n");
goto out;
@@ -137,7 +137,7 @@ static void unix_accept_incoming_migration(void *opaque)
goto out2;
}
- f = qemu_fopen_socket(c);
+ f = qemu_fopen_socket(c, "r");
if (f == NULL) {
fprintf(stderr, "could not qemu_fopen socket\n");
goto out;
@@ -67,7 +67,7 @@ QEMUFile *qemu_fopen_ops(void *opaque, QEMUFilePutBufferFunc *put_buffer,
QEMUFileGetRateLimit *get_rate_limit);
QEMUFile *qemu_fopen(const char *filename, const char *mode);
QEMUFile *qemu_fdopen(int fd, const char *mode);
-QEMUFile *qemu_fopen_socket(int fd);
+QEMUFile *qemu_fopen_socket(int fd, const char *mode);
QEMUFile *qemu_popen(FILE *popen_file, const char *mode);
QEMUFile *qemu_popen_cmd(const char *command, const char *mode);
int qemu_stdio_fd(QEMUFile *f);
@@ -205,6 +205,21 @@ static int socket_get_buffer(void *opaque, uint8_t *buf, int64_t pos, int size)
return len;
}
+static int socket_put_buffer(void *opaque, const uint8_t *buf, int64_t pos, int size)
+{
+ QEMUFileSocket *s = opaque;
+ ssize_t len;
+
+ do {
+ len = write(s->fd, buf, size);
+ } while (len == -1 && socket_error() == EINTR);
+
+ if (len == -1)
+ len = -socket_error();
+
+ return len;
+}
+
static int socket_close(void *opaque)
{
QEMUFileSocket *s = opaque;
@@ -316,7 +331,7 @@ QEMUFile *qemu_fdopen(int fd, const char *mode)
if (!s->stdio_file)
goto fail;
- if(mode[0] == 'r') {
+ if (mode[0] == 'r') {
s->file = qemu_fopen_ops(s, NULL, stdio_get_buffer, stdio_fclose,
NULL, NULL, NULL);
} else {
@@ -330,13 +345,26 @@ fail:
return NULL;
}
-QEMUFile *qemu_fopen_socket(int fd)
+QEMUFile *qemu_fopen_socket(int fd, const char *mode)
{
QEMUFileSocket *s = g_malloc0(sizeof(QEMUFileSocket));
+ if (mode == NULL ||
+ (mode[0] != 'r' && mode[0] != 'w') ||
+ mode[1] != 'b' || mode[2] != 0) {
+ fprintf(stderr, "qemu_fopen_socket: Argument validity check failed\n");
+ return NULL;
+ }
+
s->fd = fd;
- s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
- NULL, NULL, NULL);
+ if (mode[0] == 'r') {
+ s->file = qemu_fopen_ops(s, NULL, socket_get_buffer, socket_close,
+ NULL, NULL, NULL);
+ } else {
+ s->file = qemu_fopen_ops(s, socket_put_buffer, NULL, socket_close,
+ NULL, NULL, NULL);
+ }
+
return s->file;
}