10 #include <sys/param.h>
15 #define UML_DIR "~/.uml/"
19 /* Changed by set_umid, which is run early in boot */
20 static char umid
[UMID_LEN
] = { 0 };
22 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
23 static char *uml_dir
= UML_DIR
;
25 static int __init
make_uml_dir(void)
27 char dir
[512] = { '\0' };
31 char *home
= getenv("HOME");
35 printk("make_uml_dir : no value in environment for "
39 strlcpy(dir
, home
, sizeof(dir
));
42 strlcat(dir
, uml_dir
, sizeof(dir
));
44 if (len
> 0 && dir
[len
- 1] != '/')
45 strlcat(dir
, "/", sizeof(dir
));
48 uml_dir
= malloc(strlen(dir
) + 1);
49 if (uml_dir
== NULL
) {
50 printf("make_uml_dir : malloc failed, errno = %d\n", errno
);
55 if((mkdir(uml_dir
, 0777) < 0) && (errno
!= EEXIST
)){
56 printf("Failed to mkdir '%s': %s\n", uml_dir
, strerror(errno
));
70 * Unlinks the files contained in @dir and then removes @dir.
71 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
72 * ignore ENOENT errors for anything (they happen, strangely enough - possibly due
73 * to races between multiple dying UML threads).
75 static int remove_files_and_dir(char *dir
)
83 directory
= opendir(dir
);
84 if (directory
== NULL
) {
91 while ((ent
= readdir(directory
)) != NULL
) {
92 if (!strcmp(ent
->d_name
, ".") || !strcmp(ent
->d_name
, ".."))
94 len
= strlen(dir
) + sizeof("/") + strlen(ent
->d_name
) + 1;
95 if (len
> sizeof(file
)) {
100 sprintf(file
, "%s/%s", dir
, ent
->d_name
);
101 if (unlink(file
) < 0 && errno
!= ENOENT
) {
107 if (rmdir(dir
) < 0 && errno
!= ENOENT
) {
118 /* This says that there isn't already a user of the specified directory even if
119 * there are errors during the checking. This is because if these errors
120 * happen, the directory is unusable by the pre-existing UML, so we might as
121 * well take it over. This could happen either by
122 * the existing UML somehow corrupting its umid directory
123 * something other than UML sticking stuff in the directory
124 * this boot racing with a shutdown of the other UML
125 * In any of these cases, the directory isn't useful for anything else.
127 * Boolean return: 1 if in use, 0 otherwise.
129 static inline int is_umdir_used(char *dir
)
131 char file
[strlen(uml_dir
) + UMID_LEN
+ sizeof("/pid\0")];
132 char pid
[sizeof("nnnnn\0")], *end
;
133 int dead
, fd
, p
, n
, err
;
135 n
= snprintf(file
, sizeof(file
), "%s/pid", dir
);
136 if(n
>= sizeof(file
)){
137 printk("is_umdir_used - pid filename too long\n");
143 fd
= open(file
, O_RDONLY
);
147 printk("is_umdir_used : couldn't open pid file '%s', "
148 "err = %d\n", file
, -fd
);
154 n
= read(fd
, pid
, sizeof(pid
));
156 printk("is_umdir_used : couldn't read pid file '%s', "
157 "err = %d\n", file
, errno
);
160 printk("is_umdir_used : couldn't read pid file '%s', "
161 "0-byte read\n", file
);
165 p
= strtoul(pid
, &end
, 0);
167 printk("is_umdir_used : couldn't parse pid file '%s', "
168 "errno = %d\n", file
, errno
);
172 if((kill(p
, 0) == 0) || (errno
!= ESRCH
)){
173 printk("umid \"%s\" is already in use by pid %d\n", umid
, p
);
184 * Try to remove the directory @dir unless it's in use.
185 * Precondition: @dir exists.
186 * Returns 0 for success, < 0 for failure in removal or if the directory is in
189 static int umdir_take_if_dead(char *dir
)
192 if (is_umdir_used(dir
))
195 ret
= remove_files_and_dir(dir
);
197 printk("is_umdir_used - remove_files_and_dir failed with "
203 static void __init
create_pid_file(void)
205 char file
[strlen(uml_dir
) + UMID_LEN
+ sizeof("/pid\0")];
206 char pid
[sizeof("nnnnn\0")];
209 if(umid_file_name("pid", file
, sizeof(file
)))
212 fd
= open(file
, O_RDWR
| O_CREAT
| O_EXCL
, 0644);
214 printk("Open of machine pid file \"%s\" failed: %s\n",
215 file
, strerror(errno
));
219 snprintf(pid
, sizeof(pid
), "%d\n", getpid());
220 n
= write(fd
, pid
, strlen(pid
));
222 printk("Write of pid file failed - err = %d\n", errno
);
227 int __init
set_umid(char *name
)
229 if(strlen(name
) > UMID_LEN
- 1)
232 strlcpy(umid
, name
, sizeof(umid
));
237 /* Changed in make_umid, which is called during early boot */
238 static int umid_setup
= 0;
240 int __init
make_umid(void)
251 strlcpy(tmp
, uml_dir
, sizeof(tmp
));
252 strlcat(tmp
, "XXXXXX", sizeof(tmp
));
255 printk("make_umid - mkstemp(%s) failed: %s\n",
256 tmp
, strerror(errno
));
263 set_umid(&tmp
[strlen(uml_dir
)]);
265 /* There's a nice tiny little race between this unlink and
266 * the mkdir below. It'd be nice if there were a mkstemp
275 snprintf(tmp
, sizeof(tmp
), "%s%s", uml_dir
, umid
);
276 err
= mkdir(tmp
, 0777);
282 if (umdir_take_if_dead(tmp
) < 0)
285 err
= mkdir(tmp
, 0777);
289 printk("Failed to create '%s' - err = %d\n", umid
, -errno
);
302 static int __init
make_umid_init(void)
307 /* If initializing with the given umid failed, then try again with
310 printk("Failed to initialize umid \"%s\", trying with a random umid\n",
318 __initcall(make_umid_init
);
320 int __init
umid_file_name(char *name
, char *buf
, int len
)
328 n
= snprintf(buf
, len
, "%s%s/%s", uml_dir
, umid
, name
);
330 printk("umid_file_name : buffer too short\n");
342 static int __init
set_uml_dir(char *name
, int *add
)
345 printf("uml_dir can't be an empty string\n");
349 if(name
[strlen(name
) - 1] == '/'){
354 uml_dir
= malloc(strlen(name
) + 2);
356 printf("Failed to malloc uml_dir - error = %d\n", errno
);
358 /* Return 0 here because do_initcalls doesn't look at
363 sprintf(uml_dir
, "%s/", name
);
368 __uml_setup("uml_dir=", set_uml_dir
,
369 "uml_dir=<directory>\n"
370 " The location to place the pid and umid files.\n\n"
373 static void remove_umid_dir(void)
375 char dir
[strlen(uml_dir
) + UMID_LEN
+ 1], err
;
377 sprintf(dir
, "%s%s", uml_dir
, umid
);
378 err
= remove_files_and_dir(dir
);
380 printf("remove_umid_dir - remove_files_and_dir failed with "
384 __uml_exitcall(remove_umid_dir
);