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