baum: use a common prefix for chr callbacks
[qemu/kevin.git] / util / osdep.c
blob06fb1cfda6ed38ff11820dea7c167ca0ed3177d6
1 /*
2 * QEMU low level functions
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
24 #include "qemu/osdep.h"
26 /* Needed early for CONFIG_BSD etc. */
28 #ifdef CONFIG_SOLARIS
29 #include <sys/statvfs.h>
30 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
31 discussion about Solaris header problems */
32 extern int madvise(caddr_t, size_t, int);
33 #endif
35 #include "qemu-common.h"
36 #include "qemu/cutils.h"
37 #include "qemu/sockets.h"
38 #include "qemu/error-report.h"
39 #include "monitor/monitor.h"
41 static bool fips_enabled = false;
43 static const char *hw_version = QEMU_HW_VERSION;
45 int socket_set_cork(int fd, int v)
47 #if defined(SOL_TCP) && defined(TCP_CORK)
48 return qemu_setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v));
49 #else
50 return 0;
51 #endif
54 int socket_set_nodelay(int fd)
56 int v = 1;
57 return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
60 int qemu_madvise(void *addr, size_t len, int advice)
62 if (advice == QEMU_MADV_INVALID) {
63 errno = EINVAL;
64 return -1;
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
76 #ifndef _WIN32
78 * Dups an fd and sets the flags
80 static int qemu_dup_flags(int fd, int flags)
82 int ret;
83 int serrno;
84 int dup_flags;
86 ret = qemu_dup(fd);
87 if (ret == -1) {
88 goto fail;
91 dup_flags = fcntl(ret, F_GETFL);
92 if (dup_flags == -1) {
93 goto fail;
96 if ((flags & O_SYNC) != (dup_flags & O_SYNC)) {
97 errno = EINVAL;
98 goto fail;
101 /* Set/unset flags that we can with fcntl */
102 if (fcntl(ret, F_SETFL, flags) == -1) {
103 goto fail;
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;
114 return ret;
116 fail:
117 serrno = errno;
118 if (ret != -1) {
119 close(ret);
121 errno = serrno;
122 return -1;
125 int qemu_dup(int fd)
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);
135 #endif
136 return ret;
139 static int qemu_parse_fdset(const char *param)
141 return qemu_parse_fd(param);
143 #endif
146 * Opens a file with FD_CLOEXEC set
148 int qemu_open(const char *name, int flags, ...)
150 int ret;
151 int mode = 0;
153 #ifndef _WIN32
154 const char *fdset_id_str;
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;
161 fdset_id = qemu_parse_fdset(fdset_id_str);
162 if (fdset_id == -1) {
163 errno = EINVAL;
164 return -1;
167 fd = monitor_fdset_get_fd(fdset_id, flags);
168 if (fd == -1) {
169 return -1;
172 dupfd = qemu_dup_flags(fd, flags);
173 if (dupfd == -1) {
174 return -1;
177 ret = monitor_fdset_dup_fd_add(fdset_id, dupfd);
178 if (ret == -1) {
179 close(dupfd);
180 errno = EINVAL;
181 return -1;
184 return dupfd;
186 #endif
188 if (flags & O_CREAT) {
189 va_list ap;
191 va_start(ap, flags);
192 mode = va_arg(ap, int);
193 va_end(ap);
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);
203 #endif
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 */
210 #endif /* O_DIRECT */
212 return ret;
215 int qemu_close(int fd)
217 int64_t fdset_id;
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;
224 ret = close(fd);
225 if (ret == 0) {
226 monitor_fdset_dup_fd_remove(fd);
229 return ret;
232 return close(fd);
236 * A variant of write(2) which handles partial write.
238 * Return the number of bytes transferred.
239 * Set errno if fewer than `count' bytes are written.
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
246 ssize_t qemu_write_full(int fd, const void *buf, size_t count)
248 ssize_t ret = 0;
249 ssize_t total = 0;
251 while (count) {
252 ret = write(fd, buf, count);
253 if (ret < 0) {
254 if (errno == EINTR)
255 continue;
256 break;
259 count -= ret;
260 buf += ret;
261 total += ret;
264 return total;
268 * Opens a socket with FD_CLOEXEC set
270 int qemu_socket(int domain, int type, int protocol)
272 int ret;
274 #ifdef SOCK_CLOEXEC
275 ret = socket(domain, type | SOCK_CLOEXEC, protocol);
276 if (ret != -1 || errno != EINVAL) {
277 return ret;
279 #endif
280 ret = socket(domain, type, protocol);
281 if (ret >= 0) {
282 qemu_set_cloexec(ret);
285 return ret;
289 * Accept a connection and set FD_CLOEXEC
291 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
293 int ret;
295 #ifdef CONFIG_ACCEPT4
296 ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
297 if (ret != -1 || errno != ENOSYS) {
298 return ret;
300 #endif
301 ret = accept(s, addr, addrlen);
302 if (ret >= 0) {
303 qemu_set_cloexec(ret);
306 return ret;
309 void qemu_set_hw_version(const char *version)
311 hw_version = version;
314 const char *qemu_hw_version(void)
316 return hw_version;
319 void fips_set_state(bool requested)
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);
329 #else
330 fips_enabled = false;
331 #endif /* __linux__ */
333 #ifdef _FIPS_DEBUG
334 fprintf(stderr, "FIPS mode %s (requested %s)\n",
335 (fips_enabled ? "enabled" : "disabled"),
336 (requested ? "enabled" : "disabled"));
337 #endif
340 bool fips_get_state(void)
342 return fips_enabled;
345 #ifdef _WIN32
346 static void socket_cleanup(void)
348 WSACleanup();
350 #endif
352 int socket_init(void)
354 #ifdef _WIN32
355 WSADATA Data;
356 int ret, err;
358 ret = WSAStartup(MAKEWORD(2, 2), &Data);
359 if (ret != 0) {
360 err = WSAGetLastError();
361 fprintf(stderr, "WSAStartup: %d\n", err);
362 return -1;
364 atexit(socket_cleanup);
365 #endif
366 return 0;
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.
376 static void __attribute__((constructor)) thread_init(void)
378 if (!g_thread_supported()) {
379 g_thread_init(NULL);
382 #endif
384 #ifndef CONFIG_IOVEC
385 /* helper function for iov_send_recv() */
386 static ssize_t
387 readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
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;
407 break;
409 i++;
411 return ret;
414 ssize_t
415 readv(int fd, const struct iovec *iov, int iov_cnt)
417 return readv_writev(fd, iov, iov_cnt, false);
420 ssize_t
421 writev(int fd, const struct iovec *iov, int iov_cnt)
423 return readv_writev(fd, iov, iov_cnt, true);
425 #endif