Merge remote branch 'qmp/for-anthony' into staging
[qemu/aliguori.git] / os-posix.c
blob38c29d1afe42ff3b2e7fc28f938e5798a2551053
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 <unistd.h>
27 #include <fcntl.h>
28 #include <signal.h>
29 #include <sys/types.h>
30 #include <sys/wait.h>
31 /*needed for MAP_POPULATE before including qemu-options.h */
32 #include <sys/mman.h>
33 #include <pwd.h>
34 #include <libgen.h>
36 /* Needed early for CONFIG_BSD etc. */
37 #include "config-host.h"
38 #include "sysemu.h"
39 #include "net/slirp.h"
40 #include "qemu-options.h"
42 #ifdef CONFIG_LINUX
43 #include <sys/prctl.h>
44 #endif
46 #ifdef CONFIG_EVENTFD
47 #include <sys/eventfd.h>
48 #endif
50 static struct passwd *user_pwd;
51 static const char *chroot_dir;
52 static int daemonize;
53 static int fds[2];
55 void os_setup_early_signal_handling(void)
57 struct sigaction act;
58 sigfillset(&act.sa_mask);
59 act.sa_flags = 0;
60 act.sa_handler = SIG_IGN;
61 sigaction(SIGPIPE, &act, NULL);
64 static void termsig_handler(int signal)
66 qemu_system_shutdown_request();
69 static void sigchld_handler(int signal)
71 waitpid(-1, NULL, WNOHANG);
74 void os_setup_signal_handling(void)
76 struct sigaction act;
78 memset(&act, 0, sizeof(act));
79 act.sa_handler = termsig_handler;
80 sigaction(SIGINT, &act, NULL);
81 sigaction(SIGHUP, &act, NULL);
82 sigaction(SIGTERM, &act, NULL);
84 act.sa_handler = sigchld_handler;
85 act.sa_flags = SA_NOCLDSTOP;
86 sigaction(SIGCHLD, &act, NULL);
89 /* Find a likely location for support files using the location of the binary.
90 For installed binaries this will be "$bindir/../share/qemu". When
91 running from the build tree this will be "$bindir/../pc-bios". */
92 #define SHARE_SUFFIX "/share/qemu"
93 #define BUILD_SUFFIX "/pc-bios"
94 char *os_find_datadir(const char *argv0)
96 char *dir;
97 char *p = NULL;
98 char *res;
99 char buf[PATH_MAX];
100 size_t max_len;
102 #if defined(__linux__)
104 int len;
105 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
106 if (len > 0) {
107 buf[len] = 0;
108 p = buf;
111 #elif defined(__FreeBSD__)
113 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
114 size_t len = sizeof(buf) - 1;
116 *buf = '\0';
117 if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
118 *buf) {
119 buf[sizeof(buf) - 1] = '\0';
120 p = buf;
123 #endif
124 /* If we don't have any way of figuring out the actual executable
125 location then try argv[0]. */
126 if (!p) {
127 p = realpath(argv0, buf);
128 if (!p) {
129 return NULL;
132 dir = dirname(p);
133 dir = dirname(dir);
135 max_len = strlen(dir) +
136 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
137 res = qemu_mallocz(max_len);
138 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
139 if (access(res, R_OK)) {
140 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
141 if (access(res, R_OK)) {
142 qemu_free(res);
143 res = NULL;
147 return res;
149 #undef SHARE_SUFFIX
150 #undef BUILD_SUFFIX
152 void os_set_proc_name(const char *s)
154 #if defined(PR_SET_NAME)
155 char name[16];
156 if (!s)
157 return;
158 name[sizeof(name) - 1] = 0;
159 strncpy(name, s, sizeof(name));
160 /* Could rewrite argv[0] too, but that's a bit more complicated.
161 This simple way is enough for `top'. */
162 if (prctl(PR_SET_NAME, name)) {
163 perror("unable to change process name");
164 exit(1);
166 #else
167 fprintf(stderr, "Change of process name not supported by your OS\n");
168 exit(1);
169 #endif
173 * Parse OS specific command line options.
174 * return 0 if option handled, -1 otherwise
176 void os_parse_cmd_args(int index, const char *optarg)
178 switch (index) {
179 #ifdef CONFIG_SLIRP
180 case QEMU_OPTION_smb:
181 if (net_slirp_smb(optarg) < 0)
182 exit(1);
183 break;
184 #endif
185 case QEMU_OPTION_runas:
186 user_pwd = getpwnam(optarg);
187 if (!user_pwd) {
188 fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
189 exit(1);
191 break;
192 case QEMU_OPTION_chroot:
193 chroot_dir = optarg;
194 break;
195 case QEMU_OPTION_daemonize:
196 daemonize = 1;
197 break;
199 return;
202 static void change_process_uid(void)
204 if (user_pwd) {
205 if (setgid(user_pwd->pw_gid) < 0) {
206 fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
207 exit(1);
209 if (setuid(user_pwd->pw_uid) < 0) {
210 fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
211 exit(1);
213 if (setuid(0) != -1) {
214 fprintf(stderr, "Dropping privileges failed\n");
215 exit(1);
220 static void change_root(void)
222 if (chroot_dir) {
223 if (chroot(chroot_dir) < 0) {
224 fprintf(stderr, "chroot failed\n");
225 exit(1);
227 if (chdir("/")) {
228 perror("not able to chdir to /");
229 exit(1);
235 void os_daemonize(void)
237 if (daemonize) {
238 pid_t pid;
240 if (pipe(fds) == -1)
241 exit(1);
243 pid = fork();
244 if (pid > 0) {
245 uint8_t status;
246 ssize_t len;
248 close(fds[1]);
250 again:
251 len = read(fds[0], &status, 1);
252 if (len == -1 && (errno == EINTR))
253 goto again;
255 if (len != 1)
256 exit(1);
257 else if (status == 1) {
258 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
259 exit(1);
260 } else
261 exit(0);
262 } else if (pid < 0)
263 exit(1);
265 close(fds[0]);
266 qemu_set_cloexec(fds[1]);
268 setsid();
270 pid = fork();
271 if (pid > 0)
272 exit(0);
273 else if (pid < 0)
274 exit(1);
276 umask(027);
278 signal(SIGTSTP, SIG_IGN);
279 signal(SIGTTOU, SIG_IGN);
280 signal(SIGTTIN, SIG_IGN);
284 void os_setup_post(void)
286 int fd = 0;
288 if (daemonize) {
289 uint8_t status = 0;
290 ssize_t len;
292 again1:
293 len = write(fds[1], &status, 1);
294 if (len == -1 && (errno == EINTR))
295 goto again1;
297 if (len != 1)
298 exit(1);
300 if (chdir("/")) {
301 perror("not able to chdir to /");
302 exit(1);
304 TFR(fd = qemu_open("/dev/null", O_RDWR));
305 if (fd == -1)
306 exit(1);
309 change_root();
310 change_process_uid();
312 if (daemonize) {
313 dup2(fd, 0);
314 dup2(fd, 1);
315 dup2(fd, 2);
317 close(fd);
321 void os_pidfile_error(void)
323 if (daemonize) {
324 uint8_t status = 1;
325 if (write(fds[1], &status, 1) != 1) {
326 perror("daemonize. Writing to pipe\n");
328 } else
329 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
332 void os_set_line_buffering(void)
334 setvbuf(stdout, NULL, _IOLBF, 0);
338 * Creates an eventfd that looks like a pipe and has EFD_CLOEXEC set.
340 int qemu_eventfd(int fds[2])
342 #ifdef CONFIG_EVENTFD
343 int ret;
345 ret = eventfd(0, 0);
346 if (ret >= 0) {
347 fds[0] = ret;
348 qemu_set_cloexec(ret);
349 if ((fds[1] = dup(ret)) == -1) {
350 close(ret);
351 return -1;
353 qemu_set_cloexec(fds[1]);
354 return 0;
357 if (errno != ENOSYS) {
358 return -1;
360 #endif
362 return qemu_pipe(fds);
365 int qemu_create_pidfile(const char *filename)
367 char buffer[128];
368 int len;
369 int fd;
371 fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
372 if (fd == -1) {
373 return -1;
375 if (lockf(fd, F_TLOCK, 0) == -1) {
376 return -1;
378 len = snprintf(buffer, sizeof(buffer), "%ld\n", (long)getpid());
379 if (write(fd, buffer, len) != len) {
380 return -1;
383 return 0;