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