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