Jes Sorensen | 86b645e | 2010-06-10 11:42:19 +0200 | [diff] [blame] | 1 | /* |
| 2 | * os-posix.c |
| 3 | * |
| 4 | * Copyright (c) 2003-2008 Fabrice Bellard |
| 5 | * Copyright (c) 2010 Red Hat, Inc. |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | |
Peter Maydell | d38ea87 | 2016-01-29 17:50:05 +0000 | [diff] [blame] | 26 | #include "qemu/osdep.h" |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 27 | #include <sys/wait.h> |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 28 | #include <pwd.h> |
Stefan Hajnoczi | cc4662f | 2011-07-09 10:22:07 +0100 | [diff] [blame] | 29 | #include <grp.h> |
Jes Sorensen | 6170540 | 2010-06-10 11:42:23 +0200 | [diff] [blame] | 30 | #include <libgen.h> |
Jes Sorensen | 86b645e | 2010-06-10 11:42:19 +0200 | [diff] [blame] | 31 | |
| 32 | /* Needed early for CONFIG_BSD etc. */ |
Jes Sorensen | 59a5264 | 2010-06-10 11:42:25 +0200 | [diff] [blame] | 33 | #include "net/slirp.h" |
Paolo Bonzini | fd5fc4b | 2021-05-17 07:34:21 -0400 | [diff] [blame] | 34 | #include "qemu/qemu-options.h" |
Thomas Huth | f853ac6 | 2016-01-13 09:05:32 +0100 | [diff] [blame] | 35 | #include "qemu/error-report.h" |
Dimitris Aragiorgis | 96c33a4 | 2016-02-18 13:38:38 +0200 | [diff] [blame] | 36 | #include "qemu/log.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 37 | #include "sysemu/runstate.h" |
Veronia Bahaa | f348b6d | 2016-03-20 19:16:19 +0200 | [diff] [blame] | 38 | #include "qemu/cutils.h" |
Claudio Imbrenda | 80bd81c | 2023-05-05 14:00:51 +0200 | [diff] [blame] | 39 | #include "qemu/config-file.h" |
| 40 | #include "qemu/option.h" |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 41 | #include "qemu/module.h" |
Jes Sorensen | 86b645e | 2010-06-10 11:42:19 +0200 | [diff] [blame] | 42 | |
Jes Sorensen | ce798cf | 2010-06-10 11:42:31 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_LINUX |
| 44 | #include <sys/prctl.h> |
Claudio Imbrenda | c891c24 | 2022-08-12 15:34:53 +0200 | [diff] [blame] | 45 | #include "qemu/async-teardown.h" |
Jes Sorensen | 949d31e | 2010-10-26 10:39:22 +0200 | [diff] [blame] | 46 | #endif |
| 47 | |
Ian Jackson | 2c42f1e | 2017-09-15 18:10:44 +0100 | [diff] [blame] | 48 | /* |
| 49 | * Must set all three of these at once. |
| 50 | * Legal combinations are unset by name by uid |
| 51 | */ |
| 52 | static struct passwd *user_pwd; /* NULL non-NULL NULL */ |
| 53 | static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */ |
| 54 | static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */ |
| 55 | |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 56 | static const char *chroot_dir; |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 57 | static int daemonize; |
Michael Tokarev | 0be5e43 | 2014-10-30 17:30:51 +0300 | [diff] [blame] | 58 | static int daemon_pipe; |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 59 | |
Jes Sorensen | fe98ac1 | 2010-06-10 11:42:21 +0200 | [diff] [blame] | 60 | void os_setup_early_signal_handling(void) |
Jes Sorensen | 86b645e | 2010-06-10 11:42:19 +0200 | [diff] [blame] | 61 | { |
| 62 | struct sigaction act; |
| 63 | sigfillset(&act.sa_mask); |
| 64 | act.sa_flags = 0; |
| 65 | act.sa_handler = SIG_IGN; |
| 66 | sigaction(SIGPIPE, &act, NULL); |
| 67 | } |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 68 | |
Gleb Natapov | f64622c | 2011-03-15 13:56:04 +0200 | [diff] [blame] | 69 | static void termsig_handler(int signal, siginfo_t *info, void *c) |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 70 | { |
Gleb Natapov | f64622c | 2011-03-15 13:56:04 +0200 | [diff] [blame] | 71 | qemu_system_killed(info->si_signo, info->si_pid); |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 72 | } |
| 73 | |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 74 | void os_setup_signal_handling(void) |
| 75 | { |
| 76 | struct sigaction act; |
| 77 | |
| 78 | memset(&act, 0, sizeof(act)); |
Gleb Natapov | f64622c | 2011-03-15 13:56:04 +0200 | [diff] [blame] | 79 | act.sa_sigaction = termsig_handler; |
| 80 | act.sa_flags = SA_SIGINFO; |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 81 | sigaction(SIGINT, &act, NULL); |
| 82 | sigaction(SIGHUP, &act, NULL); |
| 83 | sigaction(SIGTERM, &act, NULL); |
Jes Sorensen | 8d963e6 | 2010-06-10 11:42:22 +0200 | [diff] [blame] | 84 | } |
Jes Sorensen | 6170540 | 2010-06-10 11:42:23 +0200 | [diff] [blame] | 85 | |
Jes Sorensen | ce798cf | 2010-06-10 11:42:31 +0200 | [diff] [blame] | 86 | void os_set_proc_name(const char *s) |
| 87 | { |
| 88 | #if defined(PR_SET_NAME) |
| 89 | char name[16]; |
| 90 | if (!s) |
| 91 | return; |
Jim Meyering | 3eadc68 | 2012-10-04 13:09:51 +0200 | [diff] [blame] | 92 | pstrcpy(name, sizeof(name), s); |
Jes Sorensen | ce798cf | 2010-06-10 11:42:31 +0200 | [diff] [blame] | 93 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
| 94 | This simple way is enough for `top'. */ |
| 95 | if (prctl(PR_SET_NAME, name)) { |
Ian Jackson | a7aaec1 | 2018-04-16 15:16:23 +0100 | [diff] [blame] | 96 | error_report("unable to change process name: %s", strerror(errno)); |
Jes Sorensen | ce798cf | 2010-06-10 11:42:31 +0200 | [diff] [blame] | 97 | exit(1); |
| 98 | } |
| 99 | #else |
Ian Jackson | 22cd4f4 | 2018-04-16 15:15:51 +0100 | [diff] [blame] | 100 | error_report("Change of process name not supported by your OS"); |
Jes Sorensen | ce798cf | 2010-06-10 11:42:31 +0200 | [diff] [blame] | 101 | exit(1); |
| 102 | #endif |
| 103 | } |
| 104 | |
Ian Jackson | 2c42f1e | 2017-09-15 18:10:44 +0100 | [diff] [blame] | 105 | |
| 106 | static bool os_parse_runas_uid_gid(const char *optarg) |
| 107 | { |
| 108 | unsigned long lv; |
| 109 | const char *ep; |
| 110 | uid_t got_uid; |
| 111 | gid_t got_gid; |
| 112 | int rc; |
| 113 | |
| 114 | rc = qemu_strtoul(optarg, &ep, 0, &lv); |
| 115 | got_uid = lv; /* overflow here is ID in C99 */ |
| 116 | if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) { |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | rc = qemu_strtoul(ep + 1, 0, 0, &lv); |
| 121 | got_gid = lv; /* overflow here is ID in C99 */ |
| 122 | if (rc || got_gid != lv || got_gid == (gid_t)-1) { |
| 123 | return false; |
| 124 | } |
| 125 | |
| 126 | user_pwd = NULL; |
| 127 | user_uid = got_uid; |
| 128 | user_gid = got_gid; |
| 129 | return true; |
| 130 | } |
| 131 | |
Jes Sorensen | 59a5264 | 2010-06-10 11:42:25 +0200 | [diff] [blame] | 132 | /* |
| 133 | * Parse OS specific command line options. |
| 134 | * return 0 if option handled, -1 otherwise |
| 135 | */ |
Thomas Huth | 1217d6ca | 2018-05-04 19:01:07 +0200 | [diff] [blame] | 136 | int os_parse_cmd_args(int index, const char *optarg) |
Jes Sorensen | 59a5264 | 2010-06-10 11:42:25 +0200 | [diff] [blame] | 137 | { |
| 138 | switch (index) { |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 139 | case QEMU_OPTION_runas: |
| 140 | user_pwd = getpwnam(optarg); |
Ian Jackson | 2c42f1e | 2017-09-15 18:10:44 +0100 | [diff] [blame] | 141 | if (user_pwd) { |
| 142 | user_uid = -1; |
| 143 | user_gid = -1; |
| 144 | } else if (!os_parse_runas_uid_gid(optarg)) { |
| 145 | error_report("User \"%s\" doesn't exist" |
| 146 | " (and is not <uid>:<gid>)", |
| 147 | optarg); |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 148 | exit(1); |
| 149 | } |
| 150 | break; |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 151 | case QEMU_OPTION_chroot: |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 152 | warn_report("option is deprecated, use '-run-with chroot=...' instead"); |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 153 | chroot_dir = optarg; |
| 154 | break; |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 155 | case QEMU_OPTION_daemonize: |
| 156 | daemonize = 1; |
| 157 | break; |
Claudio Imbrenda | c891c24 | 2022-08-12 15:34:53 +0200 | [diff] [blame] | 158 | #if defined(CONFIG_LINUX) |
Claudio Imbrenda | 80bd81c | 2023-05-05 14:00:51 +0200 | [diff] [blame] | 159 | /* deprecated */ |
Claudio Imbrenda | c891c24 | 2022-08-12 15:34:53 +0200 | [diff] [blame] | 160 | case QEMU_OPTION_asyncteardown: |
| 161 | init_async_teardown(); |
| 162 | break; |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 163 | #endif |
Claudio Imbrenda | 80bd81c | 2023-05-05 14:00:51 +0200 | [diff] [blame] | 164 | case QEMU_OPTION_run_with: { |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 165 | const char *str; |
Claudio Imbrenda | 80bd81c | 2023-05-05 14:00:51 +0200 | [diff] [blame] | 166 | QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"), |
| 167 | optarg, false); |
| 168 | if (!opts) { |
| 169 | exit(1); |
| 170 | } |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 171 | #if defined(CONFIG_LINUX) |
Claudio Imbrenda | 80bd81c | 2023-05-05 14:00:51 +0200 | [diff] [blame] | 172 | if (qemu_opt_get_bool(opts, "async-teardown", false)) { |
| 173 | init_async_teardown(); |
| 174 | } |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 175 | #endif |
| 176 | str = qemu_opt_get(opts, "chroot"); |
| 177 | if (str) { |
| 178 | chroot_dir = str; |
| 179 | } |
Claudio Imbrenda | 80bd81c | 2023-05-05 14:00:51 +0200 | [diff] [blame] | 180 | break; |
| 181 | } |
Thomas Huth | 1217d6ca | 2018-05-04 19:01:07 +0200 | [diff] [blame] | 182 | default: |
| 183 | return -1; |
Jes Sorensen | 59a5264 | 2010-06-10 11:42:25 +0200 | [diff] [blame] | 184 | } |
Thomas Huth | 1217d6ca | 2018-05-04 19:01:07 +0200 | [diff] [blame] | 185 | |
| 186 | return 0; |
Jes Sorensen | 59a5264 | 2010-06-10 11:42:25 +0200 | [diff] [blame] | 187 | } |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 188 | |
Jes Sorensen | e06eb60 | 2010-06-10 11:42:29 +0200 | [diff] [blame] | 189 | static void change_process_uid(void) |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 190 | { |
Ian Jackson | 2c42f1e | 2017-09-15 18:10:44 +0100 | [diff] [blame] | 191 | assert((user_uid == (uid_t)-1) || user_pwd == NULL); |
| 192 | assert((user_uid == (uid_t)-1) == |
| 193 | (user_gid == (gid_t)-1)); |
| 194 | |
| 195 | if (user_pwd || user_uid != (uid_t)-1) { |
| 196 | gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid; |
| 197 | uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid; |
| 198 | if (setgid(intended_gid) < 0) { |
| 199 | error_report("Failed to setgid(%d)", intended_gid); |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 200 | exit(1); |
| 201 | } |
Ian Jackson | 2c42f1e | 2017-09-15 18:10:44 +0100 | [diff] [blame] | 202 | if (user_pwd) { |
| 203 | if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) { |
| 204 | error_report("Failed to initgroups(\"%s\", %d)", |
| 205 | user_pwd->pw_name, user_pwd->pw_gid); |
| 206 | exit(1); |
| 207 | } |
| 208 | } else { |
| 209 | if (setgroups(1, &user_gid) < 0) { |
| 210 | error_report("Failed to setgroups(1, [%d])", |
| 211 | user_gid); |
| 212 | exit(1); |
| 213 | } |
Stefan Hajnoczi | cc4662f | 2011-07-09 10:22:07 +0100 | [diff] [blame] | 214 | } |
Ian Jackson | 2c42f1e | 2017-09-15 18:10:44 +0100 | [diff] [blame] | 215 | if (setuid(intended_uid) < 0) { |
| 216 | error_report("Failed to setuid(%d)", intended_uid); |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 217 | exit(1); |
| 218 | } |
| 219 | if (setuid(0) != -1) { |
Ian Jackson | f0a2171 | 2018-04-16 15:08:03 +0100 | [diff] [blame] | 220 | error_report("Dropping privileges failed"); |
Jes Sorensen | 8847cfe | 2010-06-10 11:42:26 +0200 | [diff] [blame] | 221 | exit(1); |
| 222 | } |
| 223 | } |
| 224 | } |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 225 | |
Jes Sorensen | e06eb60 | 2010-06-10 11:42:29 +0200 | [diff] [blame] | 226 | static void change_root(void) |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 227 | { |
| 228 | if (chroot_dir) { |
| 229 | if (chroot(chroot_dir) < 0) { |
Ian Jackson | 22cd4f4 | 2018-04-16 15:15:51 +0100 | [diff] [blame] | 230 | error_report("chroot failed"); |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 231 | exit(1); |
| 232 | } |
| 233 | if (chdir("/")) { |
Ian Jackson | a7aaec1 | 2018-04-16 15:16:23 +0100 | [diff] [blame] | 234 | error_report("not able to chdir to /: %s", strerror(errno)); |
Jes Sorensen | 0766379 | 2010-06-10 11:42:27 +0200 | [diff] [blame] | 235 | exit(1); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | } |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 240 | |
| 241 | void os_daemonize(void) |
| 242 | { |
| 243 | if (daemonize) { |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 244 | pid_t pid; |
Michael Tokarev | 0be5e43 | 2014-10-30 17:30:51 +0300 | [diff] [blame] | 245 | int fds[2]; |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 246 | |
Marc-André Lureau | 3338a41 | 2022-04-22 14:47:59 +0400 | [diff] [blame] | 247 | if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) { |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 248 | exit(1); |
| 249 | } |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 250 | |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 251 | pid = fork(); |
| 252 | if (pid > 0) { |
| 253 | uint8_t status; |
| 254 | ssize_t len; |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 255 | |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 256 | close(fds[1]); |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 257 | |
Michael Tokarev | ccea25f | 2014-10-30 17:37:16 +0300 | [diff] [blame] | 258 | do { |
| 259 | len = read(fds[0], &status, 1); |
| 260 | } while (len < 0 && errno == EINTR); |
Michael Tokarev | fee78fd | 2014-10-30 17:40:48 +0300 | [diff] [blame] | 261 | |
| 262 | /* only exit successfully if our child actually wrote |
| 263 | * a one-byte zero to our pipe, upon successful init */ |
| 264 | exit(len == 1 && status == 0 ? 0 : 1); |
| 265 | |
| 266 | } else if (pid < 0) { |
| 267 | exit(1); |
| 268 | } |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 269 | |
| 270 | close(fds[0]); |
Michael Tokarev | 0be5e43 | 2014-10-30 17:30:51 +0300 | [diff] [blame] | 271 | daemon_pipe = fds[1]; |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 272 | |
| 273 | setsid(); |
| 274 | |
| 275 | pid = fork(); |
| 276 | if (pid > 0) { |
| 277 | exit(0); |
| 278 | } else if (pid < 0) { |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 279 | exit(1); |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 280 | } |
| 281 | umask(027); |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 282 | |
| 283 | signal(SIGTSTP, SIG_IGN); |
| 284 | signal(SIGTTOU, SIG_IGN); |
| 285 | signal(SIGTTIN, SIG_IGN); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void os_setup_post(void) |
| 290 | { |
| 291 | int fd = 0; |
| 292 | |
| 293 | if (daemonize) { |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 294 | if (chdir("/")) { |
Ian Jackson | a7aaec1 | 2018-04-16 15:16:23 +0100 | [diff] [blame] | 295 | error_report("not able to chdir to /: %s", strerror(errno)); |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 296 | exit(1); |
| 297 | } |
Nikita Ivanov | 8b6aa69 | 2022-10-23 12:04:21 +0300 | [diff] [blame] | 298 | fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR)); |
Gonglei | 63ce8e1 | 2014-09-26 16:14:30 +0800 | [diff] [blame] | 299 | if (fd == -1) { |
| 300 | exit(1); |
| 301 | } |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Jes Sorensen | e06eb60 | 2010-06-10 11:42:29 +0200 | [diff] [blame] | 304 | change_root(); |
| 305 | change_process_uid(); |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 306 | |
| 307 | if (daemonize) { |
Michael Tokarev | 25cec2b | 2014-10-30 17:47:46 +0300 | [diff] [blame] | 308 | uint8_t status = 0; |
| 309 | ssize_t len; |
| 310 | |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 311 | dup2(fd, 0); |
| 312 | dup2(fd, 1); |
Dimitris Aragiorgis | 96c33a4 | 2016-02-18 13:38:38 +0200 | [diff] [blame] | 313 | /* In case -D is given do not redirect stderr to /dev/null */ |
Richard Henderson | 229ef2e | 2022-04-17 11:29:45 -0700 | [diff] [blame] | 314 | if (!qemu_log_enabled()) { |
Dimitris Aragiorgis | 96c33a4 | 2016-02-18 13:38:38 +0200 | [diff] [blame] | 315 | dup2(fd, 2); |
| 316 | } |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 317 | |
| 318 | close(fd); |
Michael Tokarev | 25cec2b | 2014-10-30 17:47:46 +0300 | [diff] [blame] | 319 | |
| 320 | do { |
| 321 | len = write(daemon_pipe, &status, 1); |
| 322 | } while (len < 0 && errno == EINTR); |
| 323 | if (len != 1) { |
| 324 | exit(1); |
| 325 | } |
Jes Sorensen | eb505be | 2010-06-10 11:42:28 +0200 | [diff] [blame] | 326 | } |
| 327 | } |
| 328 | |
Jes Sorensen | 9156d76 | 2010-06-10 11:42:30 +0200 | [diff] [blame] | 329 | void os_set_line_buffering(void) |
| 330 | { |
| 331 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 332 | } |
Jes Sorensen | 949d31e | 2010-10-26 10:39:22 +0200 | [diff] [blame] | 333 | |
Hitoshi Mitake | 995ee2b | 2012-09-15 01:15:41 +0900 | [diff] [blame] | 334 | bool is_daemonized(void) |
| 335 | { |
| 336 | return daemonize; |
| 337 | } |
Satoru Moriya | 888a6bc | 2013-04-19 16:42:06 +0200 | [diff] [blame] | 338 | |
Hanna Reitz | f22ac47 | 2022-03-03 17:48:11 +0100 | [diff] [blame] | 339 | int os_set_daemonize(bool d) |
| 340 | { |
| 341 | daemonize = d; |
| 342 | return 0; |
| 343 | } |
| 344 | |
Satoru Moriya | 888a6bc | 2013-04-19 16:42:06 +0200 | [diff] [blame] | 345 | int os_mlock(void) |
| 346 | { |
David CARLIER | 195588c | 2020-07-13 14:36:09 +0100 | [diff] [blame] | 347 | #ifdef HAVE_MLOCKALL |
Satoru Moriya | 888a6bc | 2013-04-19 16:42:06 +0200 | [diff] [blame] | 348 | int ret = 0; |
| 349 | |
| 350 | ret = mlockall(MCL_CURRENT | MCL_FUTURE); |
| 351 | if (ret < 0) { |
Ian Jackson | a7aaec1 | 2018-04-16 15:16:23 +0100 | [diff] [blame] | 352 | error_report("mlockall: %s", strerror(errno)); |
Satoru Moriya | 888a6bc | 2013-04-19 16:42:06 +0200 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | return ret; |
David CARLIER | 195588c | 2020-07-13 14:36:09 +0100 | [diff] [blame] | 356 | #else |
| 357 | return -ENOSYS; |
| 358 | #endif |
Satoru Moriya | 888a6bc | 2013-04-19 16:42:06 +0200 | [diff] [blame] | 359 | } |
Thomas Huth | 9ffcbe2 | 2023-07-03 09:44:47 +0200 | [diff] [blame] | 360 | |
| 361 | static QemuOptsList qemu_run_with_opts = { |
| 362 | .name = "run-with", |
| 363 | .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head), |
| 364 | .desc = { |
| 365 | #if defined(CONFIG_LINUX) |
| 366 | { |
| 367 | .name = "async-teardown", |
| 368 | .type = QEMU_OPT_BOOL, |
| 369 | }, |
| 370 | #endif |
| 371 | { |
| 372 | .name = "chroot", |
| 373 | .type = QEMU_OPT_STRING, |
| 374 | }, |
| 375 | { /* end of list */ } |
| 376 | }, |
| 377 | }; |
| 378 | |
| 379 | static void register_runwith(void) |
| 380 | { |
| 381 | qemu_add_opts(&qemu_run_with_opts); |
| 382 | } |
| 383 | opts_init(register_runwith); |