2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
16 #include "kern_constants.h"
20 #define UML_DIR "~/.uml/"
24 /* Changed by set_umid, which is run early in boot */
25 static char umid[UMID_LEN] = { 0 };
27 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
28 static char *uml_dir = UML_DIR;
30 static int __init make_uml_dir(void)
32 char dir[512] = { '\0' };
35 if (*uml_dir == '~') {
36 char *home = getenv("HOME");
40 printk(UM_KERN_ERR "make_uml_dir : no value in "
41 "environment for $HOME\n");
44 strlcpy(dir, home, sizeof(dir));
47 strlcat(dir, uml_dir, sizeof(dir));
49 if (len > 0 && dir[len - 1] != '/')
50 strlcat(dir, "/", sizeof(dir));
53 uml_dir = malloc(strlen(dir) + 1);
54 if (uml_dir == NULL) {
55 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
60 if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
61 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
75 * Unlinks the files contained in @dir and then removes @dir.
76 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
77 * ignore ENOENT errors for anything (they happen, strangely enough - possibly
78 * due to races between multiple dying UML threads).
80 static int remove_files_and_dir(char *dir)
88 directory = opendir(dir);
89 if (directory == NULL) {
96 while ((ent = readdir(directory)) != NULL) {
97 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
99 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
100 if (len > sizeof(file)) {
105 sprintf(file, "%s/%s", dir, ent->d_name);
106 if (unlink(file) < 0 && errno != ENOENT) {
112 if (rmdir(dir) < 0 && errno != ENOENT) {
124 * This says that there isn't already a user of the specified directory even if
125 * there are errors during the checking. This is because if these errors
126 * happen, the directory is unusable by the pre-existing UML, so we might as
127 * well take it over. This could happen either by
128 * the existing UML somehow corrupting its umid directory
129 * something other than UML sticking stuff in the directory
130 * this boot racing with a shutdown of the other UML
131 * In any of these cases, the directory isn't useful for anything else.
133 * Boolean return: 1 if in use, 0 otherwise.
135 static inline int is_umdir_used(char *dir)
137 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
138 char pid[sizeof("nnnnn\0")], *end;
139 int dead, fd, p, n, err;
141 n = snprintf(file, sizeof(file), "%s/pid", dir);
142 if (n >= sizeof(file)) {
143 printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n");
149 fd = open(file, O_RDONLY);
153 printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
154 "file '%s', err = %d\n", file, -fd);
160 n = read(fd, pid, sizeof(pid));
162 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
163 "'%s', err = %d\n", file, errno);
166 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
167 "'%s', 0-byte read\n", file);
171 p = strtoul(pid, &end, 0);
173 printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
174 "'%s', errno = %d\n", file, errno);
178 if ((kill(p, 0) == 0) || (errno != ESRCH)) {
179 printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
191 * Try to remove the directory @dir unless it's in use.
192 * Precondition: @dir exists.
193 * Returns 0 for success, < 0 for failure in removal or if the directory is in
196 static int umdir_take_if_dead(char *dir)
199 if (is_umdir_used(dir))
202 ret = remove_files_and_dir(dir);
204 printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
205 "failed with err = %d\n", ret);
210 static void __init create_pid_file(void)
212 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
213 char pid[sizeof("nnnnn\0")];
216 if (umid_file_name("pid", file, sizeof(file)))
219 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
221 printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
222 "%s\n", file, strerror(errno));
226 snprintf(pid, sizeof(pid), "%d\n", getpid());
227 n = write(fd, pid, strlen(pid));
228 if (n != strlen(pid))
229 printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
235 int __init set_umid(char *name)
237 if (strlen(name) > UMID_LEN - 1)
240 strlcpy(umid, name, sizeof(umid));
245 /* Changed in make_umid, which is called during early boot */
246 static int umid_setup = 0;
248 static int __init make_umid(void)
259 strlcpy(tmp, uml_dir, sizeof(tmp));
260 strlcat(tmp, "XXXXXX", sizeof(tmp));
263 printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
264 "%s\n", tmp, strerror(errno));
271 set_umid(&tmp[strlen(uml_dir)]);
274 * There's a nice tiny little race between this unlink and
275 * the mkdir below. It'd be nice if there were a mkstemp
284 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
285 err = mkdir(tmp, 0777);
291 if (umdir_take_if_dead(tmp) < 0)
294 err = mkdir(tmp, 0777);
298 printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
312 static int __init make_umid_init(void)
318 * If initializing with the given umid failed, then try again with
321 printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
322 "random umid\n", umid);
329 __initcall(make_umid_init);
331 int __init umid_file_name(char *name, char *buf, int len)
339 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
341 printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
353 static int __init set_uml_dir(char *name, int *add)
356 printf("uml_dir can't be an empty string\n");
360 if (name[strlen(name) - 1] == '/') {
365 uml_dir = malloc(strlen(name) + 2);
366 if (uml_dir == NULL) {
367 printf("Failed to malloc uml_dir - error = %d\n", errno);
370 * Return 0 here because do_initcalls doesn't look at
375 sprintf(uml_dir, "%s/", name);
380 __uml_setup("uml_dir=", set_uml_dir,
381 "uml_dir=<directory>\n"
382 " The location to place the pid and umid files.\n\n"
385 static void remove_umid_dir(void)
387 char dir[strlen(uml_dir) + UMID_LEN + 1], err;
389 sprintf(dir, "%s%s", uml_dir, umid);
390 err = remove_files_and_dir(dir);
392 printf("remove_umid_dir - remove_files_and_dir failed with "
396 __uml_exitcall(remove_umid_dir);