]> git.proxmox.com Git - mirror_qemu.git/blame - os-posix.c
intel_iommu: use IOMMU_ACCESS_FLAG()
[mirror_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
d38ea87a 26#include "qemu/osdep.h"
8d963e6a 27#include <sys/wait.h>
69e8b162 28/*needed for MAP_POPULATE before including qemu-options.h */
8847cfe8 29#include <pwd.h>
cc4662f9 30#include <grp.h>
6170540b 31#include <libgen.h>
86b645e7
JS
32
33/* Needed early for CONFIG_BSD etc. */
9c17d615 34#include "sysemu/sysemu.h"
59a5264b
JS
35#include "net/slirp.h"
36#include "qemu-options.h"
a59629fc 37#include "qemu/rcu.h"
f853ac66 38#include "qemu/error-report.h"
96c33a45 39#include "qemu/log.h"
f348b6d1 40#include "qemu/cutils.h"
86b645e7 41
ce798cf2
JS
42#ifdef CONFIG_LINUX
43#include <sys/prctl.h>
949d31e6
JS
44#endif
45
8847cfe8 46static struct passwd *user_pwd;
0766379d 47static const char *chroot_dir;
eb505be1 48static int daemonize;
0be5e436 49static int daemon_pipe;
8847cfe8 50
fe98ac14 51void os_setup_early_signal_handling(void)
86b645e7
JS
52{
53 struct sigaction act;
54 sigfillset(&act.sa_mask);
55 act.sa_flags = 0;
56 act.sa_handler = SIG_IGN;
57 sigaction(SIGPIPE, &act, NULL);
58}
8d963e6a 59
f64622c4 60static void termsig_handler(int signal, siginfo_t *info, void *c)
8d963e6a 61{
f64622c4 62 qemu_system_killed(info->si_signo, info->si_pid);
8d963e6a
JS
63}
64
8d963e6a
JS
65void os_setup_signal_handling(void)
66{
67 struct sigaction act;
68
69 memset(&act, 0, sizeof(act));
f64622c4
GN
70 act.sa_sigaction = termsig_handler;
71 act.sa_flags = SA_SIGINFO;
8d963e6a
JS
72 sigaction(SIGINT, &act, NULL);
73 sigaction(SIGHUP, &act, NULL);
74 sigaction(SIGTERM, &act, NULL);
8d963e6a 75}
6170540b
JS
76
77/* Find a likely location for support files using the location of the binary.
78 For installed binaries this will be "$bindir/../share/qemu". When
79 running from the build tree this will be "$bindir/../pc-bios". */
80#define SHARE_SUFFIX "/share/qemu"
81#define BUILD_SUFFIX "/pc-bios"
10f5bff6 82char *os_find_datadir(void)
6170540b 83{
10f5bff6 84 char *dir, *exec_dir;
6170540b 85 char *res;
6170540b
JS
86 size_t max_len;
87
10f5bff6
FZ
88 exec_dir = qemu_get_exec_dir();
89 if (exec_dir == NULL) {
90 return NULL;
6170540b 91 }
55ad781c 92 dir = g_path_get_dirname(exec_dir);
6170540b
JS
93
94 max_len = strlen(dir) +
95 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
7267c094 96 res = g_malloc0(max_len);
6170540b
JS
97 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
98 if (access(res, R_OK)) {
99 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
100 if (access(res, R_OK)) {
7267c094 101 g_free(res);
6170540b
JS
102 res = NULL;
103 }
104 }
105
55ad781c 106 g_free(dir);
10f5bff6 107 g_free(exec_dir);
6170540b
JS
108 return res;
109}
110#undef SHARE_SUFFIX
111#undef BUILD_SUFFIX
59a5264b 112
ce798cf2
JS
113void os_set_proc_name(const char *s)
114{
115#if defined(PR_SET_NAME)
116 char name[16];
117 if (!s)
118 return;
3eadc68e 119 pstrcpy(name, sizeof(name), s);
ce798cf2
JS
120 /* Could rewrite argv[0] too, but that's a bit more complicated.
121 This simple way is enough for `top'. */
122 if (prctl(PR_SET_NAME, name)) {
123 perror("unable to change process name");
124 exit(1);
125 }
126#else
127 fprintf(stderr, "Change of process name not supported by your OS\n");
128 exit(1);
129#endif
130}
131
59a5264b
JS
132/*
133 * Parse OS specific command line options.
134 * return 0 if option handled, -1 otherwise
135 */
136void os_parse_cmd_args(int index, const char *optarg)
137{
138 switch (index) {
139#ifdef CONFIG_SLIRP
140 case QEMU_OPTION_smb:
f853ac66
TH
141 error_report("The -smb option is deprecated. "
142 "Please use '-netdev user,smb=...' instead.");
59a5264b
JS
143 if (net_slirp_smb(optarg) < 0)
144 exit(1);
145 break;
146#endif
8847cfe8
JS
147 case QEMU_OPTION_runas:
148 user_pwd = getpwnam(optarg);
149 if (!user_pwd) {
150 fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
151 exit(1);
152 }
153 break;
0766379d
JS
154 case QEMU_OPTION_chroot:
155 chroot_dir = optarg;
156 break;
eb505be1
JS
157 case QEMU_OPTION_daemonize:
158 daemonize = 1;
159 break;
70678b82
AL
160#if defined(CONFIG_LINUX)
161 case QEMU_OPTION_enablefips:
162 fips_set_state(true);
163 break;
164#endif
59a5264b 165 }
59a5264b 166}
8847cfe8 167
e06eb601 168static void change_process_uid(void)
8847cfe8
JS
169{
170 if (user_pwd) {
171 if (setgid(user_pwd->pw_gid) < 0) {
172 fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
173 exit(1);
174 }
cc4662f9
SH
175 if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
176 fprintf(stderr, "Failed to initgroups(\"%s\", %d)\n",
177 user_pwd->pw_name, user_pwd->pw_gid);
178 exit(1);
179 }
8847cfe8
JS
180 if (setuid(user_pwd->pw_uid) < 0) {
181 fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
182 exit(1);
183 }
184 if (setuid(0) != -1) {
185 fprintf(stderr, "Dropping privileges failed\n");
186 exit(1);
187 }
188 }
189}
0766379d 190
e06eb601 191static void change_root(void)
0766379d
JS
192{
193 if (chroot_dir) {
194 if (chroot(chroot_dir) < 0) {
195 fprintf(stderr, "chroot failed\n");
196 exit(1);
197 }
198 if (chdir("/")) {
199 perror("not able to chdir to /");
200 exit(1);
201 }
202 }
203
204}
eb505be1
JS
205
206void os_daemonize(void)
207{
208 if (daemonize) {
63ce8e15 209 pid_t pid;
0be5e436 210 int fds[2];
eb505be1 211
63ce8e15
GA
212 if (pipe(fds) == -1) {
213 exit(1);
214 }
eb505be1 215
63ce8e15
GA
216 pid = fork();
217 if (pid > 0) {
218 uint8_t status;
219 ssize_t len;
eb505be1 220
63ce8e15 221 close(fds[1]);
eb505be1 222
ccea25f1
MT
223 do {
224 len = read(fds[0], &status, 1);
225 } while (len < 0 && errno == EINTR);
fee78fd6
MT
226
227 /* only exit successfully if our child actually wrote
228 * a one-byte zero to our pipe, upon successful init */
229 exit(len == 1 && status == 0 ? 0 : 1);
230
231 } else if (pid < 0) {
232 exit(1);
233 }
eb505be1 234
63ce8e15 235 close(fds[0]);
0be5e436
MT
236 daemon_pipe = fds[1];
237 qemu_set_cloexec(daemon_pipe);
eb505be1 238
63ce8e15 239 setsid();
eb505be1 240
63ce8e15
GA
241 pid = fork();
242 if (pid > 0) {
243 exit(0);
244 } else if (pid < 0) {
245 exit(1);
246 }
247 umask(027);
eb505be1
JS
248
249 signal(SIGTSTP, SIG_IGN);
250 signal(SIGTTOU, SIG_IGN);
251 signal(SIGTTIN, SIG_IGN);
a59629fc 252 rcu_after_fork();
eb505be1
JS
253 }
254}
255
256void os_setup_post(void)
257{
258 int fd = 0;
259
260 if (daemonize) {
eb505be1
JS
261 if (chdir("/")) {
262 perror("not able to chdir to /");
263 exit(1);
264 }
63ce8e15
GA
265 TFR(fd = qemu_open("/dev/null", O_RDWR));
266 if (fd == -1) {
267 exit(1);
268 }
eb505be1
JS
269 }
270
e06eb601
JS
271 change_root();
272 change_process_uid();
eb505be1
JS
273
274 if (daemonize) {
25cec2b8
MT
275 uint8_t status = 0;
276 ssize_t len;
277
eb505be1
JS
278 dup2(fd, 0);
279 dup2(fd, 1);
96c33a45
DA
280 /* In case -D is given do not redirect stderr to /dev/null */
281 if (!qemu_logfile) {
282 dup2(fd, 2);
283 }
eb505be1
JS
284
285 close(fd);
25cec2b8
MT
286
287 do {
288 len = write(daemon_pipe, &status, 1);
289 } while (len < 0 && errno == EINTR);
290 if (len != 1) {
291 exit(1);
292 }
eb505be1
JS
293 }
294}
295
9156d763
JS
296void os_set_line_buffering(void)
297{
298 setvbuf(stdout, NULL, _IOLBF, 0);
299}
949d31e6 300
bc4a957c
JS
301int qemu_create_pidfile(const char *filename)
302{
303 char buffer[128];
304 int len;
305 int fd;
306
307 fd = qemu_open(filename, O_RDWR | O_CREAT, 0600);
308 if (fd == -1) {
309 return -1;
310 }
311 if (lockf(fd, F_TLOCK, 0) == -1) {
1bbd1592 312 close(fd);
bc4a957c
JS
313 return -1;
314 }
953ffe0f 315 len = snprintf(buffer, sizeof(buffer), FMT_pid "\n", getpid());
bc4a957c 316 if (write(fd, buffer, len) != len) {
1bbd1592 317 close(fd);
bc4a957c
JS
318 return -1;
319 }
320
93dd748b 321 /* keep pidfile open & locked forever */
bc4a957c
JS
322 return 0;
323}
995ee2bf
HM
324
325bool is_daemonized(void)
326{
327 return daemonize;
328}
888a6bc6
SM
329
330int os_mlock(void)
331{
332 int ret = 0;
333
334 ret = mlockall(MCL_CURRENT | MCL_FUTURE);
335 if (ret < 0) {
336 perror("mlockall");
337 }
338
339 return ret;
340}