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