target/ppc: Clean up local variable shadowing in kvm_arch_*_registers()
[qemu/armbru.git] / os-posix.c
blob52ef6990ff9d4c4e2cd52b6f6a820cdbb8efb9fb
1 /*
2 * os-posix.c
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
23 * THE SOFTWARE.
26 #include "qemu/osdep.h"
27 #include <sys/wait.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <libgen.h>
32 #include "qemu/error-report.h"
33 #include "qemu/log.h"
34 #include "sysemu/runstate.h"
35 #include "qemu/cutils.h"
37 #ifdef CONFIG_LINUX
38 #include <sys/prctl.h>
39 #endif
42 void os_setup_early_signal_handling(void)
44 struct sigaction act;
45 sigfillset(&act.sa_mask);
46 act.sa_flags = 0;
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)
58 struct sigaction act;
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)
71 char name[16];
72 if (!s)
73 return;
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));
79 exit(1);
81 #else
82 error_report("Change of process name not supported by your OS");
83 exit(1);
84 #endif
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)
105 unsigned long lv;
106 const char *ep;
107 uid_t got_uid;
108 gid_t got_gid;
109 int rc;
111 user_pwd = getpwnam(user_id);
112 if (user_pwd) {
113 user_uid = -1;
114 user_gid = -1;
115 return true;
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) {
121 return false;
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) {
127 return false;
130 user_pwd = NULL;
131 user_uid = got_uid;
132 user_gid = got_gid;
133 return true;
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);
147 exit(1);
149 if (user_pwd) {
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);
153 exit(1);
155 } else {
156 if (setgroups(1, &user_gid) < 0) {
157 error_report("Failed to setgroups(1, [%d])",
158 user_gid);
159 exit(1);
162 if (setuid(intended_uid) < 0) {
163 error_report("Failed to setuid(%d)", intended_uid);
164 exit(1);
166 if (setuid(0) != -1) {
167 error_report("Dropping privileges failed");
168 exit(1);
174 static const char *chroot_dir;
176 void os_set_chroot(const char *path)
178 chroot_dir = path;
181 static void change_root(void)
183 if (chroot_dir) {
184 if (chroot(chroot_dir) < 0) {
185 error_report("chroot failed");
186 exit(1);
188 if (chdir("/")) {
189 error_report("not able to chdir to /: %s", strerror(errno));
190 exit(1);
197 static int daemonize;
198 static int daemon_pipe;
200 bool is_daemonized(void)
202 return daemonize;
205 int os_set_daemonize(bool d)
207 daemonize = d;
208 return 0;
211 void os_daemonize(void)
213 if (daemonize) {
214 pid_t pid;
215 int fds[2];
217 if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
218 exit(1);
221 pid = fork();
222 if (pid > 0) {
223 uint8_t status;
224 ssize_t len;
226 close(fds[1]);
228 do {
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) {
237 exit(1);
240 close(fds[0]);
241 daemon_pipe = fds[1];
243 setsid();
245 pid = fork();
246 if (pid > 0) {
247 exit(0);
248 } else if (pid < 0) {
249 exit(1);
251 umask(027);
253 signal(SIGTSTP, SIG_IGN);
254 signal(SIGTTOU, SIG_IGN);
255 signal(SIGTTIN, SIG_IGN);
259 void os_setup_post(void)
261 int fd = 0;
263 if (daemonize) {
264 if (chdir("/")) {
265 error_report("not able to chdir to /: %s", strerror(errno));
266 exit(1);
268 fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR));
269 if (fd == -1) {
270 exit(1);
274 change_root();
275 change_process_uid();
277 if (daemonize) {
278 uint8_t status = 0;
279 ssize_t len;
281 dup2(fd, 0);
282 dup2(fd, 1);
283 /* In case -D is given do not redirect stderr to /dev/null */
284 if (!qemu_log_enabled()) {
285 dup2(fd, 2);
288 close(fd);
290 do {
291 len = write(daemon_pipe, &status, 1);
292 } while (len < 0 && errno == EINTR);
293 if (len != 1) {
294 exit(1);
299 void os_set_line_buffering(void)
301 setvbuf(stdout, NULL, _IOLBF, 0);
304 int os_mlock(void)
306 #ifdef HAVE_MLOCKALL
307 int ret = 0;
309 ret = mlockall(MCL_CURRENT | MCL_FUTURE);
310 if (ret < 0) {
311 error_report("mlockall: %s", strerror(errno));
314 return ret;
315 #else
316 return -ENOSYS;
317 #endif