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 "qapi/error.h"
25 #include "qemu/module.h"
26 #include "qemu/sockets.h"
30 qio_channel_file_new_fd(int fd
)
34 ioc
= QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE
));
38 trace_qio_channel_file_new_fd(ioc
, fd
);
45 qio_channel_file_new_path(const char *path
,
52 ioc
= QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE
));
54 ioc
->fd
= qemu_open_old(path
, flags
, mode
);
56 object_unref(OBJECT(ioc
));
57 error_setg_errno(errp
, errno
,
58 "Unable to open %s", path
);
62 trace_qio_channel_file_new_path(ioc
, path
, flags
, mode
, ioc
->fd
);
68 static void qio_channel_file_init(Object
*obj
)
70 QIOChannelFile
*ioc
= QIO_CHANNEL_FILE(obj
);
74 static void qio_channel_file_finalize(Object
*obj
)
76 QIOChannelFile
*ioc
= QIO_CHANNEL_FILE(obj
);
84 static ssize_t
qio_channel_file_readv(QIOChannel
*ioc
,
85 const struct iovec
*iov
,
91 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
95 ret
= readv(fioc
->fd
, iov
, niov
);
97 if (errno
== EAGAIN
) {
98 return QIO_CHANNEL_ERR_BLOCK
;
100 if (errno
== EINTR
) {
104 error_setg_errno(errp
, errno
,
105 "Unable to read from file");
112 static ssize_t
qio_channel_file_writev(QIOChannel
*ioc
,
113 const struct iovec
*iov
,
119 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
123 ret
= writev(fioc
->fd
, iov
, niov
);
125 if (errno
== EAGAIN
) {
126 return QIO_CHANNEL_ERR_BLOCK
;
128 if (errno
== EINTR
) {
131 error_setg_errno(errp
, errno
,
132 "Unable to write to file");
138 static int qio_channel_file_set_blocking(QIOChannel
*ioc
,
142 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
145 qemu_set_block(fioc
->fd
);
147 qemu_set_nonblock(fioc
->fd
);
153 static off_t
qio_channel_file_seek(QIOChannel
*ioc
,
158 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
161 ret
= lseek(fioc
->fd
, offset
, whence
);
162 if (ret
== (off_t
)-1) {
163 error_setg_errno(errp
, errno
,
164 "Unable to seek to offset %lld whence %d in file",
165 (long long int)offset
, whence
);
172 static int qio_channel_file_close(QIOChannel
*ioc
,
175 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
177 if (qemu_close(fioc
->fd
) < 0) {
178 error_setg_errno(errp
, errno
,
179 "Unable to close file");
187 static void qio_channel_file_set_aio_fd_handler(QIOChannel
*ioc
,
193 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
194 aio_set_fd_handler(ctx
, fioc
->fd
, false, io_read
, io_write
, NULL
, opaque
);
197 static GSource
*qio_channel_file_create_watch(QIOChannel
*ioc
,
198 GIOCondition condition
)
200 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
201 return qio_channel_create_fd_watch(ioc
,
206 static void qio_channel_file_class_init(ObjectClass
*klass
,
207 void *class_data G_GNUC_UNUSED
)
209 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
211 ioc_klass
->io_writev
= qio_channel_file_writev
;
212 ioc_klass
->io_readv
= qio_channel_file_readv
;
213 ioc_klass
->io_set_blocking
= qio_channel_file_set_blocking
;
214 ioc_klass
->io_seek
= qio_channel_file_seek
;
215 ioc_klass
->io_close
= qio_channel_file_close
;
216 ioc_klass
->io_create_watch
= qio_channel_file_create_watch
;
217 ioc_klass
->io_set_aio_fd_handler
= qio_channel_file_set_aio_fd_handler
;
220 static const TypeInfo qio_channel_file_info
= {
221 .parent
= TYPE_QIO_CHANNEL
,
222 .name
= TYPE_QIO_CHANNEL_FILE
,
223 .instance_size
= sizeof(QIOChannelFile
),
224 .instance_init
= qio_channel_file_init
,
225 .instance_finalize
= qio_channel_file_finalize
,
226 .class_init
= qio_channel_file_class_init
,
229 static void qio_channel_file_register_types(void)
231 type_register_static(&qio_channel_file_info
);
234 type_init(qio_channel_file_register_types
);