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"
31 qio_channel_file_new_fd(int fd
)
35 ioc
= QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE
));
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
);
49 qio_channel_file_new_dupfd(int fd
, Error
**errp
)
54 error_setg_errno(errp
, errno
, "Could not dup FD %d", fd
);
58 return qio_channel_file_new_fd(newfd
);
62 qio_channel_file_new_path(const char *path
,
69 ioc
= QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE
));
71 ioc
->fd
= qemu_open_old(path
, flags
, mode
);
73 object_unref(OBJECT(ioc
));
74 error_setg_errno(errp
, errno
,
75 "Unable to open %s", path
);
79 if (lseek(ioc
->fd
, 0, SEEK_CUR
) != (off_t
)-1) {
80 qio_channel_set_feature(QIO_CHANNEL(ioc
), QIO_CHANNEL_FEATURE_SEEKABLE
);
83 trace_qio_channel_file_new_path(ioc
, path
, flags
, mode
, ioc
->fd
);
89 static void qio_channel_file_init(Object
*obj
)
91 QIOChannelFile
*ioc
= QIO_CHANNEL_FILE(obj
);
95 static void qio_channel_file_finalize(Object
*obj
)
97 QIOChannelFile
*ioc
= QIO_CHANNEL_FILE(obj
);
105 static ssize_t
qio_channel_file_readv(QIOChannel
*ioc
,
106 const struct iovec
*iov
,
113 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
117 ret
= readv(fioc
->fd
, iov
, niov
);
119 if (errno
== EAGAIN
) {
120 return QIO_CHANNEL_ERR_BLOCK
;
122 if (errno
== EINTR
) {
126 error_setg_errno(errp
, errno
,
127 "Unable to read from file");
134 static ssize_t
qio_channel_file_writev(QIOChannel
*ioc
,
135 const struct iovec
*iov
,
142 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
146 ret
= writev(fioc
->fd
, iov
, niov
);
148 if (errno
== EAGAIN
) {
149 return QIO_CHANNEL_ERR_BLOCK
;
151 if (errno
== EINTR
) {
154 error_setg_errno(errp
, errno
,
155 "Unable to write to file");
162 static ssize_t
qio_channel_file_preadv(QIOChannel
*ioc
,
163 const struct iovec
*iov
,
168 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
172 ret
= preadv(fioc
->fd
, iov
, niov
, offset
);
174 if (errno
== EAGAIN
) {
175 return QIO_CHANNEL_ERR_BLOCK
;
177 if (errno
== EINTR
) {
181 error_setg_errno(errp
, errno
, "Unable to read from file");
188 static ssize_t
qio_channel_file_pwritev(QIOChannel
*ioc
,
189 const struct iovec
*iov
,
194 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
198 ret
= pwritev(fioc
->fd
, iov
, niov
, offset
);
200 if (errno
== EAGAIN
) {
201 return QIO_CHANNEL_ERR_BLOCK
;
203 if (errno
== EINTR
) {
206 error_setg_errno(errp
, errno
, "Unable to write to file");
211 #endif /* CONFIG_PREADV */
213 static int qio_channel_file_set_blocking(QIOChannel
*ioc
,
218 /* not implemented */
219 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
222 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
224 if (!g_unix_set_fd_nonblocking(fioc
->fd
, !enabled
, NULL
)) {
225 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
233 static off_t
qio_channel_file_seek(QIOChannel
*ioc
,
238 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
241 ret
= lseek(fioc
->fd
, offset
, whence
);
242 if (ret
== (off_t
)-1) {
243 error_setg_errno(errp
, errno
,
244 "Unable to seek to offset %lld whence %d in file",
245 (long long int)offset
, whence
);
252 static int qio_channel_file_close(QIOChannel
*ioc
,
255 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
257 if (qemu_close(fioc
->fd
) < 0) {
258 error_setg_errno(errp
, errno
,
259 "Unable to close file");
267 static void qio_channel_file_set_aio_fd_handler(QIOChannel
*ioc
,
268 AioContext
*read_ctx
,
270 AioContext
*write_ctx
,
274 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
276 qio_channel_util_set_aio_fd_handler(fioc
->fd
, read_ctx
, io_read
,
277 fioc
->fd
, write_ctx
, io_write
,
281 static GSource
*qio_channel_file_create_watch(QIOChannel
*ioc
,
282 GIOCondition condition
)
284 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
285 return qio_channel_create_fd_watch(ioc
,
290 static void qio_channel_file_class_init(ObjectClass
*klass
,
291 void *class_data G_GNUC_UNUSED
)
293 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
295 ioc_klass
->io_writev
= qio_channel_file_writev
;
296 ioc_klass
->io_readv
= qio_channel_file_readv
;
297 ioc_klass
->io_set_blocking
= qio_channel_file_set_blocking
;
299 ioc_klass
->io_pwritev
= qio_channel_file_pwritev
;
300 ioc_klass
->io_preadv
= qio_channel_file_preadv
;
302 ioc_klass
->io_seek
= qio_channel_file_seek
;
303 ioc_klass
->io_close
= qio_channel_file_close
;
304 ioc_klass
->io_create_watch
= qio_channel_file_create_watch
;
305 ioc_klass
->io_set_aio_fd_handler
= qio_channel_file_set_aio_fd_handler
;
308 static const TypeInfo qio_channel_file_info
= {
309 .parent
= TYPE_QIO_CHANNEL
,
310 .name
= TYPE_QIO_CHANNEL_FILE
,
311 .instance_size
= sizeof(QIOChannelFile
),
312 .instance_init
= qio_channel_file_init
,
313 .instance_finalize
= qio_channel_file_finalize
,
314 .class_init
= qio_channel_file_class_init
,
317 static void qio_channel_file_register_types(void)
319 type_register_static(&qio_channel_file_info
);
322 type_init(qio_channel_file_register_types
);