]>
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 | 31 | |
f853ac66 | 32 | #include "qemu/error-report.h" |
96c33a45 | 33 | #include "qemu/log.h" |
54d31236 | 34 | #include "sysemu/runstate.h" |
f348b6d1 | 35 | #include "qemu/cutils.h" |
86b645e7 | 36 | |
ce798cf2 JS |
37 | #ifdef CONFIG_LINUX |
38 | #include <sys/prctl.h> | |
949d31e6 JS |
39 | #endif |
40 | ||
8847cfe8 | 41 | |
fe98ac14 | 42 | void os_setup_early_signal_handling(void) |
86b645e7 JS |
43 | { |
44 | struct sigaction act; | |
45 | sigfillset(&act.sa_mask); | |
46 | act.sa_flags = 0; | |
47 | act.sa_handler = SIG_IGN; | |
48 | sigaction(SIGPIPE, &act, NULL); | |
49 | } | |
8d963e6a | 50 | |
f64622c4 | 51 | static void termsig_handler(int signal, siginfo_t *info, void *c) |
8d963e6a | 52 | { |
f64622c4 | 53 | qemu_system_killed(info->si_signo, info->si_pid); |
8d963e6a JS |
54 | } |
55 | ||
8d963e6a JS |
56 | void os_setup_signal_handling(void) |
57 | { | |
58 | struct sigaction act; | |
59 | ||
60 | memset(&act, 0, sizeof(act)); | |
f64622c4 GN |
61 | act.sa_sigaction = termsig_handler; |
62 | act.sa_flags = SA_SIGINFO; | |
8d963e6a JS |
63 | sigaction(SIGINT, &act, NULL); |
64 | sigaction(SIGHUP, &act, NULL); | |
65 | sigaction(SIGTERM, &act, NULL); | |
8d963e6a | 66 | } |
6170540b | 67 | |
ce798cf2 JS |
68 | void os_set_proc_name(const char *s) |
69 | { | |
70 | #if defined(PR_SET_NAME) | |
71 | char name[16]; | |
72 | if (!s) | |
73 | return; | |
3eadc68e | 74 | pstrcpy(name, sizeof(name), s); |
ce798cf2 JS |
75 | /* Could rewrite argv[0] too, but that's a bit more complicated. |
76 | This simple way is enough for `top'. */ | |
77 | if (prctl(PR_SET_NAME, name)) { | |
a7aaec14 | 78 | error_report("unable to change process name: %s", strerror(errno)); |
ce798cf2 JS |
79 | exit(1); |
80 | } | |
81 | #else | |
22cd4f48 | 82 | error_report("Change of process name not supported by your OS"); |
ce798cf2 JS |
83 | exit(1); |
84 | #endif | |
85 | } | |
86 | ||
433aed5f MT |
87 | |
88 | /* | |
89 | * Must set all three of these at once. | |
90 | * Legal combinations are unset by name by uid | |
91 | */ | |
92 | static struct passwd *user_pwd; /* NULL non-NULL NULL */ | |
93 | static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */ | |
94 | static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */ | |
95 | ||
22d02515 | 96 | /* |
d2803376 | 97 | * Prepare to change user ID. user_id can be one of 3 forms: |
22d02515 MT |
98 | * - a username, in which case user ID will be changed to its uid, |
99 | * with primary and supplementary groups set up too; | |
100 | * - a numeric uid, in which case only the uid will be set; | |
101 | * - a pair of numeric uid:gid. | |
102 | */ | |
d2803376 | 103 | bool os_set_runas(const char *user_id) |
2c42f1e8 IJ |
104 | { |
105 | unsigned long lv; | |
106 | const char *ep; | |
107 | uid_t got_uid; | |
108 | gid_t got_gid; | |
109 | int rc; | |
110 | ||
d2803376 | 111 | user_pwd = getpwnam(user_id); |
22d02515 MT |
112 | if (user_pwd) { |
113 | user_uid = -1; | |
114 | user_gid = -1; | |
115 | return true; | |
116 | } | |
117 | ||
d2803376 | 118 | rc = qemu_strtoul(user_id, &ep, 0, &lv); |
2c42f1e8 IJ |
119 | got_uid = lv; /* overflow here is ID in C99 */ |
120 | if (rc || *ep != ':' || got_uid != lv || got_uid == (uid_t)-1) { | |
121 | return false; | |
122 | } | |
123 | ||
124 | rc = qemu_strtoul(ep + 1, 0, 0, &lv); | |
125 | got_gid = lv; /* overflow here is ID in C99 */ | |
126 | if (rc || got_gid != lv || got_gid == (gid_t)-1) { | |
127 | return false; | |
128 | } | |
129 | ||
130 | user_pwd = NULL; | |
131 | user_uid = got_uid; | |
132 | user_gid = got_gid; | |
133 | return true; | |
134 | } | |
135 | ||
e06eb601 | 136 | static void change_process_uid(void) |
8847cfe8 | 137 | { |
2c42f1e8 IJ |
138 | assert((user_uid == (uid_t)-1) || user_pwd == NULL); |
139 | assert((user_uid == (uid_t)-1) == | |
140 | (user_gid == (gid_t)-1)); | |
141 | ||
142 | if (user_pwd || user_uid != (uid_t)-1) { | |
143 | gid_t intended_gid = user_pwd ? user_pwd->pw_gid : user_gid; | |
144 | uid_t intended_uid = user_pwd ? user_pwd->pw_uid : user_uid; | |
145 | if (setgid(intended_gid) < 0) { | |
146 | error_report("Failed to setgid(%d)", intended_gid); | |
8847cfe8 JS |
147 | exit(1); |
148 | } | |
2c42f1e8 IJ |
149 | if (user_pwd) { |
150 | if (initgroups(user_pwd->pw_name, user_pwd->pw_gid) < 0) { | |
151 | error_report("Failed to initgroups(\"%s\", %d)", | |
152 | user_pwd->pw_name, user_pwd->pw_gid); | |
153 | exit(1); | |
154 | } | |
155 | } else { | |
156 | if (setgroups(1, &user_gid) < 0) { | |
157 | error_report("Failed to setgroups(1, [%d])", | |
158 | user_gid); | |
159 | exit(1); | |
160 | } | |
cc4662f9 | 161 | } |
2c42f1e8 IJ |
162 | if (setuid(intended_uid) < 0) { |
163 | error_report("Failed to setuid(%d)", intended_uid); | |
8847cfe8 JS |
164 | exit(1); |
165 | } | |
166 | if (setuid(0) != -1) { | |
f0a2171b | 167 | error_report("Dropping privileges failed"); |
8847cfe8 JS |
168 | exit(1); |
169 | } | |
170 | } | |
171 | } | |
0766379d | 172 | |
433aed5f MT |
173 | |
174 | static const char *chroot_dir; | |
175 | ||
d2803376 | 176 | void os_set_chroot(const char *path) |
5b156390 | 177 | { |
d2803376 | 178 | chroot_dir = path; |
5b156390 MT |
179 | } |
180 | ||
e06eb601 | 181 | static void change_root(void) |
0766379d JS |
182 | { |
183 | if (chroot_dir) { | |
184 | if (chroot(chroot_dir) < 0) { | |
22cd4f48 | 185 | error_report("chroot failed"); |
0766379d JS |
186 | exit(1); |
187 | } | |
188 | if (chdir("/")) { | |
a7aaec14 | 189 | error_report("not able to chdir to /: %s", strerror(errno)); |
0766379d JS |
190 | exit(1); |
191 | } | |
192 | } | |
193 | ||
194 | } | |
eb505be1 | 195 | |
433aed5f MT |
196 | |
197 | static int daemonize; | |
198 | static int daemon_pipe; | |
199 | ||
200 | bool is_daemonized(void) | |
201 | { | |
202 | return daemonize; | |
203 | } | |
204 | ||
205 | int os_set_daemonize(bool d) | |
206 | { | |
207 | daemonize = d; | |
208 | return 0; | |
209 | } | |
210 | ||
eb505be1 JS |
211 | void os_daemonize(void) |
212 | { | |
213 | if (daemonize) { | |
63ce8e15 | 214 | pid_t pid; |
0be5e436 | 215 | int fds[2]; |
eb505be1 | 216 | |
3338a41f | 217 | if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) { |
63ce8e15 GA |
218 | exit(1); |
219 | } | |
eb505be1 | 220 | |
63ce8e15 GA |
221 | pid = fork(); |
222 | if (pid > 0) { | |
223 | uint8_t status; | |
224 | ssize_t len; | |
eb505be1 | 225 | |
63ce8e15 | 226 | close(fds[1]); |
eb505be1 | 227 | |
ccea25f1 MT |
228 | do { |
229 | len = read(fds[0], &status, 1); | |
230 | } while (len < 0 && errno == EINTR); | |
fee78fd6 MT |
231 | |
232 | /* only exit successfully if our child actually wrote | |
233 | * a one-byte zero to our pipe, upon successful init */ | |
234 | exit(len == 1 && status == 0 ? 0 : 1); | |
235 | ||
236 | } else if (pid < 0) { | |
237 | exit(1); | |
238 | } | |
eb505be1 | 239 | |
63ce8e15 | 240 | close(fds[0]); |
0be5e436 | 241 | daemon_pipe = fds[1]; |
eb505be1 | 242 | |
63ce8e15 | 243 | setsid(); |
eb505be1 | 244 | |
63ce8e15 GA |
245 | pid = fork(); |
246 | if (pid > 0) { | |
247 | exit(0); | |
248 | } else if (pid < 0) { | |
249 | exit(1); | |
250 | } | |
251 | umask(027); | |
eb505be1 JS |
252 | |
253 | signal(SIGTSTP, SIG_IGN); | |
254 | signal(SIGTTOU, SIG_IGN); | |
255 | signal(SIGTTIN, SIG_IGN); | |
256 | } | |
257 | } | |
258 | ||
259 | void os_setup_post(void) | |
260 | { | |
261 | int fd = 0; | |
262 | ||
263 | if (daemonize) { | |
eb505be1 | 264 | if (chdir("/")) { |
a7aaec14 | 265 | error_report("not able to chdir to /: %s", strerror(errno)); |
eb505be1 JS |
266 | exit(1); |
267 | } | |
8b6aa693 | 268 | fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR)); |
63ce8e15 GA |
269 | if (fd == -1) { |
270 | exit(1); | |
271 | } | |
eb505be1 JS |
272 | } |
273 | ||
e06eb601 JS |
274 | change_root(); |
275 | change_process_uid(); | |
eb505be1 JS |
276 | |
277 | if (daemonize) { | |
25cec2b8 MT |
278 | uint8_t status = 0; |
279 | ssize_t len; | |
280 | ||
eb505be1 JS |
281 | dup2(fd, 0); |
282 | dup2(fd, 1); | |
96c33a45 | 283 | /* In case -D is given do not redirect stderr to /dev/null */ |
229ef2eb | 284 | if (!qemu_log_enabled()) { |
96c33a45 DA |
285 | dup2(fd, 2); |
286 | } | |
eb505be1 JS |
287 | |
288 | close(fd); | |
25cec2b8 MT |
289 | |
290 | do { | |
291 | len = write(daemon_pipe, &status, 1); | |
292 | } while (len < 0 && errno == EINTR); | |
293 | if (len != 1) { | |
294 | exit(1); | |
295 | } | |
eb505be1 JS |
296 | } |
297 | } | |
298 | ||
9156d763 JS |
299 | void os_set_line_buffering(void) |
300 | { | |
301 | setvbuf(stdout, NULL, _IOLBF, 0); | |
302 | } | |
949d31e6 | 303 | |
888a6bc6 SM |
304 | int os_mlock(void) |
305 | { | |
195588cc | 306 | #ifdef HAVE_MLOCKALL |
888a6bc6 SM |
307 | int ret = 0; |
308 | ||
309 | ret = mlockall(MCL_CURRENT | MCL_FUTURE); | |
310 | if (ret < 0) { | |
a7aaec14 | 311 | error_report("mlockall: %s", strerror(errno)); |
888a6bc6 SM |
312 | } |
313 | ||
314 | return ret; | |
195588cc DC |
315 | #else |
316 | return -ENOSYS; | |
317 | #endif | |
888a6bc6 | 318 | } |