]> git.proxmox.com Git - qemu.git/blame - os-posix.c
Make os_change_process_uid and os_change_root os-posix.c local
[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
26#include <unistd.h>
27#include <fcntl.h>
28#include <signal.h>
8d963e6a
JS
29#include <sys/types.h>
30#include <sys/wait.h>
8847cfe8 31#include <pwd.h>
6170540b 32#include <libgen.h>
86b645e7
JS
33
34/* Needed early for CONFIG_BSD etc. */
35#include "config-host.h"
36#include "sysemu.h"
59a5264b
JS
37#include "net/slirp.h"
38#include "qemu-options.h"
86b645e7 39
8847cfe8 40static struct passwd *user_pwd;
0766379d 41static const char *chroot_dir;
eb505be1
JS
42static int daemonize;
43static int fds[2];
8847cfe8 44
fe98ac14 45void os_setup_early_signal_handling(void)
86b645e7
JS
46{
47 struct sigaction act;
48 sigfillset(&act.sa_mask);
49 act.sa_flags = 0;
50 act.sa_handler = SIG_IGN;
51 sigaction(SIGPIPE, &act, NULL);
52}
8d963e6a
JS
53
54static void termsig_handler(int signal)
55{
56 qemu_system_shutdown_request();
57}
58
59static void sigchld_handler(int signal)
60{
61 waitpid(-1, NULL, WNOHANG);
62}
63
64void os_setup_signal_handling(void)
65{
66 struct sigaction act;
67
68 memset(&act, 0, sizeof(act));
69 act.sa_handler = termsig_handler;
70 sigaction(SIGINT, &act, NULL);
71 sigaction(SIGHUP, &act, NULL);
72 sigaction(SIGTERM, &act, NULL);
73
74 act.sa_handler = sigchld_handler;
75 act.sa_flags = SA_NOCLDSTOP;
76 sigaction(SIGCHLD, &act, NULL);
77}
6170540b
JS
78
79/* Find a likely location for support files using the location of the binary.
80 For installed binaries this will be "$bindir/../share/qemu". When
81 running from the build tree this will be "$bindir/../pc-bios". */
82#define SHARE_SUFFIX "/share/qemu"
83#define BUILD_SUFFIX "/pc-bios"
84char *os_find_datadir(const char *argv0)
85{
86 char *dir;
87 char *p = NULL;
88 char *res;
89 char buf[PATH_MAX];
90 size_t max_len;
91
92#if defined(__linux__)
93 {
94 int len;
95 len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
96 if (len > 0) {
97 buf[len] = 0;
98 p = buf;
99 }
100 }
101#elif defined(__FreeBSD__)
102 {
103 static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
104 size_t len = sizeof(buf) - 1;
105
106 *buf = '\0';
107 if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) &&
108 *buf) {
109 buf[sizeof(buf) - 1] = '\0';
110 p = buf;
111 }
112 }
113#endif
114 /* If we don't have any way of figuring out the actual executable
115 location then try argv[0]. */
116 if (!p) {
117 p = realpath(argv0, buf);
118 if (!p) {
119 return NULL;
120 }
121 }
122 dir = dirname(p);
123 dir = dirname(dir);
124
125 max_len = strlen(dir) +
126 MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1;
127 res = qemu_mallocz(max_len);
128 snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX);
129 if (access(res, R_OK)) {
130 snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX);
131 if (access(res, R_OK)) {
132 qemu_free(res);
133 res = NULL;
134 }
135 }
136
137 return res;
138}
139#undef SHARE_SUFFIX
140#undef BUILD_SUFFIX
59a5264b
JS
141
142/*
143 * Parse OS specific command line options.
144 * return 0 if option handled, -1 otherwise
145 */
146void os_parse_cmd_args(int index, const char *optarg)
147{
148 switch (index) {
149#ifdef CONFIG_SLIRP
150 case QEMU_OPTION_smb:
151 if (net_slirp_smb(optarg) < 0)
152 exit(1);
153 break;
154#endif
8847cfe8
JS
155 case QEMU_OPTION_runas:
156 user_pwd = getpwnam(optarg);
157 if (!user_pwd) {
158 fprintf(stderr, "User \"%s\" doesn't exist\n", optarg);
159 exit(1);
160 }
161 break;
0766379d
JS
162 case QEMU_OPTION_chroot:
163 chroot_dir = optarg;
164 break;
eb505be1
JS
165 case QEMU_OPTION_daemonize:
166 daemonize = 1;
167 break;
59a5264b
JS
168 }
169 return;
170}
8847cfe8 171
e06eb601 172static void change_process_uid(void)
8847cfe8
JS
173{
174 if (user_pwd) {
175 if (setgid(user_pwd->pw_gid) < 0) {
176 fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid);
177 exit(1);
178 }
179 if (setuid(user_pwd->pw_uid) < 0) {
180 fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid);
181 exit(1);
182 }
183 if (setuid(0) != -1) {
184 fprintf(stderr, "Dropping privileges failed\n");
185 exit(1);
186 }
187 }
188}
0766379d 189
e06eb601 190static void change_root(void)
0766379d
JS
191{
192 if (chroot_dir) {
193 if (chroot(chroot_dir) < 0) {
194 fprintf(stderr, "chroot failed\n");
195 exit(1);
196 }
197 if (chdir("/")) {
198 perror("not able to chdir to /");
199 exit(1);
200 }
201 }
202
203}
eb505be1
JS
204
205void os_daemonize(void)
206{
207 if (daemonize) {
208 pid_t pid;
209
210 if (pipe(fds) == -1)
211 exit(1);
212
213 pid = fork();
214 if (pid > 0) {
215 uint8_t status;
216 ssize_t len;
217
218 close(fds[1]);
219
220 again:
221 len = read(fds[0], &status, 1);
222 if (len == -1 && (errno == EINTR))
223 goto again;
224
225 if (len != 1)
226 exit(1);
227 else if (status == 1) {
228 fprintf(stderr, "Could not acquire pidfile: %s\n", strerror(errno));
229 exit(1);
230 } else
231 exit(0);
232 } else if (pid < 0)
233 exit(1);
234
235 close(fds[0]);
236 qemu_set_cloexec(fds[1]);
237
238 setsid();
239
240 pid = fork();
241 if (pid > 0)
242 exit(0);
243 else if (pid < 0)
244 exit(1);
245
246 umask(027);
247
248 signal(SIGTSTP, SIG_IGN);
249 signal(SIGTTOU, SIG_IGN);
250 signal(SIGTTIN, SIG_IGN);
251 }
252}
253
254void os_setup_post(void)
255{
256 int fd = 0;
257
258 if (daemonize) {
259 uint8_t status = 0;
260 ssize_t len;
261
262 again1:
263 len = write(fds[1], &status, 1);
264 if (len == -1 && (errno == EINTR))
265 goto again1;
266
267 if (len != 1)
268 exit(1);
269
270 if (chdir("/")) {
271 perror("not able to chdir to /");
272 exit(1);
273 }
274 TFR(fd = qemu_open("/dev/null", O_RDWR));
275 if (fd == -1)
276 exit(1);
277 }
278
e06eb601
JS
279 change_root();
280 change_process_uid();
eb505be1
JS
281
282 if (daemonize) {
283 dup2(fd, 0);
284 dup2(fd, 1);
285 dup2(fd, 2);
286
287 close(fd);
288 }
289}
290
291void os_pidfile_error(void)
292{
293 if (daemonize) {
294 uint8_t status = 1;
295 if (write(fds[1], &status, 1) != 1) {
296 perror("daemonize. Writing to pipe\n");
297 }
298 } else
299 fprintf(stderr, "Could not acquire pid file: %s\n", strerror(errno));
300}