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 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-watch.h"
24 #include "qemu/sockets.h"
28 qio_channel_file_new_fd(int fd)
32 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
36 trace_qio_channel_file_new_fd(ioc, fd);
43 qio_channel_file_new_path(const char *path,
50 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE));
52 if (flags & O_WRONLY) {
53 ioc->fd = open(path, flags, mode);
55 ioc->fd = open(path, flags);
58 object_unref(OBJECT(ioc));
59 error_setg_errno(errp, errno,
60 "Unable to open %s", path);
64 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd);
70 static void qio_channel_file_init(Object *obj)
72 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
76 static void qio_channel_file_finalize(Object *obj)
78 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj);
86 static ssize_t qio_channel_file_readv(QIOChannel *ioc,
87 const struct iovec *iov,
93 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
97 ret = readv(fioc->fd, iov, niov);
99 if (errno == EAGAIN) {
100 return QIO_CHANNEL_ERR_BLOCK;
102 if (errno == EINTR) {
106 error_setg_errno(errp, errno,
107 "Unable to read from file");
114 static ssize_t qio_channel_file_writev(QIOChannel *ioc,
115 const struct iovec *iov,
121 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
125 ret = writev(fioc->fd, iov, niov);
127 if (errno == EAGAIN) {
128 return QIO_CHANNEL_ERR_BLOCK;
130 if (errno == EINTR) {
133 error_setg_errno(errp, errno,
134 "Unable to write to file");
140 static int qio_channel_file_set_blocking(QIOChannel *ioc,
144 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
147 qemu_set_block(fioc->fd);
149 qemu_set_nonblock(fioc->fd);
155 static off_t qio_channel_file_seek(QIOChannel *ioc,
160 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
163 ret = lseek(fioc->fd, offset, whence);
164 if (ret == (off_t)-1) {
165 error_setg_errno(errp, errno,
166 "Unable to seek to offset %lld whence %d in file",
167 (long long int)offset, whence);
174 static int qio_channel_file_close(QIOChannel *ioc,
177 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
179 if (close(fioc->fd) < 0) {
180 error_setg_errno(errp, errno,
181 "Unable to close file");
188 static GSource *qio_channel_file_create_watch(QIOChannel *ioc,
189 GIOCondition condition)
191 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc);
192 return qio_channel_create_fd_watch(ioc,
197 static void qio_channel_file_class_init(ObjectClass *klass,
198 void *class_data G_GNUC_UNUSED)
200 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass);
202 ioc_klass->io_writev = qio_channel_file_writev;
203 ioc_klass->io_readv = qio_channel_file_readv;
204 ioc_klass->io_set_blocking = qio_channel_file_set_blocking;
205 ioc_klass->io_seek = qio_channel_file_seek;
206 ioc_klass->io_close = qio_channel_file_close;
207 ioc_klass->io_create_watch = qio_channel_file_create_watch;
210 static const TypeInfo qio_channel_file_info = {
211 .parent = TYPE_QIO_CHANNEL,
212 .name = TYPE_QIO_CHANNEL_FILE,
213 .instance_size = sizeof(QIOChannelFile),
214 .instance_init = qio_channel_file_init,
215 .instance_finalize = qio_channel_file_finalize,
216 .class_init = qio_channel_file_class_init,
219 static void qio_channel_file_register_types(void)
221 type_register_static(&qio_channel_file_info);
224 type_init(qio_channel_file_register_types);