blob: 251d277fff2a39fa19a5157a8cae713e182e9982 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 <errno.h>
9#include <string.h>
10#include <stdlib.h>
11#include <dirent.h>
12#include <signal.h>
13#include <sys/stat.h>
14#include <sys/param.h>
15#include "user.h"
16#include "umid.h"
17#include "init.h"
18#include "os.h"
19#include "user_util.h"
20#include "choose-mode.h"
21
22#define UMID_LEN 64
23#define UML_DIR "~/.uml/"
24
25/* Changed by set_umid and make_umid, which are run early in boot */
26static char umid[UMID_LEN] = { 0 };
27
28/* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
29static char *uml_dir = UML_DIR;
30
31/* Changed by set_umid */
32static int umid_is_random = 1;
33static int umid_inited = 0;
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -070034/* Have we created the files? Should we remove them? */
35static int umid_owned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
37static int make_umid(int (*printer)(const char *fmt, ...));
38
39static int __init set_umid(char *name, int is_random,
40 int (*printer)(const char *fmt, ...))
41{
42 if(umid_inited){
43 (*printer)("Unique machine name can't be set twice\n");
44 return(-1);
45 }
46
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));
51
52 umid_is_random = is_random;
53 umid_inited = 1;
54 return 0;
55}
56
57static int __init set_umid_arg(char *name, int *add)
58{
59 *add = 0;
60 return(set_umid(name, 0, printf));
61}
62
63__uml_setup("umid=", set_umid_arg,
64"umid=<name>\n"
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"
67);
68
69int __init umid_file_name(char *name, char *buf, int len)
70{
71 int n;
72
73 if(!umid_inited && make_umid(printk)) return(-1);
74
75 n = strlen(uml_dir) + strlen(umid) + strlen(name) + 1;
76 if(n > len){
77 printk("umid_file_name : buffer too short\n");
78 return(-1);
79 }
80
81 sprintf(buf, "%s%s/%s", uml_dir, umid, name);
82 return(0);
83}
84
85extern int tracing_pid;
86
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -070087static void __init create_pid_file(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
90 char pid[sizeof("nnnnn\0")];
91 int fd, n;
92
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -070093 if(umid_file_name("pid", file, sizeof(file)))
94 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))),
97 0644);
98 if(fd < 0){
99 printf("Open of machine pid file \"%s\" failed: %s\n",
100 file, strerror(-fd));
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700101 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
104 sprintf(pid, "%d\n", os_getpid());
105 n = os_write_file(fd, pid, strlen(pid));
106 if(n != strlen(pid))
107 printf("Write of pid file failed - err = %d\n", -n);
108 os_close_file(fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
111static int actually_do_remove(char *dir)
112{
113 DIR *directory;
114 struct dirent *ent;
115 int len;
116 char file[256];
117
118 directory = opendir(dir);
119 if(directory == NULL){
120 printk("actually_do_remove : couldn't open directory '%s', "
121 "errno = %d\n", dir, errno);
122 return(1);
123 }
124 while((ent = readdir(directory)) != NULL){
125 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
126 continue;
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",
130 ent->d_name, dir);
131 continue;
132 }
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,
137 errno);
138 return(1);
139 }
140 }
141 if(rmdir(dir) < 0){
142 printk("actually_do_remove : couldn't rmdir '%s', "
143 "errno = %d\n", dir, errno);
144 return(1);
145 }
146 return(0);
147}
148
149void remove_umid_dir(void)
150{
151 char dir[strlen(uml_dir) + UMID_LEN + 1];
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700152 if (!umid_owned)
153 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
155 sprintf(dir, "%s%s", uml_dir, umid);
156 actually_do_remove(dir);
157}
158
159char *get_umid(int only_if_set)
160{
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700161 if(only_if_set && umid_is_random)
162 return NULL;
163 return umid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164}
165
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700166static int not_dead_yet(char *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
169 char pid[sizeof("nnnnn\0")], *end;
170 int dead, fd, p, n;
171
172 sprintf(file, "%s/pid", dir);
173 dead = 0;
174 fd = os_open_file(file, of_read(OPENFLAGS()), 0);
175 if(fd < 0){
176 if(fd != -ENOENT){
177 printk("not_dead_yet : couldn't open pid file '%s', "
178 "err = %d\n", file, -fd);
179 return(1);
180 }
181 dead = 1;
182 }
183 if(fd > 0){
184 n = os_read_file(fd, pid, sizeof(pid));
185 if(n < 0){
186 printk("not_dead_yet : couldn't read pid file '%s', "
187 "err = %d\n", file, -n);
188 return(1);
189 }
190 p = strtoul(pid, &end, 0);
191 if(end == pid){
192 printk("not_dead_yet : couldn't parse pid file '%s', "
193 "errno = %d\n", file, errno);
194 dead = 1;
195 }
196 if(((kill(p, 0) < 0) && (errno == ESRCH)) ||
197 (p == CHOOSE_MODE(tracing_pid, os_getpid())))
198 dead = 1;
199 }
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700200 if(!dead)
201 return(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return(actually_do_remove(dir));
203}
204
205static int __init set_uml_dir(char *name, int *add)
206{
207 if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){
208 uml_dir = malloc(strlen(name) + 2);
209 if(uml_dir == NULL){
210 printf("Failed to malloc uml_dir - error = %d\n",
211 errno);
212 uml_dir = name;
213 /* Return 0 here because do_initcalls doesn't look at
214 * the return value.
215 */
216 return(0);
217 }
218 sprintf(uml_dir, "%s/", name);
219 }
220 else uml_dir = name;
221 return(0);
222}
223
224static int __init make_uml_dir(void)
225{
226 char dir[MAXPATHLEN + 1] = { '\0' };
227 int len;
228
229 if(*uml_dir == '~'){
230 char *home = getenv("HOME");
231
232 if(home == NULL){
233 printf("make_uml_dir : no value in environment for "
234 "$HOME\n");
235 exit(1);
236 }
237 strlcpy(dir, home, sizeof(dir));
238 uml_dir++;
239 }
240 len = strlen(dir);
241 strncat(dir, uml_dir, sizeof(dir) - len);
242 len = strlen(dir);
243 if((len > 0) && (len < sizeof(dir) - 1) && (dir[len - 1] != '/')){
244 dir[len] = '/';
245 dir[len + 1] = '\0';
246 }
247
248 uml_dir = malloc(strlen(dir) + 1);
249 if(uml_dir == NULL){
250 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
251 exit(1);
252 }
253 strcpy(uml_dir, dir);
254
255 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
256 printf("Failed to mkdir %s: %s\n", uml_dir, strerror(errno));
257 return(-1);
258 }
259 return 0;
260}
261
262static int __init make_umid(int (*printer)(const char *fmt, ...))
263{
264 int fd, err;
265 char tmp[strlen(uml_dir) + UMID_LEN + 1];
266
267 strlcpy(tmp, uml_dir, sizeof(tmp));
268
269 if(!umid_inited){
270 strcat(tmp, "XXXXXX");
271 fd = mkstemp(tmp);
272 if(fd < 0){
273 (*printer)("make_umid - mkstemp(%s) failed: %s\n",
274 tmp,strerror(errno));
275 return(1);
276 }
277
278 os_close_file(fd);
279 /* There's a nice tiny little race between this unlink and
280 * the mkdir below. It'd be nice if there were a mkstemp
281 * for directories.
282 */
283 unlink(tmp);
284 set_umid(&tmp[strlen(uml_dir)], 1, printer);
285 }
286
287 sprintf(tmp, "%s%s", uml_dir, umid);
288
289 err = mkdir(tmp, 0777);
290 if(err < 0){
291 if(errno == EEXIST){
292 if(not_dead_yet(tmp)){
293 (*printer)("umid '%s' is in use\n", umid);
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700294 umid_owned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return(-1);
296 }
297 err = mkdir(tmp, 0777);
298 }
299 }
300 if(err < 0){
301 (*printer)("Failed to create %s - errno = %d\n", umid, errno);
302 return(-1);
303 }
304
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700305 umid_owned = 1;
306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307}
308
309__uml_setup("uml_dir=", set_uml_dir,
310"uml_dir=<directory>\n"
311" The location to place the pid and umid files.\n\n"
312);
313
314static int __init make_umid_setup(void)
315{
316 /* one function with the ordering we need ... */
317 make_uml_dir();
318 make_umid(printf);
Paolo 'Blaisorblade' Giarrusso3a02d6c2005-09-22 21:44:12 -0700319 create_pid_file();
320 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322__uml_postsetup(make_umid_setup);
323
324/*
325 * Overrides for Emacs so that we follow Linus's tabbing style.
326 * Emacs will notice this stuff at the end of the file and automatically
327 * adjust the settings for this buffer only. This must remain at the end
328 * of the file.
329 * ---------------------------------------------------------------------------
330 * Local variables:
331 * c-file-style: "linux"
332 * End:
333 */