blob: 16bf7ed270f8c42fb12115fb52da5267b88b852b [file] [log] [blame]
Daniel P. Berranged6e48862015-02-27 18:25:25 +00001/*
2 * QEMU I/O channels files driver
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 *
19 */
20
Peter Maydellcae9fc52016-01-29 17:50:03 +000021#include "qemu/osdep.h"
Daniel P. Berranged6e48862015-02-27 18:25:25 +000022#include "io/channel-file.h"
23#include "io/channel-watch.h"
Markus Armbrusterda34e652016-03-14 09:01:28 +010024#include "qapi/error.h"
Daniel P. Berranged6e48862015-02-27 18:25:25 +000025#include "qemu/sockets.h"
26#include "trace.h"
27
28QIOChannelFile *
29qio_channel_file_new_fd(int fd)
30{
31 QIOChannelFile *ioc;
32
33 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
34
35 ioc->fd = fd;
36
37 trace_qio_channel_file_new_fd(ioc, fd);
38
39 return ioc;
40}
41
42
43QIOChannelFile *
44qio_channel_file_new_path(const char *path,
45 int flags,
46 mode_t mode,
47 Error **errp)
48{
49 QIOChannelFile *ioc;
50
51 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
52
Ross Lagerwall902f6e12017-11-01 14:25:24 +000053 ioc->fd = open(path, flags, mode);
Daniel P. Berranged6e48862015-02-27 18:25:25 +000054 if (ioc->fd < 0) {
55 object_unref(OBJECT(ioc));
56 error_setg_errno(errp, errno,
57 "Unable to open %s", path);
58 return NULL;
59 }
60
61 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
62
63 return ioc;
64}
65
66
67static void qio_channel_file_init(Object *obj)
68{
69 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
70 ioc->fd = -1;
71}
72
73static void qio_channel_file_finalize(Object *obj)
74{
75 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
76 if (ioc->fd != -1) {
77 close(ioc->fd);
78 ioc->fd = -1;
79 }
80}
81
82
83static ssize_t qio_channel_file_readv(QIOChannel *ioc,
84 const struct iovec *iov,
85 size_t niov,
86 int **fds,
87 size_t *nfds,
88 Error **errp)
89{
90 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
91 ssize_t ret;
92
93 retry:
94 ret = readv(fioc->fd, iov, niov);
95 if (ret < 0) {
Daniel P. Berrange30fd3e22016-03-10 17:17:33 +000096 if (errno == EAGAIN) {
Daniel P. Berranged6e48862015-02-27 18:25:25 +000097 return QIO_CHANNEL_ERR_BLOCK;
98 }
99 if (errno == EINTR) {
100 goto retry;
101 }
102
103 error_setg_errno(errp, errno,
104 "Unable to read from file");
105 return -1;
106 }
107
108 return ret;
109}
110
111static ssize_t qio_channel_file_writev(QIOChannel *ioc,
112 const struct iovec *iov,
113 size_t niov,
114 int *fds,
115 size_t nfds,
116 Error **errp)
117{
118 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
119 ssize_t ret;
120
121 retry:
122 ret = writev(fioc->fd, iov, niov);
123 if (ret <= 0) {
Daniel P. Berrange30fd3e22016-03-10 17:17:33 +0000124 if (errno == EAGAIN) {
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000125 return QIO_CHANNEL_ERR_BLOCK;
126 }
127 if (errno == EINTR) {
128 goto retry;
129 }
130 error_setg_errno(errp, errno,
131 "Unable to write to file");
132 return -1;
133 }
134 return ret;
135}
136
137static int qio_channel_file_set_blocking(QIOChannel *ioc,
138 bool enabled,
139 Error **errp)
140{
141 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
142
143 if (enabled) {
144 qemu_set_block(fioc->fd);
145 } else {
146 qemu_set_nonblock(fioc->fd);
147 }
148 return 0;
149}
150
151
152static off_t qio_channel_file_seek(QIOChannel *ioc,
153 off_t offset,
154 int whence,
155 Error **errp)
156{
157 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
158 off_t ret;
159
160 ret = lseek(fioc->fd, offset, whence);
161 if (ret == (off_t)-1) {
162 error_setg_errno(errp, errno,
163 "Unable to seek to offset %lld whence %d in file",
164 (long long int)offset, whence);
165 return -1;
166 }
167 return ret;
168}
169
170
171static int qio_channel_file_close(QIOChannel *ioc,
172 Error **errp)
173{
174 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
175
176 if (close(fioc->fd) < 0) {
177 error_setg_errno(errp, errno,
178 "Unable to close file");
179 return -1;
180 }
181 return 0;
182}
183
184
Paolo Bonzinibf88c122017-02-13 14:52:22 +0100185static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc,
186 AioContext *ctx,
187 IOHandler *io_read,
188 IOHandler *io_write,
189 void *opaque)
190{
191 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
192 aio_set_fd_handler(ctx, fioc->fd, false, io_read, io_write, NULL, opaque);
193}
194
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000195static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
196 GIOCondition condition)
197{
198 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
199 return qio_channel_create_fd_watch(ioc,
200 fioc->fd,
201 condition);
202}
203
204static void qio_channel_file_class_init(ObjectClass *klass,
205 void *class_data G_GNUC_UNUSED)
206{
207 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
208
209 ioc_klass->io_writev = qio_channel_file_writev;
210 ioc_klass->io_readv = qio_channel_file_readv;
211 ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
212 ioc_klass->io_seek = qio_channel_file_seek;
213 ioc_klass->io_close = qio_channel_file_close;
214 ioc_klass->io_create_watch = qio_channel_file_create_watch;
Paolo Bonzinibf88c122017-02-13 14:52:22 +0100215 ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler;
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000216}
217
218static const TypeInfo qio_channel_file_info = {
219 .parent = TYPE_QIO_CHANNEL,
220 .name = TYPE_QIO_CHANNEL_FILE,
221 .instance_size = sizeof(QIOChannelFile),
222 .instance_init = qio_channel_file_init,
223 .instance_finalize = qio_channel_file_finalize,
224 .class_init = qio_channel_file_class_init,
225};
226
227static void qio_channel_file_register_types(void)
228{
229 type_register_static(&qio_channel_file_info);
230}
231
232type_init(qio_channel_file_register_types);