]>
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 | ||
21 | #include "io/channel-file.h" | |
22 | #include "io/channel-watch.h" | |
23 | #include "qemu/sockets.h" | |
24 | #include "trace.h" | |
25 | ||
26 | QIOChannelFile * | |
27 | qio_channel_file_new_fd(int fd) | |
28 | { | |
29 | QIOChannelFile *ioc; | |
30 | ||
31 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); | |
32 | ||
33 | ioc->fd = fd; | |
34 | ||
35 | trace_qio_channel_file_new_fd(ioc, fd); | |
36 | ||
37 | return ioc; | |
38 | } | |
39 | ||
40 | ||
41 | QIOChannelFile * | |
42 | qio_channel_file_new_path(const char *path, | |
43 | int flags, | |
44 | mode_t mode, | |
45 | Error **errp) | |
46 | { | |
47 | QIOChannelFile *ioc; | |
48 | ||
49 | ioc = QIO_CHANNEL_FILE(object_new(TYPE_QIO_CHANNEL_FILE)); | |
50 | ||
51 | if (flags & O_WRONLY) { | |
52 | ioc->fd = open(path, flags, mode); | |
53 | } else { | |
54 | ioc->fd = open(path, flags); | |
55 | } | |
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) { | |
79 | close(ioc->fd); | |
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, | |
90 | Error **errp) | |
91 | { | |
92 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
93 | ssize_t ret; | |
94 | ||
95 | retry: | |
96 | ret = readv(fioc->fd, iov, niov); | |
97 | if (ret < 0) { | |
98 | if (errno == EAGAIN || | |
99 | errno == EWOULDBLOCK) { | |
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, | |
119 | Error **errp) | |
120 | { | |
121 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
122 | ssize_t ret; | |
123 | ||
124 | retry: | |
125 | ret = writev(fioc->fd, iov, niov); | |
126 | if (ret <= 0) { | |
127 | if (errno == EAGAIN || | |
128 | errno == EWOULDBLOCK) { | |
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 | { | |
145 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
146 | ||
147 | if (enabled) { | |
148 | qemu_set_block(fioc->fd); | |
149 | } else { | |
150 | qemu_set_nonblock(fioc->fd); | |
151 | } | |
152 | return 0; | |
153 | } | |
154 | ||
155 | ||
156 | static off_t qio_channel_file_seek(QIOChannel *ioc, | |
157 | off_t offset, | |
158 | int whence, | |
159 | Error **errp) | |
160 | { | |
161 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
162 | off_t ret; | |
163 | ||
164 | ret = lseek(fioc->fd, offset, whence); | |
165 | if (ret == (off_t)-1) { | |
166 | error_setg_errno(errp, errno, | |
167 | "Unable to seek to offset %lld whence %d in file", | |
168 | (long long int)offset, whence); | |
169 | return -1; | |
170 | } | |
171 | return ret; | |
172 | } | |
173 | ||
174 | ||
175 | static int qio_channel_file_close(QIOChannel *ioc, | |
176 | Error **errp) | |
177 | { | |
178 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
179 | ||
180 | if (close(fioc->fd) < 0) { | |
181 | error_setg_errno(errp, errno, | |
182 | "Unable to close file"); | |
183 | return -1; | |
184 | } | |
185 | return 0; | |
186 | } | |
187 | ||
188 | ||
189 | static GSource *qio_channel_file_create_watch(QIOChannel *ioc, | |
190 | GIOCondition condition) | |
191 | { | |
192 | QIOChannelFile *fioc = QIO_CHANNEL_FILE(ioc); | |
193 | return qio_channel_create_fd_watch(ioc, | |
194 | fioc->fd, | |
195 | condition); | |
196 | } | |
197 | ||
198 | static void qio_channel_file_class_init(ObjectClass *klass, | |
199 | void *class_data G_GNUC_UNUSED) | |
200 | { | |
201 | QIOChannelClass *ioc_klass = QIO_CHANNEL_CLASS(klass); | |
202 | ||
203 | ioc_klass->io_writev = qio_channel_file_writev; | |
204 | ioc_klass->io_readv = qio_channel_file_readv; | |
205 | ioc_klass->io_set_blocking = qio_channel_file_set_blocking; | |
206 | ioc_klass->io_seek = qio_channel_file_seek; | |
207 | ioc_klass->io_close = qio_channel_file_close; | |
208 | ioc_klass->io_create_watch = qio_channel_file_create_watch; | |
209 | } | |
210 | ||
211 | static const TypeInfo qio_channel_file_info = { | |
212 | .parent = TYPE_QIO_CHANNEL, | |
213 | .name = TYPE_QIO_CHANNEL_FILE, | |
214 | .instance_size = sizeof(QIOChannelFile), | |
215 | .instance_init = qio_channel_file_init, | |
216 | .instance_finalize = qio_channel_file_finalize, | |
217 | .class_init = qio_channel_file_class_init, | |
218 | }; | |
219 | ||
220 | static void qio_channel_file_register_types(void) | |
221 | { | |
222 | type_register_static(&qio_channel_file_info); | |
223 | } | |
224 | ||
225 | type_init(qio_channel_file_register_types); |