blob: 8b5821f4526ed8850a77a9fad4bf17aeb792bc11 [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
Chetan Pantc8198bd2020-10-14 13:40:33 +00009 * version 2.1 of the License, or (at your option) any later version.
Daniel P. Berranged6e48862015-02-27 18:25:25 +000010 *
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"
Markus Armbruster0b8fa322019-05-23 16:35:07 +020025#include "qemu/module.h"
Daniel P. Berranged6e48862015-02-27 18:25:25 +000026#include "qemu/sockets.h"
27#include "trace.h"
28
29QIOChannelFile *
30qio_channel_file_new_fd(int fd)
31{
32 QIOChannelFile *ioc;
33
34 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
35
36 ioc->fd = fd;
37
38 trace_qio_channel_file_new_fd(ioc, fd);
39
40 return ioc;
41}
42
43
44QIOChannelFile *
45qio_channel_file_new_path(const char *path,
46 int flags,
47 mode_t mode,
48 Error **errp)
49{
50 QIOChannelFile *ioc;
51
52 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
53
Daniel P. Berrangé448058a2020-07-21 13:25:21 +010054 ioc->fd = qemu_open_old(path, flags, mode);
Daniel P. Berranged6e48862015-02-27 18:25:25 +000055 if (ioc->fd < 0) {
56 object_unref(OBJECT(ioc));
57 error_setg_errno(errp, errno,
58 "Unable to open %s", path);
59 return NULL;
60 }
61
62 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
63
64 return ioc;
65}
66
67
68static void qio_channel_file_init(Object *obj)
69{
70 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
71 ioc->fd = -1;
72}
73
74static void qio_channel_file_finalize(Object *obj)
75{
76 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
77 if (ioc->fd != -1) {
Ross Lagerwallb8f244b2017-11-01 14:25:26 +000078 qemu_close(ioc->fd);
Daniel P. Berranged6e48862015-02-27 18:25:25 +000079 ioc->fd = -1;
80 }
81}
82
83
84static ssize_t qio_channel_file_readv(QIOChannel *ioc,
85 const struct iovec *iov,
86 size_t niov,
87 int **fds,
88 size_t *nfds,
manish.mishra84615a12022-12-20 18:44:17 +000089 int flags,
Daniel P. Berranged6e48862015-02-27 18:25:25 +000090 Error **errp)
91{
92 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
93 ssize_t ret;
94
95 retry:
96 ret = readv(fioc->fd, iov, niov);
97 if (ret < 0) {
Daniel P. Berrange30fd3e22016-03-10 17:17:33 +000098 if (errno == EAGAIN) {
Daniel P. Berranged6e48862015-02-27 18:25:25 +000099 return QIO_CHANNEL_ERR_BLOCK;
100 }
101 if (errno == EINTR) {
102 goto retry;
103 }
104
105 error_setg_errno(errp, errno,
106 "Unable to read from file");
107 return -1;
108 }
109
110 return ret;
111}
112
113static ssize_t qio_channel_file_writev(QIOChannel *ioc,
114 const struct iovec *iov,
115 size_t niov,
116 int *fds,
117 size_t nfds,
Leonardo Brasb88651c2022-05-13 03:28:31 -0300118 int flags,
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000119 Error **errp)
120{
121 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
122 ssize_t ret;
123
124 retry:
125 ret = writev(fioc->fd, iov, niov);
126 if (ret <= 0) {
Daniel P. Berrange30fd3e22016-03-10 17:17:33 +0000127 if (errno == EAGAIN) {
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000128 return QIO_CHANNEL_ERR_BLOCK;
129 }
130 if (errno == EINTR) {
131 goto retry;
132 }
133 error_setg_errno(errp, errno,
134 "Unable to write to file");
135 return -1;
136 }
137 return ret;
138}
139
140static int qio_channel_file_set_blocking(QIOChannel *ioc,
141 bool enabled,
142 Error **errp)
143{
Marc-André Lureau17fc1242022-04-25 17:39:06 +0400144#ifdef WIN32
145 /* not implemented */
146 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
147 return -1;
148#else
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000149 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
150
Marc-André Lureau17fc1242022-04-25 17:39:06 +0400151 if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) {
152 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
153 return -1;
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000154 }
155 return 0;
Marc-André Lureau17fc1242022-04-25 17:39:06 +0400156#endif
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000157}
158
159
160static off_t qio_channel_file_seek(QIOChannel *ioc,
161 off_t offset,
162 int whence,
163 Error **errp)
164{
165 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
166 off_t ret;
167
168 ret = lseek(fioc->fd, offset, whence);
169 if (ret == (off_t)-1) {
170 error_setg_errno(errp, errno,
171 "Unable to seek to offset %lld whence %d in file",
172 (long long int)offset, whence);
173 return -1;
174 }
175 return ret;
176}
177
178
179static int qio_channel_file_close(QIOChannel *ioc,
180 Error **errp)
181{
182 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
183
Ross Lagerwallb8f244b2017-11-01 14:25:26 +0000184 if (qemu_close(fioc->fd) < 0) {
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000185 error_setg_errno(errp, errno,
186 "Unable to close file");
187 return -1;
188 }
Ross Lagerwalla2565df2017-11-01 14:25:25 +0000189 fioc->fd = -1;
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000190 return 0;
191}
192
193
Paolo Bonzinibf88c122017-02-13 14:52:22 +0100194static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc,
195 AioContext *ctx,
196 IOHandler *io_read,
197 IOHandler *io_write,
198 void *opaque)
199{
200 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
Stefan Hajnoczi60f782b2023-05-16 15:02:38 -0400201 aio_set_fd_handler(ctx, fioc->fd, io_read, io_write, NULL, NULL, opaque);
Paolo Bonzinibf88c122017-02-13 14:52:22 +0100202}
203
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000204static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
205 GIOCondition condition)
206{
207 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
208 return qio_channel_create_fd_watch(ioc,
209 fioc->fd,
210 condition);
211}
212
213static void qio_channel_file_class_init(ObjectClass *klass,
214 void *class_data G_GNUC_UNUSED)
215{
216 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
217
218 ioc_klass->io_writev = qio_channel_file_writev;
219 ioc_klass->io_readv = qio_channel_file_readv;
220 ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
221 ioc_klass->io_seek = qio_channel_file_seek;
222 ioc_klass->io_close = qio_channel_file_close;
223 ioc_klass->io_create_watch = qio_channel_file_create_watch;
Paolo Bonzinibf88c122017-02-13 14:52:22 +0100224 ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler;
Daniel P. Berranged6e48862015-02-27 18:25:25 +0000225}
226
227static const TypeInfo qio_channel_file_info = {
228 .parent = TYPE_QIO_CHANNEL,
229 .name = TYPE_QIO_CHANNEL_FILE,
230 .instance_size = sizeof(QIOChannelFile),
231 .instance_init = qio_channel_file_init,
232 .instance_finalize = qio_channel_file_finalize,
233 .class_init = qio_channel_file_class_init,
234};
235
236static void qio_channel_file_register_types(void)
237{
238 type_register_static(&qio_channel_file_info);
239}
240
241type_init(qio_channel_file_register_types);