]>
Commit | Line | Data |
---|---|---|
ba180fd4 JD |
1 | /* |
2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | |
3 | * Licensed under the GPL | |
4 | */ | |
5 | ||
2264c475 | 6 | #include <stdio.h> |
2264c475 | 7 | #include <stdlib.h> |
ba180fd4 | 8 | #include <dirent.h> |
2264c475 | 9 | #include <errno.h> |
ba180fd4 | 10 | #include <fcntl.h> |
2264c475 | 11 | #include <signal.h> |
ba180fd4 JD |
12 | #include <string.h> |
13 | #include <unistd.h> | |
2264c475 | 14 | #include <sys/stat.h> |
37185b33 AV |
15 | #include <init.h> |
16 | #include <os.h> | |
2264c475 JD |
17 | |
18 | #define UML_DIR "~/.uml/" | |
19 | ||
20 | #define UMID_LEN 64 | |
21 | ||
22 | /* Changed by set_umid, which is run early in boot */ | |
de5fe76e | 23 | static char umid[UMID_LEN] = { 0 }; |
2264c475 JD |
24 | |
25 | /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */ | |
26 | static char *uml_dir = UML_DIR; | |
27 | ||
28 | static int __init make_uml_dir(void) | |
29 | { | |
30 | char dir[512] = { '\0' }; | |
7eebe8a9 | 31 | int len, err; |
2264c475 | 32 | |
ba180fd4 | 33 | if (*uml_dir == '~') { |
2264c475 JD |
34 | char *home = getenv("HOME"); |
35 | ||
7eebe8a9 | 36 | err = -ENOENT; |
ba180fd4 | 37 | if (home == NULL) { |
e03c78ac MH |
38 | printk(UM_KERN_ERR |
39 | "%s: no value in environment for $HOME\n", | |
40 | __func__); | |
7eebe8a9 | 41 | goto err; |
2264c475 JD |
42 | } |
43 | strlcpy(dir, home, sizeof(dir)); | |
44 | uml_dir++; | |
45 | } | |
46 | strlcat(dir, uml_dir, sizeof(dir)); | |
47 | len = strlen(dir); | |
48 | if (len > 0 && dir[len - 1] != '/') | |
49 | strlcat(dir, "/", sizeof(dir)); | |
50 | ||
7eebe8a9 | 51 | err = -ENOMEM; |
2264c475 JD |
52 | uml_dir = malloc(strlen(dir) + 1); |
53 | if (uml_dir == NULL) { | |
e03c78ac MH |
54 | printk(UM_KERN_ERR "%s : malloc failed, errno = %d\n", |
55 | __func__, errno); | |
7eebe8a9 | 56 | goto err; |
2264c475 JD |
57 | } |
58 | strcpy(uml_dir, dir); | |
59 | ||
ba180fd4 | 60 | if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) { |
e03c78ac MH |
61 | printk(UM_KERN_ERR "Failed to mkdir '%s': %s\n", |
62 | uml_dir, strerror(errno)); | |
7eebe8a9 JD |
63 | err = -errno; |
64 | goto err_free; | |
2264c475 JD |
65 | } |
66 | return 0; | |
7eebe8a9 JD |
67 | |
68 | err_free: | |
69 | free(uml_dir); | |
70 | err: | |
71 | uml_dir = NULL; | |
72 | return err; | |
2264c475 JD |
73 | } |
74 | ||
eb28931e PBG |
75 | /* |
76 | * Unlinks the files contained in @dir and then removes @dir. | |
77 | * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We | |
ba180fd4 JD |
78 | * ignore ENOENT errors for anything (they happen, strangely enough - possibly |
79 | * due to races between multiple dying UML threads). | |
eb28931e PBG |
80 | */ |
81 | static int remove_files_and_dir(char *dir) | |
2264c475 JD |
82 | { |
83 | DIR *directory; | |
84 | struct dirent *ent; | |
85 | int len; | |
86 | char file[256]; | |
eb28931e | 87 | int ret; |
2264c475 JD |
88 | |
89 | directory = opendir(dir); | |
eb28931e PBG |
90 | if (directory == NULL) { |
91 | if (errno != ENOENT) | |
92 | return -errno; | |
93 | else | |
94 | return 0; | |
95 | } | |
7eebe8a9 | 96 | |
eb28931e PBG |
97 | while ((ent = readdir(directory)) != NULL) { |
98 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) | |
2264c475 JD |
99 | continue; |
100 | len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; | |
eb28931e PBG |
101 | if (len > sizeof(file)) { |
102 | ret = -E2BIG; | |
103 | goto out; | |
104 | } | |
7eebe8a9 | 105 | |
2264c475 | 106 | sprintf(file, "%s/%s", dir, ent->d_name); |
eb28931e PBG |
107 | if (unlink(file) < 0 && errno != ENOENT) { |
108 | ret = -errno; | |
109 | goto out; | |
110 | } | |
2264c475 | 111 | } |
7eebe8a9 | 112 | |
eb28931e PBG |
113 | if (rmdir(dir) < 0 && errno != ENOENT) { |
114 | ret = -errno; | |
115 | goto out; | |
116 | } | |
117 | ||
118 | ret = 0; | |
119 | out: | |
120 | closedir(directory); | |
121 | return ret; | |
2264c475 JD |
122 | } |
123 | ||
ba180fd4 JD |
124 | /* |
125 | * This says that there isn't already a user of the specified directory even if | |
7eebe8a9 JD |
126 | * there are errors during the checking. This is because if these errors |
127 | * happen, the directory is unusable by the pre-existing UML, so we might as | |
128 | * well take it over. This could happen either by | |
129 | * the existing UML somehow corrupting its umid directory | |
130 | * something other than UML sticking stuff in the directory | |
131 | * this boot racing with a shutdown of the other UML | |
132 | * In any of these cases, the directory isn't useful for anything else. | |
912ad922 PBG |
133 | * |
134 | * Boolean return: 1 if in use, 0 otherwise. | |
7eebe8a9 | 135 | */ |
912ad922 | 136 | static inline int is_umdir_used(char *dir) |
2264c475 JD |
137 | { |
138 | char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; | |
139 | char pid[sizeof("nnnnn\0")], *end; | |
7eebe8a9 JD |
140 | int dead, fd, p, n, err; |
141 | ||
142 | n = snprintf(file, sizeof(file), "%s/pid", dir); | |
ba180fd4 JD |
143 | if (n >= sizeof(file)) { |
144 | printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n"); | |
7eebe8a9 JD |
145 | err = -E2BIG; |
146 | goto out; | |
147 | } | |
2264c475 | 148 | |
2264c475 | 149 | dead = 0; |
7eebe8a9 | 150 | fd = open(file, O_RDONLY); |
ba180fd4 | 151 | if (fd < 0) { |
d84a19ce | 152 | fd = -errno; |
ba180fd4 JD |
153 | if (fd != -ENOENT) { |
154 | printk(UM_KERN_ERR "is_umdir_used : couldn't open pid " | |
155 | "file '%s', err = %d\n", file, -fd); | |
2264c475 | 156 | } |
7eebe8a9 | 157 | goto out; |
2264c475 | 158 | } |
7eebe8a9 JD |
159 | |
160 | err = 0; | |
161 | n = read(fd, pid, sizeof(pid)); | |
ba180fd4 JD |
162 | if (n < 0) { |
163 | printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file " | |
164 | "'%s', err = %d\n", file, errno); | |
d84a19ce | 165 | goto out_close; |
ba180fd4 JD |
166 | } else if (n == 0) { |
167 | printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file " | |
168 | "'%s', 0-byte read\n", file); | |
7eebe8a9 JD |
169 | goto out_close; |
170 | } | |
171 | ||
172 | p = strtoul(pid, &end, 0); | |
ba180fd4 JD |
173 | if (end == pid) { |
174 | printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file " | |
175 | "'%s', errno = %d\n", file, errno); | |
7eebe8a9 | 176 | goto out_close; |
2264c475 | 177 | } |
7eebe8a9 | 178 | |
ba180fd4 JD |
179 | if ((kill(p, 0) == 0) || (errno != ESRCH)) { |
180 | printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n", | |
181 | umid, p); | |
7eebe8a9 | 182 | return 1; |
1fbbd684 | 183 | } |
7eebe8a9 | 184 | |
d84a19ce | 185 | out_close: |
7eebe8a9 | 186 | close(fd); |
d84a19ce | 187 | out: |
7eebe8a9 | 188 | return 0; |
2264c475 JD |
189 | } |
190 | ||
912ad922 PBG |
191 | /* |
192 | * Try to remove the directory @dir unless it's in use. | |
193 | * Precondition: @dir exists. | |
194 | * Returns 0 for success, < 0 for failure in removal or if the directory is in | |
195 | * use. | |
196 | */ | |
197 | static int umdir_take_if_dead(char *dir) | |
198 | { | |
199 | int ret; | |
200 | if (is_umdir_used(dir)) | |
201 | return -EEXIST; | |
202 | ||
eb28931e | 203 | ret = remove_files_and_dir(dir); |
912ad922 | 204 | if (ret) { |
ba180fd4 JD |
205 | printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir " |
206 | "failed with err = %d\n", ret); | |
912ad922 PBG |
207 | } |
208 | return ret; | |
209 | } | |
210 | ||
2264c475 JD |
211 | static void __init create_pid_file(void) |
212 | { | |
213 | char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; | |
214 | char pid[sizeof("nnnnn\0")]; | |
215 | int fd, n; | |
216 | ||
ba180fd4 | 217 | if (umid_file_name("pid", file, sizeof(file))) |
2264c475 JD |
218 | return; |
219 | ||
7eebe8a9 | 220 | fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644); |
ba180fd4 JD |
221 | if (fd < 0) { |
222 | printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: " | |
223 | "%s\n", file, strerror(errno)); | |
2264c475 JD |
224 | return; |
225 | } | |
226 | ||
7eebe8a9 JD |
227 | snprintf(pid, sizeof(pid), "%d\n", getpid()); |
228 | n = write(fd, pid, strlen(pid)); | |
ba180fd4 JD |
229 | if (n != strlen(pid)) |
230 | printk(UM_KERN_ERR "Write of pid file failed - err = %d\n", | |
231 | errno); | |
7eebe8a9 JD |
232 | |
233 | close(fd); | |
2264c475 JD |
234 | } |
235 | ||
7eebe8a9 | 236 | int __init set_umid(char *name) |
2264c475 | 237 | { |
ba180fd4 | 238 | if (strlen(name) > UMID_LEN - 1) |
7eebe8a9 JD |
239 | return -E2BIG; |
240 | ||
2264c475 JD |
241 | strlcpy(umid, name, sizeof(umid)); |
242 | ||
243 | return 0; | |
244 | } | |
245 | ||
de5fe76e | 246 | /* Changed in make_umid, which is called during early boot */ |
2264c475 JD |
247 | static int umid_setup = 0; |
248 | ||
99764fa4 | 249 | static int __init make_umid(void) |
2264c475 JD |
250 | { |
251 | int fd, err; | |
252 | char tmp[256]; | |
253 | ||
ba180fd4 | 254 | if (umid_setup) |
7eebe8a9 JD |
255 | return 0; |
256 | ||
2264c475 JD |
257 | make_uml_dir(); |
258 | ||
ba180fd4 | 259 | if (*umid == '\0') { |
2264c475 | 260 | strlcpy(tmp, uml_dir, sizeof(tmp)); |
7eebe8a9 | 261 | strlcat(tmp, "XXXXXX", sizeof(tmp)); |
2264c475 | 262 | fd = mkstemp(tmp); |
ba180fd4 JD |
263 | if (fd < 0) { |
264 | printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: " | |
265 | "%s\n", tmp, strerror(errno)); | |
7eebe8a9 JD |
266 | err = -errno; |
267 | goto err; | |
2264c475 JD |
268 | } |
269 | ||
7eebe8a9 JD |
270 | close(fd); |
271 | ||
272 | set_umid(&tmp[strlen(uml_dir)]); | |
273 | ||
ba180fd4 JD |
274 | /* |
275 | * There's a nice tiny little race between this unlink and | |
2264c475 JD |
276 | * the mkdir below. It'd be nice if there were a mkstemp |
277 | * for directories. | |
278 | */ | |
ba180fd4 | 279 | if (unlink(tmp)) { |
7eebe8a9 JD |
280 | err = -errno; |
281 | goto err; | |
282 | } | |
2264c475 JD |
283 | } |
284 | ||
7eebe8a9 | 285 | snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid); |
2264c475 | 286 | err = mkdir(tmp, 0777); |
ba180fd4 | 287 | if (err < 0) { |
7eebe8a9 | 288 | err = -errno; |
ba180fd4 | 289 | if (err != -EEXIST) |
7eebe8a9 JD |
290 | goto err; |
291 | ||
912ad922 | 292 | if (umdir_take_if_dead(tmp) < 0) |
7eebe8a9 JD |
293 | goto err; |
294 | ||
295 | err = mkdir(tmp, 0777); | |
2264c475 | 296 | } |
ba180fd4 | 297 | if (err) { |
1fbbd684 | 298 | err = -errno; |
ba180fd4 JD |
299 | printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid, |
300 | errno); | |
1fbbd684 | 301 | goto err; |
2264c475 JD |
302 | } |
303 | ||
304 | umid_setup = 1; | |
305 | ||
306 | create_pid_file(); | |
307 | ||
1fbbd684 | 308 | err = 0; |
7eebe8a9 JD |
309 | err: |
310 | return err; | |
2264c475 JD |
311 | } |
312 | ||
313 | static int __init make_umid_init(void) | |
314 | { | |
ba180fd4 | 315 | if (!make_umid()) |
1fbbd684 JD |
316 | return 0; |
317 | ||
ba180fd4 JD |
318 | /* |
319 | * If initializing with the given umid failed, then try again with | |
1fbbd684 JD |
320 | * a random one. |
321 | */ | |
ba180fd4 JD |
322 | printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a " |
323 | "random umid\n", umid); | |
1fbbd684 | 324 | *umid = '\0'; |
7eebe8a9 | 325 | make_umid(); |
2264c475 | 326 | |
7eebe8a9 | 327 | return 0; |
2264c475 JD |
328 | } |
329 | ||
330 | __initcall(make_umid_init); | |
331 | ||
332 | int __init umid_file_name(char *name, char *buf, int len) | |
333 | { | |
334 | int n, err; | |
335 | ||
7eebe8a9 | 336 | err = make_umid(); |
ba180fd4 | 337 | if (err) |
7eebe8a9 | 338 | return err; |
2264c475 | 339 | |
7eebe8a9 | 340 | n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name); |
ba180fd4 JD |
341 | if (n >= len) { |
342 | printk(UM_KERN_ERR "umid_file_name : buffer too short\n"); | |
7eebe8a9 | 343 | return -E2BIG; |
2264c475 JD |
344 | } |
345 | ||
7eebe8a9 | 346 | return 0; |
2264c475 JD |
347 | } |
348 | ||
7eebe8a9 | 349 | char *get_umid(void) |
2264c475 | 350 | { |
2264c475 JD |
351 | return umid; |
352 | } | |
353 | ||
354 | static int __init set_uml_dir(char *name, int *add) | |
355 | { | |
ba180fd4 | 356 | if (*name == '\0') { |
0936d4f3 | 357 | os_warn("uml_dir can't be an empty string\n"); |
7eebe8a9 | 358 | return 0; |
2264c475 | 359 | } |
7eebe8a9 | 360 | |
ba180fd4 | 361 | if (name[strlen(name) - 1] == '/') { |
7eebe8a9 JD |
362 | uml_dir = name; |
363 | return 0; | |
364 | } | |
365 | ||
366 | uml_dir = malloc(strlen(name) + 2); | |
ba180fd4 | 367 | if (uml_dir == NULL) { |
0936d4f3 | 368 | os_warn("Failed to malloc uml_dir - error = %d\n", errno); |
7eebe8a9 | 369 | |
ba180fd4 JD |
370 | /* |
371 | * Return 0 here because do_initcalls doesn't look at | |
7eebe8a9 JD |
372 | * the return value. |
373 | */ | |
374 | return 0; | |
375 | } | |
376 | sprintf(uml_dir, "%s/", name); | |
377 | ||
378 | return 0; | |
2264c475 JD |
379 | } |
380 | ||
381 | __uml_setup("uml_dir=", set_uml_dir, | |
382 | "uml_dir=<directory>\n" | |
383 | " The location to place the pid and umid files.\n\n" | |
384 | ); | |
385 | ||
386 | static void remove_umid_dir(void) | |
387 | { | |
7eebe8a9 | 388 | char dir[strlen(uml_dir) + UMID_LEN + 1], err; |
2264c475 JD |
389 | |
390 | sprintf(dir, "%s%s", uml_dir, umid); | |
eb28931e | 391 | err = remove_files_and_dir(dir); |
ba180fd4 | 392 | if (err) |
0936d4f3 MH |
393 | os_warn("%s - remove_files_and_dir failed with err = %d\n", |
394 | __func__, err); | |
2264c475 JD |
395 | } |
396 | ||
397 | __uml_exitcall(remove_umid_dir); |