1 /* 2 * QEMU low level functions 3 * 4 * Copyright (c) 2003 Fabrice Bellard 5 * 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 "qemu/osdep.h" 25 #include "qapi/error.h" 26 27 /* Needed early for CONFIG_BSD etc. */ 28 29 #ifdef CONFIG_SOLARIS 30 #include <sys/statvfs.h> 31 /* See MySQL bug #7156 (http://bugs.mysql.com/bug.php?id=7156) for 32 discussion about Solaris header problems */ 33 extern int madvise(char *, size_t, int); 34 #endif 35 36 #include <dirent.h> 37 #include "qemu-common.h" 38 #include "qemu/cutils.h" 39 #include "qemu/sockets.h" 40 #include "qemu/error-report.h" 41 #include "monitor/monitor.h" 42 43 static bool fips_enabled = false; 44 45 static const char *hw_version = QEMU_HW_VERSION; 46 47 int socket_set_cork(int fd, int v) 48 { 49 #if defined(SOL_TCP) && defined(TCP_CORK) 50 return qemu_setsockopt(fd, SOL_TCP, TCP_CORK, &v, sizeof(v)); 51 #else 52 return 0; 53 #endif 54 } 55 56 int socket_set_nodelay(int fd) 57 { 58 int v = 1; 59 return qemu_setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &v, sizeof(v)); 60 } 61 62 int qemu_madvise(void *addr, size_t len, int advice) 63 { 64 if (advice == QEMU_MADV_INVALID) { 65 errno = EINVAL; 66 return -1; 67 } 68 #if defined(CONFIG_MADVISE) 69 return madvise(addr, len, advice); 70 #elif defined(CONFIG_POSIX_MADVISE) 71 return posix_madvise(addr, len, advice); 72 #else 73 errno = EINVAL; 74 return -1; 75 #endif 76 } 77 78 static int qemu_mprotect__osdep(void *addr, size_t size, int prot) 79 { 80 g_assert(!((uintptr_t)addr & ~qemu_real_host_page_mask)); 81 g_assert(!(size & ~qemu_real_host_page_mask)); 82 83 #ifdef _WIN32 84 DWORD old_protect; 85 86 if (!VirtualProtect(addr, size, prot, &old_protect)) { 87 g_autofree gchar *emsg = g_win32_error_message(GetLastError()); 88 error_report("%s: VirtualProtect failed: %s", __func__, emsg); 89 return -1; 90 } 91 return 0; 92 #else 93 if (mprotect(addr, size, prot)) { 94 error_report("%s: mprotect failed: %s", __func__, strerror(errno)); 95 return -1; 96 } 97 return 0; 98 #endif 99 } 100 101 int qemu_mprotect_rw(void *addr, size_t size) 102 { 103 #ifdef _WIN32 104 return qemu_mprotect__osdep(addr, size, PAGE_READWRITE); 105 #else 106 return qemu_mprotect__osdep(addr, size, PROT_READ | PROT_WRITE); 107 #endif 108 } 109 110 int qemu_mprotect_rwx(void *addr, size_t size) 111 { 112 #ifdef _WIN32 113 return qemu_mprotect__osdep(addr, size, PAGE_EXECUTE_READWRITE); 114 #else 115 return qemu_mprotect__osdep(addr, size, PROT_READ | PROT_WRITE | PROT_EXEC); 116 #endif 117 } 118 119 int qemu_mprotect_none(void *addr, size_t size) 120 { 121 #ifdef _WIN32 122 return qemu_mprotect__osdep(addr, size, PAGE_NOACCESS); 123 #else 124 return qemu_mprotect__osdep(addr, size, PROT_NONE); 125 #endif 126 } 127 128 #ifndef _WIN32 129 130 static int fcntl_op_setlk = -1; 131 static int fcntl_op_getlk = -1; 132 133 /* 134 * Dups an fd and sets the flags 135 */ 136 int qemu_dup_flags(int fd, int flags) 137 { 138 int ret; 139 int serrno; 140 int dup_flags; 141 142 ret = qemu_dup(fd); 143 if (ret == -1) { 144 goto fail; 145 } 146 147 dup_flags = fcntl(ret, F_GETFL); 148 if (dup_flags == -1) { 149 goto fail; 150 } 151 152 if ((flags & O_SYNC) != (dup_flags & O_SYNC)) { 153 errno = EINVAL; 154 goto fail; 155 } 156 157 /* Set/unset flags that we can with fcntl */ 158 if (fcntl(ret, F_SETFL, flags) == -1) { 159 goto fail; 160 } 161 162 /* Truncate the file in the cases that open() would truncate it */ 163 if (flags & O_TRUNC || 164 ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))) { 165 if (ftruncate(ret, 0) == -1) { 166 goto fail; 167 } 168 } 169 170 return ret; 171 172 fail: 173 serrno = errno; 174 if (ret != -1) { 175 close(ret); 176 } 177 errno = serrno; 178 return -1; 179 } 180 181 int qemu_dup(int fd) 182 { 183 int ret; 184 #ifdef F_DUPFD_CLOEXEC 185 ret = fcntl(fd, F_DUPFD_CLOEXEC, 0); 186 #else 187 ret = dup(fd); 188 if (ret != -1) { 189 qemu_set_cloexec(ret); 190 } 191 #endif 192 return ret; 193 } 194 195 static int qemu_parse_fdset(const char *param) 196 { 197 return qemu_parse_fd(param); 198 } 199 200 static void qemu_probe_lock_ops(void) 201 { 202 if (fcntl_op_setlk == -1) { 203 #ifdef F_OFD_SETLK 204 int fd; 205 int ret; 206 struct flock fl = { 207 .l_whence = SEEK_SET, 208 .l_start = 0, 209 .l_len = 0, 210 .l_type = F_WRLCK, 211 }; 212 213 fd = open("/dev/null", O_RDWR); 214 if (fd < 0) { 215 fprintf(stderr, 216 "Failed to open /dev/null for OFD lock probing: %s\n", 217 strerror(errno)); 218 fcntl_op_setlk = F_SETLK; 219 fcntl_op_getlk = F_GETLK; 220 return; 221 } 222 ret = fcntl(fd, F_OFD_GETLK, &fl); 223 close(fd); 224 if (!ret) { 225 fcntl_op_setlk = F_OFD_SETLK; 226 fcntl_op_getlk = F_OFD_GETLK; 227 } else { 228 fcntl_op_setlk = F_SETLK; 229 fcntl_op_getlk = F_GETLK; 230 } 231 #else 232 fcntl_op_setlk = F_SETLK; 233 fcntl_op_getlk = F_GETLK; 234 #endif 235 } 236 } 237 238 bool qemu_has_ofd_lock(void) 239 { 240 qemu_probe_lock_ops(); 241 #ifdef F_OFD_SETLK 242 return fcntl_op_setlk == F_OFD_SETLK; 243 #else 244 return false; 245 #endif 246 } 247 248 static int qemu_lock_fcntl(int fd, int64_t start, int64_t len, int fl_type) 249 { 250 int ret; 251 struct flock fl = { 252 .l_whence = SEEK_SET, 253 .l_start = start, 254 .l_len = len, 255 .l_type = fl_type, 256 }; 257 qemu_probe_lock_ops(); 258 do { 259 ret = fcntl(fd, fcntl_op_setlk, &fl); 260 } while (ret == -1 && errno == EINTR); 261 return ret == -1 ? -errno : 0; 262 } 263 264 int qemu_lock_fd(int fd, int64_t start, int64_t len, bool exclusive) 265 { 266 return qemu_lock_fcntl(fd, start, len, exclusive ? F_WRLCK : F_RDLCK); 267 } 268 269 int qemu_unlock_fd(int fd, int64_t start, int64_t len) 270 { 271 return qemu_lock_fcntl(fd, start, len, F_UNLCK); 272 } 273 274 int qemu_lock_fd_test(int fd, int64_t start, int64_t len, bool exclusive) 275 { 276 int ret; 277 struct flock fl = { 278 .l_whence = SEEK_SET, 279 .l_start = start, 280 .l_len = len, 281 .l_type = exclusive ? F_WRLCK : F_RDLCK, 282 }; 283 qemu_probe_lock_ops(); 284 ret = fcntl(fd, fcntl_op_getlk, &fl); 285 if (ret == -1) { 286 return -errno; 287 } else { 288 return fl.l_type == F_UNLCK ? 0 : -EAGAIN; 289 } 290 } 291 #endif 292 293 static int qemu_open_cloexec(const char *name, int flags, mode_t mode) 294 { 295 int ret; 296 #ifdef O_CLOEXEC 297 ret = open(name, flags | O_CLOEXEC, mode); 298 #else 299 ret = open(name, flags, mode); 300 if (ret >= 0) { 301 qemu_set_cloexec(ret); 302 } 303 #endif 304 return ret; 305 } 306 307 /* 308 * Opens a file with FD_CLOEXEC set 309 */ 310 static int 311 qemu_open_internal(const char *name, int flags, mode_t mode, Error **errp) 312 { 313 int ret; 314 315 #ifndef _WIN32 316 const char *fdset_id_str; 317 318 /* Attempt dup of fd from fd set */ 319 if (strstart(name, "/dev/fdset/", &fdset_id_str)) { 320 int64_t fdset_id; 321 int dupfd; 322 323 fdset_id = qemu_parse_fdset(fdset_id_str); 324 if (fdset_id == -1) { 325 error_setg(errp, "Could not parse fdset %s", name); 326 errno = EINVAL; 327 return -1; 328 } 329 330 dupfd = monitor_fdset_dup_fd_add(fdset_id, flags); 331 if (dupfd == -1) { 332 error_setg_errno(errp, errno, "Could not dup FD for %s flags %x", 333 name, flags); 334 return -1; 335 } 336 337 return dupfd; 338 } 339 #endif 340 341 ret = qemu_open_cloexec(name, flags, mode); 342 343 if (ret == -1) { 344 const char *action = flags & O_CREAT ? "create" : "open"; 345 #ifdef O_DIRECT 346 /* Give more helpful error message for O_DIRECT */ 347 if (errno == EINVAL && (flags & O_DIRECT)) { 348 ret = open(name, flags & ~O_DIRECT, mode); 349 if (ret != -1) { 350 close(ret); 351 error_setg(errp, "Could not %s '%s': " 352 "filesystem does not support O_DIRECT", 353 action, name); 354 errno = EINVAL; /* restore first open()'s errno */ 355 return -1; 356 } 357 } 358 #endif /* O_DIRECT */ 359 error_setg_errno(errp, errno, "Could not %s '%s'", 360 action, name); 361 } 362 363 return ret; 364 } 365 366 367 int qemu_open(const char *name, int flags, Error **errp) 368 { 369 assert(!(flags & O_CREAT)); 370 371 return qemu_open_internal(name, flags, 0, errp); 372 } 373 374 375 int qemu_create(const char *name, int flags, mode_t mode, Error **errp) 376 { 377 assert(!(flags & O_CREAT)); 378 379 return qemu_open_internal(name, flags | O_CREAT, mode, errp); 380 } 381 382 383 int qemu_open_old(const char *name, int flags, ...) 384 { 385 va_list ap; 386 mode_t mode = 0; 387 int ret; 388 389 va_start(ap, flags); 390 if (flags & O_CREAT) { 391 mode = va_arg(ap, int); 392 } 393 va_end(ap); 394 395 ret = qemu_open_internal(name, flags, mode, NULL); 396 397 #ifdef O_DIRECT 398 if (ret == -1 && errno == EINVAL && (flags & O_DIRECT)) { 399 error_report("file system may not support O_DIRECT"); 400 errno = EINVAL; /* in case it was clobbered */ 401 } 402 #endif /* O_DIRECT */ 403 404 return ret; 405 } 406 407 int qemu_close(int fd) 408 { 409 int64_t fdset_id; 410 411 /* Close fd that was dup'd from an fdset */ 412 fdset_id = monitor_fdset_dup_fd_find(fd); 413 if (fdset_id != -1) { 414 int ret; 415 416 ret = close(fd); 417 if (ret == 0) { 418 monitor_fdset_dup_fd_remove(fd); 419 } 420 421 return ret; 422 } 423 424 return close(fd); 425 } 426 427 /* 428 * Delete a file from the filesystem, unless the filename is /dev/fdset/... 429 * 430 * Returns: On success, zero is returned. On error, -1 is returned, 431 * and errno is set appropriately. 432 */ 433 int qemu_unlink(const char *name) 434 { 435 if (g_str_has_prefix(name, "/dev/fdset/")) { 436 return 0; 437 } 438 439 return unlink(name); 440 } 441 442 /* 443 * A variant of write(2) which handles partial write. 444 * 445 * Return the number of bytes transferred. 446 * Set errno if fewer than `count' bytes are written. 447 * 448 * This function don't work with non-blocking fd's. 449 * Any of the possibilities with non-blocking fd's is bad: 450 * - return a short write (then name is wrong) 451 * - busy wait adding (errno == EAGAIN) to the loop 452 */ 453 ssize_t qemu_write_full(int fd, const void *buf, size_t count) 454 { 455 ssize_t ret = 0; 456 ssize_t total = 0; 457 458 while (count) { 459 ret = write(fd, buf, count); 460 if (ret < 0) { 461 if (errno == EINTR) 462 continue; 463 break; 464 } 465 466 count -= ret; 467 buf += ret; 468 total += ret; 469 } 470 471 return total; 472 } 473 474 /* 475 * Opens a socket with FD_CLOEXEC set 476 */ 477 int qemu_socket(int domain, int type, int protocol) 478 { 479 int ret; 480 481 #ifdef SOCK_CLOEXEC 482 ret = socket(domain, type | SOCK_CLOEXEC, protocol); 483 if (ret != -1 || errno != EINVAL) { 484 return ret; 485 } 486 #endif 487 ret = socket(domain, type, protocol); 488 if (ret >= 0) { 489 qemu_set_cloexec(ret); 490 } 491 492 return ret; 493 } 494 495 /* 496 * Accept a connection and set FD_CLOEXEC 497 */ 498 int qemu_accept(int s, struct sockaddr *addr, socklen_t *addrlen) 499 { 500 int ret; 501 502 #ifdef CONFIG_ACCEPT4 503 ret = accept4(s, addr, addrlen, SOCK_CLOEXEC); 504 if (ret != -1 || errno != ENOSYS) { 505 return ret; 506 } 507 #endif 508 ret = accept(s, addr, addrlen); 509 if (ret >= 0) { 510 qemu_set_cloexec(ret); 511 } 512 513 return ret; 514 } 515 516 void qemu_set_hw_version(const char *version) 517 { 518 hw_version = version; 519 } 520 521 const char *qemu_hw_version(void) 522 { 523 return hw_version; 524 } 525 526 void fips_set_state(bool requested) 527 { 528 #ifdef __linux__ 529 if (requested) { 530 FILE *fds = fopen("/proc/sys/crypto/fips_enabled", "r"); 531 if (fds != NULL) { 532 fips_enabled = (fgetc(fds) == '1'); 533 fclose(fds); 534 } 535 } 536 #else 537 fips_enabled = false; 538 #endif /* __linux__ */ 539 540 #ifdef _FIPS_DEBUG 541 fprintf(stderr, "FIPS mode %s (requested %s)\n", 542 (fips_enabled ? "enabled" : "disabled"), 543 (requested ? "enabled" : "disabled")); 544 #endif 545 } 546 547 bool fips_get_state(void) 548 { 549 return fips_enabled; 550 } 551 552 #ifdef _WIN32 553 static void socket_cleanup(void) 554 { 555 WSACleanup(); 556 } 557 #endif 558 559 int socket_init(void) 560 { 561 #ifdef _WIN32 562 WSADATA Data; 563 int ret, err; 564 565 ret = WSAStartup(MAKEWORD(2, 2), &Data); 566 if (ret != 0) { 567 err = WSAGetLastError(); 568 fprintf(stderr, "WSAStartup: %d\n", err); 569 return -1; 570 } 571 atexit(socket_cleanup); 572 #endif 573 return 0; 574 } 575 576 577 #ifndef CONFIG_IOVEC 578 /* helper function for iov_send_recv() */ 579 static ssize_t 580 readv_writev(int fd, const struct iovec *iov, int iov_cnt, bool do_write) 581 { 582 unsigned i = 0; 583 ssize_t ret = 0; 584 while (i < iov_cnt) { 585 ssize_t r = do_write 586 ? write(fd, iov[i].iov_base, iov[i].iov_len) 587 : read(fd, iov[i].iov_base, iov[i].iov_len); 588 if (r > 0) { 589 ret += r; 590 } else if (!r) { 591 break; 592 } else if (errno == EINTR) { 593 continue; 594 } else { 595 /* else it is some "other" error, 596 * only return if there was no data processed. */ 597 if (ret == 0) { 598 ret = -1; 599 } 600 break; 601 } 602 i++; 603 } 604 return ret; 605 } 606 607 ssize_t 608 readv(int fd, const struct iovec *iov, int iov_cnt) 609 { 610 return readv_writev(fd, iov, iov_cnt, false); 611 } 612 613 ssize_t 614 writev(int fd, const struct iovec *iov, int iov_cnt) 615 { 616 return readv_writev(fd, iov, iov_cnt, true); 617 } 618 #endif 619 620 struct dirent * 621 qemu_dirent_dup(struct dirent *dent) 622 { 623 size_t sz = 0; 624 #if defined _DIRENT_HAVE_D_RECLEN 625 /* Avoid use of strlen() if platform supports d_reclen. */ 626 sz = dent->d_reclen; 627 #endif 628 /* 629 * Test sz for zero even if d_reclen is available 630 * because some drivers may set d_reclen to zero. 631 */ 632 if (sz == 0) { 633 /* Fallback to the most portable way. */ 634 sz = offsetof(struct dirent, d_name) + 635 strlen(dent->d_name) + 1; 636 } 637 return g_memdup(dent, sz); 638 } 639