Kohei Tokunaga | 0f46bf6 | 2025-04-28 15:39:03 +0900 | [diff] [blame] | 1 | /* SPDX-License-Identifier: MIT */ |
| 2 | /* |
| 3 | * os-wasm.c |
| 4 | * Forked from os-posix.c, removing functions not working on Emscripten |
| 5 | * |
| 6 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 7 | * Copyright (c) 2010 Red Hat, Inc. |
| 8 | * |
| 9 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | * of this software and associated documentation files (the "Software"), to deal |
| 11 | * in the Software without restriction, including without limitation the rights |
| 12 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | * copies of the Software, and to permit persons to whom the Software is |
| 14 | * furnished to do so, subject to the following conditions: |
| 15 | * |
| 16 | * The above copyright notice and this permission notice shall be included in |
| 17 | * all copies or substantial portions of the Software. |
| 18 | * |
| 19 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 22 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | * THE SOFTWARE. |
| 26 | */ |
| 27 | |
| 28 | #include "qemu/osdep.h" |
| 29 | #include <sys/resource.h> |
| 30 | #include <sys/wait.h> |
| 31 | #include <pwd.h> |
| 32 | #include <grp.h> |
| 33 | #include <libgen.h> |
| 34 | |
| 35 | #include "qemu/error-report.h" |
| 36 | #include "qemu/log.h" |
| 37 | #include "system/runstate.h" |
| 38 | #include "qemu/cutils.h" |
| 39 | |
| 40 | void os_setup_post(void){} |
| 41 | void os_set_line_buffering(void) |
| 42 | { |
| 43 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 44 | } |
| 45 | void os_setup_early_signal_handling(void) |
| 46 | { |
| 47 | struct sigaction act; |
| 48 | sigfillset(&act.sa_mask); |
| 49 | act.sa_flags = 0; |
| 50 | act.sa_handler = SIG_IGN; |
| 51 | sigaction(SIGPIPE, &act, NULL); |
| 52 | } |
| 53 | void os_set_proc_name(const char *s) |
| 54 | { |
| 55 | error_report("Change of process name not supported by your OS"); |
| 56 | exit(1); |
| 57 | } |
| 58 | static void termsig_handler(int signal, siginfo_t *info, void *c) |
| 59 | { |
| 60 | qemu_system_killed(info->si_signo, info->si_pid); |
| 61 | } |
| 62 | |
| 63 | void os_setup_signal_handling(void) |
| 64 | { |
| 65 | struct sigaction act; |
| 66 | |
| 67 | memset(&act, 0, sizeof(act)); |
| 68 | act.sa_sigaction = termsig_handler; |
| 69 | act.sa_flags = SA_SIGINFO; |
| 70 | sigaction(SIGINT, &act, NULL); |
| 71 | sigaction(SIGHUP, &act, NULL); |
| 72 | sigaction(SIGTERM, &act, NULL); |
| 73 | } |
| 74 | void os_setup_limits(void) |
| 75 | { |
| 76 | struct rlimit nofile; |
| 77 | |
| 78 | if (getrlimit(RLIMIT_NOFILE, &nofile) < 0) { |
| 79 | warn_report("unable to query NOFILE limit: %s", strerror(errno)); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (nofile.rlim_cur == nofile.rlim_max) { |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | nofile.rlim_cur = nofile.rlim_max; |
| 88 | |
| 89 | if (setrlimit(RLIMIT_NOFILE, &nofile) < 0) { |
| 90 | warn_report("unable to set NOFILE limit: %s", strerror(errno)); |
| 91 | return; |
| 92 | } |
| 93 | } |
| 94 | int os_mlock(bool on_fault) |
| 95 | { |
| 96 | #ifdef HAVE_MLOCKALL |
| 97 | int ret = 0; |
| 98 | int flags = MCL_CURRENT | MCL_FUTURE; |
| 99 | |
| 100 | if (on_fault) { |
| 101 | #ifdef HAVE_MLOCK_ONFAULT |
| 102 | flags |= MCL_ONFAULT; |
| 103 | #else |
| 104 | error_report("mlockall: on_fault not supported"); |
| 105 | return -EINVAL; |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | ret = mlockall(flags); |
| 110 | if (ret < 0) { |
| 111 | error_report("mlockall: %s", strerror(errno)); |
| 112 | } |
| 113 | |
| 114 | return ret; |
| 115 | #else |
| 116 | (void)on_fault; |
| 117 | return -ENOSYS; |
| 118 | #endif |
| 119 | } |