]>
Commit | Line | Data |
---|---|---|
03557b9a PM |
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 | */ | |
ea88812f FB |
25 | #ifndef QEMU_OSDEP_H |
26 | #define QEMU_OSDEP_H | |
27 | ||
9adea5f7 | 28 | #include "config-host.h" |
49120868 | 29 | #include "qemu/compiler.h" |
d5db2ec1 | 30 | |
79f56d82 PM |
31 | /* Older versions of C++ don't get definitions of various macros from |
32 | * stdlib.h unless we define these macros before first inclusion of | |
33 | * that system header. | |
34 | */ | |
35 | #ifndef __STDC_CONSTANT_MACROS | |
36 | #define __STDC_CONSTANT_MACROS | |
37 | #endif | |
38 | #ifndef __STDC_LIMIT_MACROS | |
39 | #define __STDC_LIMIT_MACROS | |
40 | #endif | |
41 | #ifndef __STDC_FORMAT_MACROS | |
42 | #define __STDC_FORMAT_MACROS | |
43 | #endif | |
44 | ||
d5db2ec1 PM |
45 | /* The following block of code temporarily renames the daemon() function so the |
46 | * compiler does not see the warning associated with it in stdlib.h on OSX | |
47 | */ | |
48 | #ifdef __APPLE__ | |
49 | #define daemon qemu_fake_daemon_function | |
50 | #include <stdlib.h> | |
51 | #undef daemon | |
52 | extern int daemon(int, int); | |
53 | #endif | |
54 | ||
ea88812f | 55 | #include <stdarg.h> |
de5071c5 | 56 | #include <stddef.h> |
0f66998f | 57 | #include <stdbool.h> |
a2b257d6 | 58 | #include <stdint.h> |
128ab2ff | 59 | #include <sys/types.h> |
bfe7e449 PM |
60 | #include <stdlib.h> |
61 | #include <stdio.h> | |
62 | #include <string.h> | |
63 | #include <strings.h> | |
64 | #include <inttypes.h> | |
65 | #include <limits.h> | |
4d9310f4 DB |
66 | /* Put unistd.h before time.h as that triggers localtime_r/gmtime_r |
67 | * function availability on recentish Mingw-w64 platforms. */ | |
68 | #include <unistd.h> | |
bfe7e449 PM |
69 | #include <time.h> |
70 | #include <ctype.h> | |
71 | #include <errno.h> | |
bfe7e449 PM |
72 | #include <fcntl.h> |
73 | #include <sys/stat.h> | |
74 | #include <sys/time.h> | |
75 | #include <assert.h> | |
76 | #include <signal.h> | |
77 | ||
98b2d199 | 78 | #ifdef __OpenBSD__ |
128ab2ff BS |
79 | #include <sys/signal.h> |
80 | #endif | |
ea88812f | 81 | |
13c7b2da PB |
82 | #ifndef _WIN32 |
83 | #include <sys/wait.h> | |
84 | #else | |
85 | #define WIFEXITED(x) 1 | |
86 | #define WEXITSTATUS(x) (x) | |
87 | #endif | |
88 | ||
bfe7e449 PM |
89 | #ifdef _WIN32 |
90 | #include "sysemu/os-win32.h" | |
91 | #endif | |
92 | ||
93 | #ifdef CONFIG_POSIX | |
94 | #include "sysemu/os-posix.h" | |
95 | #endif | |
f7b4a940 | 96 | |
529490e5 PM |
97 | #include "glib-compat.h" |
98 | ||
57cb38b3 DB |
99 | #include "qapi/error.h" |
100 | ||
bfe7e449 PM |
101 | #ifndef O_LARGEFILE |
102 | #define O_LARGEFILE 0 | |
103 | #endif | |
104 | #ifndef O_BINARY | |
105 | #define O_BINARY 0 | |
106 | #endif | |
107 | #ifndef MAP_ANONYMOUS | |
108 | #define MAP_ANONYMOUS MAP_ANON | |
109 | #endif | |
110 | #ifndef ENOMEDIUM | |
111 | #define ENOMEDIUM ENODEV | |
112 | #endif | |
113 | #if !defined(ENOTSUP) | |
114 | #define ENOTSUP 4096 | |
115 | #endif | |
116 | #if !defined(ECANCELED) | |
117 | #define ECANCELED 4097 | |
118 | #endif | |
119 | #if !defined(EMEDIUMTYPE) | |
120 | #define EMEDIUMTYPE 4098 | |
121 | #endif | |
122 | #ifndef TIME_MAX | |
123 | #define TIME_MAX LONG_MAX | |
124 | #endif | |
125 | ||
df2542c7 JM |
126 | #ifndef MIN |
127 | #define MIN(a, b) (((a) < (b)) ? (a) : (b)) | |
128 | #endif | |
129 | #ifndef MAX | |
130 | #define MAX(a, b) (((a) > (b)) ? (a) : (b)) | |
131 | #endif | |
132 | ||
ac3a8726 PL |
133 | /* Minimum function that returns zero only iff both values are zero. |
134 | * Intended for use with unsigned values only. */ | |
135 | #ifndef MIN_NON_ZERO | |
136 | #define MIN_NON_ZERO(a, b) (((a) != 0 && (a) < (b)) ? (a) : (b)) | |
137 | #endif | |
138 | ||
292c8e50 PB |
139 | #ifndef ROUND_UP |
140 | #define ROUND_UP(n,d) (((n) + (d) - 1) & -(d)) | |
141 | #endif | |
142 | ||
e0e53b2f CC |
143 | #ifndef DIV_ROUND_UP |
144 | #define DIV_ROUND_UP(n,d) (((n) + (d) - 1) / (d)) | |
145 | #endif | |
146 | ||
0954d0d9 BS |
147 | #ifndef ARRAY_SIZE |
148 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) | |
149 | #endif | |
150 | ||
f97742d0 | 151 | int qemu_daemon(int nochdir, int noclose); |
7d2a35cc | 152 | void *qemu_try_memalign(size_t alignment, size_t size); |
33f00271 | 153 | void *qemu_memalign(size_t alignment, size_t size); |
a2b257d6 | 154 | void *qemu_anon_ram_alloc(size_t size, uint64_t *align); |
49b470eb | 155 | void qemu_vfree(void *ptr); |
e7a09b92 | 156 | void qemu_anon_ram_free(void *ptr, size_t size); |
ea88812f | 157 | |
e78815a5 AF |
158 | #define QEMU_MADV_INVALID -1 |
159 | ||
160 | #if defined(CONFIG_MADVISE) | |
161 | ||
ebf81150 DDAG |
162 | #include <sys/mman.h> |
163 | ||
e78815a5 AF |
164 | #define QEMU_MADV_WILLNEED MADV_WILLNEED |
165 | #define QEMU_MADV_DONTNEED MADV_DONTNEED | |
166 | #ifdef MADV_DONTFORK | |
167 | #define QEMU_MADV_DONTFORK MADV_DONTFORK | |
168 | #else | |
169 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
170 | #endif | |
171 | #ifdef MADV_MERGEABLE | |
172 | #define QEMU_MADV_MERGEABLE MADV_MERGEABLE | |
173 | #else | |
174 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
175 | #endif | |
605d0a94 PB |
176 | #ifdef MADV_UNMERGEABLE |
177 | #define QEMU_MADV_UNMERGEABLE MADV_UNMERGEABLE | |
178 | #else | |
179 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID | |
180 | #endif | |
181 | #ifdef MADV_DODUMP | |
182 | #define QEMU_MADV_DODUMP MADV_DODUMP | |
183 | #else | |
184 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
185 | #endif | |
ddb97f1d JB |
186 | #ifdef MADV_DONTDUMP |
187 | #define QEMU_MADV_DONTDUMP MADV_DONTDUMP | |
188 | #else | |
189 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID | |
190 | #endif | |
ad0b5321 LC |
191 | #ifdef MADV_HUGEPAGE |
192 | #define QEMU_MADV_HUGEPAGE MADV_HUGEPAGE | |
193 | #else | |
194 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID | |
195 | #endif | |
ebf81150 DDAG |
196 | #ifdef MADV_NOHUGEPAGE |
197 | #define QEMU_MADV_NOHUGEPAGE MADV_NOHUGEPAGE | |
198 | #else | |
199 | #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID | |
200 | #endif | |
e78815a5 AF |
201 | |
202 | #elif defined(CONFIG_POSIX_MADVISE) | |
203 | ||
204 | #define QEMU_MADV_WILLNEED POSIX_MADV_WILLNEED | |
205 | #define QEMU_MADV_DONTNEED POSIX_MADV_DONTNEED | |
206 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
207 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
2925020d MT |
208 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID |
209 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
ddb97f1d | 210 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
8473f377 | 211 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID |
ebf81150 | 212 | #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID |
e78815a5 AF |
213 | |
214 | #else /* no-op */ | |
215 | ||
216 | #define QEMU_MADV_WILLNEED QEMU_MADV_INVALID | |
217 | #define QEMU_MADV_DONTNEED QEMU_MADV_INVALID | |
218 | #define QEMU_MADV_DONTFORK QEMU_MADV_INVALID | |
219 | #define QEMU_MADV_MERGEABLE QEMU_MADV_INVALID | |
2925020d MT |
220 | #define QEMU_MADV_UNMERGEABLE QEMU_MADV_INVALID |
221 | #define QEMU_MADV_DODUMP QEMU_MADV_INVALID | |
ddb97f1d | 222 | #define QEMU_MADV_DONTDUMP QEMU_MADV_INVALID |
8473f377 | 223 | #define QEMU_MADV_HUGEPAGE QEMU_MADV_INVALID |
ebf81150 | 224 | #define QEMU_MADV_NOHUGEPAGE QEMU_MADV_INVALID |
e78815a5 AF |
225 | |
226 | #endif | |
227 | ||
228 | int qemu_madvise(void *addr, size_t len, int advice); | |
229 | ||
17e0b6ab AF |
230 | int qemu_open(const char *name, int flags, ...); |
231 | int qemu_close(int fd); | |
232 | ||
953ffe0f AF |
233 | #if defined(__HAIKU__) && defined(__i386__) |
234 | #define FMT_pid "%ld" | |
0e0167ba SW |
235 | #elif defined(WIN64) |
236 | #define FMT_pid "%" PRId64 | |
953ffe0f AF |
237 | #else |
238 | #define FMT_pid "%d" | |
239 | #endif | |
240 | ||
aa26bb2d | 241 | int qemu_create_pidfile(const char *filename); |
dc7a09cf | 242 | int qemu_get_thread_id(void); |
aa26bb2d | 243 | |
9adea5f7 PB |
244 | #ifndef CONFIG_IOVEC |
245 | struct iovec { | |
246 | void *iov_base; | |
247 | size_t iov_len; | |
248 | }; | |
249 | /* | |
250 | * Use the same value as Linux for now. | |
251 | */ | |
252 | #define IOV_MAX 1024 | |
253 | ||
254 | ssize_t readv(int fd, const struct iovec *iov, int iov_cnt); | |
255 | ssize_t writev(int fd, const struct iovec *iov, int iov_cnt); | |
256 | #else | |
257 | #include <sys/uio.h> | |
258 | #endif | |
259 | ||
ad620c29 BS |
260 | #ifdef _WIN32 |
261 | static inline void qemu_timersub(const struct timeval *val1, | |
262 | const struct timeval *val2, | |
263 | struct timeval *res) | |
264 | { | |
265 | res->tv_sec = val1->tv_sec - val2->tv_sec; | |
266 | if (val1->tv_usec < val2->tv_usec) { | |
267 | res->tv_sec--; | |
268 | res->tv_usec = val1->tv_usec - val2->tv_usec + 1000 * 1000; | |
269 | } else { | |
270 | res->tv_usec = val1->tv_usec - val2->tv_usec; | |
271 | } | |
272 | } | |
273 | #else | |
274 | #define qemu_timersub timersub | |
275 | #endif | |
276 | ||
49ee3590 AL |
277 | void qemu_set_cloexec(int fd); |
278 | ||
fac862ff EH |
279 | /* QEMU "hardware version" setting. Used to replace code that exposed |
280 | * QEMU_VERSION to guests in the past and need to keep compatibilty. | |
281 | * Do not use qemu_hw_version() in new code. | |
282 | */ | |
35c2c8dc EH |
283 | void qemu_set_hw_version(const char *); |
284 | const char *qemu_hw_version(void); | |
93bfef4c | 285 | |
0f66998f PM |
286 | void fips_set_state(bool requested); |
287 | bool fips_get_state(void); | |
288 | ||
e2ea3515 LE |
289 | /* Return a dynamically allocated pathname denoting a file or directory that is |
290 | * appropriate for storing local state. | |
291 | * | |
292 | * @relative_pathname need not start with a directory separator; one will be | |
293 | * added automatically. | |
294 | * | |
295 | * The caller is responsible for releasing the value returned with g_free() | |
296 | * after use. | |
297 | */ | |
298 | char *qemu_get_local_state_pathname(const char *relative_pathname); | |
299 | ||
10f5bff6 FZ |
300 | /* Find program directory, and save it for later usage with |
301 | * qemu_get_exec_dir(). | |
302 | * Try OS specific API first, if not working, parse from argv0. */ | |
303 | void qemu_init_exec_dir(const char *argv0); | |
304 | ||
305 | /* Get the saved exec dir. | |
306 | * Caller needs to release the returned string by g_free() */ | |
307 | char *qemu_get_exec_dir(void); | |
308 | ||
b6a3e690 RH |
309 | /** |
310 | * qemu_getauxval: | |
311 | * @type: the auxiliary vector key to lookup | |
312 | * | |
313 | * Search the auxiliary vector for @type, returning the value | |
314 | * or 0 if @type is not present. | |
315 | */ | |
b6a3e690 | 316 | unsigned long qemu_getauxval(unsigned long type); |
b6a3e690 | 317 | |
13401ba0 SH |
318 | void qemu_set_tty_echo(int fd, bool echo); |
319 | ||
38183310 PB |
320 | void os_mem_prealloc(int fd, char *area, size_t sz); |
321 | ||
d57e4e48 DB |
322 | int qemu_read_password(char *buf, int buf_size); |
323 | ||
57cb38b3 DB |
324 | /** |
325 | * qemu_fork: | |
326 | * | |
327 | * A version of fork that avoids signal handler race | |
328 | * conditions that can lead to child process getting | |
329 | * signals that are otherwise only expected by the | |
330 | * parent. It also resets all signal handlers to the | |
331 | * default settings. | |
332 | * | |
333 | * Returns 0 to child process, pid number to parent | |
334 | * or -1 on failure. | |
335 | */ | |
336 | pid_t qemu_fork(Error **errp); | |
337 | ||
ea88812f | 338 | #endif |