18 #define UML_DIR "~/.uml/"
28 static int __init make_uml_dir(
void)
30 char dir[512] = {
'\0' };
33 if (*uml_dir ==
'~') {
34 char *home = getenv(
"HOME");
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] !=
'/')
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)) {
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) {
133 static inline int is_umdir_used(
char *dir)
136 char pid[
sizeof(
"nnnnn\0")], *
end;
139 n =
snprintf(file,
sizeof(file),
"%s/pid", dir);
140 if (n >=
sizeof(file)) {
152 "file '%s', err = %d\n", file, -fd);
161 "'%s', err = %d\n", file, errno);
165 "'%s', 0-byte read\n", file);
169 p = strtoul(
pid, &
end, 0);
172 "'%s', errno = %d\n", file, errno);
176 if ((kill(p, 0) == 0) || (errno !=
ESRCH)) {
194 static int umdir_take_if_dead(
char *dir)
197 if (is_umdir_used(dir))
200 ret = remove_files_and_dir(dir);
203 "failed with err = %d\n", ret);
208 static void __init create_pid_file(
void)
211 char pid[
sizeof(
"nnnnn\0")];
238 strlcpy(umid, name,
sizeof(umid));
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));
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);
310 static int __init make_umid_init(
void)
320 "random umid\n", umid);
337 n =
snprintf(buf, len,
"%s%s/%s", uml_dir, umid, name);
354 printf(
"uml_dir can't be an empty string\n");
358 if (name[
strlen(name) - 1] ==
'/') {
364 if (uml_dir ==
NULL) {
365 printf(
"Failed to malloc uml_dir - error = %d\n", errno);
379 "uml_dir=<directory>\n"
380 " The location to place the pid and umid files.\n\n"
383 static void remove_umid_dir(
void)
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 "