2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
18 #define UML_DIR "~/.uml/"
22 /* Changed by set_umid, which is run early in boot */
23 static char umid
[UMID_LEN
] = { 0 };
25 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
26 static char *uml_dir
= UML_DIR
;
28 static int __init
make_uml_dir(void)
30 char dir
[512] = { '\0' };
33 if (*uml_dir
== '~') {
34 char *home
= getenv("HOME");
38 printk(UM_KERN_ERR
"make_uml_dir : no value in "
39 "environment for $HOME\n");
42 strlcpy(dir
, home
, sizeof(dir
));
45 strlcat(dir
, uml_dir
, sizeof(dir
));
47 if (len
> 0 && dir
[len
- 1] != '/')
48 strlcat(dir
, "/", sizeof(dir
));
51 uml_dir
= malloc(strlen(dir
) + 1);
52 if (uml_dir
== NULL
) {
53 printf("make_uml_dir : malloc failed, errno = %d\n", errno
);
58 if ((mkdir(uml_dir
, 0777) < 0) && (errno
!= EEXIST
)) {
59 printf("Failed to mkdir '%s': %s\n", uml_dir
, strerror(errno
));
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
75 * ignore ENOENT errors for anything (they happen, strangely enough - possibly
76 * due to races between multiple dying UML threads).
78 static int remove_files_and_dir(char *dir
)
86 directory
= opendir(dir
);
87 if (directory
== NULL
) {
94 while ((ent
= readdir(directory
)) != NULL
) {
95 if (!strcmp(ent
->d_name
, ".") || !strcmp(ent
->d_name
, ".."))
97 len
= strlen(dir
) + sizeof("/") + strlen(ent
->d_name
) + 1;
98 if (len
> sizeof(file
)) {
103 sprintf(file
, "%s/%s", dir
, ent
->d_name
);
104 if (unlink(file
) < 0 && errno
!= ENOENT
) {
110 if (rmdir(dir
) < 0 && errno
!= ENOENT
) {
122 * This says that there isn't already a user of the specified directory even if
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.
131 * Boolean return: 1 if in use, 0 otherwise.
133 static inline int is_umdir_used(char *dir
)
135 char file
[strlen(uml_dir
) + UMID_LEN
+ sizeof("/pid\0")];
136 char pid
[sizeof("nnnnn\0")], *end
;
137 int dead
, fd
, p
, n
, err
;
139 n
= snprintf(file
, sizeof(file
), "%s/pid", dir
);
140 if (n
>= sizeof(file
)) {
141 printk(UM_KERN_ERR
"is_umdir_used - pid filename too long\n");
147 fd
= open(file
, O_RDONLY
);
151 printk(UM_KERN_ERR
"is_umdir_used : couldn't open pid "
152 "file '%s', err = %d\n", file
, -fd
);
158 n
= read(fd
, pid
, sizeof(pid
));
160 printk(UM_KERN_ERR
"is_umdir_used : couldn't read pid file "
161 "'%s', err = %d\n", file
, errno
);
164 printk(UM_KERN_ERR
"is_umdir_used : couldn't read pid file "
165 "'%s', 0-byte read\n", file
);
169 p
= strtoul(pid
, &end
, 0);
171 printk(UM_KERN_ERR
"is_umdir_used : couldn't parse pid file "
172 "'%s', errno = %d\n", file
, errno
);
176 if ((kill(p
, 0) == 0) || (errno
!= ESRCH
)) {
177 printk(UM_KERN_ERR
"umid \"%s\" is already in use by pid %d\n",
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
194 static int umdir_take_if_dead(char *dir
)
197 if (is_umdir_used(dir
))
200 ret
= remove_files_and_dir(dir
);
202 printk(UM_KERN_ERR
"is_umdir_used - remove_files_and_dir "
203 "failed with err = %d\n", ret
);
208 static void __init
create_pid_file(void)
210 char file
[strlen(uml_dir
) + UMID_LEN
+ sizeof("/pid\0")];
211 char pid
[sizeof("nnnnn\0")];
214 if (umid_file_name("pid", file
, sizeof(file
)))
217 fd
= open(file
, O_RDWR
| O_CREAT
| O_EXCL
, 0644);
219 printk(UM_KERN_ERR
"Open of machine pid file \"%s\" failed: "
220 "%s\n", file
, strerror(errno
));
224 snprintf(pid
, sizeof(pid
), "%d\n", getpid());
225 n
= write(fd
, pid
, strlen(pid
));
226 if (n
!= strlen(pid
))
227 printk(UM_KERN_ERR
"Write of pid file failed - err = %d\n",
233 int __init
set_umid(char *name
)
235 if (strlen(name
) > UMID_LEN
- 1)
238 strlcpy(umid
, name
, sizeof(umid
));
243 /* Changed in make_umid, which is called during early boot */
244 static int umid_setup
= 0;
246 static int __init
make_umid(void)
257 strlcpy(tmp
, uml_dir
, sizeof(tmp
));
258 strlcat(tmp
, "XXXXXX", sizeof(tmp
));
261 printk(UM_KERN_ERR
"make_umid - mkstemp(%s) failed: "
262 "%s\n", tmp
, strerror(errno
));
269 set_umid(&tmp
[strlen(uml_dir
)]);
272 * There's a nice tiny little race between this unlink and
273 * the mkdir below. It'd be nice if there were a mkstemp
282 snprintf(tmp
, sizeof(tmp
), "%s%s", uml_dir
, umid
);
283 err
= mkdir(tmp
, 0777);
289 if (umdir_take_if_dead(tmp
) < 0)
292 err
= mkdir(tmp
, 0777);
296 printk(UM_KERN_ERR
"Failed to create '%s' - err = %d\n", umid
,
310 static int __init
make_umid_init(void)
316 * If initializing with the given umid failed, then try again with
319 printk(UM_KERN_ERR
"Failed to initialize umid \"%s\", trying with a "
320 "random umid\n", umid
);
327 __initcall(make_umid_init
);
329 int __init
umid_file_name(char *name
, char *buf
, int len
)
337 n
= snprintf(buf
, len
, "%s%s/%s", uml_dir
, umid
, name
);
339 printk(UM_KERN_ERR
"umid_file_name : buffer too short\n");
351 static int __init
set_uml_dir(char *name
, int *add
)
354 printf("uml_dir can't be an empty string\n");
358 if (name
[strlen(name
) - 1] == '/') {
363 uml_dir
= malloc(strlen(name
) + 2);
364 if (uml_dir
== NULL
) {
365 printf("Failed to malloc uml_dir - error = %d\n", errno
);
368 * Return 0 here because do_initcalls doesn't look at
373 sprintf(uml_dir
, "%s/", name
);
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"
383 static void remove_umid_dir(void)
385 char dir
[strlen(uml_dir
) + UMID_LEN
+ 1], err
;
387 sprintf(dir
, "%s%s", uml_dir
, umid
);
388 err
= remove_files_and_dir(dir
);
390 printf("remove_umid_dir - remove_files_and_dir failed with "
394 __uml_exitcall(remove_umid_dir
);