MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / arch / um / os-Linux / umid.c
blob48092b95c8ab997b4914a072afcd17f0e1aa7eda
1 #include <stdio.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6 #include <signal.h>
7 #include <dirent.h>
8 #include <sys/fcntl.h>
9 #include <sys/stat.h>
10 #include <sys/param.h>
11 #include "init.h"
12 #include "os.h"
13 #include "user.h"
14 #include "mode.h"
16 #define UML_DIR "~/.uml/"
18 #define UMID_LEN 64
20 /* Changed by set_umid, which is run early in boot */
21 char umid[UMID_LEN] = { 0 };
23 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
24 static char *uml_dir = UML_DIR;
26 static int __init make_uml_dir(void)
28 char dir[512] = { '\0' };
29 int len, err;
31 if(*uml_dir == '~'){
32 char *home = getenv("HOME");
34 err = -ENOENT;
35 if(home == NULL){
36 printk("make_uml_dir : no value in environment for "
37 "$HOME\n");
38 goto err;
40 strlcpy(dir, home, sizeof(dir));
41 uml_dir++;
43 strlcat(dir, uml_dir, sizeof(dir));
44 len = strlen(dir);
45 if (len > 0 && dir[len - 1] != '/')
46 strlcat(dir, "/", sizeof(dir));
48 err = -ENOMEM;
49 uml_dir = malloc(strlen(dir) + 1);
50 if (uml_dir == NULL) {
51 printf("make_uml_dir : malloc failed, errno = %d\n", errno);
52 goto err;
54 strcpy(uml_dir, dir);
56 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){
57 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
58 err = -errno;
59 goto err_free;
61 return 0;
63 err_free:
64 free(uml_dir);
65 err:
66 uml_dir = NULL;
67 return err;
71 * Unlinks the files contained in @dir and then removes @dir.
72 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
73 * ignore ENOENT errors for anything (they happen, strangely enough - possibly due
74 * to races between multiple dying UML threads).
76 static int remove_files_and_dir(char *dir)
78 DIR *directory;
79 struct dirent *ent;
80 int len;
81 char file[256];
82 int ret;
84 directory = opendir(dir);
85 if (directory == NULL) {
86 if (errno != ENOENT)
87 return -errno;
88 else
89 return 0;
92 while ((ent = readdir(directory)) != NULL) {
93 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
94 continue;
95 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
96 if (len > sizeof(file)) {
97 ret = -E2BIG;
98 goto out;
101 sprintf(file, "%s/%s", dir, ent->d_name);
102 if (unlink(file) < 0 && errno != ENOENT) {
103 ret = -errno;
104 goto out;
108 if (rmdir(dir) < 0 && errno != ENOENT) {
109 ret = -errno;
110 goto out;
113 ret = 0;
114 out:
115 closedir(directory);
116 return ret;
119 /* This says that there isn't already a user of the specified directory even if
120 * there are errors during the checking. This is because if these errors
121 * happen, the directory is unusable by the pre-existing UML, so we might as
122 * well take it over. This could happen either by
123 * the existing UML somehow corrupting its umid directory
124 * something other than UML sticking stuff in the directory
125 * this boot racing with a shutdown of the other UML
126 * In any of these cases, the directory isn't useful for anything else.
128 * Boolean return: 1 if in use, 0 otherwise.
130 static inline int is_umdir_used(char *dir)
132 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
133 char pid[sizeof("nnnnn\0")], *end;
134 int dead, fd, p, n, err;
136 n = snprintf(file, sizeof(file), "%s/pid", dir);
137 if(n >= sizeof(file)){
138 printk("is_umdir_used - pid filename too long\n");
139 err = -E2BIG;
140 goto out;
143 dead = 0;
144 fd = open(file, O_RDONLY);
145 if(fd < 0) {
146 fd = -errno;
147 if(fd != -ENOENT){
148 printk("is_umdir_used : couldn't open pid file '%s', "
149 "err = %d\n", file, -fd);
151 goto out;
154 err = 0;
155 n = read(fd, pid, sizeof(pid));
156 if(n < 0){
157 printk("is_umdir_used : couldn't read pid file '%s', "
158 "err = %d\n", file, errno);
159 goto out_close;
160 } else if(n == 0){
161 printk("is_umdir_used : couldn't read pid file '%s', "
162 "0-byte read\n", file);
163 goto out_close;
166 p = strtoul(pid, &end, 0);
167 if(end == pid){
168 printk("is_umdir_used : couldn't parse pid file '%s', "
169 "errno = %d\n", file, errno);
170 goto out_close;
173 if((kill(p, 0) == 0) || (errno != ESRCH)){
174 printk("umid \"%s\" is already in use by pid %d\n", umid, p);
175 return 1;
178 out_close:
179 close(fd);
180 out:
181 return 0;
185 * Try to remove the directory @dir unless it's in use.
186 * Precondition: @dir exists.
187 * Returns 0 for success, < 0 for failure in removal or if the directory is in
188 * use.
190 static int umdir_take_if_dead(char *dir)
192 int ret;
193 if (is_umdir_used(dir))
194 return -EEXIST;
196 ret = remove_files_and_dir(dir);
197 if (ret) {
198 printk("is_umdir_used - remove_files_and_dir failed with "
199 "err = %d\n", ret);
201 return ret;
204 static void __init create_pid_file(void)
206 char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
207 char pid[sizeof("nnnnn\0")];
208 int fd, n;
210 if(umid_file_name("pid", file, sizeof(file)))
211 return;
213 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
214 if(fd < 0){
215 printk("Open of machine pid file \"%s\" failed: %s\n",
216 file, strerror(errno));
217 return;
220 snprintf(pid, sizeof(pid), "%d\n", getpid());
221 n = write(fd, pid, strlen(pid));
222 if(n != strlen(pid))
223 printk("Write of pid file failed - err = %d\n", errno);
225 close(fd);
228 int __init set_umid(char *name)
230 if(strlen(name) > UMID_LEN - 1)
231 return -E2BIG;
233 strlcpy(umid, name, sizeof(umid));
235 return 0;
238 static int umid_setup = 0;
240 int __init make_umid(void)
242 int fd, err;
243 char tmp[256];
245 if(umid_setup)
246 return 0;
248 make_uml_dir();
250 if(*umid == '\0'){
251 strlcpy(tmp, uml_dir, sizeof(tmp));
252 strlcat(tmp, "XXXXXX", sizeof(tmp));
253 fd = mkstemp(tmp);
254 if(fd < 0){
255 printk("make_umid - mkstemp(%s) failed: %s\n",
256 tmp, strerror(errno));
257 err = -errno;
258 goto err;
261 close(fd);
263 set_umid(&tmp[strlen(uml_dir)]);
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.
269 if(unlink(tmp)){
270 err = -errno;
271 goto err;
275 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
276 err = mkdir(tmp, 0777);
277 if(err < 0){
278 err = -errno;
279 if(err != -EEXIST)
280 goto err;
282 if (umdir_take_if_dead(tmp) < 0)
283 goto err;
285 err = mkdir(tmp, 0777);
287 if(err){
288 err = -errno;
289 printk("Failed to create '%s' - err = %d\n", umid, -errno);
290 goto err;
293 umid_setup = 1;
295 create_pid_file();
297 err = 0;
298 err:
299 return err;
302 static int __init make_umid_init(void)
304 if(!make_umid())
305 return 0;
307 /* If initializing with the given umid failed, then try again with
308 * a random one.
310 printk("Failed to initialize umid \"%s\", trying with a random umid\n",
311 umid);
312 *umid = '\0';
313 make_umid();
315 return 0;
318 __initcall(make_umid_init);
320 int __init umid_file_name(char *name, char *buf, int len)
322 int n, err;
324 err = make_umid();
325 if(err)
326 return err;
328 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
329 if(n >= len){
330 printk("umid_file_name : buffer too short\n");
331 return -E2BIG;
334 return 0;
337 char *get_umid(void)
339 return umid;
342 static int __init set_uml_dir(char *name, int *add)
344 if(*name == '\0'){
345 printf("uml_dir can't be an empty string\n");
346 return 0;
349 if(name[strlen(name) - 1] == '/'){
350 uml_dir = name;
351 return 0;
354 uml_dir = malloc(strlen(name) + 2);
355 if(uml_dir == NULL){
356 printf("Failed to malloc uml_dir - error = %d\n", errno);
358 /* Return 0 here because do_initcalls doesn't look at
359 * the return value.
361 return 0;
363 sprintf(uml_dir, "%s/", name);
365 return 0;
368 __uml_setup("uml_dir=", set_uml_dir,
369 "uml_dir=<directory>\n"
370 " The location to place the pid and umid files.\n\n"
373 static void remove_umid_dir(void)
375 char dir[strlen(uml_dir) + UMID_LEN + 1], err;
377 sprintf(dir, "%s%s", uml_dir, umid);
378 err = remove_files_and_dir(dir);
379 if(err)
380 printf("remove_umid_dir - remove_files_and_dir failed with "
381 "err = %d\n", err);
384 __uml_exitcall(remove_umid_dir);