]>
Commit | Line | Data |
---|---|---|
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> |
8847cfe8 | 28 | #include <pwd.h> |
cc4662f9 | 29 | #include <grp.h> |
6170540b | 30 | #include <libgen.h> |
86b645e7 JS |
31 | |
32 | /* Needed early for CONFIG_BSD etc. */ | |
59a5264b | 33 | #include "net/slirp.h" |
fd5fc4b1 | 34 | #include "qemu/qemu-options.h" |
f853ac66 | 35 | #include "qemu/error-report.h" |
96c33a45 | 36 | #include "qemu/log.h" |
54d31236 | 37 | #include "sysemu/runstate.h" |
f348b6d1 | 38 | #include "qemu/cutils.h" |
80bd81ca CI |
39 | #include "qemu/config-file.h" |
40 | #include "qemu/option.h" | |
9ffcbe2a | 41 | #include "qemu/module.h" |
86b645e7 | 42 | |
ce798cf2 JS |
43 | #ifdef CONFIG_LINUX |
44 | #include <sys/prctl.h> | |
c891c24b | 45 | #include "qemu/async-teardown.h" |
949d31e6 JS |
46 | #endif |
47 | ||
2c42f1e8 IJ |
48 | /* |
49 | * Must set all three of these at once. | |
50 | * Legal combinations are unset by name by uid | |
51 | */ | |
52 | static struct passwd *user_pwd; /* NULL non-NULL NULL */ | |
53 | static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */ | |
54 | static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */ | |
55 | ||
0766379d | 56 | static const char *chroot_dir; |
eb505be1 | 57 | static int daemonize; |
0be5e436 | 58 | static int daemon_pipe; |
8847cfe8 | 59 | |
fe98ac14 | 60 | void os_setup_early_signal_handling(void) |
86b645e7 JS |
61 | { |
62 | struct sigaction act; | |
63 | sigfillset(&act.sa_mask); | |
64 | act.sa_flags = 0; | |
65 | act.sa_handler = SIG_IGN; | |
66 | sigaction(SIGPIPE, &act, NULL); | |
67 | } | |
8d963e6a | 68 | |
f64622c4 | 69 | static void termsig_handler(int signal, siginfo_t *info, void *c) |
8d963e6a | 70 | { |
f64622c4 | 71 | qemu_system_killed(info->si_signo, info->si_pid); |
8d963e6a JS |
72 | } |
73 | ||
8d963e6a JS |
74 | void os_setup_signal_handling(void) |
75 | { | |
76 | struct sigaction act; | |
77 | ||
78 | memset(&act, 0, sizeof(act)); | |
f64622c4 GN |
79 | act.sa_sigaction = termsig_handler; |
80 | act.sa_flags = SA_SIGINFO; | |
8d963e6a JS |
81 | sigaction(SIGINT, &act, NULL); |
82 | sigaction(SIGHUP, &act, NULL); | |
83 | sigaction(SIGTERM, &act, NULL); | |
8d963e6a | 84 | } |
6170540b | 85 | |
ce798cf2 JS |
86 | void os_set_proc_name(const char *s) |
87 | { | |
88 | #if defined(PR_SET_NAME) | |
89 | char name[16]; | |
90 | if (!s) | |
91 | return; | |
3eadc68e | 92 | pstrcpy(name, sizeof(name), s); |
ce798cf2 JS |
93 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
94 | This simple way is enough for `top'. */ | |
95 | if (prctl(PR_SET_NAME, name)) { | |
a7aaec14 | 96 | error_report("unable to change process name: %s", strerror(errno)); |
ce798cf2 JS |
97 | exit(1); |
98 | } | |
99 | #else | |
22cd4f48 | 100 | error_report("Change of process name not supported by your OS"); |
ce798cf2 JS |
101 | exit(1); |
102 | #endif | |
103 | } | |
104 | ||
22d02515 MT |
105 | /* |
106 | * Prepare to change user ID. optarg can be one of 3 forms: | |
107 | * - a username, in which case user ID will be changed to its uid, | |
108 | * with primary and supplementary groups set up too; | |
109 | * - a numeric uid, in which case only the uid will be set; | |
110 | * - a pair of numeric uid:gid. | |
111 | */ | |
112 | bool os_set_runas(const char *optarg) | |
2c42f1e8 IJ |
113 | { |
114 | unsigned long lv; | |
115 | const char *ep; | |
116 | uid_t got_uid; | |
117 | gid_t got_gid; | |
118 | int rc; | |
119 | ||
22d02515 MT |
120 | user_pwd = getpwnam(optarg); |
121 | if (user_pwd) { | |
122 | user_uid = -1; | |
123 | user_gid = -1; | |
124 | return true; | |
125 | } | |
126 | ||
2c42f1e8 IJ |
127 | rc = qemu_strtoul(optarg, &ep, 0, &lv); |
128 | got_uid = lv; /* overflow here is ID in C99 */ | |
129 | if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) { | |
130 | return false; | |
131 | } | |
132 | ||
133 | rc = qemu_strtoul(ep + 1, 0, 0, &lv); | |
134 | got_gid = lv; /* overflow here is ID in C99 */ | |
135 | if (rc || got_gid != lv || got_gid == (gid_t)-1) { | |
136 | return false; | |
137 | } | |
138 | ||
139 | user_pwd = NULL; | |
140 | user_uid = got_uid; | |
141 | user_gid = got_gid; | |
142 | return true; | |
143 | } | |
144 | ||
59a5264b JS |
145 | /* |
146 | * Parse OS specific command line options. | |
147 | * return 0 if option handled, -1 otherwise | |
148 | */ | |
1217d6ca | 149 | int os_parse_cmd_args(int index, const char *optarg) |
59a5264b JS |
150 | { |
151 | switch (index) { | |
8847cfe8 | 152 | case QEMU_OPTION_runas: |
22d02515 | 153 | if (!os_set_runas(optarg)) { |
2c42f1e8 IJ |
154 | error_report("User \"%s\" doesn't exist" |
155 | " (and is not <uid>:<gid>)", | |
156 | optarg); | |
8847cfe8 JS |
157 | exit(1); |
158 | } | |
159 | break; | |
0766379d | 160 | case QEMU_OPTION_chroot: |
9ffcbe2a | 161 | warn_report("option is deprecated, use '-run-with chroot=...' instead"); |
0766379d JS |
162 | chroot_dir = optarg; |
163 | break; | |
eb505be1 JS |
164 | case QEMU_OPTION_daemonize: |
165 | daemonize = 1; | |
166 | break; | |
c891c24b | 167 | #if defined(CONFIG_LINUX) |
80bd81ca | 168 | /* deprecated */ |
c891c24b CI |
169 | case QEMU_OPTION_asyncteardown: |
170 | init_async_teardown(); | |
171 | break; | |
9ffcbe2a | 172 | #endif |
80bd81ca | 173 | case QEMU_OPTION_run_with: { |
9ffcbe2a | 174 | const char *str; |
80bd81ca CI |
175 | QemuOpts *opts = qemu_opts_parse_noisily(qemu_find_opts("run-with"), |
176 | optarg, false); | |
177 | if (!opts) { | |
178 | exit(1); | |
179 | } | |
9ffcbe2a | 180 | #if defined(CONFIG_LINUX) |
80bd81ca CI |
181 | if (qemu_opt_get_bool(opts, "async-teardown", false)) { |
182 | init_async_teardown(); | |
183 | } | |
9ffcbe2a TH |
184 | #endif |
185 | str = qemu_opt_get(opts, "chroot"); | |
186 | if (str) { | |
187 | chroot_dir = str; | |
188 | } | |
80bd81ca CI |
189 | break; |
190 | } | |
1217d6ca TH |
191 | default: |
192 | return -1; | |
59a5264b | 193 | } |
1217d6ca TH |
194 | |
195 | return 0; | |
59a5264b | 196 | } |
8847cfe8 | 197 | |
e06eb601 | 198 | static void change_process_uid(void) |
8847cfe8 | 199 | { |
2c42f1e8 IJ |
200 | assert((user_uid == (uid_t)-1) || user_pwd == NULL); |
201 | assert((user_uid == (uid_t)-1) == | |
202 | (user_gid == (gid_t)-1)); | |
203 | ||
204 | if (user_pwd || user_uid != (uid_t)-1) { | |
205 | gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid; | |
206 | uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid; | |
207 | if (setgid(intended_gid) < 0) { | |
208 | error_report("Failed to setgid(%d)", intended_gid); | |
8847cfe8 JS |
209 | exit(1); |
210 | } | |
2c42f1e8 IJ |
211 | if (user_pwd) { |
212 | if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) { | |
213 | error_report("Failed to initgroups(\"%s\", %d)", | |
214 | user_pwd->pw_name, user_pwd->pw_gid); | |
215 | exit(1); | |
216 | } | |
217 | } else { | |
218 | if (setgroups(1, &user_gid) < 0) { | |
219 | error_report("Failed to setgroups(1, [%d])", | |
220 | user_gid); | |
221 | exit(1); | |
222 | } | |
cc4662f9 | 223 | } |
2c42f1e8 IJ |
224 | if (setuid(intended_uid) < 0) { |
225 | error_report("Failed to setuid(%d)", intended_uid); | |
8847cfe8 JS |
226 | exit(1); |
227 | } | |
228 | if (setuid(0) != -1) { | |
f0a2171b | 229 | error_report("Dropping privileges failed"); |
8847cfe8 JS |
230 | exit(1); |
231 | } | |
232 | } | |
233 | } | |
0766379d | 234 | |
e06eb601 | 235 | static void change_root(void) |
0766379d JS |
236 | { |
237 | if (chroot_dir) { | |
238 | if (chroot(chroot_dir) < 0) { | |
22cd4f48 | 239 | error_report("chroot failed"); |
0766379d JS |
240 | exit(1); |
241 | } | |
242 | if (chdir("/")) { | |
a7aaec14 | 243 | error_report("not able to chdir to /: %s", strerror(errno)); |
0766379d JS |
244 | exit(1); |
245 | } | |
246 | } | |
247 | ||
248 | } | |
eb505be1 JS |
249 | |
250 | void os_daemonize(void) | |
251 | { | |
252 | if (daemonize) { | |
63ce8e15 | 253 | pid_t pid; |
0be5e436 | 254 | int fds[2]; |
eb505be1 | 255 | |
3338a41f | 256 | if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) { |
63ce8e15 GA |
257 | exit(1); |
258 | } | |
eb505be1 | 259 | |
63ce8e15 GA |
260 | pid = fork(); |
261 | if (pid > 0) { | |
262 | uint8_t status; | |
263 | ssize_t len; | |
eb505be1 | 264 | |
63ce8e15 | 265 | close(fds[1]); |
eb505be1 | 266 | |
ccea25f1 MT |
267 | do { |
268 | len = read(fds[0], &status, 1); | |
269 | } while (len < 0 && errno == EINTR); | |
fee78fd6 MT |
270 | |
271 | /* only exit successfully if our child actually wrote | |
272 | * a one-byte zero to our pipe, upon successful init */ | |
273 | exit(len == 1 && status == 0 ? 0 : 1); | |
274 | ||
275 | } else if (pid < 0) { | |
276 | exit(1); | |
277 | } | |
eb505be1 | 278 | |
63ce8e15 | 279 | close(fds[0]); |
0be5e436 | 280 | daemon_pipe = fds[1]; |
eb505be1 | 281 | |
63ce8e15 | 282 | setsid(); |
eb505be1 | 283 | |
63ce8e15 GA |
284 | pid = fork(); |
285 | if (pid > 0) { | |
286 | exit(0); | |
287 | } else if (pid < 0) { | |
288 | exit(1); | |
289 | } | |
290 | umask(027); | |
eb505be1 JS |
291 | |
292 | signal(SIGTSTP, SIG_IGN); | |
293 | signal(SIGTTOU, SIG_IGN); | |
294 | signal(SIGTTIN, SIG_IGN); | |
295 | } | |
296 | } | |
297 | ||
298 | void os_setup_post(void) | |
299 | { | |
300 | int fd = 0; | |
301 | ||
302 | if (daemonize) { | |
eb505be1 | 303 | if (chdir("/")) { |
a7aaec14 | 304 | error_report("not able to chdir to /: %s", strerror(errno)); |
eb505be1 JS |
305 | exit(1); |
306 | } | |
8b6aa693 | 307 | fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR)); |
63ce8e15 GA |
308 | if (fd == -1) { |
309 | exit(1); | |
310 | } | |
eb505be1 JS |
311 | } |
312 | ||
e06eb601 JS |
313 | change_root(); |
314 | change_process_uid(); | |
eb505be1 JS |
315 | |
316 | if (daemonize) { | |
25cec2b8 MT |
317 | uint8_t status = 0; |
318 | ssize_t len; | |
319 | ||
eb505be1 JS |
320 | dup2(fd, 0); |
321 | dup2(fd, 1); | |
96c33a45 | 322 | /* In case -D is given do not redirect stderr to /dev/null */ |
229ef2eb | 323 | if (!qemu_log_enabled()) { |
96c33a45 DA |
324 | dup2(fd, 2); |
325 | } | |
eb505be1 JS |
326 | |
327 | close(fd); | |
25cec2b8 MT |
328 | |
329 | do { | |
330 | len = write(daemon_pipe, &status, 1); | |
331 | } while (len < 0 && errno == EINTR); | |
332 | if (len != 1) { | |
333 | exit(1); | |
334 | } | |
eb505be1 JS |
335 | } |
336 | } | |
337 | ||
9156d763 JS |
338 | void os_set_line_buffering(void) |
339 | { | |
340 | setvbuf(stdout, NULL, _IOLBF, 0); | |
341 | } | |
949d31e6 | 342 | |
995ee2bf HM |
343 | bool is_daemonized(void) |
344 | { | |
345 | return daemonize; | |
346 | } | |
888a6bc6 | 347 | |
f22ac472 HR |
348 | int os_set_daemonize(bool d) |
349 | { | |
350 | daemonize = d; | |
351 | return 0; | |
352 | } | |
353 | ||
888a6bc6 SM |
354 | int os_mlock(void) |
355 | { | |
195588cc | 356 | #ifdef HAVE_MLOCKALL |
888a6bc6 SM |
357 | int ret = 0; |
358 | ||
359 | ret = mlockall(MCL_CURRENT | MCL_FUTURE); | |
360 | if (ret < 0) { | |
a7aaec14 | 361 | error_report("mlockall: %s", strerror(errno)); |
888a6bc6 SM |
362 | } |
363 | ||
364 | return ret; | |
195588cc DC |
365 | #else |
366 | return -ENOSYS; | |
367 | #endif | |
888a6bc6 | 368 | } |
9ffcbe2a TH |
369 | |
370 | static QemuOptsList qemu_run_with_opts = { | |
371 | .name = "run-with", | |
372 | .head = QTAILQ_HEAD_INITIALIZER(qemu_run_with_opts.head), | |
373 | .desc = { | |
374 | #if defined(CONFIG_LINUX) | |
375 | { | |
376 | .name = "async-teardown", | |
377 | .type = QEMU_OPT_BOOL, | |
378 | }, | |
379 | #endif | |
380 | { | |
381 | .name = "chroot", | |
382 | .type = QEMU_OPT_STRING, | |
383 | }, | |
384 | { /* end of list */ } | |
385 | }, | |
386 | }; | |
387 | ||
388 | static void register_runwith(void) | |
389 | { | |
390 | qemu_add_opts(&qemu_run_with_opts); | |
391 | } | |
392 | opts_init(register_runwith); |