]> git.proxmox.com Git - mirror_qemu.git/blame - os-posix.c
tests: acpi: x86: whitelist expected blobs
[mirror_qemu.git] / os-posix.c
CommitLineData
86b645e7
JS
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
d38ea87a 26#include "qemu/osdep.h"
8d963e6a 27#include <sys/wait.h>
8847cfe8 28#include <pwd.h>
cc4662f9 29#include <grp.h>
6170540b 30#include <libgen.h>
86b645e7
JS
31
32/* Needed early for CONFIG_BSD etc. */
59a5264b 33#include "net/slirp.h"
fd5fc4b1 34#include "qemu/qemu-options.h"
f853ac66 35#include "qemu/error-report.h"
96c33a45 36#include "qemu/log.h"
54d31236 37#include "sysemu/runstate.h"
f348b6d1 38#include "qemu/cutils.h"
80bd81ca
CI
39#include "qemu/config-file.h"
40#include "qemu/option.h"
9ffcbe2a 41#include "qemu/module.h"
86b645e7 42
ce798cf2
JS
43#ifdef CONFIG_LINUX
44#include <sys/prctl.h>
c891c24b 45#include "qemu/async-teardown.h"
949d31e6
JS
46#endif
47
2c42f1e8
IJ
48/*
49 * Must set all three of these at once.
50 * Legal combinations are unset by name by uid
51 */
52static struct passwd *user_pwd; /* NULL non-NULL NULL */
53static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
54static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
55
0766379d 56static const char *chroot_dir;
eb505be1 57static int daemonize;
0be5e436 58static int daemon_pipe;
8847cfe8 59
fe98ac14 60void os_setup_early_signal_handling(void)
86b645e7
JS
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}
8d963e6a 68
f64622c4 69static void termsig_handler(int signal, siginfo_t *info, void *c)
8d963e6a 70{
f64622c4 71 qemu_system_killed(info->si_signo, info->si_pid);
8d963e6a
JS
72}
73
8d963e6a
JS
74void os_setup_signal_handling(void)
75{
76 struct sigaction act;
77
78 memset(&act, 0, sizeof(act));
f64622c4
GN
79 act.sa_sigaction = termsig_handler;
80 act.sa_flags = SA_SIGINFO;
8d963e6a
JS
81 sigaction(SIGINT, &act, NULL);
82 sigaction(SIGHUP, &act, NULL);
83 sigaction(SIGTERM, &act, NULL);
8d963e6a 84}
6170540b 85
ce798cf2
JS
86void os_set_proc_name(const char *s)
87{
88#if defined(PR_SET_NAME)
89 char name[16];
90 if (!s)
91 return;
3eadc68e 92 pstrcpy(name, sizeof(name), s);
ce798cf2
JS
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)) {
a7aaec14 96 error_report("unable to change process name: %s", strerror(errno));
ce798cf2
JS
97 exit(1);
98 }
99#else
22cd4f48 100 error_report("Change of process name not supported by your OS");
ce798cf2
JS
101 exit(1);
102#endif
103}
104
2c42f1e8
IJ
105
106static 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
59a5264b
JS
132/*
133 * Parse OS specific command line options.
134 * return 0 if option handled, -1 otherwise
135 */
1217d6ca 136int os_parse_cmd_args(int index, const char *optarg)
59a5264b
JS
137{
138 switch (index) {
8847cfe8
JS
139 case QEMU_OPTION_runas:
140 user_pwd = getpwnam(optarg);
2c42f1e8
IJ
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);
8847cfe8
JS
148 exit(1);
149 }
150 break;
0766379d 151 case QEMU_OPTION_chroot:
9ffcbe2a 152 warn_report("option is deprecated, use '-run-with chroot=...' instead");
0766379d
JS
153 chroot_dir = optarg;
154 break;
eb505be1
JS
155 case QEMU_OPTION_daemonize:
156 daemonize = 1;
157 break;
c891c24b 158#if defined(CONFIG_LINUX)
80bd81ca 159 /* deprecated */
c891c24b
CI
160 case QEMU_OPTION_asyncteardown:
161 init_async_teardown();
162 break;
9ffcbe2a 163#endif
80bd81ca 164 case QEMU_OPTION_run_with: {
9ffcbe2a 165 const char *str;
80bd81ca
CI
166 QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"),
167 optarg, false);
168 if (!opts) {
169 exit(1);
170 }
9ffcbe2a 171#if defined(CONFIG_LINUX)
80bd81ca
CI
172 if (qemu_opt_get_bool(opts, "async-teardown", false)) {
173 init_async_teardown();
174 }
9ffcbe2a
TH
175#endif
176 str = qemu_opt_get(opts, "chroot");
177 if (str) {
178 chroot_dir = str;
179 }
80bd81ca
CI
180 break;
181 }
1217d6ca
TH
182 default:
183 return -1;
59a5264b 184 }
1217d6ca
TH
185
186 return 0;
59a5264b 187}
8847cfe8 188
e06eb601 189static void change_process_uid(void)
8847cfe8 190{
2c42f1e8
IJ
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);
8847cfe8
JS
200 exit(1);
201 }
2c42f1e8
IJ
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 }
cc4662f9 214 }
2c42f1e8
IJ
215 if (setuid(intended_uid) < 0) {
216 error_report("Failed to setuid(%d)", intended_uid);
8847cfe8
JS
217 exit(1);
218 }
219 if (setuid(0) != -1) {
f0a2171b 220 error_report("Dropping privileges failed");
8847cfe8
JS
221 exit(1);
222 }
223 }
224}
0766379d 225
e06eb601 226static void change_root(void)
0766379d
JS
227{
228 if (chroot_dir) {
229 if (chroot(chroot_dir) < 0) {
22cd4f48 230 error_report("chroot failed");
0766379d
JS
231 exit(1);
232 }
233 if (chdir("/")) {
a7aaec14 234 error_report("not able to chdir to /: %s", strerror(errno));
0766379d
JS
235 exit(1);
236 }
237 }
238
239}
eb505be1
JS
240
241void os_daemonize(void)
242{
243 if (daemonize) {
63ce8e15 244 pid_t pid;
0be5e436 245 int fds[2];
eb505be1 246
3338a41f 247 if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
63ce8e15
GA
248 exit(1);
249 }
eb505be1 250
63ce8e15
GA
251 pid = fork();
252 if (pid > 0) {
253 uint8_t status;
254 ssize_t len;
eb505be1 255
63ce8e15 256 close(fds[1]);
eb505be1 257
ccea25f1
MT
258 do {
259 len = read(fds[0], &status, 1);
260 } while (len < 0 && errno == EINTR);
fee78fd6
MT
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 }
eb505be1 269
63ce8e15 270 close(fds[0]);
0be5e436 271 daemon_pipe = fds[1];
eb505be1 272
63ce8e15 273 setsid();
eb505be1 274
63ce8e15
GA
275 pid = fork();
276 if (pid > 0) {
277 exit(0);
278 } else if (pid < 0) {
279 exit(1);
280 }
281 umask(027);
eb505be1
JS
282
283 signal(SIGTSTP, SIG_IGN);
284 signal(SIGTTOU, SIG_IGN);
285 signal(SIGTTIN, SIG_IGN);
286 }
287}
288
289void os_setup_post(void)
290{
291 int fd = 0;
292
293 if (daemonize) {
eb505be1 294 if (chdir("/")) {
a7aaec14 295 error_report("not able to chdir to /: %s", strerror(errno));
eb505be1
JS
296 exit(1);
297 }
8b6aa693 298 fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR));
63ce8e15
GA
299 if (fd == -1) {
300 exit(1);
301 }
eb505be1
JS
302 }
303
e06eb601
JS
304 change_root();
305 change_process_uid();
eb505be1
JS
306
307 if (daemonize) {
25cec2b8
MT
308 uint8_t status = 0;
309 ssize_t len;
310
eb505be1
JS
311 dup2(fd, 0);
312 dup2(fd, 1);
96c33a45 313 /* In case -D is given do not redirect stderr to /dev/null */
229ef2eb 314 if (!qemu_log_enabled()) {
96c33a45
DA
315 dup2(fd, 2);
316 }
eb505be1
JS
317
318 close(fd);
25cec2b8
MT
319
320 do {
321 len = write(daemon_pipe, &status, 1);
322 } while (len < 0 && errno == EINTR);
323 if (len != 1) {
324 exit(1);
325 }
eb505be1
JS
326 }
327}
328
9156d763
JS
329void os_set_line_buffering(void)
330{
331 setvbuf(stdout, NULL, _IOLBF, 0);
332}
949d31e6 333
995ee2bf
HM
334bool is_daemonized(void)
335{
336 return daemonize;
337}
888a6bc6 338
f22ac472
HR
339int os_set_daemonize(bool d)
340{
341 daemonize = d;
342 return 0;
343}
344
888a6bc6
SM
345int os_mlock(void)
346{
195588cc 347#ifdef HAVE_MLOCKALL
888a6bc6
SM
348 int ret = 0;
349
350 ret = mlockall(MCL_CURRENT | MCL_FUTURE);
351 if (ret < 0) {
a7aaec14 352 error_report("mlockall: %s", strerror(errno));
888a6bc6
SM
353 }
354
355 return ret;
195588cc
DC
356#else
357 return -ENOSYS;
358#endif
888a6bc6 359}
9ffcbe2a
TH
360
361static 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
379static void register_runwith(void)
380{
381 qemu_add_opts(&qemu_run_with_opts);
382}
383opts_init(register_runwith);