4 * Copyright (c) 2003-2008 Fabrice Bellard
5 * Copyright (c) 2010 Red Hat, Inc.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include "qemu/osdep.h"
32 /* Needed early for CONFIG_BSD etc. */
33 #include "net/slirp.h"
34 #include "qemu/qemu-options.h"
35 #include "qemu/error-report.h"
37 #include "sysemu/runstate.h"
38 #include "qemu/cutils.h"
39 #include "qemu/config-file.h"
40 #include "qemu/option.h"
41 #include "qemu/module.h"
44 #include <sys/prctl.h>
45 #include "qemu/async-teardown.h"
49 * Must set all three of these at once.
50 * Legal combinations are unset by name by uid
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 */
56 static const char *chroot_dir
;
58 static int daemon_pipe
;
60 void os_setup_early_signal_handling(void)
63 sigfillset(&act
.sa_mask
);
65 act
.sa_handler
= SIG_IGN
;
66 sigaction(SIGPIPE
, &act
, NULL
);
69 static void termsig_handler(int signal
, siginfo_t
*info
, void *c
)
71 qemu_system_killed(info
->si_signo
, info
->si_pid
);
74 void os_setup_signal_handling(void)
78 memset(&act
, 0, sizeof(act
));
79 act
.sa_sigaction
= termsig_handler
;
80 act
.sa_flags
= SA_SIGINFO
;
81 sigaction(SIGINT
, &act
, NULL
);
82 sigaction(SIGHUP
, &act
, NULL
);
83 sigaction(SIGTERM
, &act
, NULL
);
86 void os_set_proc_name(const char *s
)
88 #if defined(PR_SET_NAME)
92 pstrcpy(name
, sizeof(name
), s
);
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
)) {
96 error_report("unable to change process name: %s", strerror(errno
));
100 error_report("Change of process name not supported by your OS");
106 static bool os_parse_runas_uid_gid(const char *optarg
)
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) {
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) {
133 * Parse OS specific command line options.
134 * return 0 if option handled, -1 otherwise
136 int os_parse_cmd_args(int index
, const char *optarg
)
139 case QEMU_OPTION_runas
:
140 user_pwd
= getpwnam(optarg
);
144 } else if (!os_parse_runas_uid_gid(optarg
)) {
145 error_report("User \"%s\" doesn't exist"
146 " (and is not <uid>:<gid>)",
151 case QEMU_OPTION_chroot
:
152 warn_report("option is deprecated, use '-run-with chroot=...' instead");
155 case QEMU_OPTION_daemonize
:
158 #if defined(CONFIG_LINUX)
160 case QEMU_OPTION_asyncteardown
:
161 init_async_teardown();
164 case QEMU_OPTION_run_with
: {
166 QemuOpts
*opts
= qemu_opts_parse_noisily(qemu_find_opts("run-with"),
171 #if defined(CONFIG_LINUX)
172 if (qemu_opt_get_bool(opts
, "async-teardown", false)) {
173 init_async_teardown();
176 str
= qemu_opt_get(opts
, "chroot");
189 static void change_process_uid(void)
191 assert((user_uid
== (uid_t
)-1) || user_pwd
== NULL
);
192 assert((user_uid
== (uid_t
)-1) ==
193 (user_gid
== (gid_t
)-1));
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
);
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
);
209 if (setgroups(1, &user_gid
) < 0) {
210 error_report("Failed to setgroups(1, [%d])",
215 if (setuid(intended_uid
) < 0) {
216 error_report("Failed to setuid(%d)", intended_uid
);
219 if (setuid(0) != -1) {
220 error_report("Dropping privileges failed");
226 static void change_root(void)
229 if (chroot(chroot_dir
) < 0) {
230 error_report("chroot failed");
234 error_report("not able to chdir to /: %s", strerror(errno
));
241 void os_daemonize(void)
247 if (!g_unix_open_pipe(fds
, FD_CLOEXEC
, NULL
)) {
259 len
= read(fds
[0], &status
, 1);
260 } while (len
< 0 && errno
== EINTR
);
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);
266 } else if (pid
< 0) {
271 daemon_pipe
= fds
[1];
278 } else if (pid
< 0) {
283 signal(SIGTSTP
, SIG_IGN
);
284 signal(SIGTTOU
, SIG_IGN
);
285 signal(SIGTTIN
, SIG_IGN
);
289 void os_setup_post(void)
295 error_report("not able to chdir to /: %s", strerror(errno
));
298 fd
= RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR
));
305 change_process_uid();
313 /* In case -D is given do not redirect stderr to /dev/null */
314 if (!qemu_log_enabled()) {
321 len
= write(daemon_pipe
, &status
, 1);
322 } while (len
< 0 && errno
== EINTR
);
329 void os_set_line_buffering(void)
331 setvbuf(stdout
, NULL
, _IOLBF
, 0);
334 bool is_daemonized(void)
339 int os_set_daemonize(bool d
)
350 ret
= mlockall(MCL_CURRENT
| MCL_FUTURE
);
352 error_report("mlockall: %s", strerror(errno
));
361 static QemuOptsList qemu_run_with_opts
= {
363 .head
= QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts
.head
),
365 #if defined(CONFIG_LINUX)
367 .name
= "async-teardown",
368 .type
= QEMU_OPT_BOOL
,
373 .type
= QEMU_OPT_STRING
,
375 { /* end of list */ }
379 static void register_runwith(void)
381 qemu_add_opts(&qemu_run_with_opts
);
383 opts_init(register_runwith
);