]> git.proxmox.com Git - qemu.git/blame - os-posix.c
qemu-barrier: Fix compiler version check for future gcc versions
[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
26#include <unistd.h>
27#include <fcntl.h>
28#include <signal.h>
8d963e6a
JS
29#include <sys/types.h>
30#include <sys/wait.h>
69e8b162
JS
31/*needed for MAP_POPULATE before including qemu-options.h */
32#include <sys/mman.h>
8847cfe8 33#include <pwd.h>
cc4662f9 34#include <grp.h>
6170540b 35#include <libgen.h>
86b645e7
JS
36
37/* Needed early for CONFIG_BSD etc. */
38#include "config-host.h"
39#include "sysemu.h"
59a5264b
JS
40#include "net/slirp.h"
41#include "qemu-options.h"
86b645e7 42
ce798cf2
JS
43#ifdef CONFIG_LINUX
44#include <sys/prctl.h>
949d31e6
JS
45#endif
46
15fdaee3
NW
47#ifdef __FreeBSD__
48#include <sys/sysctl.h>
49#endif
50
8847cfe8 51static struct passwd *user_pwd;
0766379d 52static const char *chroot_dir;
eb505be1
JS
53static int daemonize;
54static int fds[2];
8847cfe8 55
fe98ac14 56void os_setup_early_signal_handling(void)
86b645e7
JS
57{
58 struct sigaction act;
59 sigfillset(&act.sa_mask);
60 act.sa_flags = 0;
61 act.sa_handler = SIG_IGN;
62 sigaction(SIGPIPE, &act, NULL);
63}
8d963e6a 64
f64622c4 65static void termsig_handler(int signal, siginfo_t *info, void *c)
8d963e6a 66{
f64622c4 67 qemu_system_killed(info->si_signo, info->si_pid);
8d963e6a
JS
68}
69
8d963e6a
JS
70void os_setup_signal_handling(void)
71{
72 struct sigaction act;
73
74 memset(&act, 0, sizeof(act));
f64622c4
GN
75 act.sa_sigaction = termsig_handler;
76 act.sa_flags = SA_SIGINFO;
8d963e6a
JS
77 sigaction(SIGINT, &act, NULL);
78 sigaction(SIGHUP, &act, NULL);
79 sigaction(SIGTERM, &act, NULL);
8d963e6a 80}
6170540b
JS
81
82/* Find a likely location for support files using the location of the binary.
83 For installed binaries this will be "$bindir/../share/qemu". When
84 running from the build tree this will be "$bindir/../pc-bios". */
85#define SHARE_SUFFIX "/share/qemu"
86#define BUILD_SUFFIX "/pc-bios"
87char *os_find_datadir(const char *argv0)
88{
89 char *dir;
90 char *p = NULL;
91 char *res;
92 char buf[PATH_MAX];
93 size_t max_len;
94
95#if defined(__linux__)
96 {
97 int len;
98 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
99 if (len > 0) {
100 buf[len] = 0;
101 p = buf;
102 }
103 }
104#elif defined(__FreeBSD__)
105 {
106 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
107 size_t len = sizeof(buf) - 1;
108
109 *buf = '\0';
66fe09ee 110 if (!sysctl(mib, ARRAY_SIZE(mib), buf, &len, NULL, 0) &&
6170540b
JS
111 *buf) {
112 buf[sizeof(buf) - 1] = '\0';
113 p = buf;
114 }
115 }
116#endif
117 /* If we don't have any way of figuring out the actual executable
118 location then try argv[0]. */
119 if (!p) {
120 p = realpath(argv0, buf);
121 if (!p) {
122 return NULL;
123 }
124 }
125 dir = dirname(p);
126 dir = dirname(dir);
127
128 max_len = strlen(dir) +
129 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
7267c094 130 res = g_malloc0(max_len);
6170540b
JS
131 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
132 if (access(res, R_OK)) {
133 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
134 if (access(res, R_OK)) {
7267c094 135 g_free(res);
6170540b
JS
136 res = NULL;
137 }
138 }
139
140 return res;
141}
142#undef SHARE_SUFFIX
143#undef BUILD_SUFFIX
59a5264b 144
ce798cf2
JS
145void os_set_proc_name(const char *s)
146{
147#if defined(PR_SET_NAME)
148 char name[16];
149 if (!s)
150 return;
151 name[sizeof(name) - 1] = 0;
152 strncpy(name, s, sizeof(name));
153 /* Could rewrite argv[0] too, but that's a bit more complicated.
154 This simple way is enough for `top'. */
155 if (prctl(PR_SET_NAME, name)) {
156 perror("unable to change process name");
157 exit(1);
158 }
159#else
160 fprintf(stderr, "Change of process name not supported by your OS\n");
161 exit(1);
162#endif
163}
164
59a5264b
JS
165/*
166 * Parse OS specific command line options.
167 * return 0 if option handled, -1 otherwise
168 */
169void os_parse_cmd_args(int index, const char *optarg)
170{
171 switch (index) {
172#ifdef CONFIG_SLIRP
173 case QEMU_OPTION_smb:
174 if (net_slirp_smb(optarg) < 0)
175 exit(1);
176 break;
177#endif
8847cfe8
JS
178 case QEMU_OPTION_runas:
179 user_pwd = getpwnam(optarg);
180 if (!user_pwd) {
181 fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
182 exit(1);
183 }
184 break;
0766379d
JS
185 case QEMU_OPTION_chroot:
186 chroot_dir = optarg;
187 break;
eb505be1
JS
188 case QEMU_OPTION_daemonize:
189 daemonize = 1;
190 break;
70678b82
AL
191#if defined(CONFIG_LINUX)
192 case QEMU_OPTION_enablefips:
193 fips_set_state(true);
194 break;
195#endif
59a5264b 196 }
59a5264b 197}
8847cfe8 198
e06eb601 199static void change_process_uid(void)
8847cfe8
JS
200{
201 if (user_pwd) {
202 if (setgid(user_pwd->pw_gid) < 0) {
203 fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
204 exit(1);
205 }
cc4662f9
SH
206 if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
207 fprintf(stderr, "Failed to initgroups(\"%s\", %d)\n",
208 user_pwd->pw_name, user_pwd->pw_gid);
209 exit(1);
210 }
8847cfe8
JS
211 if (setuid(user_pwd->pw_uid) < 0) {
212 fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
213 exit(1);
214 }
215 if (setuid(0) != -1) {
216 fprintf(stderr, "Dropping privileges failed\n");
217 exit(1);
218 }
219 }
220}
0766379d 221
e06eb601 222static void change_root(void)
0766379d
JS
223{
224 if (chroot_dir) {
225 if (chroot(chroot_dir) < 0) {
226 fprintf(stderr, "chroot failed\n");
227 exit(1);
228 }
229 if (chdir("/")) {
230 perror("not able to chdir to /");
231 exit(1);
232 }
233 }
234
235}
eb505be1
JS
236
237void os_daemonize(void)
238{
239 if (daemonize) {
240 pid_t pid;
241
242 if (pipe(fds) == -1)
243 exit(1);
244
245 pid = fork();
246 if (pid > 0) {
247 uint8_t status;
248 ssize_t len;
249
250 close(fds[1]);
251
252 again:
253 len = read(fds[0], &status, 1);
254 if (len == -1 && (errno == EINTR))
255 goto again;
256
257 if (len != 1)
258 exit(1);
259 else if (status == 1) {
260 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
261 exit(1);
262 } else
263 exit(0);
264 } else if (pid < 0)
265 exit(1);
266
267 close(fds[0]);
268 qemu_set_cloexec(fds[1]);
269
270 setsid();
271
272 pid = fork();
273 if (pid > 0)
274 exit(0);
275 else if (pid < 0)
276 exit(1);
277
278 umask(027);
279
280 signal(SIGTSTP, SIG_IGN);
281 signal(SIGTTOU, SIG_IGN);
282 signal(SIGTTIN, SIG_IGN);
283 }
284}
285
286void os_setup_post(void)
287{
288 int fd = 0;
289
290 if (daemonize) {
291 uint8_t status = 0;
292 ssize_t len;
293
294 again1:
295 len = write(fds[1], &status, 1);
296 if (len == -1 && (errno == EINTR))
297 goto again1;
298
299 if (len != 1)
300 exit(1);
301
302 if (chdir("/")) {
303 perror("not able to chdir to /");
304 exit(1);
305 }
306 TFR(fd = qemu_open("/dev/null", O_RDWR));
307 if (fd == -1)
308 exit(1);
309 }
310
e06eb601
JS
311 change_root();
312 change_process_uid();
eb505be1
JS
313
314 if (daemonize) {
315 dup2(fd, 0);
316 dup2(fd, 1);
317 dup2(fd, 2);
318
319 close(fd);
320 }
321}
322
323void os_pidfile_error(void)
324{
325 if (daemonize) {
326 uint8_t status = 1;
327 if (write(fds[1], &status, 1) != 1) {
328 perror("daemonize. Writing to pipe\n");
329 }
330 } else
331 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
332}
9156d763
JS
333
334void os_set_line_buffering(void)
335{
336 setvbuf(stdout, NULL, _IOLBF, 0);
337}
949d31e6 338
bc4a957c
JS
339int qemu_create_pidfile(const char *filename)
340{
341 char buffer[128];
342 int len;
343 int fd;
344
345 fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
346 if (fd == -1) {
347 return -1;
348 }
349 if (lockf(fd, F_TLOCK, 0) == -1) {
1bbd1592 350 close(fd);
bc4a957c
JS
351 return -1;
352 }
953ffe0f 353 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
bc4a957c 354 if (write(fd, buffer, len) != len) {
1bbd1592 355 close(fd);
bc4a957c
JS
356 return -1;
357 }
358
93dd748b 359 /* keep pidfile open & locked forever */
bc4a957c
JS
360 return 0;
361}
995ee2bf
HM
362
363bool is_daemonized(void)
364{
365 return daemonize;
366}