Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / io / channel-file.c
blob2ea8d083600c7f51ae85ba343fbbd6136a0b0bb6
1 /*
2 * QEMU I/O channels files driver
4 * Copyright (c) 2015 Red Hat, Inc.
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.1 of the License, or (at your option) any later version.
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.
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/>.
21 #include "qemu/osdep.h"
22 #include "io/channel-file.h"
23 #include "io/channel-util.h"
24 #include "io/channel-watch.h"
25 #include "qapi/error.h"
26 #include "qemu/module.h"
27 #include "qemu/sockets.h"
28 #include "trace.h"
30 QIOChannelFile *
31 qio_channel_file_new_fd(int fd)
33 QIOChannelFile *ioc;
35 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
37 ioc->fd = fd;
39 if (lseek(fd, 0, SEEK_CUR) != (off_t)-1) {
40 qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_SEEKABLE);
43 trace_qio_channel_file_new_fd(ioc, fd);
45 return ioc;
48 QIOChannelFile *
49 qio_channel_file_new_dupfd(int fd, Error **errp)
51 int newfd = dup(fd);
53 if (newfd < 0) {
54 error_setg_errno(errp, errno, "Could not dup FD %d", fd);
55 return NULL;
58 return qio_channel_file_new_fd(newfd);
61 QIOChannelFile *
62 qio_channel_file_new_path(const char *path,
63 int flags,
64 mode_t mode,
65 Error **errp)
67 QIOChannelFile *ioc;
69 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
71 if (flags & O_CREAT) {
72 ioc->fd = qemu_create(path, flags & ~O_CREAT, mode, errp);
73 } else {
74 ioc->fd = qemu_open(path, flags, errp);
76 if (ioc->fd < 0) {
77 object_unref(OBJECT(ioc));
78 return NULL;
81 if (lseek(ioc->fd, 0, SEEK_CUR) != (off_t)-1) {
82 qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_SEEKABLE);
85 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
87 return ioc;
91 static void qio_channel_file_init(Object *obj)
93 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
94 ioc->fd = -1;
97 static void qio_channel_file_finalize(Object *obj)
99 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
100 if (ioc->fd != -1) {
101 qemu_close(ioc->fd);
102 ioc->fd = -1;
107 static ssize_t qio_channel_file_readv(QIOChannel *ioc,
108 const struct iovec *iov,
109 size_t niov,
110 int **fds,
111 size_t *nfds,
112 int flags,
113 Error **errp)
115 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
116 ssize_t ret;
118 retry:
119 ret = readv(fioc->fd, iov, niov);
120 if (ret < 0) {
121 if (errno == EAGAIN) {
122 return QIO_CHANNEL_ERR_BLOCK;
124 if (errno == EINTR) {
125 goto retry;
128 error_setg_errno(errp, errno,
129 "Unable to read from file");
130 return -1;
133 return ret;
136 static ssize_t qio_channel_file_writev(QIOChannel *ioc,
137 const struct iovec *iov,
138 size_t niov,
139 int *fds,
140 size_t nfds,
141 int flags,
142 Error **errp)
144 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
145 ssize_t ret;
147 retry:
148 ret = writev(fioc->fd, iov, niov);
149 if (ret <= 0) {
150 if (errno == EAGAIN) {
151 return QIO_CHANNEL_ERR_BLOCK;
153 if (errno == EINTR) {
154 goto retry;
156 error_setg_errno(errp, errno,
157 "Unable to write to file");
158 return -1;
160 return ret;
163 #ifdef CONFIG_PREADV
164 static ssize_t qio_channel_file_preadv(QIOChannel *ioc,
165 const struct iovec *iov,
166 size_t niov,
167 off_t offset,
168 Error **errp)
170 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
171 ssize_t ret;
173 retry:
174 ret = preadv(fioc->fd, iov, niov, offset);
175 if (ret < 0) {
176 if (errno == EAGAIN) {
177 return QIO_CHANNEL_ERR_BLOCK;
179 if (errno == EINTR) {
180 goto retry;
183 error_setg_errno(errp, errno, "Unable to read from file");
184 return -1;
187 return ret;
190 static ssize_t qio_channel_file_pwritev(QIOChannel *ioc,
191 const struct iovec *iov,
192 size_t niov,
193 off_t offset,
194 Error **errp)
196 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
197 ssize_t ret;
199 retry:
200 ret = pwritev(fioc->fd, iov, niov, offset);
201 if (ret <= 0) {
202 if (errno == EAGAIN) {
203 return QIO_CHANNEL_ERR_BLOCK;
205 if (errno == EINTR) {
206 goto retry;
208 error_setg_errno(errp, errno, "Unable to write to file");
209 return -1;
211 return ret;
213 #endif /* CONFIG_PREADV */
215 static int qio_channel_file_set_blocking(QIOChannel *ioc,
216 bool enabled,
217 Error **errp)
219 #ifdef WIN32
220 /* not implemented */
221 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
222 return -1;
223 #else
224 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
226 if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) {
227 error_setg_errno(errp, errno, "Failed to set FD nonblocking");
228 return -1;
230 return 0;
231 #endif
235 static off_t qio_channel_file_seek(QIOChannel *ioc,
236 off_t offset,
237 int whence,
238 Error **errp)
240 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
241 off_t ret;
243 ret = lseek(fioc->fd, offset, whence);
244 if (ret == (off_t)-1) {
245 error_setg_errno(errp, errno,
246 "Unable to seek to offset %lld whence %d in file",
247 (long long int)offset, whence);
248 return -1;
250 return ret;
254 static int qio_channel_file_close(QIOChannel *ioc,
255 Error **errp)
257 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
259 if (qemu_close(fioc->fd) < 0) {
260 error_setg_errno(errp, errno,
261 "Unable to close file");
262 return -1;
264 fioc->fd = -1;
265 return 0;
269 static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc,
270 AioContext *read_ctx,
271 IOHandler *io_read,
272 AioContext *write_ctx,
273 IOHandler *io_write,
274 void *opaque)
276 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
278 qio_channel_util_set_aio_fd_handler(fioc->fd, read_ctx, io_read,
279 fioc->fd, write_ctx, io_write,
280 opaque);
283 static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
284 GIOCondition condition)
286 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
287 return qio_channel_create_fd_watch(ioc,
288 fioc->fd,
289 condition);
292 static void qio_channel_file_class_init(ObjectClass *klass,
293 void *class_data G_GNUC_UNUSED)
295 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
297 ioc_klass->io_writev = qio_channel_file_writev;
298 ioc_klass->io_readv = qio_channel_file_readv;
299 ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
300 #ifdef CONFIG_PREADV
301 ioc_klass->io_pwritev = qio_channel_file_pwritev;
302 ioc_klass->io_preadv = qio_channel_file_preadv;
303 #endif
304 ioc_klass->io_seek = qio_channel_file_seek;
305 ioc_klass->io_close = qio_channel_file_close;
306 ioc_klass->io_create_watch = qio_channel_file_create_watch;
307 ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler;
310 static const TypeInfo qio_channel_file_info = {
311 .parent = TYPE_QIO_CHANNEL,
312 .name = TYPE_QIO_CHANNEL_FILE,
313 .instance_size = sizeof(QIOChannelFile),
314 .instance_init = qio_channel_file_init,
315 .instance_finalize = qio_channel_file_finalize,
316 .class_init = qio_channel_file_class_init,
319 static void qio_channel_file_register_types(void)
321 type_register_static(&qio_channel_file_info);
324 type_init(qio_channel_file_register_types);