]> git.proxmox.com Git - mirror_qemu.git/blob - os-posix.c
oslib: do not call g_strdup from qemu_get_exec_dir
[mirror_qemu.git] / os-posix.c
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 "qemu/osdep.h"
27 #include <sys/wait.h>
28 #include <pwd.h>
29 #include <grp.h>
30 #include <libgen.h>
31
32 #include "qemu-common.h"
33 /* Needed early for CONFIG_BSD etc. */
34 #include "net/slirp.h"
35 #include "qemu-options.h"
36 #include "qemu/error-report.h"
37 #include "qemu/log.h"
38 #include "sysemu/runstate.h"
39 #include "qemu/cutils.h"
40
41 #ifdef CONFIG_LINUX
42 #include <sys/prctl.h>
43 #endif
44
45 /*
46 * Must set all three of these at once.
47 * Legal combinations are unset by name by uid
48 */
49 static struct passwd *user_pwd; /* NULL non-NULL NULL */
50 static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
51 static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
52
53 static const char *chroot_dir;
54 static int daemonize;
55 static int daemon_pipe;
56
57 void os_setup_early_signal_handling(void)
58 {
59 struct sigaction act;
60 sigfillset(&act.sa_mask);
61 act.sa_flags = 0;
62 act.sa_handler = SIG_IGN;
63 sigaction(SIGPIPE, &act, NULL);
64 }
65
66 static void termsig_handler(int signal, siginfo_t *info, void *c)
67 {
68 qemu_system_killed(info->si_signo, info->si_pid);
69 }
70
71 void os_setup_signal_handling(void)
72 {
73 struct sigaction act;
74
75 memset(&act, 0, sizeof(act));
76 act.sa_sigaction = termsig_handler;
77 act.sa_flags = SA_SIGINFO;
78 sigaction(SIGINT, &act, NULL);
79 sigaction(SIGHUP, &act, NULL);
80 sigaction(SIGTERM, &act, NULL);
81 }
82
83 /*
84 * Find a likely location for support files using the location of the binary.
85 * When running from the build tree this will be "$bindir/pc-bios".
86 * Otherwise, this is CONFIG_QEMU_DATADIR.
87 *
88 * The caller must use g_free() to free the returned data when it is
89 * no longer required.
90 */
91 char *os_find_datadir(void)
92 {
93 g_autofree char *dir = NULL;
94
95 dir = g_build_filename(qemu_get_exec_dir(), "pc-bios", NULL);
96 if (g_file_test(dir, G_FILE_TEST_IS_DIR)) {
97 return g_steal_pointer(&dir);
98 }
99
100 return g_strdup(CONFIG_QEMU_DATADIR);
101 }
102
103 void os_set_proc_name(const char *s)
104 {
105 #if defined(PR_SET_NAME)
106 char name[16];
107 if (!s)
108 return;
109 pstrcpy(name, sizeof(name), s);
110 /* Could rewrite argv[0] too, but that's a bit more complicated.
111 This simple way is enough for `top'. */
112 if (prctl(PR_SET_NAME, name)) {
113 error_report("unable to change process name: %s", strerror(errno));
114 exit(1);
115 }
116 #else
117 error_report("Change of process name not supported by your OS");
118 exit(1);
119 #endif
120 }
121
122
123 static bool os_parse_runas_uid_gid(const char *optarg)
124 {
125 unsigned long lv;
126 const char *ep;
127 uid_t got_uid;
128 gid_t got_gid;
129 int rc;
130
131 rc = qemu_strtoul(optarg, &ep, 0, &lv);
132 got_uid = lv; /* overflow here is ID in C99 */
133 if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) {
134 return false;
135 }
136
137 rc = qemu_strtoul(ep + 1, 0, 0, &lv);
138 got_gid = lv; /* overflow here is ID in C99 */
139 if (rc || got_gid != lv || got_gid == (gid_t)-1) {
140 return false;
141 }
142
143 user_pwd = NULL;
144 user_uid = got_uid;
145 user_gid = got_gid;
146 return true;
147 }
148
149 /*
150 * Parse OS specific command line options.
151 * return 0 if option handled, -1 otherwise
152 */
153 int os_parse_cmd_args(int index, const char *optarg)
154 {
155 switch (index) {
156 case QEMU_OPTION_runas:
157 user_pwd = getpwnam(optarg);
158 if (user_pwd) {
159 user_uid = -1;
160 user_gid = -1;
161 } else if (!os_parse_runas_uid_gid(optarg)) {
162 error_report("User \"%s\" doesn't exist"
163 " (and is not <uid>:<gid>)",
164 optarg);
165 exit(1);
166 }
167 break;
168 case QEMU_OPTION_chroot:
169 chroot_dir = optarg;
170 break;
171 case QEMU_OPTION_daemonize:
172 daemonize = 1;
173 break;
174 #if defined(CONFIG_LINUX)
175 case QEMU_OPTION_enablefips:
176 fips_set_state(true);
177 break;
178 #endif
179 default:
180 return -1;
181 }
182
183 return 0;
184 }
185
186 static void change_process_uid(void)
187 {
188 assert((user_uid == (uid_t)-1) || user_pwd == NULL);
189 assert((user_uid == (uid_t)-1) ==
190 (user_gid == (gid_t)-1));
191
192 if (user_pwd || user_uid != (uid_t)-1) {
193 gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid;
194 uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid;
195 if (setgid(intended_gid) < 0) {
196 error_report("Failed to setgid(%d)", intended_gid);
197 exit(1);
198 }
199 if (user_pwd) {
200 if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) {
201 error_report("Failed to initgroups(\"%s\", %d)",
202 user_pwd->pw_name, user_pwd->pw_gid);
203 exit(1);
204 }
205 } else {
206 if (setgroups(1, &user_gid) < 0) {
207 error_report("Failed to setgroups(1, [%d])",
208 user_gid);
209 exit(1);
210 }
211 }
212 if (setuid(intended_uid) < 0) {
213 error_report("Failed to setuid(%d)", intended_uid);
214 exit(1);
215 }
216 if (setuid(0) != -1) {
217 error_report("Dropping privileges failed");
218 exit(1);
219 }
220 }
221 }
222
223 static void change_root(void)
224 {
225 if (chroot_dir) {
226 if (chroot(chroot_dir) < 0) {
227 error_report("chroot failed");
228 exit(1);
229 }
230 if (chdir("/")) {
231 error_report("not able to chdir to /: %s", strerror(errno));
232 exit(1);
233 }
234 }
235
236 }
237
238 void os_daemonize(void)
239 {
240 if (daemonize) {
241 pid_t pid;
242 int fds[2];
243
244 if (pipe(fds) == -1) {
245 exit(1);
246 }
247
248 pid = fork();
249 if (pid > 0) {
250 uint8_t status;
251 ssize_t len;
252
253 close(fds[1]);
254
255 do {
256 len = read(fds[0], &status, 1);
257 } while (len < 0 && errno == EINTR);
258
259 /* only exit successfully if our child actually wrote
260 * a one-byte zero to our pipe, upon successful init */
261 exit(len == 1 && status == 0 ? 0 : 1);
262
263 } else if (pid < 0) {
264 exit(1);
265 }
266
267 close(fds[0]);
268 daemon_pipe = fds[1];
269 qemu_set_cloexec(daemon_pipe);
270
271 setsid();
272
273 pid = fork();
274 if (pid > 0) {
275 exit(0);
276 } else if (pid < 0) {
277 exit(1);
278 }
279 umask(027);
280
281 signal(SIGTSTP, SIG_IGN);
282 signal(SIGTTOU, SIG_IGN);
283 signal(SIGTTIN, SIG_IGN);
284 }
285 }
286
287 void os_setup_post(void)
288 {
289 int fd = 0;
290
291 if (daemonize) {
292 if (chdir("/")) {
293 error_report("not able to chdir to /: %s", strerror(errno));
294 exit(1);
295 }
296 TFR(fd = qemu_open_old("/dev/null", O_RDWR));
297 if (fd == -1) {
298 exit(1);
299 }
300 }
301
302 change_root();
303 change_process_uid();
304
305 if (daemonize) {
306 uint8_t status = 0;
307 ssize_t len;
308
309 dup2(fd, 0);
310 dup2(fd, 1);
311 /* In case -D is given do not redirect stderr to /dev/null */
312 if (!qemu_logfile) {
313 dup2(fd, 2);
314 }
315
316 close(fd);
317
318 do {
319 len = write(daemon_pipe, &status, 1);
320 } while (len < 0 && errno == EINTR);
321 if (len != 1) {
322 exit(1);
323 }
324 }
325 }
326
327 void os_set_line_buffering(void)
328 {
329 setvbuf(stdout, NULL, _IOLBF, 0);
330 }
331
332 bool is_daemonized(void)
333 {
334 return daemonize;
335 }
336
337 int os_mlock(void)
338 {
339 #ifdef HAVE_MLOCKALL
340 int ret = 0;
341
342 ret = mlockall(MCL_CURRENT | MCL_FUTURE);
343 if (ret < 0) {
344 error_report("mlockall: %s", strerror(errno));
345 }
346
347 return ret;
348 #else
349 return -ENOSYS;
350 #endif
351 }