]>
Commit | Line | Data |
---|---|---|
ea88812f FB |
1 | /* |
2 | * QEMU low level functions | |
5fafdf24 | 3 | * |
ea88812f | 4 | * Copyright (c) 2003 Fabrice Bellard |
5fafdf24 | 5 | * |
ea88812f FB |
6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
7 | * of this software and associated documentation files (the "Software"), to deal | |
8 | * in the Software without restriction, including without limitation the rights | |
9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
10 | * copies of the Software, and to permit persons to whom the Software is | |
11 | * furnished to do so, subject to the following conditions: | |
12 | * | |
13 | * The above copyright notice and this permission notice shall be included in | |
14 | * all copies or substantial portions of the Software. | |
15 | * | |
16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | |
19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
22 | * THE SOFTWARE. | |
23 | */ | |
24 | #include <stdlib.h> | |
25 | #include <stdio.h> | |
26 | #include <stdarg.h> | |
0f66998f | 27 | #include <stdbool.h> |
ea88812f | 28 | #include <string.h> |
ea88812f FB |
29 | #include <errno.h> |
30 | #include <unistd.h> | |
aa26bb2d | 31 | #include <fcntl.h> |
f582af58 PB |
32 | |
33 | /* Needed early for CONFIG_BSD etc. */ | |
34 | #include "config-host.h" | |
35 | ||
e78815a5 AF |
36 | #if defined(CONFIG_MADVISE) || defined(CONFIG_POSIX_MADVISE) |
37 | #include <sys/mman.h> | |
38 | #endif | |
39 | ||
dfe5fff3 | 40 | #ifdef CONFIG_SOLARIS |
605686cd TS |
41 | #include <sys/types.h> |
42 | #include <sys/statvfs.h> | |
e78815a5 AF |
43 | /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for |
44 | discussion about Solaris header problems */ | |
45 | extern int madvise(caddr_t, size_t, int); | |
605686cd | 46 | #endif |
ea88812f | 47 | |
511d2b14 | 48 | #include "qemu-common.h" |
cd245a19 | 49 | #include "trace.h" |
03ff3ca3 | 50 | #include "qemu_socket.h" |
adb696f3 | 51 | #include "monitor.h" |
03ff3ca3 | 52 | |
0f66998f PM |
53 | static bool fips_enabled = false; |
54 | ||
93bfef4c CV |
55 | static const char *qemu_version = QEMU_VERSION; |
56 | ||
128aa589 PB |
57 | int socket_set_cork(int fd, int v) |
58 | { | |
59 | #if defined(SOL_TCP) && defined(TCP_CORK) | |
60 | return setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v)); | |
61 | #else | |
62 | return 0; | |
63 | #endif | |
64 | } | |
65 | ||
e78815a5 AF |
66 | int qemu_madvise(void *addr, size_t len, int advice) |
67 | { | |
68 | if (advice == QEMU_MADV_INVALID) { | |
69 | errno = EINVAL; | |
70 | return -1; | |
71 | } | |
72 | #if defined(CONFIG_MADVISE) | |
73 | return madvise(addr, len, advice); | |
74 | #elif defined(CONFIG_POSIX_MADVISE) | |
75 | return posix_madvise(addr, len, advice); | |
76 | #else | |
77 | errno = EINVAL; | |
78 | return -1; | |
79 | #endif | |
80 | } | |
81 | ||
adb696f3 CB |
82 | #ifndef _WIN32 |
83 | /* | |
84 | * Dups an fd and sets the flags | |
85 | */ | |
86 | static int qemu_dup_flags(int fd, int flags) | |
87 | { | |
88 | int ret; | |
89 | int serrno; | |
90 | int dup_flags; | |
91 | int setfl_flags; | |
92 | ||
93 | #ifdef F_DUPFD_CLOEXEC | |
94 | ret = fcntl(fd, F_DUPFD_CLOEXEC, 0); | |
95 | #else | |
96 | ret = dup(fd); | |
97 | if (ret != -1) { | |
98 | qemu_set_cloexec(ret); | |
99 | } | |
100 | #endif | |
101 | if (ret == -1) { | |
102 | goto fail; | |
103 | } | |
104 | ||
105 | dup_flags = fcntl(ret, F_GETFL); | |
106 | if (dup_flags == -1) { | |
107 | goto fail; | |
108 | } | |
109 | ||
110 | if ((flags & O_SYNC) != (dup_flags & O_SYNC)) { | |
111 | errno = EINVAL; | |
112 | goto fail; | |
113 | } | |
114 | ||
115 | /* Set/unset flags that we can with fcntl */ | |
116 | setfl_flags = O_APPEND | O_ASYNC | O_DIRECT | O_NOATIME | O_NONBLOCK; | |
117 | dup_flags &= ~setfl_flags; | |
118 | dup_flags |= (flags & setfl_flags); | |
119 | if (fcntl(ret, F_SETFL, dup_flags) == -1) { | |
120 | goto fail; | |
121 | } | |
122 | ||
123 | /* Truncate the file in the cases that open() would truncate it */ | |
124 | if (flags & O_TRUNC || | |
125 | ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) { | |
126 | if (ftruncate(ret, 0) == -1) { | |
127 | goto fail; | |
128 | } | |
129 | } | |
130 | ||
131 | return ret; | |
132 | ||
133 | fail: | |
134 | serrno = errno; | |
135 | if (ret != -1) { | |
136 | close(ret); | |
137 | } | |
138 | errno = serrno; | |
139 | return -1; | |
140 | } | |
141 | #endif | |
03ff3ca3 | 142 | |
40ff6d7e KW |
143 | /* |
144 | * Opens a file with FD_CLOEXEC set | |
145 | */ | |
146 | int qemu_open(const char *name, int flags, ...) | |
147 | { | |
148 | int ret; | |
149 | int mode = 0; | |
150 | ||
adb696f3 CB |
151 | #ifndef _WIN32 |
152 | const char *fdset_id_str; | |
153 | ||
154 | /* Attempt dup of fd from fd set */ | |
155 | if (strstart(name, "/dev/fdset/", &fdset_id_str)) { | |
156 | int64_t fdset_id; | |
157 | int fd, dupfd; | |
158 | ||
159 | fdset_id = qemu_parse_fdset(fdset_id_str); | |
160 | if (fdset_id == -1) { | |
161 | errno = EINVAL; | |
162 | return -1; | |
163 | } | |
164 | ||
165 | fd = monitor_fdset_get_fd(fdset_id, flags); | |
166 | if (fd == -1) { | |
167 | return -1; | |
168 | } | |
169 | ||
170 | dupfd = qemu_dup_flags(fd, flags); | |
171 | if (dupfd == -1) { | |
172 | return -1; | |
173 | } | |
174 | ||
175 | ret = monitor_fdset_dup_fd_add(fdset_id, dupfd); | |
176 | if (ret == -1) { | |
177 | close(dupfd); | |
178 | errno = EINVAL; | |
179 | return -1; | |
180 | } | |
181 | ||
182 | return dupfd; | |
183 | } | |
184 | #endif | |
185 | ||
40ff6d7e KW |
186 | if (flags & O_CREAT) { |
187 | va_list ap; | |
188 | ||
189 | va_start(ap, flags); | |
190 | mode = va_arg(ap, int); | |
191 | va_end(ap); | |
192 | } | |
193 | ||
194 | #ifdef O_CLOEXEC | |
195 | ret = open(name, flags | O_CLOEXEC, mode); | |
196 | #else | |
197 | ret = open(name, flags, mode); | |
198 | if (ret >= 0) { | |
199 | qemu_set_cloexec(ret); | |
200 | } | |
03ff3ca3 | 201 | #endif |
40ff6d7e KW |
202 | |
203 | return ret; | |
204 | } | |
205 | ||
2e1e79da CB |
206 | int qemu_close(int fd) |
207 | { | |
adb696f3 CB |
208 | int64_t fdset_id; |
209 | ||
210 | /* Close fd that was dup'd from an fdset */ | |
211 | fdset_id = monitor_fdset_dup_fd_find(fd); | |
212 | if (fdset_id != -1) { | |
213 | int ret; | |
214 | ||
215 | ret = close(fd); | |
216 | if (ret == 0) { | |
217 | monitor_fdset_dup_fd_remove(fd); | |
218 | } | |
219 | ||
220 | return ret; | |
221 | } | |
222 | ||
2e1e79da CB |
223 | return close(fd); |
224 | } | |
225 | ||
7b5f699d KS |
226 | /* |
227 | * A variant of write(2) which handles partial write. | |
228 | * | |
229 | * Return the number of bytes transferred. | |
230 | * Set errno if fewer than `count' bytes are written. | |
1298cb68 JQ |
231 | * |
232 | * This function don't work with non-blocking fd's. | |
233 | * Any of the possibilities with non-bloking fd's is bad: | |
234 | * - return a short write (then name is wrong) | |
235 | * - busy wait adding (errno == EAGAIN) to the loop | |
7b5f699d KS |
236 | */ |
237 | ssize_t qemu_write_full(int fd, const void *buf, size_t count) | |
238 | { | |
239 | ssize_t ret = 0; | |
240 | ssize_t total = 0; | |
241 | ||
242 | while (count) { | |
243 | ret = write(fd, buf, count); | |
244 | if (ret < 0) { | |
245 | if (errno == EINTR) | |
246 | continue; | |
247 | break; | |
248 | } | |
249 | ||
250 | count -= ret; | |
251 | buf += ret; | |
252 | total += ret; | |
253 | } | |
254 | ||
255 | return total; | |
256 | } | |
257 | ||
40ff6d7e KW |
258 | /* |
259 | * Opens a socket with FD_CLOEXEC set | |
260 | */ | |
261 | int qemu_socket(int domain, int type, int protocol) | |
262 | { | |
263 | int ret; | |
264 | ||
265 | #ifdef SOCK_CLOEXEC | |
266 | ret = socket(domain, type | SOCK_CLOEXEC, protocol); | |
3a03bfa5 AP |
267 | if (ret != -1 || errno != EINVAL) { |
268 | return ret; | |
269 | } | |
270 | #endif | |
40ff6d7e KW |
271 | ret = socket(domain, type, protocol); |
272 | if (ret >= 0) { | |
273 | qemu_set_cloexec(ret); | |
274 | } | |
40ff6d7e KW |
275 | |
276 | return ret; | |
277 | } | |
278 | ||
279 | /* | |
280 | * Accept a connection and set FD_CLOEXEC | |
281 | */ | |
282 | int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen) | |
283 | { | |
284 | int ret; | |
285 | ||
286 | #ifdef CONFIG_ACCEPT4 | |
287 | ret = accept4(s, addr, addrlen, SOCK_CLOEXEC); | |
347ed55c | 288 | if (ret != -1 || errno != ENOSYS) { |
3a03bfa5 AP |
289 | return ret; |
290 | } | |
291 | #endif | |
40ff6d7e KW |
292 | ret = accept(s, addr, addrlen); |
293 | if (ret >= 0) { | |
294 | qemu_set_cloexec(ret); | |
295 | } | |
40ff6d7e KW |
296 | |
297 | return ret; | |
298 | } | |
993295fe PB |
299 | |
300 | /* | |
301 | * A variant of send(2) which handles partial write. | |
302 | * | |
303 | * Return the number of bytes transferred, which is only | |
304 | * smaller than `count' if there is an error. | |
305 | * | |
306 | * This function won't work with non-blocking fd's. | |
307 | * Any of the possibilities with non-bloking fd's is bad: | |
308 | * - return a short write (then name is wrong) | |
309 | * - busy wait adding (errno == EAGAIN) to the loop | |
310 | */ | |
311 | ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags) | |
312 | { | |
313 | ssize_t ret = 0; | |
314 | ssize_t total = 0; | |
315 | ||
316 | while (count) { | |
317 | ret = send(fd, buf, count, flags); | |
318 | if (ret < 0) { | |
319 | if (errno == EINTR) { | |
320 | continue; | |
321 | } | |
322 | break; | |
323 | } | |
324 | ||
325 | count -= ret; | |
326 | buf += ret; | |
327 | total += ret; | |
328 | } | |
329 | ||
330 | return total; | |
331 | } | |
332 | ||
333 | /* | |
334 | * A variant of recv(2) which handles partial write. | |
335 | * | |
336 | * Return the number of bytes transferred, which is only | |
337 | * smaller than `count' if there is an error. | |
338 | * | |
339 | * This function won't work with non-blocking fd's. | |
340 | * Any of the possibilities with non-bloking fd's is bad: | |
341 | * - return a short write (then name is wrong) | |
342 | * - busy wait adding (errno == EAGAIN) to the loop | |
343 | */ | |
344 | ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags) | |
345 | { | |
346 | ssize_t ret = 0; | |
347 | ssize_t total = 0; | |
348 | ||
349 | while (count) { | |
350 | ret = qemu_recv(fd, buf, count, flags); | |
351 | if (ret <= 0) { | |
352 | if (ret < 0 && errno == EINTR) { | |
353 | continue; | |
354 | } | |
355 | break; | |
356 | } | |
357 | ||
358 | count -= ret; | |
359 | buf += ret; | |
360 | total += ret; | |
361 | } | |
362 | ||
363 | return total; | |
364 | } | |
365 | ||
93bfef4c CV |
366 | void qemu_set_version(const char *version) |
367 | { | |
368 | qemu_version = version; | |
369 | } | |
370 | ||
371 | const char *qemu_get_version(void) | |
372 | { | |
373 | return qemu_version; | |
374 | } | |
0f66998f PM |
375 | |
376 | void fips_set_state(bool requested) | |
377 | { | |
378 | #ifdef __linux__ | |
379 | if (requested) { | |
380 | FILE *fds = fopen("/proc/sys/crypto/fips_enabled", "r"); | |
381 | if (fds != NULL) { | |
382 | fips_enabled = (fgetc(fds) == '1'); | |
383 | fclose(fds); | |
384 | } | |
385 | } | |
386 | #else | |
387 | fips_enabled = false; | |
388 | #endif /* __linux__ */ | |
389 | ||
390 | #ifdef _FIPS_DEBUG | |
391 | fprintf(stderr, "FIPS mode %s (requested %s)\n", | |
392 | (fips_enabled ? "enabled" : "disabled"), | |
393 | (requested ? "enabled" : "disabled")); | |
394 | #endif | |
395 | } | |
396 | ||
397 | bool fips_get_state(void) | |
398 | { | |
399 | return fips_enabled; | |
400 | } |