Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU I/O channels files driver |
| 3 | * |
| 4 | * Copyright (c) 2015 Red Hat, Inc. |
| 5 | * |
| 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 |
Chetan Pant | c8198bd | 2020-10-14 13:40:33 +0000 | [diff] [blame] | 9 | * version 2.1 of the License, or (at your option) any later version. |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 10 | * |
| 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. |
| 15 | * |
| 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/>. |
| 18 | * |
| 19 | */ |
| 20 | |
Peter Maydell | cae9fc5 | 2016-01-29 17:50:03 +0000 | [diff] [blame] | 21 | #include "qemu/osdep.h" |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 22 | #include "io/channel-file.h" |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 23 | #include "io/channel-util.h" |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 24 | #include "io/channel-watch.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 25 | #include "qapi/error.h" |
Markus Armbruster | 0b8fa32 | 2019-05-23 16:35:07 +0200 | [diff] [blame] | 26 | #include "qemu/module.h" |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 27 | #include "qemu/sockets.h" |
| 28 | #include "trace.h" |
| 29 | |
| 30 | QIOChannelFile * |
| 31 | qio_channel_file_new_fd(int fd) |
| 32 | { |
| 33 | QIOChannelFile *ioc; |
| 34 | |
| 35 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); |
| 36 | |
| 37 | ioc->fd = fd; |
| 38 | |
| 39 | trace_qio_channel_file_new_fd(ioc, fd); |
| 40 | |
| 41 | return ioc; |
| 42 | } |
| 43 | |
| 44 | |
| 45 | QIOChannelFile * |
| 46 | qio_channel_file_new_path(const char *path, |
| 47 | int flags, |
| 48 | mode_t mode, |
| 49 | Error **errp) |
| 50 | { |
| 51 | QIOChannelFile *ioc; |
| 52 | |
| 53 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); |
| 54 | |
Daniel P. Berrangé | 448058a | 2020-07-21 13:25:21 +0100 | [diff] [blame] | 55 | ioc->fd = qemu_open_old(path, flags, mode); |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 56 | if (ioc->fd < 0) { |
| 57 | object_unref(OBJECT(ioc)); |
| 58 | error_setg_errno(errp, errno, |
| 59 | "Unable to open %s", path); |
| 60 | return NULL; |
| 61 | } |
| 62 | |
| 63 | trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd); |
| 64 | |
| 65 | return ioc; |
| 66 | } |
| 67 | |
| 68 | |
| 69 | static void qio_channel_file_init(Object *obj) |
| 70 | { |
| 71 | QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); |
| 72 | ioc->fd = -1; |
| 73 | } |
| 74 | |
| 75 | static void qio_channel_file_finalize(Object *obj) |
| 76 | { |
| 77 | QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); |
| 78 | if (ioc->fd != -1) { |
Ross Lagerwall | b8f244b | 2017-11-01 14:25:26 +0000 | [diff] [blame] | 79 | qemu_close(ioc->fd); |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 80 | ioc->fd = -1; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |
| 85 | static ssize_t qio_channel_file_readv(QIOChannel *ioc, |
| 86 | const struct iovec *iov, |
| 87 | size_t niov, |
| 88 | int **fds, |
| 89 | size_t *nfds, |
manish.mishra | 84615a1 | 2022-12-20 18:44:17 +0000 | [diff] [blame] | 90 | int flags, |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 91 | Error **errp) |
| 92 | { |
| 93 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 94 | ssize_t ret; |
| 95 | |
| 96 | retry: |
| 97 | ret = readv(fioc->fd, iov, niov); |
| 98 | if (ret < 0) { |
Daniel P. Berrange | 30fd3e2 | 2016-03-10 17:17:33 +0000 | [diff] [blame] | 99 | if (errno == EAGAIN) { |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 100 | return QIO_CHANNEL_ERR_BLOCK; |
| 101 | } |
| 102 | if (errno == EINTR) { |
| 103 | goto retry; |
| 104 | } |
| 105 | |
| 106 | error_setg_errno(errp, errno, |
| 107 | "Unable to read from file"); |
| 108 | return -1; |
| 109 | } |
| 110 | |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | static ssize_t qio_channel_file_writev(QIOChannel *ioc, |
| 115 | const struct iovec *iov, |
| 116 | size_t niov, |
| 117 | int *fds, |
| 118 | size_t nfds, |
Leonardo Bras | b88651c | 2022-05-13 03:28:31 -0300 | [diff] [blame] | 119 | int flags, |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 120 | Error **errp) |
| 121 | { |
| 122 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 123 | ssize_t ret; |
| 124 | |
| 125 | retry: |
| 126 | ret = writev(fioc->fd, iov, niov); |
| 127 | if (ret <= 0) { |
Daniel P. Berrange | 30fd3e2 | 2016-03-10 17:17:33 +0000 | [diff] [blame] | 128 | if (errno == EAGAIN) { |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 129 | return QIO_CHANNEL_ERR_BLOCK; |
| 130 | } |
| 131 | if (errno == EINTR) { |
| 132 | goto retry; |
| 133 | } |
| 134 | error_setg_errno(errp, errno, |
| 135 | "Unable to write to file"); |
| 136 | return -1; |
| 137 | } |
| 138 | return ret; |
| 139 | } |
| 140 | |
| 141 | static int qio_channel_file_set_blocking(QIOChannel *ioc, |
| 142 | bool enabled, |
| 143 | Error **errp) |
| 144 | { |
Marc-André Lureau | 17fc124 | 2022-04-25 17:39:06 +0400 | [diff] [blame] | 145 | #ifdef WIN32 |
| 146 | /* not implemented */ |
| 147 | error_setg_errno(errp, errno, "Failed to set FD nonblocking"); |
| 148 | return -1; |
| 149 | #else |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 150 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 151 | |
Marc-André Lureau | 17fc124 | 2022-04-25 17:39:06 +0400 | [diff] [blame] | 152 | if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) { |
| 153 | error_setg_errno(errp, errno, "Failed to set FD nonblocking"); |
| 154 | return -1; |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 155 | } |
| 156 | return 0; |
Marc-André Lureau | 17fc124 | 2022-04-25 17:39:06 +0400 | [diff] [blame] | 157 | #endif |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | |
| 161 | static off_t qio_channel_file_seek(QIOChannel *ioc, |
| 162 | off_t offset, |
| 163 | int whence, |
| 164 | Error **errp) |
| 165 | { |
| 166 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 167 | off_t ret; |
| 168 | |
| 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); |
| 174 | return -1; |
| 175 | } |
| 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | static int qio_channel_file_close(QIOChannel *ioc, |
| 181 | Error **errp) |
| 182 | { |
| 183 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 184 | |
Ross Lagerwall | b8f244b | 2017-11-01 14:25:26 +0000 | [diff] [blame] | 185 | if (qemu_close(fioc->fd) < 0) { |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 186 | error_setg_errno(errp, errno, |
| 187 | "Unable to close file"); |
| 188 | return -1; |
| 189 | } |
Ross Lagerwall | a2565df | 2017-11-01 14:25:25 +0000 | [diff] [blame] | 190 | fioc->fd = -1; |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 195 | static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc, |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 196 | AioContext *read_ctx, |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 197 | IOHandler *io_read, |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 198 | AioContext *write_ctx, |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 199 | IOHandler *io_write, |
| 200 | void *opaque) |
| 201 | { |
| 202 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 203 | |
| 204 | qio_channel_util_set_aio_fd_handler(fioc->fd, read_ctx, io_read, |
| 205 | fioc->fd, write_ctx, io_write, |
| 206 | opaque); |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 207 | } |
| 208 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 209 | static GSource *qio_channel_file_create_watch(QIOChannel *ioc, |
| 210 | GIOCondition condition) |
| 211 | { |
| 212 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 213 | return qio_channel_create_fd_watch(ioc, |
| 214 | fioc->fd, |
| 215 | condition); |
| 216 | } |
| 217 | |
| 218 | static void qio_channel_file_class_init(ObjectClass *klass, |
| 219 | void *class_data G_GNUC_UNUSED) |
| 220 | { |
| 221 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); |
| 222 | |
| 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; |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 229 | ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler; |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 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, |
| 239 | }; |
| 240 | |
| 241 | static void qio_channel_file_register_types(void) |
| 242 | { |
| 243 | type_register_static(&qio_channel_file_info); |
| 244 | } |
| 245 | |
| 246 | type_init(qio_channel_file_register_types); |