ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / um / kernel / umid.c
1 /* 
2  * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com)
3  * Licensed under the GPL
4  */
5
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <errno.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <dirent.h>
13 #include <signal.h>
14 #include <sys/stat.h>
15 #include <sys/param.h>
16 #include "user.h"
17 #include "umid.h"
18 #include "init.h"
19 #include "os.h"
20 #include "user_util.h"
21 #include "choose-mode.h"
22
23 #define UMID_LEN 64
24 #define UML_DIR "~/.uml/"
25
26 /* Changed by set_umid and make_umid, which are run early in boot */
27 static char umid[UMID_LEN] = { 0 };
28
29 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
30 static char *uml_dir = UML_DIR;
31
32 /* Changed by set_umid */
33 static int umid_is_random = 1;
34 static int umid_inited = 0;
35
36 static int make_umid(void);
37
38 static int __init set_umid(char *name, int is_random)
39 {
40         if(umid_inited){
41                 printk("Unique machine name can't be set twice\n");
42                 return(-1);
43         }
44
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));
49
50         umid_is_random = is_random;
51         umid_inited = 1;
52         return 0;
53 }
54
55 static int __init set_umid_arg(char *name, int *add)
56 {
57         return(set_umid(name, 0));
58 }
59
60 __uml_setup("umid=", set_umid_arg,
61 "umid=<name>\n"
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"
64 );
65
66 int __init umid_file_name(char *name, char *buf, int len)
67 {
68         int n;
69
70         if(!umid_inited && make_umid()) return(-1);
71
72         n = strlen(uml_dir) + strlen(umid) + strlen(name) + 1;
73         if(n > len){
74                 printk("umid_file_name : buffer too short\n");
75                 return(-1);
76         }
77
78         sprintf(buf, "%s%s/%s", uml_dir, umid, name);
79         return(0);
80 }
81
82 extern int tracing_pid;
83
84 static int __init create_pid_file(void)
85 {
86         char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
87         char pid[sizeof("nnnnn\0")];
88         int fd;
89
90         if(umid_file_name("pid", file, sizeof(file))) return 0;
91
92         fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))), 
93                           0644);
94         if(fd < 0){
95                 printk("Open of machine pid file \"%s\" failed - "
96                        "errno = %d\n", file, -fd);
97                 return 0;
98         }
99
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);
103         close(fd);
104         return 0;
105 }
106
107 static int actually_do_remove(char *dir)
108 {
109         DIR *directory;
110         struct dirent *ent;
111         int len;
112         char file[256];
113
114         if((directory = opendir(dir)) == NULL){
115                 printk("actually_do_remove : couldn't open directory '%s', "
116                        "errno = %d\n", dir, errno);
117                 return(1);
118         }
119         while((ent = readdir(directory)) != NULL){
120                 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
121                         continue;
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",
125                                ent->d_name, dir);
126                         continue;
127                 }
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, 
132                                errno);
133                         return(1);
134                 }
135         }
136         if(rmdir(dir) < 0){
137                 printk("actually_do_remove : couldn't rmdir '%s', "
138                        "errno = %d\n", dir, errno);
139                 return(1);
140         }
141         return(0);
142 }
143
144 void remove_umid_dir(void)
145 {
146         char dir[strlen(uml_dir) + UMID_LEN + 1];
147         if(!umid_inited) return;
148
149         sprintf(dir, "%s%s", uml_dir, umid);
150         actually_do_remove(dir);
151 }
152
153 char *get_umid(int only_if_set)
154 {
155         if(only_if_set && umid_is_random) return(NULL);
156         return(umid);
157 }
158
159 int not_dead_yet(char *dir)
160 {
161         char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
162         char pid[sizeof("nnnnn\0")], *end;
163         int dead, fd, p;
164
165         sprintf(file, "%s/pid", dir);
166         dead = 0;
167         if((fd = os_open_file(file, of_read(OPENFLAGS()), 0)) < 0){
168                 if(fd != -ENOENT){
169                         printk("not_dead_yet : couldn't open pid file '%s', "
170                                "errno = %d\n", file, -fd);
171                         return(1);
172                 }
173                 dead = 1;
174         }
175         if(fd > 0){
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);
179                         return(1);
180                 }
181                 p = strtoul(pid, &end, 0);
182                 if(end == pid){
183                         printk("not_dead_yet : couldn't parse pid file '%s', "
184                                "errno = %d\n", file, errno);
185                         dead = 1;
186                 }
187                 if(((kill(p, 0) < 0) && (errno == ESRCH)) ||
188                    (p == CHOOSE_MODE(tracing_pid, os_getpid())))
189                         dead = 1;
190         }
191         if(!dead) return(1);
192         return(actually_do_remove(dir));
193 }
194
195 static int __init set_uml_dir(char *name, int *add)
196 {
197         if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){
198                 uml_dir = malloc(strlen(name) + 1);
199                 if(uml_dir == NULL){
200                         printk("Failed to malloc uml_dir - error = %d\n",
201                                errno);
202                         uml_dir = name;
203                         return(0);
204                 }
205                 sprintf(uml_dir, "%s/", name);
206         }
207         else uml_dir = name;
208         return 0;
209 }
210
211 static int __init make_uml_dir(void)
212 {
213         char dir[MAXPATHLEN + 1] = { '\0' };
214         int len;
215
216         if(*uml_dir == '~'){
217                 char *home = getenv("HOME");
218
219                 if(home == NULL){
220                         printk("make_uml_dir : no value in environment for "
221                                "$HOME\n");
222                         exit(1);
223                 }
224                 strlcpy(dir, home, sizeof(dir));
225                 uml_dir++;
226         }
227         len = strlen(dir);
228         strncat(dir, uml_dir, sizeof(dir) - len);
229         len = strlen(dir);
230         if((len > 0) && (len < sizeof(dir) - 1) && (dir[len - 1] != '/')){
231                 dir[len] = '/';
232                 dir[len + 1] = '\0';
233         }
234
235         if((uml_dir = malloc(strlen(dir) + 1)) == NULL){
236                 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
237                 exit(1);
238         }
239         strcpy(uml_dir, dir);
240         
241         if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
242                 printk("Failed to mkdir %s - errno = %i\n", uml_dir, errno);
243                 return(-1);
244         }
245         return 0;
246 }
247
248 static int __init make_umid(void)
249 {
250         int fd, err;
251         char tmp[strlen(uml_dir) + UMID_LEN + 1];
252
253         strlcpy(tmp, uml_dir, sizeof(tmp));
254
255         if(*umid == 0){
256                 strcat(tmp, "XXXXXX");
257                 fd = mkstemp(tmp);
258                 if(fd < 0){
259                         printk("make_umid - mkstemp failed, errno = %d\n",
260                                errno);
261                         return(1);
262                 }
263
264                 close(fd);
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
267                  * for directories.
268                  */
269                 unlink(tmp);
270                 set_umid(&tmp[strlen(uml_dir)], 1);
271         }
272         
273         sprintf(tmp, "%s%s", uml_dir, umid);
274
275         if((err = mkdir(tmp, 0777)) < 0){
276                 if(errno == EEXIST){
277                         if(not_dead_yet(tmp)){
278                                 printk("umid '%s' is in use\n", umid);
279                                 return(-1);
280                         }
281                         err = mkdir(tmp, 0777);
282                 }
283         }
284         if(err < 0){
285                 printk("Failed to create %s - errno = %d\n", umid, errno);
286                 return(-1);
287         }
288
289         return(0);
290 }
291
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"
295 );
296
297 __uml_postsetup(make_uml_dir);
298 __uml_postsetup(make_umid);
299 __uml_postsetup(create_pid_file);
300
301 /*
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
305  * of the file.
306  * ---------------------------------------------------------------------------
307  * Local variables:
308  * c-file-style: "linux"
309  * End:
310  */