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 | |
Nikolay Borisov | 401e311 | 2024-02-29 12:29:56 -0300 | [diff] [blame] | 39 | if (lseek(fd, 0, SEEK_CUR) != (off_t)-1) { |
| 40 | qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_SEEKABLE); |
| 41 | } |
| 42 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 43 | trace_qio_channel_file_new_fd(ioc, fd); |
| 44 | |
| 45 | return ioc; |
| 46 | } |
| 47 | |
Fabiano Rosas | 4760ced | 2024-03-11 20:33:34 -0300 | [diff] [blame] | 48 | QIOChannelFile * |
| 49 | qio_channel_file_new_dupfd(int fd, Error **errp) |
| 50 | { |
| 51 | int newfd = dup(fd); |
| 52 | |
| 53 | if (newfd < 0) { |
| 54 | error_setg_errno(errp, errno, "Could not dup FD %d", fd); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | return qio_channel_file_new_fd(newfd); |
| 59 | } |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 60 | |
| 61 | QIOChannelFile * |
| 62 | qio_channel_file_new_path(const char *path, |
| 63 | int flags, |
| 64 | mode_t mode, |
| 65 | Error **errp) |
| 66 | { |
| 67 | QIOChannelFile *ioc; |
| 68 | |
| 69 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); |
| 70 | |
Fabiano Rosas | 46cec74 | 2024-06-17 15:57:25 -0300 | [diff] [blame] | 71 | if (flags & O_CREAT) { |
| 72 | ioc->fd = qemu_create(path, flags & ~O_CREAT, mode, errp); |
| 73 | } else { |
| 74 | ioc->fd = qemu_open(path, flags, errp); |
| 75 | } |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 76 | if (ioc->fd < 0) { |
| 77 | object_unref(OBJECT(ioc)); |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 78 | return NULL; |
| 79 | } |
| 80 | |
Nikolay Borisov | 401e311 | 2024-02-29 12:29:56 -0300 | [diff] [blame] | 81 | if (lseek(ioc->fd, 0, SEEK_CUR) != (off_t)-1) { |
| 82 | qio_channel_set_feature(QIO_CHANNEL(ioc), QIO_CHANNEL_FEATURE_SEEKABLE); |
| 83 | } |
| 84 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 85 | trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd); |
| 86 | |
| 87 | return ioc; |
| 88 | } |
| 89 | |
| 90 | |
| 91 | static void qio_channel_file_init(Object *obj) |
| 92 | { |
| 93 | QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); |
| 94 | ioc->fd = -1; |
| 95 | } |
| 96 | |
| 97 | static void qio_channel_file_finalize(Object *obj) |
| 98 | { |
| 99 | QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); |
| 100 | if (ioc->fd != -1) { |
Ross Lagerwall | b8f244b | 2017-11-01 14:25:26 +0000 | [diff] [blame] | 101 | qemu_close(ioc->fd); |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 102 | ioc->fd = -1; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | |
| 107 | static ssize_t qio_channel_file_readv(QIOChannel *ioc, |
| 108 | const struct iovec *iov, |
| 109 | size_t niov, |
| 110 | int **fds, |
| 111 | size_t *nfds, |
manish.mishra | 84615a1 | 2022-12-20 18:44:17 +0000 | [diff] [blame] | 112 | int flags, |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 113 | Error **errp) |
| 114 | { |
| 115 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 116 | ssize_t ret; |
| 117 | |
| 118 | retry: |
| 119 | ret = readv(fioc->fd, iov, niov); |
| 120 | if (ret < 0) { |
Daniel P. Berrange | 30fd3e2 | 2016-03-10 17:17:33 +0000 | [diff] [blame] | 121 | if (errno == EAGAIN) { |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 122 | return QIO_CHANNEL_ERR_BLOCK; |
| 123 | } |
| 124 | if (errno == EINTR) { |
| 125 | goto retry; |
| 126 | } |
| 127 | |
| 128 | error_setg_errno(errp, errno, |
| 129 | "Unable to read from file"); |
| 130 | return -1; |
| 131 | } |
| 132 | |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | static ssize_t qio_channel_file_writev(QIOChannel *ioc, |
| 137 | const struct iovec *iov, |
| 138 | size_t niov, |
| 139 | int *fds, |
| 140 | size_t nfds, |
Leonardo Bras | b88651c | 2022-05-13 03:28:31 -0300 | [diff] [blame] | 141 | int flags, |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 142 | Error **errp) |
| 143 | { |
| 144 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 145 | ssize_t ret; |
| 146 | |
| 147 | retry: |
| 148 | ret = writev(fioc->fd, iov, niov); |
| 149 | if (ret <= 0) { |
Daniel P. Berrange | 30fd3e2 | 2016-03-10 17:17:33 +0000 | [diff] [blame] | 150 | if (errno == EAGAIN) { |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 151 | return QIO_CHANNEL_ERR_BLOCK; |
| 152 | } |
| 153 | if (errno == EINTR) { |
| 154 | goto retry; |
| 155 | } |
| 156 | error_setg_errno(errp, errno, |
| 157 | "Unable to write to file"); |
| 158 | return -1; |
| 159 | } |
| 160 | return ret; |
| 161 | } |
| 162 | |
Nikolay Borisov | 0478b03 | 2024-02-29 12:29:58 -0300 | [diff] [blame] | 163 | #ifdef CONFIG_PREADV |
| 164 | static ssize_t qio_channel_file_preadv(QIOChannel *ioc, |
| 165 | const struct iovec *iov, |
| 166 | size_t niov, |
| 167 | off_t offset, |
| 168 | Error **errp) |
| 169 | { |
| 170 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 171 | ssize_t ret; |
| 172 | |
| 173 | retry: |
| 174 | ret = preadv(fioc->fd, iov, niov, offset); |
| 175 | if (ret < 0) { |
| 176 | if (errno == EAGAIN) { |
| 177 | return QIO_CHANNEL_ERR_BLOCK; |
| 178 | } |
| 179 | if (errno == EINTR) { |
| 180 | goto retry; |
| 181 | } |
| 182 | |
| 183 | error_setg_errno(errp, errno, "Unable to read from file"); |
| 184 | return -1; |
| 185 | } |
| 186 | |
| 187 | return ret; |
| 188 | } |
| 189 | |
| 190 | static ssize_t qio_channel_file_pwritev(QIOChannel *ioc, |
| 191 | const struct iovec *iov, |
| 192 | size_t niov, |
| 193 | off_t offset, |
| 194 | Error **errp) |
| 195 | { |
| 196 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 197 | ssize_t ret; |
| 198 | |
| 199 | retry: |
| 200 | ret = pwritev(fioc->fd, iov, niov, offset); |
| 201 | if (ret <= 0) { |
| 202 | if (errno == EAGAIN) { |
| 203 | return QIO_CHANNEL_ERR_BLOCK; |
| 204 | } |
| 205 | if (errno == EINTR) { |
| 206 | goto retry; |
| 207 | } |
| 208 | error_setg_errno(errp, errno, "Unable to write to file"); |
| 209 | return -1; |
| 210 | } |
| 211 | return ret; |
| 212 | } |
| 213 | #endif /* CONFIG_PREADV */ |
| 214 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 215 | static int qio_channel_file_set_blocking(QIOChannel *ioc, |
| 216 | bool enabled, |
| 217 | Error **errp) |
| 218 | { |
Marc-André Lureau | 17fc124 | 2022-04-25 17:39:06 +0400 | [diff] [blame] | 219 | #ifdef WIN32 |
| 220 | /* not implemented */ |
| 221 | error_setg_errno(errp, errno, "Failed to set FD nonblocking"); |
| 222 | return -1; |
| 223 | #else |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 224 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 225 | |
Marc-André Lureau | 17fc124 | 2022-04-25 17:39:06 +0400 | [diff] [blame] | 226 | if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) { |
| 227 | error_setg_errno(errp, errno, "Failed to set FD nonblocking"); |
| 228 | return -1; |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 229 | } |
| 230 | return 0; |
Marc-André Lureau | 17fc124 | 2022-04-25 17:39:06 +0400 | [diff] [blame] | 231 | #endif |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | |
| 235 | static off_t qio_channel_file_seek(QIOChannel *ioc, |
| 236 | off_t offset, |
| 237 | int whence, |
| 238 | Error **errp) |
| 239 | { |
| 240 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 241 | off_t ret; |
| 242 | |
| 243 | ret = lseek(fioc->fd, offset, whence); |
| 244 | if (ret == (off_t)-1) { |
| 245 | error_setg_errno(errp, errno, |
| 246 | "Unable to seek to offset %lld whence %d in file", |
| 247 | (long long int)offset, whence); |
| 248 | return -1; |
| 249 | } |
| 250 | return ret; |
| 251 | } |
| 252 | |
| 253 | |
| 254 | static int qio_channel_file_close(QIOChannel *ioc, |
| 255 | Error **errp) |
| 256 | { |
| 257 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 258 | |
Ross Lagerwall | b8f244b | 2017-11-01 14:25:26 +0000 | [diff] [blame] | 259 | if (qemu_close(fioc->fd) < 0) { |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 260 | error_setg_errno(errp, errno, |
| 261 | "Unable to close file"); |
| 262 | return -1; |
| 263 | } |
Ross Lagerwall | a2565df | 2017-11-01 14:25:25 +0000 | [diff] [blame] | 264 | fioc->fd = -1; |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 265 | return 0; |
| 266 | } |
| 267 | |
| 268 | |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 269 | static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc, |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 270 | AioContext *read_ctx, |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 271 | IOHandler *io_read, |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 272 | AioContext *write_ctx, |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 273 | IOHandler *io_write, |
| 274 | void *opaque) |
| 275 | { |
| 276 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
Stefan Hajnoczi | 06e0f09 | 2023-08-30 18:48:02 -0400 | [diff] [blame] | 277 | |
| 278 | qio_channel_util_set_aio_fd_handler(fioc->fd, read_ctx, io_read, |
| 279 | fioc->fd, write_ctx, io_write, |
| 280 | opaque); |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 283 | static GSource *qio_channel_file_create_watch(QIOChannel *ioc, |
| 284 | GIOCondition condition) |
| 285 | { |
| 286 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); |
| 287 | return qio_channel_create_fd_watch(ioc, |
| 288 | fioc->fd, |
| 289 | condition); |
| 290 | } |
| 291 | |
| 292 | static void qio_channel_file_class_init(ObjectClass *klass, |
| 293 | void *class_data G_GNUC_UNUSED) |
| 294 | { |
| 295 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); |
| 296 | |
| 297 | ioc_klass->io_writev = qio_channel_file_writev; |
| 298 | ioc_klass->io_readv = qio_channel_file_readv; |
| 299 | ioc_klass->io_set_blocking = qio_channel_file_set_blocking; |
Nikolay Borisov | 0478b03 | 2024-02-29 12:29:58 -0300 | [diff] [blame] | 300 | #ifdef CONFIG_PREADV |
| 301 | ioc_klass->io_pwritev = qio_channel_file_pwritev; |
| 302 | ioc_klass->io_preadv = qio_channel_file_preadv; |
| 303 | #endif |
Daniel P. Berrange | d6e4886 | 2015-02-27 18:25:25 +0000 | [diff] [blame] | 304 | ioc_klass->io_seek = qio_channel_file_seek; |
| 305 | ioc_klass->io_close = qio_channel_file_close; |
| 306 | ioc_klass->io_create_watch = qio_channel_file_create_watch; |
Paolo Bonzini | bf88c12 | 2017-02-13 14:52:22 +0100 | [diff] [blame] | 307 | 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] | 308 | } |
| 309 | |
| 310 | static const TypeInfo qio_channel_file_info = { |
| 311 | .parent = TYPE_QIO_CHANNEL, |
| 312 | .name = TYPE_QIO_CHANNEL_FILE, |
| 313 | .instance_size = sizeof(QIOChannelFile), |
| 314 | .instance_init = qio_channel_file_init, |
| 315 | .instance_finalize = qio_channel_file_finalize, |
| 316 | .class_init = qio_channel_file_class_init, |
| 317 | }; |
| 318 | |
| 319 | static void qio_channel_file_register_types(void) |
| 320 | { |
| 321 | type_register_static(&qio_channel_file_info); |
| 322 | } |
| 323 | |
| 324 | type_init(qio_channel_file_register_types); |