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 9 * version 2.1 of the License, or (at your option) any later version. 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 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" 27 #include "trace.h" 28 29 QIOChannelFile * 30 qio_channel_file_new_fd(int fd) 31 { 32 QIOChannelFile *ioc; 33 34 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); 35 36 ioc->fd = fd; 37 38 trace_qio_channel_file_new_fd(ioc, fd); 39 40 return ioc; 41 } 42 43 44 QIOChannelFile * 45 qio_channel_file_new_path(const char *path, 46 int flags, 47 mode_t mode, 48 Error **errp) 49 { 50 QIOChannelFile *ioc; 51 52 ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); 53 54 ioc->fd = qemu_open_old(path, flags, mode); 55 if (ioc->fd < 0) { 56 object_unref(OBJECT(ioc)); 57 error_setg_errno(errp, errno, 58 "Unable to open %s", path); 59 return NULL; 60 } 61 62 trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd); 63 64 return ioc; 65 } 66 67 68 static void qio_channel_file_init(Object *obj) 69 { 70 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); 71 ioc->fd = -1; 72 } 73 74 static void qio_channel_file_finalize(Object *obj) 75 { 76 QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); 77 if (ioc->fd != -1) { 78 qemu_close(ioc->fd); 79 ioc->fd = -1; 80 } 81 } 82 83 84 static ssize_t qio_channel_file_readv(QIOChannel *ioc, 85 const struct iovec *iov, 86 size_t niov, 87 int **fds, 88 size_t *nfds, 89 Error **errp) 90 { 91 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 92 ssize_t ret; 93 94 retry: 95 ret = readv(fioc->fd, iov, niov); 96 if (ret < 0) { 97 if (errno == EAGAIN) { 98 return QIO_CHANNEL_ERR_BLOCK; 99 } 100 if (errno == EINTR) { 101 goto retry; 102 } 103 104 error_setg_errno(errp, errno, 105 "Unable to read from file"); 106 return -1; 107 } 108 109 return ret; 110 } 111 112 static ssize_t qio_channel_file_writev(QIOChannel *ioc, 113 const struct iovec *iov, 114 size_t niov, 115 int *fds, 116 size_t nfds, 117 Error **errp) 118 { 119 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 120 ssize_t ret; 121 122 retry: 123 ret = writev(fioc->fd, iov, niov); 124 if (ret <= 0) { 125 if (errno == EAGAIN) { 126 return QIO_CHANNEL_ERR_BLOCK; 127 } 128 if (errno == EINTR) { 129 goto retry; 130 } 131 error_setg_errno(errp, errno, 132 "Unable to write to file"); 133 return -1; 134 } 135 return ret; 136 } 137 138 static int qio_channel_file_set_blocking(QIOChannel *ioc, 139 bool enabled, 140 Error **errp) 141 { 142 #ifdef WIN32 143 /* not implemented */ 144 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 145 return -1; 146 #else 147 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 148 149 if (!g_unix_set_fd_nonblocking(fioc->fd, !enabled, NULL)) { 150 error_setg_errno(errp, errno, "Failed to set FD nonblocking"); 151 return -1; 152 } 153 return 0; 154 #endif 155 } 156 157 158 static off_t qio_channel_file_seek(QIOChannel *ioc, 159 off_t offset, 160 int whence, 161 Error **errp) 162 { 163 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 164 off_t ret; 165 166 ret = lseek(fioc->fd, offset, whence); 167 if (ret == (off_t)-1) { 168 error_setg_errno(errp, errno, 169 "Unable to seek to offset %lld whence %d in file", 170 (long long int)offset, whence); 171 return -1; 172 } 173 return ret; 174 } 175 176 177 static int qio_channel_file_close(QIOChannel *ioc, 178 Error **errp) 179 { 180 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 181 182 if (qemu_close(fioc->fd) < 0) { 183 error_setg_errno(errp, errno, 184 "Unable to close file"); 185 return -1; 186 } 187 fioc->fd = -1; 188 return 0; 189 } 190 191 192 static void qio_channel_file_set_aio_fd_handler(QIOChannel *ioc, 193 AioContext *ctx, 194 IOHandler *io_read, 195 IOHandler *io_write, 196 void *opaque) 197 { 198 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 199 aio_set_fd_handler(ctx, fioc->fd, false, io_read, io_write, 200 NULL, NULL, opaque); 201 } 202 203 static GSource *qio_channel_file_create_watch(QIOChannel *ioc, 204 GIOCondition condition) 205 { 206 QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); 207 return qio_channel_create_fd_watch(ioc, 208 fioc->fd, 209 condition); 210 } 211 212 static void qio_channel_file_class_init(ObjectClass *klass, 213 void *class_data G_GNUC_UNUSED) 214 { 215 QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); 216 217 ioc_klass->io_writev = qio_channel_file_writev; 218 ioc_klass->io_readv = qio_channel_file_readv; 219 ioc_klass->io_set_blocking = qio_channel_file_set_blocking; 220 ioc_klass->io_seek = qio_channel_file_seek; 221 ioc_klass->io_close = qio_channel_file_close; 222 ioc_klass->io_create_watch = qio_channel_file_create_watch; 223 ioc_klass->io_set_aio_fd_handler = qio_channel_file_set_aio_fd_handler; 224 } 225 226 static const TypeInfo qio_channel_file_info = { 227 .parent = TYPE_QIO_CHANNEL, 228 .name = TYPE_QIO_CHANNEL_FILE, 229 .instance_size = sizeof(QIOChannelFile), 230 .instance_init = qio_channel_file_init, 231 .instance_finalize = qio_channel_file_finalize, 232 .class_init = qio_channel_file_class_init, 233 }; 234 235 static void qio_channel_file_register_types(void) 236 { 237 type_register_static(&qio_channel_file_info); 238 } 239 240 type_init(qio_channel_file_register_types); 241