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;
35 static int make_umid(int (*printer
)(const char *fmt
, ...));
37 static int __init
set_umid(char *name
, int is_random
,
38 int (*printer
)(const char *fmt
, ...))
41 (*printer
)("Unique machine name can't be set twice\n");
45 if(strlen(name
) > UMID_LEN
- 1)
46 (*printer
)("Unique machine name is being truncated to %d "
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
)
58 return(set_umid(name
, 0, printf
));
61 __uml_setup("umid=", set_umid_arg
,
63 " This is used to assign a unique identity to this UML machine and\n"
64 " is used for naming the pid file and management console socket.\n\n"
67 int __init
umid_file_name(char *name
, char *buf
, int len
)
71 if(!umid_inited
&& make_umid(printk
)) return(-1);
73 n
= strlen(uml_dir
) + strlen(umid
) + strlen(name
) + 1;
75 printk("umid_file_name : buffer too short\n");
79 sprintf(buf
, "%s%s/%s", uml_dir
, umid
, name
);
83 extern int tracing_pid
;
85 static int __init
create_pid_file(void)
87 char file
[strlen(uml_dir
) + UMID_LEN
+ sizeof("/pid\0")];
88 char pid
[sizeof("nnnnn\0")];
91 if(umid_file_name("pid", file
, sizeof(file
))) return 0;
93 fd
= os_open_file(file
, of_create(of_excl(of_rdwr(OPENFLAGS()))),
96 printf("Open of machine pid file \"%s\" failed: %s\n",
101 sprintf(pid
, "%d\n", os_getpid());
102 n
= os_write_file(fd
, pid
, strlen(pid
));
104 printf("Write of pid file failed - err = %d\n", -n
);
109 static int actually_do_remove(char *dir
)
116 directory
= opendir(dir
);
117 if(directory
== NULL
){
118 printk("actually_do_remove : couldn't open directory '%s', "
119 "errno = %d\n", dir
, errno
);
122 while((ent
= readdir(directory
)) != NULL
){
123 if(!strcmp(ent
->d_name
, ".") || !strcmp(ent
->d_name
, ".."))
125 len
= strlen(dir
) + sizeof("/") + strlen(ent
->d_name
) + 1;
126 if(len
> sizeof(file
)){
127 printk("Not deleting '%s' from '%s' - name too long\n",
131 sprintf(file
, "%s/%s", dir
, ent
->d_name
);
132 if(unlink(file
) < 0){
133 printk("actually_do_remove : couldn't remove '%s' "
134 "from '%s', errno = %d\n", ent
->d_name
, dir
,
140 printk("actually_do_remove : couldn't rmdir '%s', "
141 "errno = %d\n", dir
, errno
);
147 void remove_umid_dir(void)
149 char dir
[strlen(uml_dir
) + UMID_LEN
+ 1];
150 if(!umid_inited
) return;
152 sprintf(dir
, "%s%s", uml_dir
, umid
);
153 actually_do_remove(dir
);
156 char *get_umid(int only_if_set
)
158 if(only_if_set
&& umid_is_random
) return(NULL
);
162 int not_dead_yet(char *dir
)
164 char file
[strlen(uml_dir
) + UMID_LEN
+ sizeof("/pid\0")];
165 char pid
[sizeof("nnnnn\0")], *end
;
168 sprintf(file
, "%s/pid", dir
);
170 fd
= os_open_file(file
, of_read(OPENFLAGS()), 0);
173 printk("not_dead_yet : couldn't open pid file '%s', "
174 "err = %d\n", file
, -fd
);
180 n
= os_read_file(fd
, pid
, sizeof(pid
));
182 printk("not_dead_yet : couldn't read pid file '%s', "
183 "err = %d\n", file
, -n
);
186 p
= strtoul(pid
, &end
, 0);
188 printk("not_dead_yet : couldn't parse pid file '%s', "
189 "errno = %d\n", file
, errno
);
192 if(((kill(p
, 0) < 0) && (errno
== ESRCH
)) ||
193 (p
== CHOOSE_MODE(tracing_pid
, os_getpid())))
197 return(actually_do_remove(dir
));
200 static int __init
set_uml_dir(char *name
, int *add
)
202 if((strlen(name
) > 0) && (name
[strlen(name
) - 1] != '/')){
203 uml_dir
= malloc(strlen(name
) + 2);
205 printf("Failed to malloc uml_dir - error = %d\n",
208 /* Return 0 here because do_initcalls doesn't look at
213 sprintf(uml_dir
, "%s/", name
);
219 static int __init
make_uml_dir(void)
221 char dir
[MAXPATHLEN
+ 1] = { '\0' };
225 char *home
= getenv("HOME");
228 printf("make_uml_dir : no value in environment for "
232 strlcpy(dir
, home
, sizeof(dir
));
236 strncat(dir
, uml_dir
, sizeof(dir
) - len
);
238 if((len
> 0) && (len
< sizeof(dir
) - 1) && (dir
[len
- 1] != '/')){
243 uml_dir
= malloc(strlen(dir
) + 1);
245 printf("make_uml_dir : malloc failed, errno = %d\n", errno
);
248 strcpy(uml_dir
, dir
);
250 if((mkdir(uml_dir
, 0777) < 0) && (errno
!= EEXIST
)){
251 printf("Failed to mkdir %s: %s\n", uml_dir
, strerror(errno
));
257 static int __init
make_umid(int (*printer
)(const char *fmt
, ...))
260 char tmp
[strlen(uml_dir
) + UMID_LEN
+ 1];
262 strlcpy(tmp
, uml_dir
, sizeof(tmp
));
265 strcat(tmp
, "XXXXXX");
268 (*printer
)("make_umid - mkstemp(%s) failed: %s\n",
269 tmp
,strerror(errno
));
274 /* There's a nice tiny little race between this unlink and
275 * the mkdir below. It'd be nice if there were a mkstemp
279 set_umid(&tmp
[strlen(uml_dir
)], 1, printer
);
282 sprintf(tmp
, "%s%s", uml_dir
, umid
);
284 err
= mkdir(tmp
, 0777);
287 if(not_dead_yet(tmp
)){
288 (*printer
)("umid '%s' is in use\n", umid
);
291 err
= mkdir(tmp
, 0777);
295 (*printer
)("Failed to create %s - errno = %d\n", umid
, errno
);
302 __uml_setup("uml_dir=", set_uml_dir
,
303 "uml_dir=<directory>\n"
304 " The location to place the pid and umid files.\n\n"
307 static int __init
make_umid_setup(void)
309 /* one function with the ordering we need ... */
312 return create_pid_file();
314 __uml_postsetup(make_umid_setup
);
317 * Overrides for Emacs so that we follow Linus's tabbing style.
318 * Emacs will notice this stuff at the end of the file and automatically
319 * adjust the settings for this buffer only. This must remain at the end
321 * ---------------------------------------------------------------------------
323 * c-file-style: "linux"