15 #define UML_DIR "~/.uml/"
19 /* Changed by set_umid, which is run early in boot */
20 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");
34 printf("make_uml_dir : no value in environment for "
38 strlcpy(dir, home, sizeof(dir));
41 strlcat(dir, uml_dir, sizeof(dir));
43 if (len > 0 && dir[len - 1] != '/')
44 strlcat(dir, "/", sizeof(dir));
46 uml_dir = malloc(strlen(dir) + 1);
47 if (uml_dir == NULL) {
48 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
53 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
54 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
60 static int actually_do_remove(char *dir)
67 directory = opendir(dir);
68 if(directory == NULL){
69 printk("actually_do_remove : couldn't open directory '%s', "
70 "errno = %d\n", dir, errno);
73 while((ent = readdir(directory)) != NULL){
74 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
76 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
77 if(len > sizeof(file)){
78 printk("Not deleting '%s' from '%s' - name too long\n",
82 sprintf(file, "%s/%s", dir, ent->d_name);
84 printk("actually_do_remove : couldn't remove '%s' "
85 "from '%s', errno = %d\n", ent->d_name, dir,
91 printk("actually_do_remove : couldn't rmdir '%s', "
92 "errno = %d\n", dir, errno);
98 extern int tracing_pid;
100 static int not_dead_yet(char *dir)
102 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
103 char pid[sizeof("nnnnn\0")], *end;
106 sprintf(file, "%s/pid", dir);
108 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
111 printk("not_dead_yet : couldn't open pid file '%s', "
112 "err = %d\n", file, -fd);
118 n = os_read_file(fd, pid, sizeof(pid));
120 printk("not_dead_yet : couldn't read pid file '%s', "
121 "err = %d\n", file, -n);
124 p = strtoul(pid, &end, 0);
126 printk("not_dead_yet : couldn't parse pid file '%s', "
127 "errno = %d\n", file, errno);
130 if(((kill(p, 0) < 0) && (errno == ESRCH)) ||
131 (p == CHOOSE_MODE(tracing_pid, os_getpid())))
136 return(actually_do_remove(dir));
139 static void __init create_pid_file(void)
141 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
142 char pid[sizeof("nnnnn\0")];
145 if(umid_file_name("pid", file, sizeof(file)))
148 fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))),
151 printf("Open of machine pid file \"%s\" failed: %s\n",
152 file, strerror(-fd));
156 sprintf(pid, "%d\n", os_getpid());
157 n = os_write_file(fd, pid, strlen(pid));
159 printf("Write of pid file failed - err = %d\n", -n);
163 int __init set_umid(char *name, int (*printer)(const char *fmt, ...))
165 if(strlen(name) > UMID_LEN - 1)
166 (*printer)("Unique machine name is being truncated to %d "
167 "characters\n", UMID_LEN);
168 strlcpy(umid, name, sizeof(umid));
173 static int umid_setup = 0;
175 int __init make_umid(int (*printer)(const char *fmt, ...))
183 strlcpy(tmp, uml_dir, sizeof(tmp));
184 strcat(tmp, "XXXXXX");
187 (*printer)("make_umid - mkstemp(%s) failed: %s\n",
188 tmp,strerror(errno));
193 /* There's a nice tiny little race between this unlink and
194 * the mkdir below. It'd be nice if there were a mkstemp
198 set_umid(&tmp[strlen(uml_dir)], printer);
201 sprintf(tmp, "%s%s", uml_dir, umid);
202 err = mkdir(tmp, 0777);
205 if(not_dead_yet(tmp))
207 err = mkdir(tmp, 0777);
211 (*printer)("Failed to create %s - errno = %d\n", umid, errno);
222 static int __init make_umid_init(void)
229 __initcall(make_umid_init);
231 int __init umid_file_name(char *name, char *buf, int len)
236 err = make_umid(printk);
241 n = strlen(uml_dir) + strlen(umid) + strlen("/") + strlen(name) + 1;
243 printk("umid_file_name : buffer too short\n");
247 sprintf(buf, "%s%s/%s", uml_dir, umid, name);
251 extern int umid_is_random;
253 char *get_umid(int only_if_set)
255 if(only_if_set && umid_is_random)
260 static int __init set_uml_dir(char *name, int *add)
262 if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){
263 uml_dir = malloc(strlen(name) + 2);
265 printf("Failed to malloc uml_dir - error = %d\n",
268 /* Return 0 here because do_initcalls doesn't look at
273 sprintf(uml_dir, "%s/", name);
279 __uml_setup("uml_dir=", set_uml_dir,
280 "uml_dir=<directory>\n"
281 " The location to place the pid and umid files.\n\n"
284 static void remove_umid_dir(void)
286 char dir[strlen(uml_dir) + UMID_LEN + 1];
288 sprintf(dir, "%s%s", uml_dir, umid);
289 actually_do_remove(dir);
292 __uml_exitcall(remove_umid_dir);