]>
Commit | Line | Data |
---|---|---|
d6e48869 DB |
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 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 | ||
cae9fc56 | 21 | #include "qemu/osdep.h" |
d6e48869 DB |
22 | #include "io/channel-file.h" |
23 | #include "io/channel-watch.h" | |
24 | #include "qemu/sockets.h" | |
25 | #include "trace.h" | |
26 | ||
27 | QIOChannelFile * | |
28 | qio_channel_file_new_fd(int fd) | |
29 | { | |
30 | QIOChannelFile *ioc; | |
31 | ||
32 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); | |
33 | ||
34 | ioc->fd = fd; | |
35 | ||
36 | trace_qio_channel_file_new_fd(ioc, fd); | |
37 | ||
38 | return ioc; | |
39 | } | |
40 | ||
41 | ||
42 | QIOChannelFile * | |
43 | qio_channel_file_new_path(const char *path, | |
44 | int flags, | |
45 | mode_t mode, | |
46 | Error **errp) | |
47 | { | |
48 | QIOChannelFile *ioc; | |
49 | ||
50 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); | |
51 | ||
52 | if (flags & O_WRONLY) { | |
53 | ioc->fd = open(path, flags, mode); | |
54 | } else { | |
55 | ioc->fd = open(path, flags); | |
56 | } | |
57 | if (ioc->fd < 0) { | |
58 | object_unref(OBJECT(ioc)); | |
59 | error_setg_errno(errp, errno, | |
60 | "Unable to open %s", path); | |
61 | return NULL; | |
62 | } | |
63 | ||
64 | trace_qio_channel_file_new_path(ioc, path, flags, mode, ioc->fd); | |
65 | ||
66 | return ioc; | |
67 | } | |
68 | ||
69 | ||
70 | static void qio_channel_file_init(Object *obj) | |
71 | { | |
72 | QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); | |
73 | ioc->fd = -1; | |
74 | } | |
75 | ||
76 | static void qio_channel_file_finalize(Object *obj) | |
77 | { | |
78 | QIOChannelFile *ioc = QIO_CHANNEL_FILE(obj); | |
79 | if (ioc->fd != -1) { | |
80 | close(ioc->fd); | |
81 | ioc->fd = -1; | |
82 | } | |
83 | } | |
84 | ||
85 | ||
86 | static ssize_t qio_channel_file_readv(QIOChannel *ioc, | |
87 | const struct iovec *iov, | |
88 | size_t niov, | |
89 | int **fds, | |
90 | size_t *nfds, | |
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) { | |
99 | if (errno == EAGAIN || | |
100 | errno == EWOULDBLOCK) { | |
101 | return QIO_CHANNEL_ERR_BLOCK; | |
102 | } | |
103 | if (errno == EINTR) { | |
104 | goto retry; | |
105 | } | |
106 | ||
107 | error_setg_errno(errp, errno, | |
108 | "Unable to read from file"); | |
109 | return -1; | |
110 | } | |
111 | ||
112 | return ret; | |
113 | } | |
114 | ||
115 | static ssize_t qio_channel_file_writev(QIOChannel *ioc, | |
116 | const struct iovec *iov, | |
117 | size_t niov, | |
118 | int *fds, | |
119 | size_t nfds, | |
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) { | |
128 | if (errno == EAGAIN || | |
129 | errno == EWOULDBLOCK) { | |
130 | return QIO_CHANNEL_ERR_BLOCK; | |
131 | } | |
132 | if (errno == EINTR) { | |
133 | goto retry; | |
134 | } | |
135 | error_setg_errno(errp, errno, | |
136 | "Unable to write to file"); | |
137 | return -1; | |
138 | } | |
139 | return ret; | |
140 | } | |
141 | ||
142 | static int qio_channel_file_set_blocking(QIOChannel *ioc, | |
143 | bool enabled, | |
144 | Error **errp) | |
145 | { | |
146 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
147 | ||
148 | if (enabled) { | |
149 | qemu_set_block(fioc->fd); | |
150 | } else { | |
151 | qemu_set_nonblock(fioc->fd); | |
152 | } | |
153 | return 0; | |
154 | } | |
155 | ||
156 | ||
157 | static off_t qio_channel_file_seek(QIOChannel *ioc, | |
158 | off_t offset, | |
159 | int whence, | |
160 | Error **errp) | |
161 | { | |
162 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
163 | off_t ret; | |
164 | ||
165 | ret = lseek(fioc->fd, offset, whence); | |
166 | if (ret == (off_t)-1) { | |
167 | error_setg_errno(errp, errno, | |
168 | "Unable to seek to offset %lld whence %d in file", | |
169 | (long long int)offset, whence); | |
170 | return -1; | |
171 | } | |
172 | return ret; | |
173 | } | |
174 | ||
175 | ||
176 | static int qio_channel_file_close(QIOChannel *ioc, | |
177 | Error **errp) | |
178 | { | |
179 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
180 | ||
181 | if (close(fioc->fd) < 0) { | |
182 | error_setg_errno(errp, errno, | |
183 | "Unable to close file"); | |
184 | return -1; | |
185 | } | |
186 | return 0; | |
187 | } | |
188 | ||
189 | ||
190 | static GSource *qio_channel_file_create_watch(QIOChannel *ioc, | |
191 | GIOCondition condition) | |
192 | { | |
193 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
194 | return qio_channel_create_fd_watch(ioc, | |
195 | fioc->fd, | |
196 | condition); | |
197 | } | |
198 | ||
199 | static void qio_channel_file_class_init(ObjectClass *klass, | |
200 | void *class_data G_GNUC_UNUSED) | |
201 | { | |
202 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); | |
203 | ||
204 | ioc_klass->io_writev = qio_channel_file_writev; | |
205 | ioc_klass->io_readv = qio_channel_file_readv; | |
206 | ioc_klass->io_set_blocking = qio_channel_file_set_blocking; | |
207 | ioc_klass->io_seek = qio_channel_file_seek; | |
208 | ioc_klass->io_close = qio_channel_file_close; | |
209 | ioc_klass->io_create_watch = qio_channel_file_create_watch; | |
210 | } | |
211 | ||
212 | static const TypeInfo qio_channel_file_info = { | |
213 | .parent = TYPE_QIO_CHANNEL, | |
214 | .name = TYPE_QIO_CHANNEL_FILE, | |
215 | .instance_size = sizeof(QIOChannelFile), | |
216 | .instance_init = qio_channel_file_init, | |
217 | .instance_finalize = qio_channel_file_finalize, | |
218 | .class_init = qio_channel_file_class_init, | |
219 | }; | |
220 | ||
221 | static void qio_channel_file_register_types(void) | |
222 | { | |
223 | type_register_static(&qio_channel_file_info); | |
224 | } | |
225 | ||
226 | type_init(qio_channel_file_register_types); |