xref: /qemu/include/qemu/osdep.h (revision 7a4e543d)
1 /*
2  * OS includes and handling of OS dependencies
3  *
4  * This header exists to pull in some common system headers that
5  * most code in QEMU will want, and to fix up some possible issues with
6  * it (missing defines, Windows weirdness, and so on).
7  *
8  * To avoid getting into possible circular include dependencies, this
9  * file should not include any other QEMU headers, with the exceptions
10  * of config-host.h, compiler.h, os-posix.h and os-win32.h, all of which
11  * are doing a similar job to this file and are under similar constraints.
12  *
13  * This header also contains prototypes for functions defined in
14  * os-*.c and util/oslib-*.c; those would probably be better split
15  * out into separate header files.
16  *
17  * In an ideal world this header would contain only:
18  *  (1) things which everybody needs
19  *  (2) things without which code would work on most platforms but
20  *      fail to compile or misbehave on a minority of host OSes
21  *
22  * This work is licensed under the terms of the GNU GPL, version 2 or later.
23  * See the COPYING file in the top-level directory.
24  */
25 #ifndef QEMU_OSDEP_H
26 #define QEMU_OSDEP_H
27 
28 #include "config-host.h"
29 #include "qemu/compiler.h"
30 #include <stdarg.h>
31 #include <stddef.h>
32 #include <stdbool.h>
33 #include <stdint.h>
34 #include <sys/types.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <strings.h>
39 #include <inttypes.h>
40 #include <limits.h>
41 /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r
42  * function availability on recentish Mingw-w64 platforms. */
43 #include <unistd.h>
44 #include <time.h>
45 #include <ctype.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <sys/stat.h>
49 #include <sys/time.h>
50 #include <assert.h>
51 #include <signal.h>
52 
53 #ifdef __OpenBSD__
54 #include <sys/signal.h>
55 #endif
56 
57 #ifndef _WIN32
58 #include <sys/wait.h>
59 #else
60 #define WIFEXITED(x)   1
61 #define WEXITSTATUS(x) (x)
62 #endif
63 
64 #ifdef _WIN32
65 #include "sysemu/os-win32.h"
66 #endif
67 
68 #ifdef CONFIG_POSIX
69 #include "sysemu/os-posix.h"
70 #endif
71 
72 #include "glib-compat.h"
73 
74 #include "qapi/error.h"
75 
76 #if defined(CONFIG_SOLARIS) && CONFIG_SOLARIS_VERSION < 10
77 /* [u]int_fast*_t not in <sys/int_types.h> */
78 typedef unsigned char           uint_fast8_t;
79 typedef unsigned int            uint_fast16_t;
80 typedef signed int              int_fast16_t;
81 #endif
82 
83 #ifndef O_LARGEFILE
84 #define O_LARGEFILE 0
85 #endif
86 #ifndef O_BINARY
87 #define O_BINARY 0
88 #endif
89 #ifndef MAP_ANONYMOUS
90 #define MAP_ANONYMOUS MAP_ANON
91 #endif
92 #ifndef ENOMEDIUM
93 #define ENOMEDIUM ENODEV
94 #endif
95 #if !defined(ENOTSUP)
96 #define ENOTSUP 4096
97 #endif
98 #if !defined(ECANCELED)
99 #define ECANCELED 4097
100 #endif
101 #if !defined(EMEDIUMTYPE)
102 #define EMEDIUMTYPE 4098
103 #endif
104 #ifndef TIME_MAX
105 #define TIME_MAX LONG_MAX
106 #endif
107 
108 #ifndef MIN
109 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
110 #endif
111 #ifndef MAX
112 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
113 #endif
114 
115 /* Minimum function that returns zero only iff both values are zero.
116  * Intended for use with unsigned values only. */
117 #ifndef MIN_NON_ZERO
118 #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b))
119 #endif
120 
121 #ifndef ROUND_UP
122 #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d))
123 #endif
124 
125 #ifndef DIV_ROUND_UP
126 #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d))
127 #endif
128 
129 #ifndef ARRAY_SIZE
130 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
131 #endif
132 
133 int qemu_daemon(int nochdir, int noclose);
134 void *qemu_try_memalign(size_t alignment, size_t size);
135 void *qemu_memalign(size_t alignment, size_t size);
136 void *qemu_anon_ram_alloc(size_t size, uint64_t *align);
137 void qemu_vfree(void *ptr);
138 void qemu_anon_ram_free(void *ptr, size_t size);
139 
140 #define QEMU_MADV_INVALID -1
141 
142 #if defined(CONFIG_MADVISE)
143 
144 #include <sys/mman.h>
145 
146 #define QEMU_MADV_WILLNEED  MADV_WILLNEED
147 #define QEMU_MADV_DONTNEED  MADV_DONTNEED
148 #ifdef MADV_DONTFORK
149 #define QEMU_MADV_DONTFORK  MADV_DONTFORK
150 #else
151 #define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
152 #endif
153 #ifdef MADV_MERGEABLE
154 #define QEMU_MADV_MERGEABLE MADV_MERGEABLE
155 #else
156 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
157 #endif
158 #ifdef MADV_UNMERGEABLE
159 #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE
160 #else
161 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
162 #endif
163 #ifdef MADV_DODUMP
164 #define QEMU_MADV_DODUMP MADV_DODUMP
165 #else
166 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
167 #endif
168 #ifdef MADV_DONTDUMP
169 #define QEMU_MADV_DONTDUMP MADV_DONTDUMP
170 #else
171 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
172 #endif
173 #ifdef MADV_HUGEPAGE
174 #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE
175 #else
176 #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID
177 #endif
178 #ifdef MADV_NOHUGEPAGE
179 #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE
180 #else
181 #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID
182 #endif
183 
184 #elif defined(CONFIG_POSIX_MADVISE)
185 
186 #define QEMU_MADV_WILLNEED  POSIX_MADV_WILLNEED
187 #define QEMU_MADV_DONTNEED  POSIX_MADV_DONTNEED
188 #define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
189 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
190 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
191 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
192 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
193 #define QEMU_MADV_HUGEPAGE  QEMU_MADV_INVALID
194 #define QEMU_MADV_NOHUGEPAGE  QEMU_MADV_INVALID
195 
196 #else /* no-op */
197 
198 #define QEMU_MADV_WILLNEED  QEMU_MADV_INVALID
199 #define QEMU_MADV_DONTNEED  QEMU_MADV_INVALID
200 #define QEMU_MADV_DONTFORK  QEMU_MADV_INVALID
201 #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID
202 #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID
203 #define QEMU_MADV_DODUMP QEMU_MADV_INVALID
204 #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID
205 #define QEMU_MADV_HUGEPAGE  QEMU_MADV_INVALID
206 #define QEMU_MADV_NOHUGEPAGE  QEMU_MADV_INVALID
207 
208 #endif
209 
210 int qemu_madvise(void *addr, size_t len, int advice);
211 
212 int qemu_open(const char *name, int flags, ...);
213 int qemu_close(int fd);
214 
215 #if defined(__HAIKU__) && defined(__i386__)
216 #define FMT_pid "%ld"
217 #elif defined(WIN64)
218 #define FMT_pid "%" PRId64
219 #else
220 #define FMT_pid "%d"
221 #endif
222 
223 int qemu_create_pidfile(const char *filename);
224 int qemu_get_thread_id(void);
225 
226 #ifndef CONFIG_IOVEC
227 struct iovec {
228     void *iov_base;
229     size_t iov_len;
230 };
231 /*
232  * Use the same value as Linux for now.
233  */
234 #define IOV_MAX 1024
235 
236 ssize_t readv(int fd, const struct iovec *iov, int iov_cnt);
237 ssize_t writev(int fd, const struct iovec *iov, int iov_cnt);
238 #else
239 #include <sys/uio.h>
240 #endif
241 
242 #ifdef _WIN32
243 static inline void qemu_timersub(const struct timeval *val1,
244                                  const struct timeval *val2,
245                                  struct timeval *res)
246 {
247     res->tv_sec = val1->tv_sec - val2->tv_sec;
248     if (val1->tv_usec < val2->tv_usec) {
249         res->tv_sec--;
250         res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000;
251     } else {
252         res->tv_usec = val1->tv_usec - val2->tv_usec;
253     }
254 }
255 #else
256 #define qemu_timersub timersub
257 #endif
258 
259 void qemu_set_cloexec(int fd);
260 
261 /* QEMU "hardware version" setting. Used to replace code that exposed
262  * QEMU_VERSION to guests in the past and need to keep compatibilty.
263  * Do not use qemu_hw_version() in new code.
264  */
265 void qemu_set_hw_version(const char *);
266 const char *qemu_hw_version(void);
267 
268 void fips_set_state(bool requested);
269 bool fips_get_state(void);
270 
271 /* Return a dynamically allocated pathname denoting a file or directory that is
272  * appropriate for storing local state.
273  *
274  * @relative_pathname need not start with a directory separator; one will be
275  * added automatically.
276  *
277  * The caller is responsible for releasing the value returned with g_free()
278  * after use.
279  */
280 char *qemu_get_local_state_pathname(const char *relative_pathname);
281 
282 /* Find program directory, and save it for later usage with
283  * qemu_get_exec_dir().
284  * Try OS specific API first, if not working, parse from argv0. */
285 void qemu_init_exec_dir(const char *argv0);
286 
287 /* Get the saved exec dir.
288  * Caller needs to release the returned string by g_free() */
289 char *qemu_get_exec_dir(void);
290 
291 /**
292  * qemu_getauxval:
293  * @type: the auxiliary vector key to lookup
294  *
295  * Search the auxiliary vector for @type, returning the value
296  * or 0 if @type is not present.
297  */
298 unsigned long qemu_getauxval(unsigned long type);
299 
300 void qemu_set_tty_echo(int fd, bool echo);
301 
302 void os_mem_prealloc(int fd, char *area, size_t sz);
303 
304 int qemu_read_password(char *buf, int buf_size);
305 
306 /**
307  * qemu_fork:
308  *
309  * A version of fork that avoids signal handler race
310  * conditions that can lead to child process getting
311  * signals that are otherwise only expected by the
312  * parent. It also resets all signal handlers to the
313  * default settings.
314  *
315  * Returns 0 to child process, pid number to parent
316  * or -1 on failure.
317  */
318 pid_t qemu_fork(Error **errp);
319 
320 #endif
321