xref: /qemu/util/osdep.c (revision 2c533c54)
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 "qemu/osdep.h"
25 
26 /* Needed early for CONFIG_BSD etc. */
27 
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
34 
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"
40 
41 static bool fips_enabled = false;
42 
43 static const char *hw_version = QEMU_HW_VERSION;
44 
45 int socket_set_cork(int fd, int v)
46 {
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
52 }
53 
54 int socket_set_nodelay(int fd)
55 {
56     int v = 1;
57     return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v));
58 }
59 
60 int 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 
76 #ifndef _WIN32
77 /*
78  * Dups an fd and sets the flags
79  */
80 static int qemu_dup_flags(int fd, int flags)
81 {
82     int ret;
83     int serrno;
84     int dup_flags;
85 
86     ret = qemu_dup(fd);
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 */
102     if (fcntl(ret, F_SETFL, flags) == -1) {
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 
116 fail:
117     serrno = errno;
118     if (ret != -1) {
119         close(ret);
120     }
121     errno = serrno;
122     return -1;
123 }
124 
125 int 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 
139 static int qemu_parse_fdset(const char *param)
140 {
141     return qemu_parse_fd(param);
142 }
143 #endif
144 
145 /*
146  * Opens a file with FD_CLOEXEC set
147  */
148 int qemu_open(const char *name, int flags, ...)
149 {
150     int ret;
151     int mode = 0;
152 
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 
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     }
203 #endif
204 
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 
212     return ret;
213 }
214 
215 int qemu_close(int fd)
216 {
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 
232     return close(fd);
233 }
234 
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.
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
245  */
246 ssize_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 
267 /*
268  * Opens a socket with FD_CLOEXEC set
269  */
270 int 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);
276     if (ret != -1 || errno != EINVAL) {
277         return ret;
278     }
279 #endif
280     ret = socket(domain, type, protocol);
281     if (ret >= 0) {
282         qemu_set_cloexec(ret);
283     }
284 
285     return ret;
286 }
287 
288 /*
289  * Accept a connection and set FD_CLOEXEC
290  */
291 int 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);
297     if (ret != -1 || errno != ENOSYS) {
298         return ret;
299     }
300 #endif
301     ret = accept(s, addr, addrlen);
302     if (ret >= 0) {
303         qemu_set_cloexec(ret);
304     }
305 
306     return ret;
307 }
308 
309 void qemu_set_hw_version(const char *version)
310 {
311     hw_version = version;
312 }
313 
314 const char *qemu_hw_version(void)
315 {
316     return hw_version;
317 }
318 
319 void 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 
340 bool fips_get_state(void)
341 {
342     return fips_enabled;
343 }
344 
345 #ifdef _WIN32
346 static void socket_cleanup(void)
347 {
348     WSACleanup();
349 }
350 #endif
351 
352 int 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 }
368 
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  */
376 static void __attribute__((constructor)) thread_init(void)
377 {
378     if (!g_thread_supported()) {
379        g_thread_init(NULL);
380     }
381 }
382 #endif
383 
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)
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 
414 ssize_t
415 readv(int fd, const struct iovec *iov, int iov_cnt)
416 {
417     return readv_writev(fd, iov, iov_cnt, false);
418 }
419 
420 ssize_t
421 writev(int fd, const struct iovec *iov, int iov_cnt)
422 {
423     return readv_writev(fd, iov, iov_cnt, true);
424 }
425 #endif
426