2 * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3 * Licensed under the GPL
15 #include <sys/param.h>
20 #include "user_util.h"
21 #include "choose-mode.h"
24 #define UML_DIR "~/.uml/"
26 /* Changed by set_umid and make_umid, which are run early in boot */
27 static char umid[UMID_LEN] = { 0 };
29 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
30 static char *uml_dir = UML_DIR;
32 /* Changed by set_umid */
33 static int umid_is_random = 1;
34 static int umid_inited = 0;
36 static int make_umid(void);
38 static int __init set_umid(char *name, int is_random)
41 printk("Unique machine name can't be set twice\n");
45 if(strlen(name) > UMID_LEN - 1)
46 printk("Unique machine name is being truncated to %s "
47 "characters\n", UMID_LEN);
48 strlcpy(umid, name, sizeof(umid));
50 umid_is_random = is_random;
55 static int __init set_umid_arg(char *name, int *add)
57 return(set_umid(name, 0));
60 __uml_setup("umid=", set_umid_arg,
62 " This is used to assign a unique identity to this UML machine and\n"
63 " is used for naming the pid file and management console socket.\n\n"
66 int __init umid_file_name(char *name, char *buf, int len)
70 if(!umid_inited && make_umid()) return(-1);
72 n = strlen(uml_dir) + strlen(umid) + strlen(name) + 1;
74 printk("umid_file_name : buffer too short\n");
78 sprintf(buf, "%s%s/%s", uml_dir, umid, name);
82 extern int tracing_pid;
84 static int __init create_pid_file(void)
86 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
87 char pid[sizeof("nnnnn\0")];
90 if(umid_file_name("pid", file, sizeof(file))) return 0;
92 fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))),
95 printk("Open of machine pid file \"%s\" failed - "
96 "errno = %d\n", file, -fd);
100 sprintf(pid, "%d\n", os_getpid());
101 if(write(fd, pid, strlen(pid)) != strlen(pid))
102 printk("Write of pid file failed - errno = %d\n", errno);
107 static int actually_do_remove(char *dir)
114 if((directory = opendir(dir)) == NULL){
115 printk("actually_do_remove : couldn't open directory '%s', "
116 "errno = %d\n", dir, errno);
119 while((ent = readdir(directory)) != NULL){
120 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
122 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
123 if(len > sizeof(file)){
124 printk("Not deleting '%s' from '%s' - name too long\n",
128 sprintf(file, "%s/%s", dir, ent->d_name);
129 if(unlink(file) < 0){
130 printk("actually_do_remove : couldn't remove '%s' "
131 "from '%s', errno = %d\n", ent->d_name, dir,
137 printk("actually_do_remove : couldn't rmdir '%s', "
138 "errno = %d\n", dir, errno);
144 void remove_umid_dir(void)
146 char dir[strlen(uml_dir) + UMID_LEN + 1];
147 if(!umid_inited) return;
149 sprintf(dir, "%s%s", uml_dir, umid);
150 actually_do_remove(dir);
153 char *get_umid(int only_if_set)
155 if(only_if_set && umid_is_random) return(NULL);
159 int not_dead_yet(char *dir)
161 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
162 char pid[sizeof("nnnnn\0")], *end;
165 sprintf(file, "%s/pid", dir);
167 if((fd = os_open_file(file, of_read(OPENFLAGS()), 0)) < 0){
169 printk("not_dead_yet : couldn't open pid file '%s', "
170 "errno = %d\n", file, -fd);
176 if(read(fd, pid, sizeof(pid)) < 0){
177 printk("not_dead_yet : couldn't read pid file '%s', "
178 "errno = %d\n", file, errno);
181 p = strtoul(pid, &end, 0);
183 printk("not_dead_yet : couldn't parse pid file '%s', "
184 "errno = %d\n", file, errno);
187 if(((kill(p, 0) < 0) && (errno == ESRCH)) ||
188 (p == CHOOSE_MODE(tracing_pid, os_getpid())))
192 return(actually_do_remove(dir));
195 static int __init set_uml_dir(char *name, int *add)
197 if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){
198 uml_dir = malloc(strlen(name) + 1);
200 printk("Failed to malloc uml_dir - error = %d\n",
205 sprintf(uml_dir, "%s/", name);
211 static int __init make_uml_dir(void)
213 char dir[MAXPATHLEN + 1] = { '\0' };
217 char *home = getenv("HOME");
220 printk("make_uml_dir : no value in environment for "
224 strlcpy(dir, home, sizeof(dir));
228 strncat(dir, uml_dir, sizeof(dir) - len);
230 if((len > 0) && (len < sizeof(dir) - 1) && (dir[len - 1] != '/')){
235 if((uml_dir = malloc(strlen(dir) + 1)) == NULL){
236 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
239 strcpy(uml_dir, dir);
241 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
242 printk("Failed to mkdir %s - errno = %i\n", uml_dir, errno);
248 static int __init make_umid(void)
251 char tmp[strlen(uml_dir) + UMID_LEN + 1];
253 strlcpy(tmp, uml_dir, sizeof(tmp));
256 strcat(tmp, "XXXXXX");
259 printk("make_umid - mkstemp failed, errno = %d\n",
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
270 set_umid(&tmp[strlen(uml_dir)], 1);
273 sprintf(tmp, "%s%s", uml_dir, umid);
275 if((err = mkdir(tmp, 0777)) < 0){
277 if(not_dead_yet(tmp)){
278 printk("umid '%s' is in use\n", umid);
281 err = mkdir(tmp, 0777);
285 printk("Failed to create %s - errno = %d\n", umid, errno);
292 __uml_setup("uml_dir=", set_uml_dir,
293 "uml_dir=<directory>\n"
294 " The location to place the pid and umid files.\n\n"
297 __uml_postsetup(make_uml_dir);
298 __uml_postsetup(make_umid);
299 __uml_postsetup(create_pid_file);
302 * Overrides for Emacs so that we follow Linus's tabbing style.
303 * Emacs will notice this stuff at the end of the file and automatically
304 * adjust the settings for this buffer only. This must remain at the end
306 * ---------------------------------------------------------------------------
308 * c-file-style: "linux"