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 #include "qemu/error-report.h"
34 #include "sysemu/runstate.h"
35 #include "qemu/cutils.h"
38 #include <sys/prctl.h>
42 void os_setup_early_signal_handling(void)
45 sigfillset(&act.sa_mask);
47 act.sa_handler = SIG_IGN;
48 sigaction(SIGPIPE, &act, NULL);
51 static void termsig_handler(int signal, siginfo_t *info, void *c)
53 qemu_system_killed(info->si_signo, info->si_pid);
56 void os_setup_signal_handling(void)
60 memset(&act, 0, sizeof(act));
61 act.sa_sigaction = termsig_handler;
62 act.sa_flags = SA_SIGINFO;
63 sigaction(SIGINT, &act, NULL);
64 sigaction(SIGHUP, &act, NULL);
65 sigaction(SIGTERM, &act, NULL);
68 void os_set_proc_name(const char *s)
70 #if defined(PR_SET_NAME)
74 pstrcpy(name, sizeof(name), s);
75 /* Could rewrite argv[0] too, but that's a bit more complicated.
76 This simple way is enough for `top'. */
77 if (prctl(PR_SET_NAME, name)) {
78 error_report("unable to change process name: %s", strerror(errno));
82 error_report("Change of process name not supported by your OS");
89 * Must set all three of these at once.
90 * Legal combinations are unset by name by uid
92 static struct passwd *user_pwd; /* NULL non-NULL NULL */
93 static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
94 static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
97 * Prepare to change user ID. user_id can be one of 3 forms:
98 * - a username, in which case user ID will be changed to its uid,
99 * with primary and supplementary groups set up too;
100 * - a numeric uid, in which case only the uid will be set;
101 * - a pair of numeric uid:gid.
103 bool os_set_runas(const char *user_id)
111 user_pwd = getpwnam(user_id);
118 rc = qemu_strtoul(user_id, &ep, 0, &lv);
119 got_uid = lv; /* overflow here is ID in C99 */
120 if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
124 rc = qemu_strtoul(ep + 1, 0, 0, &lv);
125 got_gid = lv; /* overflow here is ID in C99 */
126 if (rc || got_gid != lv || got_gid == (gid_t)-1) {
136 static void change_process_uid(void)
138 assert((user_uid == (uid_t)-1) || user_pwd == NULL);
139 assert((user_uid == (uid_t)-1) ==
140 (user_gid == (gid_t)-1));
142 if (user_pwd || user_uid != (uid_t)-1) {
143 gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid;
144 uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid;
145 if (setgid(intended_gid) < 0) {
146 error_report("Failed to setgid(%d)", intended_gid);
150 if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
151 error_report("Failed to initgroups(\"%s\", %d)",
152 user_pwd->pw_name, user_pwd->pw_gid);
156 if (setgroups(1, &user_gid) < 0) {
157 error_report("Failed to setgroups(1, [%d])",
162 if (setuid(intended_uid) < 0) {
163 error_report("Failed to setuid(%d)", intended_uid);
166 if (setuid(0) != -1) {
167 error_report("Dropping privileges failed");
174 static const char *chroot_dir;
176 void os_set_chroot(const char *path)
181 static void change_root(void)
184 if (chroot(chroot_dir) < 0) {
185 error_report("chroot failed");
189 error_report("not able to chdir to /: %s", strerror(errno));
197 static int daemonize;
198 static int daemon_pipe;
200 bool is_daemonized(void)
205 int os_set_daemonize(bool d)
211 void os_daemonize(void)
217 if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
229 len = read(fds[0], &status, 1);
230 } while (len < 0 && errno == EINTR);
232 /* only exit successfully if our child actually wrote
233 * a one-byte zero to our pipe, upon successful init */
234 exit(len == 1 && status == 0 ? 0 : 1);
236 } else if (pid < 0) {
241 daemon_pipe = fds[1];
248 } else if (pid < 0) {
253 signal(SIGTSTP, SIG_IGN);
254 signal(SIGTTOU, SIG_IGN);
255 signal(SIGTTIN, SIG_IGN);
259 void os_setup_post(void)
265 error_report("not able to chdir to /: %s", strerror(errno));
268 fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR));
275 change_process_uid();
283 /* In case -D is given do not redirect stderr to /dev/null */
284 if (!qemu_log_enabled()) {
291 len = write(daemon_pipe, &status, 1);
292 } while (len < 0 && errno == EINTR);
299 void os_set_line_buffering(void)
301 setvbuf(stdout, NULL, _IOLBF, 0);
309 ret = mlockall(MCL_CURRENT | MCL_FUTURE);
311 error_report("mlockall: %s", strerror(errno));