bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 1 | /* |
| 2 | * QEMU low level functions |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 3 | * |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 4 | * Copyright (c) 2003 Fabrice Bellard |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 5 | * |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to deal |
| 8 | * in the Software without restriction, including without limitation the rights |
| 9 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | * copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in |
| 14 | * all copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 19 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | * THE SOFTWARE. |
| 23 | */ |
| 24 | #include <stdlib.h> |
| 25 | #include <stdio.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <string.h> |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 28 | #include <errno.h> |
| 29 | #include <unistd.h> |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 30 | #include <fcntl.h> |
Paolo Bonzini | f582af5 | 2010-02-02 20:33:11 +0100 | [diff] [blame] | 31 | |
| 32 | /* Needed early for CONFIG_BSD etc. */ |
| 33 | #include "config-host.h" |
| 34 | |
Juan Quintela | dfe5fff | 2009-07-27 16:12:40 +0200 | [diff] [blame] | 35 | #ifdef CONFIG_SOLARIS |
ths | 605686c | 2007-01-17 23:31:19 +0000 | [diff] [blame] | 36 | #include <sys/types.h> |
| 37 | #include <sys/statvfs.h> |
| 38 | #endif |
bellard | ea88812 | 2004-02-16 22:12:40 +0000 | [diff] [blame] | 39 | |
Paolo Bonzini | f3dfda6 | 2010-02-11 00:23:46 +0100 | [diff] [blame] | 40 | #ifdef CONFIG_EVENTFD |
| 41 | #include <sys/eventfd.h> |
| 42 | #endif |
| 43 | |
bellard | 6e4255f | 2005-04-17 18:33:47 +0000 | [diff] [blame] | 44 | #ifdef _WIN32 |
| 45 | #include <windows.h> |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 46 | #elif defined(CONFIG_BSD) |
bellard | 194884d | 2005-02-21 20:10:36 +0000 | [diff] [blame] | 47 | #include <stdlib.h> |
| 48 | #else |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 49 | #include <malloc.h> |
bellard | 194884d | 2005-02-21 20:10:36 +0000 | [diff] [blame] | 50 | #endif |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 51 | |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 52 | #include "qemu-common.h" |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 53 | #include "trace.h" |
blueswir1 | 511d2b1 | 2009-03-07 15:32:56 +0000 | [diff] [blame] | 54 | #include "sysemu.h" |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 55 | #include "qemu_socket.h" |
| 56 | |
Blue Swirl | d741429 | 2009-09-12 12:36:04 +0000 | [diff] [blame] | 57 | #if !defined(_POSIX_C_SOURCE) || defined(_WIN32) || defined(__sun__) |
malc | d644f8b | 2009-07-08 18:24:05 +0400 | [diff] [blame] | 58 | static void *oom_check(void *ptr) |
| 59 | { |
| 60 | if (ptr == NULL) { |
Stefan Weil | d2d5adc | 2010-01-21 22:24:58 +0100 | [diff] [blame] | 61 | #if defined(_WIN32) |
| 62 | fprintf(stderr, "Failed to allocate memory: %lu\n", GetLastError()); |
| 63 | #else |
| 64 | fprintf(stderr, "Failed to allocate memory: %s\n", strerror(errno)); |
| 65 | #endif |
malc | d644f8b | 2009-07-08 18:24:05 +0400 | [diff] [blame] | 66 | abort(); |
| 67 | } |
| 68 | return ptr; |
| 69 | } |
| 70 | #endif |
| 71 | |
bellard | 6e4255f | 2005-04-17 18:33:47 +0000 | [diff] [blame] | 72 | #if defined(_WIN32) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 73 | void *qemu_memalign(size_t alignment, size_t size) |
| 74 | { |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 75 | void *ptr; |
| 76 | |
malc | d644f8b | 2009-07-08 18:24:05 +0400 | [diff] [blame] | 77 | if (!size) { |
| 78 | abort(); |
| 79 | } |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 80 | ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
| 81 | trace_qemu_memalign(alignment, size, ptr); |
| 82 | return ptr; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 83 | } |
bellard | 6e4255f | 2005-04-17 18:33:47 +0000 | [diff] [blame] | 84 | |
| 85 | void *qemu_vmalloc(size_t size) |
| 86 | { |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 87 | void *ptr; |
| 88 | |
bellard | 6e4255f | 2005-04-17 18:33:47 +0000 | [diff] [blame] | 89 | /* FIXME: this is not exactly optimal solution since VirtualAlloc |
| 90 | has 64Kb granularity, but at least it guarantees us that the |
| 91 | memory is page aligned. */ |
malc | d644f8b | 2009-07-08 18:24:05 +0400 | [diff] [blame] | 92 | if (!size) { |
| 93 | abort(); |
| 94 | } |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 95 | ptr = oom_check(VirtualAlloc(NULL, size, MEM_COMMIT, PAGE_READWRITE)); |
| 96 | trace_qemu_vmalloc(size, ptr); |
| 97 | return ptr; |
bellard | 6e4255f | 2005-04-17 18:33:47 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void qemu_vfree(void *ptr) |
| 101 | { |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 102 | trace_qemu_vfree(ptr); |
bellard | 6e4255f | 2005-04-17 18:33:47 +0000 | [diff] [blame] | 103 | VirtualFree(ptr, 0, MEM_RELEASE); |
| 104 | } |
| 105 | |
pbrook | 6cb7ee8 | 2006-05-22 14:10:48 +0000 | [diff] [blame] | 106 | #else |
| 107 | |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 108 | void *qemu_memalign(size_t alignment, size_t size) |
| 109 | { |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 110 | void *ptr; |
Blue Swirl | d741429 | 2009-09-12 12:36:04 +0000 | [diff] [blame] | 111 | #if defined(_POSIX_C_SOURCE) && !defined(__sun__) |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 112 | int ret; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 113 | ret = posix_memalign(&ptr, alignment, size); |
Stefan Weil | d2d5adc | 2010-01-21 22:24:58 +0100 | [diff] [blame] | 114 | if (ret != 0) { |
| 115 | fprintf(stderr, "Failed to allocate %zu B: %s\n", |
| 116 | size, strerror(ret)); |
malc | d644f8b | 2009-07-08 18:24:05 +0400 | [diff] [blame] | 117 | abort(); |
Stefan Weil | d2d5adc | 2010-01-21 22:24:58 +0100 | [diff] [blame] | 118 | } |
Juan Quintela | 71e72a1 | 2009-07-27 16:12:56 +0200 | [diff] [blame] | 119 | #elif defined(CONFIG_BSD) |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 120 | ptr = oom_check(valloc(size)); |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 121 | #else |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 122 | ptr = oom_check(memalign(alignment, size)); |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 123 | #endif |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 124 | trace_qemu_memalign(alignment, size, ptr); |
| 125 | return ptr; |
balrog | 33f0027 | 2007-12-24 14:33:24 +0000 | [diff] [blame] | 126 | } |
| 127 | |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 128 | /* alloc shared memory pages */ |
| 129 | void *qemu_vmalloc(size_t size) |
| 130 | { |
malc | 48253bd | 2008-11-18 01:42:15 +0000 | [diff] [blame] | 131 | return qemu_memalign(getpagesize(), size); |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | void qemu_vfree(void *ptr) |
| 135 | { |
Stefan Hajnoczi | cd245a1 | 2010-05-22 18:09:25 +0100 | [diff] [blame^] | 136 | trace_qemu_vfree(ptr); |
bellard | 49b470e | 2005-02-10 21:59:25 +0000 | [diff] [blame] | 137 | free(ptr); |
| 138 | } |
| 139 | |
| 140 | #endif |
| 141 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 142 | int qemu_create_pidfile(const char *filename) |
| 143 | { |
| 144 | char buffer[128]; |
| 145 | int len; |
| 146 | #ifndef _WIN32 |
| 147 | int fd; |
| 148 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 149 | fd = qemu_open(filename, O_RDWR | O_CREAT, 0600); |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 150 | if (fd == -1) |
| 151 | return -1; |
| 152 | |
| 153 | if (lockf(fd, F_TLOCK, 0) == -1) |
| 154 | return -1; |
| 155 | |
| 156 | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
| 157 | if (write(fd, buffer, len) != len) |
| 158 | return -1; |
| 159 | #else |
| 160 | HANDLE file; |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 161 | OVERLAPPED overlap; |
| 162 | BOOL ret; |
Juha Riihimäki | 099fe23 | 2009-12-03 15:56:03 +0200 | [diff] [blame] | 163 | memset(&overlap, 0, sizeof(overlap)); |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 164 | |
Juha Riihimäki | 099fe23 | 2009-12-03 15:56:03 +0200 | [diff] [blame] | 165 | file = CreateFile(filename, GENERIC_WRITE, FILE_SHARE_READ, NULL, |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 166 | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
| 167 | |
| 168 | if (file == INVALID_HANDLE_VALUE) |
| 169 | return -1; |
| 170 | |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 171 | len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid()); |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 172 | ret = WriteFileEx(file, (LPCVOID)buffer, (DWORD)len, |
ths | aa26bb2 | 2007-03-25 21:33:06 +0000 | [diff] [blame] | 173 | &overlap, NULL); |
| 174 | if (ret == 0) |
| 175 | return -1; |
| 176 | #endif |
| 177 | return 0; |
| 178 | } |
pbrook | 29b3a66 | 2007-06-07 23:09:47 +0000 | [diff] [blame] | 179 | |
| 180 | #ifdef _WIN32 |
| 181 | |
Stefan Weil | 4972d59 | 2010-06-12 16:07:12 +0200 | [diff] [blame] | 182 | /* mingw32 needs ffs for compilations without optimization. */ |
| 183 | int ffs(int i) |
| 184 | { |
| 185 | /* Use gcc's builtin ffs. */ |
| 186 | return __builtin_ffs(i); |
| 187 | } |
| 188 | |
pbrook | 29b3a66 | 2007-06-07 23:09:47 +0000 | [diff] [blame] | 189 | /* Offset between 1/1/1601 and 1/1/1970 in 100 nanosec units */ |
| 190 | #define _W32_FT_OFFSET (116444736000000000ULL) |
| 191 | |
| 192 | int qemu_gettimeofday(qemu_timeval *tp) |
| 193 | { |
| 194 | union { |
| 195 | unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */ |
| 196 | FILETIME ft; |
| 197 | } _now; |
| 198 | |
| 199 | if(tp) |
| 200 | { |
| 201 | GetSystemTimeAsFileTime (&_now.ft); |
| 202 | tp->tv_usec=(long)((_now.ns100 / 10ULL) % 1000000ULL ); |
| 203 | tp->tv_sec= (long)((_now.ns100 - _W32_FT_OFFSET) / 10000000ULL); |
| 204 | } |
| 205 | /* Always return 0 as per Open Group Base Specifications Issue 6. |
| 206 | Do not set errno on error. */ |
| 207 | return 0; |
| 208 | } |
| 209 | #endif /* _WIN32 */ |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 210 | |
| 211 | |
| 212 | #ifdef _WIN32 |
| 213 | void socket_set_nonblock(int fd) |
| 214 | { |
| 215 | unsigned long opt = 1; |
| 216 | ioctlsocket(fd, FIONBIO, &opt); |
| 217 | } |
| 218 | |
| 219 | int inet_aton(const char *cp, struct in_addr *ia) |
| 220 | { |
| 221 | uint32_t addr = inet_addr(cp); |
| 222 | if (addr == 0xffffffff) |
| 223 | return 0; |
| 224 | ia->s_addr = addr; |
| 225 | return 1; |
| 226 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 227 | |
| 228 | void qemu_set_cloexec(int fd) |
| 229 | { |
| 230 | } |
| 231 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 232 | #else |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 233 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 234 | void socket_set_nonblock(int fd) |
| 235 | { |
| 236 | int f; |
| 237 | f = fcntl(fd, F_GETFL); |
| 238 | fcntl(fd, F_SETFL, f | O_NONBLOCK); |
| 239 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 240 | |
| 241 | void qemu_set_cloexec(int fd) |
| 242 | { |
| 243 | int f; |
| 244 | f = fcntl(fd, F_GETFD); |
| 245 | fcntl(fd, F_SETFD, f | FD_CLOEXEC); |
| 246 | } |
| 247 | |
aliguori | 03ff3ca | 2008-09-15 15:51:35 +0000 | [diff] [blame] | 248 | #endif |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 249 | |
| 250 | /* |
| 251 | * Opens a file with FD_CLOEXEC set |
| 252 | */ |
| 253 | int qemu_open(const char *name, int flags, ...) |
| 254 | { |
| 255 | int ret; |
| 256 | int mode = 0; |
| 257 | |
| 258 | if (flags & O_CREAT) { |
| 259 | va_list ap; |
| 260 | |
| 261 | va_start(ap, flags); |
| 262 | mode = va_arg(ap, int); |
| 263 | va_end(ap); |
| 264 | } |
| 265 | |
| 266 | #ifdef O_CLOEXEC |
| 267 | ret = open(name, flags | O_CLOEXEC, mode); |
| 268 | #else |
| 269 | ret = open(name, flags, mode); |
| 270 | if (ret >= 0) { |
| 271 | qemu_set_cloexec(ret); |
| 272 | } |
| 273 | #endif |
| 274 | |
| 275 | return ret; |
| 276 | } |
| 277 | |
Kirill A. Shutemov | 7b5f699 | 2010-01-20 00:56:08 +0100 | [diff] [blame] | 278 | /* |
| 279 | * A variant of write(2) which handles partial write. |
| 280 | * |
| 281 | * Return the number of bytes transferred. |
| 282 | * Set errno if fewer than `count' bytes are written. |
Juan Quintela | 1298cb6 | 2010-03-04 10:00:39 +0100 | [diff] [blame] | 283 | * |
| 284 | * This function don't work with non-blocking fd's. |
| 285 | * Any of the possibilities with non-bloking fd's is bad: |
| 286 | * - return a short write (then name is wrong) |
| 287 | * - busy wait adding (errno == EAGAIN) to the loop |
Kirill A. Shutemov | 7b5f699 | 2010-01-20 00:56:08 +0100 | [diff] [blame] | 288 | */ |
| 289 | ssize_t qemu_write_full(int fd, const void *buf, size_t count) |
| 290 | { |
| 291 | ssize_t ret = 0; |
| 292 | ssize_t total = 0; |
| 293 | |
| 294 | while (count) { |
| 295 | ret = write(fd, buf, count); |
| 296 | if (ret < 0) { |
| 297 | if (errno == EINTR) |
| 298 | continue; |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | count -= ret; |
| 303 | buf += ret; |
| 304 | total += ret; |
| 305 | } |
| 306 | |
| 307 | return total; |
| 308 | } |
| 309 | |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 310 | #ifndef _WIN32 |
| 311 | /* |
Paolo Bonzini | f3dfda6 | 2010-02-11 00:23:46 +0100 | [diff] [blame] | 312 | * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set. |
| 313 | */ |
| 314 | int qemu_eventfd(int fds[2]) |
| 315 | { |
Avi Kivity | 153ceef | 2010-02-23 10:16:53 +0100 | [diff] [blame] | 316 | #ifdef CONFIG_EVENTFD |
Paolo Bonzini | f3dfda6 | 2010-02-11 00:23:46 +0100 | [diff] [blame] | 317 | int ret; |
| 318 | |
Paolo Bonzini | f3dfda6 | 2010-02-11 00:23:46 +0100 | [diff] [blame] | 319 | ret = eventfd(0, 0); |
| 320 | if (ret >= 0) { |
| 321 | fds[0] = ret; |
| 322 | qemu_set_cloexec(ret); |
| 323 | if ((fds[1] = dup(ret)) == -1) { |
| 324 | close(ret); |
| 325 | return -1; |
| 326 | } |
| 327 | qemu_set_cloexec(fds[1]); |
| 328 | return 0; |
| 329 | } |
| 330 | |
| 331 | if (errno != ENOSYS) { |
| 332 | return -1; |
| 333 | } |
| 334 | #endif |
| 335 | |
| 336 | return qemu_pipe(fds); |
| 337 | } |
| 338 | |
| 339 | /* |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 340 | * Creates a pipe with FD_CLOEXEC set on both file descriptors |
| 341 | */ |
| 342 | int qemu_pipe(int pipefd[2]) |
| 343 | { |
| 344 | int ret; |
| 345 | |
| 346 | #ifdef CONFIG_PIPE2 |
| 347 | ret = pipe2(pipefd, O_CLOEXEC); |
Andre Przywara | 3a03bfa | 2009-12-18 10:45:07 +0100 | [diff] [blame] | 348 | if (ret != -1 || errno != ENOSYS) { |
| 349 | return ret; |
| 350 | } |
| 351 | #endif |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 352 | ret = pipe(pipefd); |
| 353 | if (ret == 0) { |
| 354 | qemu_set_cloexec(pipefd[0]); |
| 355 | qemu_set_cloexec(pipefd[1]); |
| 356 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 357 | |
| 358 | return ret; |
| 359 | } |
| 360 | #endif |
| 361 | |
| 362 | /* |
| 363 | * Opens a socket with FD_CLOEXEC set |
| 364 | */ |
| 365 | int qemu_socket(int domain, int type, int protocol) |
| 366 | { |
| 367 | int ret; |
| 368 | |
| 369 | #ifdef SOCK_CLOEXEC |
| 370 | ret = socket(domain, type | SOCK_CLOEXEC, protocol); |
Andre Przywara | 3a03bfa | 2009-12-18 10:45:07 +0100 | [diff] [blame] | 371 | if (ret != -1 || errno != EINVAL) { |
| 372 | return ret; |
| 373 | } |
| 374 | #endif |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 375 | ret = socket(domain, type, protocol); |
| 376 | if (ret >= 0) { |
| 377 | qemu_set_cloexec(ret); |
| 378 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 379 | |
| 380 | return ret; |
| 381 | } |
| 382 | |
| 383 | /* |
| 384 | * Accept a connection and set FD_CLOEXEC |
| 385 | */ |
| 386 | int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen) |
| 387 | { |
| 388 | int ret; |
| 389 | |
| 390 | #ifdef CONFIG_ACCEPT4 |
| 391 | ret = accept4(s, addr, addrlen, SOCK_CLOEXEC); |
Kevin Wolf | 347ed55 | 2010-01-13 16:20:56 +0100 | [diff] [blame] | 392 | if (ret != -1 || errno != ENOSYS) { |
Andre Przywara | 3a03bfa | 2009-12-18 10:45:07 +0100 | [diff] [blame] | 393 | return ret; |
| 394 | } |
| 395 | #endif |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 396 | ret = accept(s, addr, addrlen); |
| 397 | if (ret >= 0) { |
| 398 | qemu_set_cloexec(ret); |
| 399 | } |
Kevin Wolf | 40ff6d7 | 2009-12-02 12:24:42 +0100 | [diff] [blame] | 400 | |
| 401 | return ret; |
| 402 | } |