2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
14 #include <sys/param.h>
19 #include "user_util.h"
20 #include "choose-mode.h"
23 #define UML_DIR "~/.uml/"
25 /* Changed by set_umid and make_umid, which are run early in boot */
26 static char umid[UMID_LEN] = { 0 };
28 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
29 static char *uml_dir = UML_DIR;
31 /* Changed by set_umid */
32 static int umid_is_random = 1;
33 static int umid_inited = 0;
34 /* Have we created the files? Should we remove them? */
35 static int umid_owned = 0;
37 static int make_umid(int (*printer)(const char *fmt, ...));
39 static int __init set_umid(char *name, int is_random,
40 int (*printer)(const char *fmt, ...))
43 (*printer)("Unique machine name can't be set twice\n");
47 if(strlen(name) > UMID_LEN - 1)
48 (*printer)("Unique machine name is being truncated to %d "
49 "characters\n", UMID_LEN);
50 strlcpy(umid, name, sizeof(umid));
52 umid_is_random = is_random;
57 static int __init set_umid_arg(char *name, int *add)
60 return(set_umid(name, 0, printf));
63 __uml_setup("umid=", set_umid_arg,
65 " This is used to assign a unique identity to this UML machine and\n"
66 " is used for naming the pid file and management console socket.\n\n"
69 int __init umid_file_name(char *name, char *buf, int len)
73 if(!umid_inited && make_umid(printk)) return(-1);
75 n = strlen(uml_dir) + strlen(umid) + strlen(name) + 1;
77 printk("umid_file_name : buffer too short\n");
81 sprintf(buf, "%s%s/%s", uml_dir, umid, name);
85 extern int tracing_pid;
87 static void __init create_pid_file(void)
89 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
90 char pid[sizeof("nnnnn\0")];
93 if(umid_file_name("pid", file, sizeof(file)))
96 fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))),
99 printf("Open of machine pid file \"%s\" failed: %s\n",
100 file, strerror(-fd));
104 sprintf(pid, "%d\n", os_getpid());
105 n = os_write_file(fd, pid, strlen(pid));
107 printf("Write of pid file failed - err = %d\n", -n);
111 static int actually_do_remove(char *dir)
118 directory = opendir(dir);
119 if(directory == NULL){
120 printk("actually_do_remove : couldn't open directory '%s', "
121 "errno = %d\n", dir, errno);
124 while((ent = readdir(directory)) != NULL){
125 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
127 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
128 if(len > sizeof(file)){
129 printk("Not deleting '%s' from '%s' - name too long\n",
133 sprintf(file, "%s/%s", dir, ent->d_name);
134 if(unlink(file) < 0){
135 printk("actually_do_remove : couldn't remove '%s' "
136 "from '%s', errno = %d\n", ent->d_name, dir,
142 printk("actually_do_remove : couldn't rmdir '%s', "
143 "errno = %d\n", dir, errno);
149 void remove_umid_dir(void)
151 char dir[strlen(uml_dir) + UMID_LEN + 1];
155 sprintf(dir, "%s%s", uml_dir, umid);
156 actually_do_remove(dir);
159 char *get_umid(int only_if_set)
161 if(only_if_set && umid_is_random)
166 static int not_dead_yet(char *dir)
168 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
169 char pid[sizeof("nnnnn\0")], *end;
172 sprintf(file, "%s/pid", dir);
174 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
177 printk("not_dead_yet : couldn't open pid file '%s', "
178 "err = %d\n", file, -fd);
184 n = os_read_file(fd, pid, sizeof(pid));
186 printk("not_dead_yet : couldn't read pid file '%s', "
187 "err = %d\n", file, -n);
190 p = strtoul(pid, &end, 0);
192 printk("not_dead_yet : couldn't parse pid file '%s', "
193 "errno = %d\n", file, errno);
196 if(((kill(p, 0) < 0) && (errno == ESRCH)) ||
197 (p == CHOOSE_MODE(tracing_pid, os_getpid())))
202 return(actually_do_remove(dir));
205 static int __init set_uml_dir(char *name, int *add)
207 if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){
208 uml_dir = malloc(strlen(name) + 2);
210 printf("Failed to malloc uml_dir - error = %d\n",
213 /* Return 0 here because do_initcalls doesn't look at
218 sprintf(uml_dir, "%s/", name);
224 static int __init make_uml_dir(void)
226 char dir[MAXPATHLEN + 1] = { '\0' };
230 char *home = getenv("HOME");
233 printf("make_uml_dir : no value in environment for "
237 strlcpy(dir, home, sizeof(dir));
240 strlcat(dir, uml_dir, sizeof(dir));
242 if (len > 0 && dir[len - 1] != '/')
243 strlcat(dir, "/", sizeof(dir));
245 uml_dir = malloc(strlen(dir) + 1);
246 if (uml_dir == NULL) {
247 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
250 strcpy(uml_dir, dir);
252 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
253 printf("Failed to mkdir %s: %s\n", uml_dir, strerror(errno));
259 static int __init make_umid(int (*printer)(const char *fmt, ...))
262 char tmp[strlen(uml_dir) + UMID_LEN + 1];
264 strlcpy(tmp, uml_dir, sizeof(tmp));
267 strcat(tmp, "XXXXXX");
270 (*printer)("make_umid - mkstemp(%s) failed: %s\n",
271 tmp,strerror(errno));
276 /* There's a nice tiny little race between this unlink and
277 * the mkdir below. It'd be nice if there were a mkstemp
281 set_umid(&tmp[strlen(uml_dir)], 1, printer);
284 sprintf(tmp, "%s%s", uml_dir, umid);
286 err = mkdir(tmp, 0777);
289 if(not_dead_yet(tmp)){
290 (*printer)("umid '%s' is in use\n", umid);
294 err = mkdir(tmp, 0777);
298 (*printer)("Failed to create %s - errno = %d\n", umid, errno);
306 __uml_setup("uml_dir=", set_uml_dir,
307 "uml_dir=<directory>\n"
308 " The location to place the pid and umid files.\n\n"
311 static int __init make_umid_setup(void)
313 /* one function with the ordering we need ... */
319 __uml_postsetup(make_umid_setup);
322 * Overrides for Emacs so that we follow Linus's tabbing style.
323 * Emacs will notice this stuff at the end of the file and automatically
324 * adjust the settings for this buffer only. This must remain at the end
326 * ---------------------------------------------------------------------------
328 * c-file-style: "linux"