qga/command: use QEMU atomic primitives
[qemu/ar7.git] / migration / qemu-file-channel.c
blob45c13f10285f9938af30bc7c34cc21b5f16d632b
1 /*
2 * QEMUFile backend for QIOChannel objects
4 * Copyright (c) 2015-2016 Red Hat, Inc
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
25 #include "qemu/osdep.h"
26 #include "migration/qemu-file.h"
27 #include "io/channel-socket.h"
28 #include "qemu/iov.h"
31 static ssize_t channel_writev_buffer(void *opaque,
32 struct iovec *iov,
33 int iovcnt,
34 int64_t pos)
36 QIOChannel *ioc = QIO_CHANNEL(opaque);
37 ssize_t done = 0;
38 struct iovec *local_iov = g_new(struct iovec, iovcnt);
39 struct iovec *local_iov_head = local_iov;
40 unsigned int nlocal_iov = iovcnt;
42 nlocal_iov = iov_copy(local_iov, nlocal_iov,
43 iov, iovcnt,
44 0, iov_size(iov, iovcnt));
46 while (nlocal_iov > 0) {
47 ssize_t len;
48 len = qio_channel_writev(ioc, local_iov, nlocal_iov, NULL);
49 if (len == QIO_CHANNEL_ERR_BLOCK) {
50 qio_channel_wait(ioc, G_IO_OUT);
51 continue;
53 if (len < 0) {
54 /* XXX handle Error objects */
55 done = -EIO;
56 goto cleanup;
59 iov_discard_front(&local_iov, &nlocal_iov, len);
60 done += len;
63 cleanup:
64 g_free(local_iov_head);
65 return done;
69 static ssize_t channel_get_buffer(void *opaque,
70 uint8_t *buf,
71 int64_t pos,
72 size_t size)
74 QIOChannel *ioc = QIO_CHANNEL(opaque);
75 ssize_t ret;
77 do {
78 ret = qio_channel_read(ioc, (char *)buf, size, NULL);
79 if (ret < 0) {
80 if (ret == QIO_CHANNEL_ERR_BLOCK) {
81 qio_channel_yield(ioc, G_IO_IN);
82 } else {
83 /* XXX handle Error * object */
84 return -EIO;
87 } while (ret == QIO_CHANNEL_ERR_BLOCK);
89 return ret;
93 static int channel_close(void *opaque)
95 QIOChannel *ioc = QIO_CHANNEL(opaque);
96 qio_channel_close(ioc, NULL);
97 object_unref(OBJECT(ioc));
98 return 0;
102 static int channel_shutdown(void *opaque,
103 bool rd,
104 bool wr)
106 QIOChannel *ioc = QIO_CHANNEL(opaque);
108 if (qio_channel_has_feature(ioc,
109 QIO_CHANNEL_FEATURE_SHUTDOWN)) {
110 QIOChannelShutdown mode;
111 if (rd && wr) {
112 mode = QIO_CHANNEL_SHUTDOWN_BOTH;
113 } else if (rd) {
114 mode = QIO_CHANNEL_SHUTDOWN_READ;
115 } else {
116 mode = QIO_CHANNEL_SHUTDOWN_WRITE;
118 if (qio_channel_shutdown(ioc, mode, NULL) < 0) {
119 /* XXX handler Error * object */
120 return -EIO;
123 return 0;
127 static int channel_set_blocking(void *opaque,
128 bool enabled)
130 QIOChannel *ioc = QIO_CHANNEL(opaque);
132 if (qio_channel_set_blocking(ioc, enabled, NULL) < 0) {
133 return -1;
135 return 0;
138 static QEMUFile *channel_get_input_return_path(void *opaque)
140 QIOChannel *ioc = QIO_CHANNEL(opaque);
142 return qemu_fopen_channel_output(ioc);
145 static QEMUFile *channel_get_output_return_path(void *opaque)
147 QIOChannel *ioc = QIO_CHANNEL(opaque);
149 return qemu_fopen_channel_input(ioc);
152 static const QEMUFileOps channel_input_ops = {
153 .get_buffer = channel_get_buffer,
154 .close = channel_close,
155 .shut_down = channel_shutdown,
156 .set_blocking = channel_set_blocking,
157 .get_return_path = channel_get_input_return_path,
161 static const QEMUFileOps channel_output_ops = {
162 .writev_buffer = channel_writev_buffer,
163 .close = channel_close,
164 .shut_down = channel_shutdown,
165 .set_blocking = channel_set_blocking,
166 .get_return_path = channel_get_output_return_path,
170 QEMUFile *qemu_fopen_channel_input(QIOChannel *ioc)
172 object_ref(OBJECT(ioc));
173 return qemu_fopen_ops(ioc, &channel_input_ops);
176 QEMUFile *qemu_fopen_channel_output(QIOChannel *ioc)
178 object_ref(OBJECT(ioc));
179 return qemu_fopen_ops(ioc, &channel_output_ops);