]>
Commit | Line | Data |
---|---|---|
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> | |
29 | #include <sys/types.h> | |
30 | #include <sys/wait.h> | |
31 | #include <pwd.h> | |
32 | #include <libgen.h> | |
33 | ||
34 | /* Needed early for CONFIG_BSD etc. */ | |
35 | #include "config-host.h" | |
36 | #include "sysemu.h" | |
37 | #include "net/slirp.h" | |
38 | #include "qemu-options.h" | |
39 | ||
40 | static struct passwd *user_pwd; | |
41 | ||
42 | void os_setup_early_signal_handling(void) | |
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 | } | |
50 | ||
51 | static void termsig_handler(int signal) | |
52 | { | |
53 | qemu_system_shutdown_request(); | |
54 | } | |
55 | ||
56 | static void sigchld_handler(int signal) | |
57 | { | |
58 | waitpid(-1, NULL, WNOHANG); | |
59 | } | |
60 | ||
61 | void os_setup_signal_handling(void) | |
62 | { | |
63 | struct sigaction act; | |
64 | ||
65 | memset(&act, 0, sizeof(act)); | |
66 | act.sa_handler = termsig_handler; | |
67 | sigaction(SIGINT, &act, NULL); | |
68 | sigaction(SIGHUP, &act, NULL); | |
69 | sigaction(SIGTERM, &act, NULL); | |
70 | ||
71 | act.sa_handler = sigchld_handler; | |
72 | act.sa_flags = SA_NOCLDSTOP; | |
73 | sigaction(SIGCHLD, &act, NULL); | |
74 | } | |
75 | ||
76 | /* Find a likely location for support files using the location of the binary. | |
77 | For installed binaries this will be "$bindir/../share/qemu". When | |
78 | running from the build tree this will be "$bindir/../pc-bios". */ | |
79 | #define SHARE_SUFFIX "/share/qemu" | |
80 | #define BUILD_SUFFIX "/pc-bios" | |
81 | char *os_find_datadir(const char *argv0) | |
82 | { | |
83 | char *dir; | |
84 | char *p = NULL; | |
85 | char *res; | |
86 | char buf[PATH_MAX]; | |
87 | size_t max_len; | |
88 | ||
89 | #if defined(__linux__) | |
90 | { | |
91 | int len; | |
92 | len = readlink("/proc/self/exe", buf, sizeof(buf) - 1); | |
93 | if (len > 0) { | |
94 | buf[len] = 0; | |
95 | p = buf; | |
96 | } | |
97 | } | |
98 | #elif defined(__FreeBSD__) | |
99 | { | |
100 | static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; | |
101 | size_t len = sizeof(buf) - 1; | |
102 | ||
103 | *buf = '\0'; | |
104 | if (!sysctl(mib, sizeof(mib)/sizeof(*mib), buf, &len, NULL, 0) && | |
105 | *buf) { | |
106 | buf[sizeof(buf) - 1] = '\0'; | |
107 | p = buf; | |
108 | } | |
109 | } | |
110 | #endif | |
111 | /* If we don't have any way of figuring out the actual executable | |
112 | location then try argv[0]. */ | |
113 | if (!p) { | |
114 | p = realpath(argv0, buf); | |
115 | if (!p) { | |
116 | return NULL; | |
117 | } | |
118 | } | |
119 | dir = dirname(p); | |
120 | dir = dirname(dir); | |
121 | ||
122 | max_len = strlen(dir) + | |
123 | MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1; | |
124 | res = qemu_mallocz(max_len); | |
125 | snprintf(res, max_len, "%s%s", dir, SHARE_SUFFIX); | |
126 | if (access(res, R_OK)) { | |
127 | snprintf(res, max_len, "%s%s", dir, BUILD_SUFFIX); | |
128 | if (access(res, R_OK)) { | |
129 | qemu_free(res); | |
130 | res = NULL; | |
131 | } | |
132 | } | |
133 | ||
134 | return res; | |
135 | } | |
136 | #undef SHARE_SUFFIX | |
137 | #undef BUILD_SUFFIX | |
138 | ||
139 | /* | |
140 | * Parse OS specific command line options. | |
141 | * return 0 if option handled, -1 otherwise | |
142 | */ | |
143 | void os_parse_cmd_args(int index, const char *optarg) | |
144 | { | |
145 | switch (index) { | |
146 | #ifdef CONFIG_SLIRP | |
147 | case QEMU_OPTION_smb: | |
148 | if (net_slirp_smb(optarg) < 0) | |
149 | exit(1); | |
150 | break; | |
151 | #endif | |
152 | case QEMU_OPTION_runas: | |
153 | user_pwd = getpwnam(optarg); | |
154 | if (!user_pwd) { | |
155 | fprintf(stderr, "User \"%s\" doesn't exist\n", optarg); | |
156 | exit(1); | |
157 | } | |
158 | break; | |
159 | } | |
160 | return; | |
161 | } | |
162 | ||
163 | void os_change_process_uid(void) | |
164 | { | |
165 | if (user_pwd) { | |
166 | if (setgid(user_pwd->pw_gid) < 0) { | |
167 | fprintf(stderr, "Failed to setgid(%d)\n", user_pwd->pw_gid); | |
168 | exit(1); | |
169 | } | |
170 | if (setuid(user_pwd->pw_uid) < 0) { | |
171 | fprintf(stderr, "Failed to setuid(%d)\n", user_pwd->pw_uid); | |
172 | exit(1); | |
173 | } | |
174 | if (setuid(0) != -1) { | |
175 | fprintf(stderr, "Dropping privileges failed\n"); | |
176 | exit(1); | |
177 | } | |
178 | } | |
179 | } |