10 #include <sys/param.h>
16 #define UML_DIR "~/.uml/"
20 /* Changed by set_umid, which is run early in boot */
21 char umid[UMID_LEN] = { 0 };
23 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
24 static char *uml_dir = UML_DIR;
26 static int __init make_uml_dir(void)
28 char dir[512] = { '\0' };
32 char *home = getenv("HOME");
36 printk("make_uml_dir : no value in environment for "
40 strlcpy(dir, home, sizeof(dir));
43 strlcat(dir, uml_dir, sizeof(dir));
45 if (len > 0 && dir[len - 1] != '/')
46 strlcat(dir, "/", sizeof(dir));
49 uml_dir = malloc(strlen(dir) + 1);
50 if (uml_dir == NULL) {
51 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
56 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
57 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
70 static int actually_do_remove(char *dir)
77 directory = opendir(dir);
81 while((ent = readdir(directory)) != NULL){
82 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
84 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
85 if(len > sizeof(file))
88 sprintf(file, "%s/%s", dir, ent->d_name);
98 /* This says that there isn't already a user of the specified directory even if
99 * there are errors during the checking. This is because if these errors
100 * happen, the directory is unusable by the pre-existing UML, so we might as
101 * well take it over. This could happen either by
102 * the existing UML somehow corrupting its umid directory
103 * something other than UML sticking stuff in the directory
104 * this boot racing with a shutdown of the other UML
105 * In any of these cases, the directory isn't useful for anything else.
108 static int not_dead_yet(char *dir)
110 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
111 char pid[sizeof("nnnnn\0")], *end;
112 int dead, fd, p, n, err;
114 n = snprintf(file, sizeof(file), "%s/pid", dir);
115 if(n >= sizeof(file)){
116 printk("not_dead_yet - pid filename too long\n");
122 fd = open(file, O_RDONLY);
126 printk("not_dead_yet : couldn't open pid file '%s', "
127 "err = %d\n", file, -fd);
133 n = read(fd, pid, sizeof(pid));
135 printk("not_dead_yet : couldn't read pid file '%s', "
136 "err = %d\n", file, errno);
139 printk("not_dead_yet : couldn't read pid file '%s', "
140 "0-byte read\n", file);
144 p = strtoul(pid, &end, 0);
146 printk("not_dead_yet : couldn't parse pid file '%s', "
147 "errno = %d\n", file, errno);
151 if((kill(p, 0) == 0) || (errno != ESRCH)){
152 printk("umid \"%s\" is already in use by pid %d\n", umid, p);
156 err = actually_do_remove(dir);
158 printk("not_dead_yet - actually_do_remove failed with "
169 static void __init create_pid_file(void)
171 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
172 char pid[sizeof("nnnnn\0")];
175 if(umid_file_name("pid", file, sizeof(file)))
178 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
180 printk("Open of machine pid file \"%s\" failed: %s\n",
181 file, strerror(errno));
185 snprintf(pid, sizeof(pid), "%d\n", getpid());
186 n = write(fd, pid, strlen(pid));
188 printk("Write of pid file failed - err = %d\n", errno);
193 int __init set_umid(char *name)
195 if(strlen(name) > UMID_LEN - 1)
198 strlcpy(umid, name, sizeof(umid));
203 static int umid_setup = 0;
205 int __init make_umid(void)
216 strlcpy(tmp, uml_dir, sizeof(tmp));
217 strlcat(tmp, "XXXXXX", sizeof(tmp));
220 printk("make_umid - mkstemp(%s) failed: %s\n",
221 tmp, strerror(errno));
228 set_umid(&tmp[strlen(uml_dir)]);
230 /* There's a nice tiny little race between this unlink and
231 * the mkdir below. It'd be nice if there were a mkstemp
240 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
241 err = mkdir(tmp, 0777);
247 /* 1 -> this umid is already in use
248 * < 0 -> we couldn't remove the umid directory
249 * In either case, we can't use this umid, so return -EEXIST.
251 if(not_dead_yet(tmp) != 0)
254 err = mkdir(tmp, 0777);
258 printk("Failed to create '%s' - err = %d\n", umid, -errno);
271 static int __init make_umid_init(void)
276 /* If initializing with the given umid failed, then try again with
279 printk("Failed to initialize umid \"%s\", trying with a random umid\n",
287 __initcall(make_umid_init);
289 int __init umid_file_name(char *name, char *buf, int len)
297 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
299 printk("umid_file_name : buffer too short\n");
311 static int __init set_uml_dir(char *name, int *add)
314 printf("uml_dir can't be an empty string\n");
318 if(name[strlen(name) - 1] == '/'){
323 uml_dir = malloc(strlen(name) + 2);
325 printf("Failed to malloc uml_dir - error = %d\n", errno);
327 /* Return 0 here because do_initcalls doesn't look at
332 sprintf(uml_dir, "%s/", name);
337 __uml_setup("uml_dir=", set_uml_dir,
338 "uml_dir=<directory>\n"
339 " The location to place the pid and umid files.\n\n"
342 static void remove_umid_dir(void)
344 char dir[strlen(uml_dir) + UMID_LEN + 1], err;
346 sprintf(dir, "%s%s", uml_dir, umid);
347 err = actually_do_remove(dir);
349 printf("remove_umid_dir - actually_do_remove failed with "
353 __uml_exitcall(remove_umid_dir);