]> git.proxmox.com Git - mirror_qemu.git/blame - util/osdep.c
Update version for v2.9.0-rc3 release
[mirror_qemu.git] / util / osdep.c
CommitLineData
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 */
aafd7584 24#include "qemu/osdep.h"
f582af58
PB
25
26/* Needed early for CONFIG_BSD etc. */
f582af58 27
dfe5fff3 28#ifdef CONFIG_SOLARIS
605686cd 29#include <sys/statvfs.h>
e78815a5
AF
30/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
31 discussion about Solaris header problems */
32extern int madvise(caddr_t, size_t, int);
605686cd 33#endif
ea88812f 34
511d2b14 35#include "qemu-common.h"
f348b6d1 36#include "qemu/cutils.h"
1de7afc9 37#include "qemu/sockets.h"
d49b6836 38#include "qemu/error-report.h"
83c9089e 39#include "monitor/monitor.h"
03ff3ca3 40
0f66998f
PM
41static bool fips_enabled = false;
42
d494352c 43static const char *hw_version = QEMU_HW_VERSION;
93bfef4c 44
128aa589
PB
45int socket_set_cork(int fd, int v)
46{
47#if defined(SOL_TCP) && defined(TCP_CORK)
4bd1afbd 48 return qemu_setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v));
128aa589
PB
49#else
50 return 0;
51#endif
52}
53
bf1c852a
MK
54int socket_set_nodelay(int fd)
55{
56 int v = 1;
4bd1afbd 57 return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
bf1c852a
MK
58}
59
e78815a5
AF
60int qemu_madvise(void *addr, size_t len, int advice)
61{
62 if (advice == QEMU_MADV_INVALID) {
63 errno = EINVAL;
64 return -1;
65 }
66#if defined(CONFIG_MADVISE)
67 return madvise(addr, len, advice);
68#elif defined(CONFIG_POSIX_MADVISE)
69 return posix_madvise(addr, len, advice);
70#else
71 errno = EINVAL;
72 return -1;
73#endif
74}
75
adb696f3
CB
76#ifndef _WIN32
77/*
78 * Dups an fd and sets the flags
79 */
80static int qemu_dup_flags(int fd, int flags)
81{
82 int ret;
83 int serrno;
84 int dup_flags;
adb696f3 85
761d1ddf 86 ret = qemu_dup(fd);
adb696f3
CB
87 if (ret == -1) {
88 goto fail;
89 }
90
91 dup_flags = fcntl(ret, F_GETFL);
92 if (dup_flags == -1) {
93 goto fail;
94 }
95
96 if ((flags & O_SYNC) != (dup_flags & O_SYNC)) {
97 errno = EINVAL;
98 goto fail;
99 }
100
101 /* Set/unset flags that we can with fcntl */
3b6eda2f 102 if (fcntl(ret, F_SETFL, flags) == -1) {
adb696f3
CB
103 goto fail;
104 }
105
106 /* Truncate the file in the cases that open() would truncate it */
107 if (flags & O_TRUNC ||
108 ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {
109 if (ftruncate(ret, 0) == -1) {
110 goto fail;
111 }
112 }
113
114 return ret;
115
116fail:
117 serrno = errno;
118 if (ret != -1) {
119 close(ret);
120 }
121 errno = serrno;
122 return -1;
123}
0100fbbe 124
761d1ddf
FZ
125int qemu_dup(int fd)
126{
127 int ret;
128#ifdef F_DUPFD_CLOEXEC
129 ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);
130#else
131 ret = dup(fd);
132 if (ret != -1) {
133 qemu_set_cloexec(ret);
134 }
135#endif
136 return ret;
137}
138
0100fbbe
PB
139static int qemu_parse_fdset(const char *param)
140{
141 return qemu_parse_fd(param);
142}
adb696f3 143#endif
03ff3ca3 144
40ff6d7e
KW
145/*
146 * Opens a file with FD_CLOEXEC set
147 */
148int qemu_open(const char *name, int flags, ...)
149{
150 int ret;
151 int mode = 0;
152
adb696f3
CB
153#ifndef _WIN32
154 const char *fdset_id_str;
155
156 /* Attempt dup of fd from fd set */
157 if (strstart(name, "/dev/fdset/", &fdset_id_str)) {
158 int64_t fdset_id;
159 int fd, dupfd;
160
161 fdset_id = qemu_parse_fdset(fdset_id_str);
162 if (fdset_id == -1) {
163 errno = EINVAL;
164 return -1;
165 }
166
167 fd = monitor_fdset_get_fd(fdset_id, flags);
168 if (fd == -1) {
169 return -1;
170 }
171
172 dupfd = qemu_dup_flags(fd, flags);
173 if (dupfd == -1) {
174 return -1;
175 }
176
177 ret = monitor_fdset_dup_fd_add(fdset_id, dupfd);
178 if (ret == -1) {
179 close(dupfd);
180 errno = EINVAL;
181 return -1;
182 }
183
184 return dupfd;
185 }
186#endif
187
40ff6d7e
KW
188 if (flags & O_CREAT) {
189 va_list ap;
190
191 va_start(ap, flags);
192 mode = va_arg(ap, int);
193 va_end(ap);
194 }
195
196#ifdef O_CLOEXEC
197 ret = open(name, flags | O_CLOEXEC, mode);
198#else
199 ret = open(name, flags, mode);
200 if (ret >= 0) {
201 qemu_set_cloexec(ret);
202 }
03ff3ca3 203#endif
40ff6d7e 204
a5813077
SH
205#ifdef O_DIRECT
206 if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
207 error_report("file system may not support O_DIRECT");
208 errno = EINVAL; /* in case it was clobbered */
209 }
210#endif /* O_DIRECT */
211
40ff6d7e
KW
212 return ret;
213}
214
2e1e79da
CB
215int qemu_close(int fd)
216{
adb696f3
CB
217 int64_t fdset_id;
218
219 /* Close fd that was dup'd from an fdset */
220 fdset_id = monitor_fdset_dup_fd_find(fd);
221 if (fdset_id != -1) {
222 int ret;
223
224 ret = close(fd);
225 if (ret == 0) {
226 monitor_fdset_dup_fd_remove(fd);
227 }
228
229 return ret;
230 }
231
2e1e79da
CB
232 return close(fd);
233}
234
7b5f699d
KS
235/*
236 * A variant of write(2) which handles partial write.
237 *
238 * Return the number of bytes transferred.
239 * Set errno if fewer than `count' bytes are written.
1298cb68
JQ
240 *
241 * This function don't work with non-blocking fd's.
242 * Any of the possibilities with non-bloking fd's is bad:
243 * - return a short write (then name is wrong)
244 * - busy wait adding (errno == EAGAIN) to the loop
7b5f699d
KS
245 */
246ssize_t qemu_write_full(int fd, const void *buf, size_t count)
247{
248 ssize_t ret = 0;
249 ssize_t total = 0;
250
251 while (count) {
252 ret = write(fd, buf, count);
253 if (ret < 0) {
254 if (errno == EINTR)
255 continue;
256 break;
257 }
258
259 count -= ret;
260 buf += ret;
261 total += ret;
262 }
263
264 return total;
265}
266
40ff6d7e
KW
267/*
268 * Opens a socket with FD_CLOEXEC set
269 */
270int qemu_socket(int domain, int type, int protocol)
271{
272 int ret;
273
274#ifdef SOCK_CLOEXEC
275 ret = socket(domain, type | SOCK_CLOEXEC, protocol);
3a03bfa5
AP
276 if (ret != -1 || errno != EINVAL) {
277 return ret;
278 }
279#endif
40ff6d7e
KW
280 ret = socket(domain, type, protocol);
281 if (ret >= 0) {
282 qemu_set_cloexec(ret);
283 }
40ff6d7e
KW
284
285 return ret;
286}
287
288/*
289 * Accept a connection and set FD_CLOEXEC
290 */
291int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
292{
293 int ret;
294
295#ifdef CONFIG_ACCEPT4
296 ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
347ed55c 297 if (ret != -1 || errno != ENOSYS) {
3a03bfa5
AP
298 return ret;
299 }
300#endif
40ff6d7e
KW
301 ret = accept(s, addr, addrlen);
302 if (ret >= 0) {
303 qemu_set_cloexec(ret);
304 }
40ff6d7e
KW
305
306 return ret;
307}
993295fe 308
35c2c8dc 309void qemu_set_hw_version(const char *version)
93bfef4c 310{
35c2c8dc 311 hw_version = version;
93bfef4c
CV
312}
313
35c2c8dc 314const char *qemu_hw_version(void)
93bfef4c 315{
35c2c8dc 316 return hw_version;
93bfef4c 317}
0f66998f
PM
318
319void fips_set_state(bool requested)
320{
321#ifdef __linux__
322 if (requested) {
323 FILE *fds = fopen("/proc/sys/crypto/fips_enabled", "r");
324 if (fds != NULL) {
325 fips_enabled = (fgetc(fds) == '1');
326 fclose(fds);
327 }
328 }
329#else
330 fips_enabled = false;
331#endif /* __linux__ */
332
333#ifdef _FIPS_DEBUG
334 fprintf(stderr, "FIPS mode %s (requested %s)\n",
335 (fips_enabled ? "enabled" : "disabled"),
336 (requested ? "enabled" : "disabled"));
337#endif
338}
339
340bool fips_get_state(void)
341{
342 return fips_enabled;
343}
0100fbbe 344
d3bf825e
MAL
345#ifdef _WIN32
346static void socket_cleanup(void)
347{
348 WSACleanup();
349}
350#endif
351
352int socket_init(void)
353{
354#ifdef _WIN32
355 WSADATA Data;
356 int ret, err;
357
358 ret = WSAStartup(MAKEWORD(2, 2), &Data);
359 if (ret != 0) {
360 err = WSAGetLastError();
361 fprintf(stderr, "WSAStartup: %d\n", err);
362 return -1;
363 }
364 atexit(socket_cleanup);
365#endif
366 return 0;
367}
9adea5f7 368
f33cc84d
MT
369#if !GLIB_CHECK_VERSION(2, 31, 0)
370/* Ensure that glib is running in multi-threaded mode
371 * Old versions of glib require explicit initialization. Failure to do
372 * this results in the single-threaded code paths being taken inside
373 * glib. For example, the g_slice allocator will not be thread-safe
374 * and cause crashes.
375 */
ae2990c2
SH
376static void __attribute__((constructor)) thread_init(void)
377{
378 if (!g_thread_supported()) {
f33cc84d 379 g_thread_init(NULL);
ae2990c2
SH
380 }
381}
f33cc84d 382#endif
ae2990c2 383
9adea5f7
PB
384#ifndef CONFIG_IOVEC
385/* helper function for iov_send_recv() */
386static ssize_t
387readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
388{
389 unsigned i = 0;
390 ssize_t ret = 0;
391 while (i < iov_cnt) {
392 ssize_t r = do_write
393 ? write(fd, iov[i].iov_base, iov[i].iov_len)
394 : read(fd, iov[i].iov_base, iov[i].iov_len);
395 if (r > 0) {
396 ret += r;
397 } else if (!r) {
398 break;
399 } else if (errno == EINTR) {
400 continue;
401 } else {
402 /* else it is some "other" error,
403 * only return if there was no data processed. */
404 if (ret == 0) {
405 ret = -1;
406 }
407 break;
408 }
409 i++;
410 }
411 return ret;
412}
413
414ssize_t
415readv(int fd, const struct iovec *iov, int iov_cnt)
416{
417 return readv_writev(fd, iov, iov_cnt, false);
418}
419
420ssize_t
421writev(int fd, const struct iovec *iov, int iov_cnt)
422{
423 return readv_writev(fd, iov, iov_cnt, true);
424}
425#endif