blob: 52ef6990ff9d4c4e2cd52b6f6a820cdbb8efb9fb [file] [log] [blame]
Jes Sorensen86b645e2010-06-10 11:42:19 +02001/*
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
Peter Maydelld38ea872016-01-29 17:50:05 +000026#include "qemu/osdep.h"
Jes Sorensen8d963e62010-06-10 11:42:22 +020027#include <sys/wait.h>
Jes Sorensen8847cfe2010-06-10 11:42:26 +020028#include <pwd.h>
Stefan Hajnoczicc4662f2011-07-09 10:22:07 +010029#include <grp.h>
Jes Sorensen61705402010-06-10 11:42:23 +020030#include <libgen.h>
Jes Sorensen86b645e2010-06-10 11:42:19 +020031
Thomas Huthf853ac62016-01-13 09:05:32 +010032#include "qemu/error-report.h"
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +020033#include "qemu/log.h"
Markus Armbruster54d31232019-08-12 07:23:59 +020034#include "sysemu/runstate.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020035#include "qemu/cutils.h"
Jes Sorensen86b645e2010-06-10 11:42:19 +020036
Jes Sorensence798cf2010-06-10 11:42:31 +020037#ifdef CONFIG_LINUX
38#include <sys/prctl.h>
Jes Sorensen949d31e2010-10-26 10:39:22 +020039#endif
40
Jes Sorensen8847cfe2010-06-10 11:42:26 +020041
Jes Sorensenfe98ac12010-06-10 11:42:21 +020042void os_setup_early_signal_handling(void)
Jes Sorensen86b645e2010-06-10 11:42:19 +020043{
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}
Jes Sorensen8d963e62010-06-10 11:42:22 +020050
Gleb Natapovf64622c2011-03-15 13:56:04 +020051static void termsig_handler(int signal, siginfo_t *info, void *c)
Jes Sorensen8d963e62010-06-10 11:42:22 +020052{
Gleb Natapovf64622c2011-03-15 13:56:04 +020053 qemu_system_killed(info->si_signo, info->si_pid);
Jes Sorensen8d963e62010-06-10 11:42:22 +020054}
55
Jes Sorensen8d963e62010-06-10 11:42:22 +020056void os_setup_signal_handling(void)
57{
58 struct sigaction act;
59
60 memset(&act, 0, sizeof(act));
Gleb Natapovf64622c2011-03-15 13:56:04 +020061 act.sa_sigaction = termsig_handler;
62 act.sa_flags = SA_SIGINFO;
Jes Sorensen8d963e62010-06-10 11:42:22 +020063 sigaction(SIGINT, &act, NULL);
64 sigaction(SIGHUP, &act, NULL);
65 sigaction(SIGTERM, &act, NULL);
Jes Sorensen8d963e62010-06-10 11:42:22 +020066}
Jes Sorensen61705402010-06-10 11:42:23 +020067
Jes Sorensence798cf2010-06-10 11:42:31 +020068void os_set_proc_name(const char *s)
69{
70#if defined(PR_SET_NAME)
71 char name[16];
72 if (!s)
73 return;
Jim Meyering3eadc682012-10-04 13:09:51 +020074 pstrcpy(name, sizeof(name), s);
Jes Sorensence798cf2010-06-10 11:42:31 +020075 /* 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)) {
Ian Jacksona7aaec12018-04-16 15:16:23 +010078 error_report("unable to change process name: %s", strerror(errno));
Jes Sorensence798cf2010-06-10 11:42:31 +020079 exit(1);
80 }
81#else
Ian Jackson22cd4f42018-04-16 15:15:51 +010082 error_report("Change of process name not supported by your OS");
Jes Sorensence798cf2010-06-10 11:42:31 +020083 exit(1);
84#endif
85}
86
Michael Tokarev433aed52023-09-01 13:12:59 +030087
88/*
89 * Must set all three of these at once.
90 * Legal combinations are unset by name by uid
91 */
92static struct passwd *user_pwd; /* NULL non-NULL NULL */
93static uid_t user_uid = (uid_t)-1; /* -1 -1 >=0 */
94static gid_t user_gid = (gid_t)-1; /* -1 -1 >=0 */
95
Michael Tokarev22d02512023-09-01 13:12:56 +030096/*
Philippe Mathieu-Daudéd2803372023-10-04 14:00:07 +020097 * Prepare to change user ID. user_id can be one of 3 forms:
Michael Tokarev22d02512023-09-01 13:12:56 +030098 * - 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 */
Philippe Mathieu-Daudéd2803372023-10-04 14:00:07 +0200103bool os_set_runas(const char *user_id)
Ian Jackson2c42f1e2017-09-15 18:10:44 +0100104{
105 unsigned long lv;
106 const char *ep;
107 uid_t got_uid;
108 gid_t got_gid;
109 int rc;
110
Philippe Mathieu-Daudéd2803372023-10-04 14:00:07 +0200111 user_pwd = getpwnam(user_id);
Michael Tokarev22d02512023-09-01 13:12:56 +0300112 if (user_pwd) {
113 user_uid = -1;
114 user_gid = -1;
115 return true;
116 }
117
Philippe Mathieu-Daudéd2803372023-10-04 14:00:07 +0200118 rc = qemu_strtoul(user_id, &ep, 0, &lv);
Ian Jackson2c42f1e2017-09-15 18:10:44 +0100119 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
Jes Sorensene06eb602010-06-10 11:42:29 +0200136static void change_process_uid(void)
Jes Sorensen8847cfe2010-06-10 11:42:26 +0200137{
Ian Jackson2c42f1e2017-09-15 18:10:44 +0100138 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);
Jes Sorensen8847cfe2010-06-10 11:42:26 +0200147 exit(1);
148 }
Ian Jackson2c42f1e2017-09-15 18:10:44 +0100149 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 }
Stefan Hajnoczicc4662f2011-07-09 10:22:07 +0100161 }
Ian Jackson2c42f1e2017-09-15 18:10:44 +0100162 if (setuid(intended_uid) < 0) {
163 error_report("Failed to setuid(%d)", intended_uid);
Jes Sorensen8847cfe2010-06-10 11:42:26 +0200164 exit(1);
165 }
166 if (setuid(0) != -1) {
Ian Jacksonf0a21712018-04-16 15:08:03 +0100167 error_report("Dropping privileges failed");
Jes Sorensen8847cfe2010-06-10 11:42:26 +0200168 exit(1);
169 }
170 }
171}
Jes Sorensen07663792010-06-10 11:42:27 +0200172
Michael Tokarev433aed52023-09-01 13:12:59 +0300173
174static const char *chroot_dir;
175
Philippe Mathieu-Daudéd2803372023-10-04 14:00:07 +0200176void os_set_chroot(const char *path)
Michael Tokarev5b156392023-09-01 13:12:57 +0300177{
Philippe Mathieu-Daudéd2803372023-10-04 14:00:07 +0200178 chroot_dir = path;
Michael Tokarev5b156392023-09-01 13:12:57 +0300179}
180
Jes Sorensene06eb602010-06-10 11:42:29 +0200181static void change_root(void)
Jes Sorensen07663792010-06-10 11:42:27 +0200182{
183 if (chroot_dir) {
184 if (chroot(chroot_dir) < 0) {
Ian Jackson22cd4f42018-04-16 15:15:51 +0100185 error_report("chroot failed");
Jes Sorensen07663792010-06-10 11:42:27 +0200186 exit(1);
187 }
188 if (chdir("/")) {
Ian Jacksona7aaec12018-04-16 15:16:23 +0100189 error_report("not able to chdir to /: %s", strerror(errno));
Jes Sorensen07663792010-06-10 11:42:27 +0200190 exit(1);
191 }
192 }
193
194}
Jes Sorenseneb505be2010-06-10 11:42:28 +0200195
Michael Tokarev433aed52023-09-01 13:12:59 +0300196
197static int daemonize;
198static int daemon_pipe;
199
200bool is_daemonized(void)
201{
202 return daemonize;
203}
204
205int os_set_daemonize(bool d)
206{
207 daemonize = d;
208 return 0;
209}
210
Jes Sorenseneb505be2010-06-10 11:42:28 +0200211void os_daemonize(void)
212{
213 if (daemonize) {
Gonglei63ce8e12014-09-26 16:14:30 +0800214 pid_t pid;
Michael Tokarev0be5e432014-10-30 17:30:51 +0300215 int fds[2];
Jes Sorenseneb505be2010-06-10 11:42:28 +0200216
Marc-André Lureau3338a412022-04-22 14:47:59 +0400217 if (!g_unix_open_pipe(fds, FD_CLOEXEC, NULL)) {
Gonglei63ce8e12014-09-26 16:14:30 +0800218 exit(1);
219 }
Jes Sorenseneb505be2010-06-10 11:42:28 +0200220
Gonglei63ce8e12014-09-26 16:14:30 +0800221 pid = fork();
222 if (pid > 0) {
223 uint8_t status;
224 ssize_t len;
Jes Sorenseneb505be2010-06-10 11:42:28 +0200225
Gonglei63ce8e12014-09-26 16:14:30 +0800226 close(fds[1]);
Jes Sorenseneb505be2010-06-10 11:42:28 +0200227
Michael Tokarevccea25f2014-10-30 17:37:16 +0300228 do {
229 len = read(fds[0], &status, 1);
230 } while (len < 0 && errno == EINTR);
Michael Tokarevfee78fd2014-10-30 17:40:48 +0300231
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 }
Gonglei63ce8e12014-09-26 16:14:30 +0800239
240 close(fds[0]);
Michael Tokarev0be5e432014-10-30 17:30:51 +0300241 daemon_pipe = fds[1];
Gonglei63ce8e12014-09-26 16:14:30 +0800242
243 setsid();
244
245 pid = fork();
246 if (pid > 0) {
247 exit(0);
248 } else if (pid < 0) {
Jes Sorenseneb505be2010-06-10 11:42:28 +0200249 exit(1);
Gonglei63ce8e12014-09-26 16:14:30 +0800250 }
251 umask(027);
Jes Sorenseneb505be2010-06-10 11:42:28 +0200252
253 signal(SIGTSTP, SIG_IGN);
254 signal(SIGTTOU, SIG_IGN);
255 signal(SIGTTIN, SIG_IGN);
256 }
257}
258
259void os_setup_post(void)
260{
261 int fd = 0;
262
263 if (daemonize) {
Jes Sorenseneb505be2010-06-10 11:42:28 +0200264 if (chdir("/")) {
Ian Jacksona7aaec12018-04-16 15:16:23 +0100265 error_report("not able to chdir to /: %s", strerror(errno));
Jes Sorenseneb505be2010-06-10 11:42:28 +0200266 exit(1);
267 }
Nikita Ivanov8b6aa692022-10-23 12:04:21 +0300268 fd = RETRY_ON_EINTR(qemu_open_old("/dev/null", O_RDWR));
Gonglei63ce8e12014-09-26 16:14:30 +0800269 if (fd == -1) {
270 exit(1);
271 }
Jes Sorenseneb505be2010-06-10 11:42:28 +0200272 }
273
Jes Sorensene06eb602010-06-10 11:42:29 +0200274 change_root();
275 change_process_uid();
Jes Sorenseneb505be2010-06-10 11:42:28 +0200276
277 if (daemonize) {
Michael Tokarev25cec2b2014-10-30 17:47:46 +0300278 uint8_t status = 0;
279 ssize_t len;
280
Jes Sorenseneb505be2010-06-10 11:42:28 +0200281 dup2(fd, 0);
282 dup2(fd, 1);
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +0200283 /* In case -D is given do not redirect stderr to /dev/null */
Richard Henderson229ef2e2022-04-17 11:29:45 -0700284 if (!qemu_log_enabled()) {
Dimitris Aragiorgis96c33a42016-02-18 13:38:38 +0200285 dup2(fd, 2);
286 }
Jes Sorenseneb505be2010-06-10 11:42:28 +0200287
288 close(fd);
Michael Tokarev25cec2b2014-10-30 17:47:46 +0300289
290 do {
291 len = write(daemon_pipe, &status, 1);
292 } while (len < 0 && errno == EINTR);
293 if (len != 1) {
294 exit(1);
295 }
Jes Sorenseneb505be2010-06-10 11:42:28 +0200296 }
297}
298
Jes Sorensen9156d762010-06-10 11:42:30 +0200299void os_set_line_buffering(void)
300{
301 setvbuf(stdout, NULL, _IOLBF, 0);
302}
Jes Sorensen949d31e2010-10-26 10:39:22 +0200303
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200304int os_mlock(void)
305{
David CARLIER195588c2020-07-13 14:36:09 +0100306#ifdef HAVE_MLOCKALL
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200307 int ret = 0;
308
309 ret = mlockall(MCL_CURRENT | MCL_FUTURE);
310 if (ret < 0) {
Ian Jacksona7aaec12018-04-16 15:16:23 +0100311 error_report("mlockall: %s", strerror(errno));
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200312 }
313
314 return ret;
David CARLIER195588c2020-07-13 14:36:09 +0100315#else
316 return -ENOSYS;
317#endif
Satoru Moriya888a6bc2013-04-19 16:42:06 +0200318}