1/*
2 * QEMU low level functions
3 *
4 * Copyright (c) 2003 Fabrice Bellard
5 *
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>
27#include <stdbool.h>
28#include <string.h>
29#include <errno.h>
30#include <unistd.h>
31#include <fcntl.h>
32
33#ifndef _WIN32
34#include <sys/socket.h>
35#endif
36
37/* Needed early for CONFIG_BSD etc. */
38#include "config-host.h"
39
40#if defined(CONFIG_MADVISE) || defined(CONFIG_POSIX_MADVISE)
41#include <sys/mman.h>
42#endif
43
44#ifdef CONFIG_SOLARIS
45#include <sys/types.h>
46#include <sys/statvfs.h>
47/* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for
48   discussion about Solaris header problems */
49extern int madvise(caddr_t, size_t, int);
50#endif
51
52#include "qemu-common.h"
53#include "trace.h"
54#include "qemu/sockets.h"
55#include "monitor/monitor.h"
56
57static bool fips_enabled = false;
58
59static const char *qemu_version = QEMU_VERSION;
60
61#ifndef CONFIG_ANDROID  // See android/sockets.c instead.
62int socket_set_cork(int fd, int v)
63{
64#if defined(SOL_TCP) && defined(TCP_CORK)
65    return qemu_setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v));
66#else
67    return 0;
68#endif
69}
70
71int socket_set_nodelay(int fd)
72{
73    int v = 1;
74    return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
75}
76#endif  // !CONFIG_ANDROID
77
78int qemu_madvise(void *addr, size_t len, int advice)
79{
80    if (advice == QEMU_MADV_INVALID) {
81        errno = EINVAL;
82        return -1;
83    }
84#if defined(CONFIG_MADVISE)
85    return madvise(addr, len, advice);
86#elif defined(CONFIG_POSIX_MADVISE)
87    return posix_madvise(addr, len, advice);
88#else
89    errno = EINVAL;
90    return -1;
91#endif
92}
93
94#ifndef CONFIG_ANDROID
95#ifndef _WIN32
96/*
97 * Dups an fd and sets the flags
98 */
99static int qemu_dup_flags(int fd, int flags)
100{
101    int ret;
102    int serrno;
103    int dup_flags;
104
105#ifdef F_DUPFD_CLOEXEC
106    ret = fcntl(fd, F_DUPFD_CLOEXEC, 0);
107#else
108    ret = dup(fd);
109    if (ret != -1) {
110        qemu_set_cloexec(ret);
111    }
112#endif
113    if (ret == -1) {
114        goto fail;
115    }
116
117    dup_flags = fcntl(ret, F_GETFL);
118    if (dup_flags == -1) {
119        goto fail;
120    }
121
122    if ((flags & O_SYNC) != (dup_flags & O_SYNC)) {
123        errno = EINVAL;
124        goto fail;
125    }
126
127    /* Set/unset flags that we can with fcntl */
128    if (fcntl(ret, F_SETFL, flags) == -1) {
129        goto fail;
130    }
131
132    /* Truncate the file in the cases that open() would truncate it */
133    if (flags & O_TRUNC ||
134            ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) {
135        if (ftruncate(ret, 0) == -1) {
136            goto fail;
137        }
138    }
139
140    return ret;
141
142fail:
143    serrno = errno;
144    if (ret != -1) {
145        close(ret);
146    }
147    errno = serrno;
148    return -1;
149}
150
151static int qemu_parse_fdset(const char *param)
152{
153    return qemu_parse_fd(param);
154}
155#endif
156#endif  // !CONFIG_ANDROID
157
158/*
159 * Opens a file with FD_CLOEXEC set
160 */
161int qemu_open(const char *name, int flags, ...)
162{
163    int ret;
164    int mode = 0;
165
166#ifndef CONFIG_ANDROID
167#ifndef _WIN32
168    const char *fdset_id_str;
169
170    /* Attempt dup of fd from fd set */
171    if (strstart(name, "/dev/fdset/", &fdset_id_str)) {
172        int64_t fdset_id;
173        int fd, dupfd;
174
175        fdset_id = qemu_parse_fdset(fdset_id_str);
176        if (fdset_id == -1) {
177            errno = EINVAL;
178            return -1;
179        }
180
181        fd = monitor_fdset_get_fd(fdset_id, flags);
182        if (fd == -1) {
183            return -1;
184        }
185
186        dupfd = qemu_dup_flags(fd, flags);
187        if (dupfd == -1) {
188            return -1;
189        }
190
191        ret = monitor_fdset_dup_fd_add(fdset_id, dupfd);
192        if (ret == -1) {
193            close(dupfd);
194            errno = EINVAL;
195            return -1;
196        }
197        return dupfd;
198    }
199#endif
200#endif  // !CONFIG_ANDROID
201
202    if (flags & O_CREAT) {
203        va_list ap;
204
205        va_start(ap, flags);
206        mode = va_arg(ap, int);
207        va_end(ap);
208    }
209
210#ifdef O_CLOEXEC
211    ret = open(name, flags | O_CLOEXEC, mode);
212#else
213    ret = open(name, flags, mode);
214    if (ret >= 0) {
215        qemu_set_cloexec(ret);
216    }
217#endif
218
219#ifdef O_DIRECT
220    if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) {
221        error_report("file system may not support O_DIRECT");
222        errno = EINVAL; /* in case it was clobbered */
223    }
224#endif /* O_DIRECT */
225
226    return ret;
227}
228
229int qemu_close(int fd)
230{
231#ifndef CONFIG_ANDROID
232    int64_t fdset_id;
233
234    /* Close fd that was dup'd from an fdset */
235    fdset_id = monitor_fdset_dup_fd_find(fd);
236    if (fdset_id != -1) {
237        int ret;
238
239        ret = close(fd);
240        if (ret == 0) {
241            monitor_fdset_dup_fd_remove(fd);
242        }
243
244        return ret;
245    }
246#endif
247    return close(fd);
248}
249
250/*
251 * A variant of write(2) which handles partial write.
252 *
253 * Return the number of bytes transferred.
254 * Set errno if fewer than `count' bytes are written.
255 *
256 * This function don't work with non-blocking fd's.
257 * Any of the possibilities with non-bloking fd's is bad:
258 *   - return a short write (then name is wrong)
259 *   - busy wait adding (errno == EAGAIN) to the loop
260 */
261ssize_t qemu_write_full(int fd, const void *buf, size_t count)
262{
263    ssize_t ret = 0;
264    ssize_t total = 0;
265
266    while (count) {
267        ret = write(fd, buf, count);
268        if (ret < 0) {
269            if (errno == EINTR)
270                continue;
271            break;
272        }
273
274        count -= ret;
275        buf += ret;
276        total += ret;
277    }
278
279    return total;
280}
281
282/*
283 * Opens a socket with FD_CLOEXEC set
284 */
285int qemu_socket(int domain, int type, int protocol)
286{
287    int ret;
288
289#ifdef SOCK_CLOEXEC
290    ret = socket(domain, type | SOCK_CLOEXEC, protocol);
291    if (ret != -1 || errno != EINVAL) {
292        return ret;
293    }
294#endif
295    ret = socket(domain, type, protocol);
296    if (ret >= 0) {
297        qemu_set_cloexec(ret);
298    }
299
300    return ret;
301}
302
303#ifndef CONFIG_ANDROID
304/*
305 * Accept a connection and set FD_CLOEXEC
306 */
307int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen)
308{
309    int ret;
310
311#ifdef CONFIG_ACCEPT4
312    ret = accept4(s, addr, addrlen, SOCK_CLOEXEC);
313    if (ret != -1 || errno != ENOSYS) {
314        return ret;
315    }
316#endif
317    ret = accept(s, addr, addrlen);
318    if (ret >= 0) {
319        qemu_set_cloexec(ret);
320    }
321
322    return ret;
323}
324#endif
325
326/*
327 * A variant of send(2) which handles partial write.
328 *
329 * Return the number of bytes transferred, which is only
330 * smaller than `count' if there is an error.
331 *
332 * This function won't work with non-blocking fd's.
333 * Any of the possibilities with non-bloking fd's is bad:
334 *   - return a short write (then name is wrong)
335 *   - busy wait adding (errno == EAGAIN) to the loop
336 */
337ssize_t qemu_send_full(int fd, const void *buf, size_t count, int flags)
338{
339    ssize_t ret = 0;
340    ssize_t total = 0;
341
342    while (count) {
343        ret = send(fd, buf, count, flags);
344        if (ret < 0) {
345            if (errno == EINTR) {
346                continue;
347            }
348            break;
349        }
350
351        count -= ret;
352        buf += ret;
353        total += ret;
354    }
355
356    return total;
357}
358
359/*
360 * A variant of recv(2) which handles partial write.
361 *
362 * Return the number of bytes transferred, which is only
363 * smaller than `count' if there is an error.
364 *
365 * This function won't work with non-blocking fd's.
366 * Any of the possibilities with non-bloking fd's is bad:
367 *   - return a short write (then name is wrong)
368 *   - busy wait adding (errno == EAGAIN) to the loop
369 */
370ssize_t qemu_recv_full(int fd, void *buf, size_t count, int flags)
371{
372    ssize_t ret = 0;
373    ssize_t total = 0;
374
375    while (count) {
376        ret = qemu_recv(fd, buf, count, flags);
377        if (ret <= 0) {
378            if (ret < 0 && errno == EINTR) {
379                continue;
380            }
381            break;
382        }
383
384        count -= ret;
385        buf += ret;
386        total += ret;
387    }
388
389    return total;
390}
391
392void qemu_set_version(const char *version)
393{
394    qemu_version = version;
395}
396
397const char *qemu_get_version(void)
398{
399    return qemu_version;
400}
401
402void fips_set_state(bool requested)
403{
404#ifdef __linux__
405    if (requested) {
406        FILE *fds = fopen("/proc/sys/crypto/fips_enabled", "r");
407        if (fds != NULL) {
408            fips_enabled = (fgetc(fds) == '1');
409            fclose(fds);
410        }
411    }
412#else
413    fips_enabled = false;
414#endif /* __linux__ */
415
416#ifdef _FIPS_DEBUG
417    fprintf(stderr, "FIPS mode %s (requested %s)\n",
418	    (fips_enabled ? "enabled" : "disabled"),
419	    (requested ? "enabled" : "disabled"));
420#endif
421}
422
423bool fips_get_state(void)
424{
425    return fips_enabled;
426}
427
428#ifndef CONFIG_ANDROID
429#ifdef _WIN32
430static void socket_cleanup(void)
431{
432    WSACleanup();
433}
434#endif
435
436int socket_init(void)
437{
438#ifdef _WIN32
439    WSADATA Data;
440    int ret, err;
441
442    ret = WSAStartup(MAKEWORD(2, 2), &Data);
443    if (ret != 0) {
444        err = WSAGetLastError();
445        fprintf(stderr, "WSAStartup: %d\n", err);
446        return -1;
447    }
448    atexit(socket_cleanup);
449#endif
450    return 0;
451}
452#endif  // !CONFIG_ANDROID
453
454#ifndef CONFIG_IOVEC
455/* helper function for iov_send_recv() */
456static ssize_t
457readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write)
458{
459    unsigned i = 0;
460    ssize_t ret = 0;
461    while (i < iov_cnt) {
462        ssize_t r = do_write
463            ? write(fd, iov[i].iov_base, iov[i].iov_len)
464            : read(fd, iov[i].iov_base, iov[i].iov_len);
465        if (r > 0) {
466            ret += r;
467        } else if (!r) {
468            break;
469        } else if (errno == EINTR) {
470            continue;
471        } else {
472            /* else it is some "other" error,
473             * only return if there was no data processed. */
474            if (ret == 0) {
475                ret = -1;
476            }
477            break;
478        }
479        i++;
480    }
481    return ret;
482}
483
484ssize_t
485readv(int fd, const struct iovec *iov, int iov_cnt)
486{
487    return readv_writev(fd, iov, iov_cnt, false);
488}
489
490ssize_t
491writev(int fd, const struct iovec *iov, int iov_cnt)
492{
493    return readv_writev(fd, iov, iov_cnt, true);
494}
495#endif
496
497#if defined(_WIN32) && !QEMU_GNUC_PREREQ(4,4)
498// Older Mingw32 didn't provide these.
499int asprintf( char **, const char *, ... );
500int vasprintf( char **, const char *, va_list );
501
502int vasprintf( char **sptr, const char *fmt, va_list argv )
503{
504    int wanted = vsnprintf( *sptr = NULL, 0, fmt, argv );
505    if( (wanted > 0) && ((*sptr = malloc( 1 + wanted )) != NULL) )
506        return vsprintf( *sptr, fmt, argv );
507
508    return wanted;
509}
510
511int asprintf( char **sptr, const char *fmt, ... )
512{
513    int retval;
514    va_list argv;
515    va_start( argv, fmt );
516    retval = vasprintf( sptr, fmt, argv );
517    va_end( argv );
518    return retval;
519}
520#endif  // _WIN32 && !QEMU_GNUC_PREREQ(4,4)
521