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 trace_qio_channel_file_new_fd(ioc
, fd
);
46 qio_channel_file_new_path(const char *path
,
53 ioc
= QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE
));
55 ioc
->fd
= qemu_open_old(path
, flags
, mode
);
57 object_unref(OBJECT(ioc
));
58 error_setg_errno(errp
, errno
,
59 "Unable to open %s", path
);
63 trace_qio_channel_file_new_path(ioc
, path
, flags
, mode
, ioc
->fd
);
69 static void qio_channel_file_init(Object
*obj
)
71 QIOChannelFile
*ioc
= QIO_CHANNEL_FILE(obj
);
75 static void qio_channel_file_finalize(Object
*obj
)
77 QIOChannelFile
*ioc
= QIO_CHANNEL_FILE(obj
);
85 static ssize_t
qio_channel_file_readv(QIOChannel
*ioc
,
86 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
,
122 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
126 ret
= writev(fioc
->fd
, iov
, niov
);
128 if (errno
== EAGAIN
) {
129 return QIO_CHANNEL_ERR_BLOCK
;
131 if (errno
== EINTR
) {
134 error_setg_errno(errp
, errno
,
135 "Unable to write to file");
141 static int qio_channel_file_set_blocking(QIOChannel
*ioc
,
146 /* not implemented */
147 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
150 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
152 if (!g_unix_set_fd_nonblocking(fioc
->fd
, !enabled
, NULL
)) {
153 error_setg_errno(errp
, errno
, "Failed to set FD nonblocking");
161 static off_t
qio_channel_file_seek(QIOChannel
*ioc
,
166 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
169 ret
= lseek(fioc
->fd
, offset
, whence
);
170 if (ret
== (off_t
)-1) {
171 error_setg_errno(errp
, errno
,
172 "Unable to seek to offset %lld whence %d in file",
173 (long long int)offset
, whence
);
180 static int qio_channel_file_close(QIOChannel
*ioc
,
183 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
185 if (qemu_close(fioc
->fd
) < 0) {
186 error_setg_errno(errp
, errno
,
187 "Unable to close file");
195 static void qio_channel_file_set_aio_fd_handler(QIOChannel
*ioc
,
196 AioContext
*read_ctx
,
198 AioContext
*write_ctx
,
202 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
204 qio_channel_util_set_aio_fd_handler(fioc
->fd
, read_ctx
, io_read
,
205 fioc
->fd
, write_ctx
, io_write
,
209 static GSource
*qio_channel_file_create_watch(QIOChannel
*ioc
,
210 GIOCondition condition
)
212 QIOChannelFile
*fioc
= QIO_CHANNEL_FILE(ioc
);
213 return qio_channel_create_fd_watch(ioc
,
218 static void qio_channel_file_class_init(ObjectClass
*klass
,
219 void *class_data G_GNUC_UNUSED
)
221 QIOChannelClass
*ioc_klass
= QIO_CHANNEL_CLASS(klass
);
223 ioc_klass
->io_writev
= qio_channel_file_writev
;
224 ioc_klass
->io_readv
= qio_channel_file_readv
;
225 ioc_klass
->io_set_blocking
= qio_channel_file_set_blocking
;
226 ioc_klass
->io_seek
= qio_channel_file_seek
;
227 ioc_klass
->io_close
= qio_channel_file_close
;
228 ioc_klass
->io_create_watch
= qio_channel_file_create_watch
;
229 ioc_klass
->io_set_aio_fd_handler
= qio_channel_file_set_aio_fd_handler
;
232 static const TypeInfo qio_channel_file_info
= {
233 .parent
= TYPE_QIO_CHANNEL
,
234 .name
= TYPE_QIO_CHANNEL_FILE
,
235 .instance_size
= sizeof(QIOChannelFile
),
236 .instance_init
= qio_channel_file_init
,
237 .instance_finalize
= qio_channel_file_finalize
,
238 .class_init
= qio_channel_file_class_init
,
241 static void qio_channel_file_register_types(void)
243 type_register_static(&qio_channel_file_info
);
246 type_init(qio_channel_file_register_types
);