aboutsummaryrefslogtreecommitdiffstats
path: root/arch/um/os-Linux/umid.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/um/os-Linux/umid.c')
-rw-r--r--arch/um/os-Linux/umid.c136
1 files changed, 74 insertions, 62 deletions
diff --git a/arch/um/os-Linux/umid.c b/arch/um/os-Linux/umid.c
index e36541e5ec00..106fa8641553 100644
--- a/arch/um/os-Linux/umid.c
+++ b/arch/um/os-Linux/umid.c
@@ -1,14 +1,19 @@
1/*
2 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
1#include <stdio.h> 6#include <stdio.h>
2#include <unistd.h>
3#include <stdlib.h> 7#include <stdlib.h>
4#include <string.h> 8#include <dirent.h>
5#include <errno.h> 9#include <errno.h>
10#include <fcntl.h>
6#include <signal.h> 11#include <signal.h>
7#include <dirent.h> 12#include <string.h>
8#include <sys/fcntl.h> 13#include <unistd.h>
9#include <sys/stat.h> 14#include <sys/stat.h>
10#include <sys/param.h>
11#include "init.h" 15#include "init.h"
16#include "kern_constants.h"
12#include "os.h" 17#include "os.h"
13#include "user.h" 18#include "user.h"
14 19
@@ -27,13 +32,13 @@ static int __init make_uml_dir(void)
27 char dir[512] = { '\0' }; 32 char dir[512] = { '\0' };
28 int len, err; 33 int len, err;
29 34
30 if(*uml_dir == '~'){ 35 if (*uml_dir == '~') {
31 char *home = getenv("HOME"); 36 char *home = getenv("HOME");
32 37
33 err = -ENOENT; 38 err = -ENOENT;
34 if(home == NULL){ 39 if (home == NULL) {
35 printk("make_uml_dir : no value in environment for " 40 printk(UM_KERN_ERR "make_uml_dir : no value in "
36 "$HOME\n"); 41 "environment for $HOME\n");
37 goto err; 42 goto err;
38 } 43 }
39 strlcpy(dir, home, sizeof(dir)); 44 strlcpy(dir, home, sizeof(dir));
@@ -52,7 +57,7 @@ static int __init make_uml_dir(void)
52 } 57 }
53 strcpy(uml_dir, dir); 58 strcpy(uml_dir, dir);
54 59
55 if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){ 60 if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
56 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno)); 61 printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
57 err = -errno; 62 err = -errno;
58 goto err_free; 63 goto err_free;
@@ -69,8 +74,8 @@ err:
69/* 74/*
70 * Unlinks the files contained in @dir and then removes @dir. 75 * Unlinks the files contained in @dir and then removes @dir.
71 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We 76 * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
72 * ignore ENOENT errors for anything (they happen, strangely enough - possibly due 77 * ignore ENOENT errors for anything (they happen, strangely enough - possibly
73 * to races between multiple dying UML threads). 78 * due to races between multiple dying UML threads).
74 */ 79 */
75static int remove_files_and_dir(char *dir) 80static int remove_files_and_dir(char *dir)
76{ 81{
@@ -115,7 +120,8 @@ out:
115 return ret; 120 return ret;
116} 121}
117 122
118/* This says that there isn't already a user of the specified directory even if 123/*
124 * This says that there isn't already a user of the specified directory even if
119 * there are errors during the checking. This is because if these errors 125 * there are errors during the checking. This is because if these errors
120 * happen, the directory is unusable by the pre-existing UML, so we might as 126 * happen, the directory is unusable by the pre-existing UML, so we might as
121 * well take it over. This could happen either by 127 * well take it over. This could happen either by
@@ -133,44 +139,45 @@ static inline int is_umdir_used(char *dir)
133 int dead, fd, p, n, err; 139 int dead, fd, p, n, err;
134 140
135 n = snprintf(file, sizeof(file), "%s/pid", dir); 141 n = snprintf(file, sizeof(file), "%s/pid", dir);
136 if(n >= sizeof(file)){ 142 if (n >= sizeof(file)) {
137 printk("is_umdir_used - pid filename too long\n"); 143 printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n");
138 err = -E2BIG; 144 err = -E2BIG;
139 goto out; 145 goto out;
140 } 146 }
141 147
142 dead = 0; 148 dead = 0;
143 fd = open(file, O_RDONLY); 149 fd = open(file, O_RDONLY);
144 if(fd < 0) { 150 if (fd < 0) {
145 fd = -errno; 151 fd = -errno;
146 if(fd != -ENOENT){ 152 if (fd != -ENOENT) {
147 printk("is_umdir_used : couldn't open pid file '%s', " 153 printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
148 "err = %d\n", file, -fd); 154 "file '%s', err = %d\n", file, -fd);
149 } 155 }
150 goto out; 156 goto out;
151 } 157 }
152 158
153 err = 0; 159 err = 0;
154 n = read(fd, pid, sizeof(pid)); 160 n = read(fd, pid, sizeof(pid));
155 if(n < 0){ 161 if (n < 0) {
156 printk("is_umdir_used : couldn't read pid file '%s', " 162 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
157 "err = %d\n", file, errno); 163 "'%s', err = %d\n", file, errno);
158 goto out_close; 164 goto out_close;
159 } else if(n == 0){ 165 } else if (n == 0) {
160 printk("is_umdir_used : couldn't read pid file '%s', " 166 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
161 "0-byte read\n", file); 167 "'%s', 0-byte read\n", file);
162 goto out_close; 168 goto out_close;
163 } 169 }
164 170
165 p = strtoul(pid, &end, 0); 171 p = strtoul(pid, &end, 0);
166 if(end == pid){ 172 if (end == pid) {
167 printk("is_umdir_used : couldn't parse pid file '%s', " 173 printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
168 "errno = %d\n", file, errno); 174 "'%s', errno = %d\n", file, errno);
169 goto out_close; 175 goto out_close;
170 } 176 }
171 177
172 if((kill(p, 0) == 0) || (errno != ESRCH)){ 178 if ((kill(p, 0) == 0) || (errno != ESRCH)) {
173 printk("umid \"%s\" is already in use by pid %d\n", umid, p); 179 printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
180 umid, p);
174 return 1; 181 return 1;
175 } 182 }
176 183
@@ -194,8 +201,8 @@ static int umdir_take_if_dead(char *dir)
194 201
195 ret = remove_files_and_dir(dir); 202 ret = remove_files_and_dir(dir);
196 if (ret) { 203 if (ret) {
197 printk("is_umdir_used - remove_files_and_dir failed with " 204 printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
198 "err = %d\n", ret); 205 "failed with err = %d\n", ret);
199 } 206 }
200 return ret; 207 return ret;
201} 208}
@@ -206,27 +213,28 @@ static void __init create_pid_file(void)
206 char pid[sizeof("nnnnn\0")]; 213 char pid[sizeof("nnnnn\0")];
207 int fd, n; 214 int fd, n;
208 215
209 if(umid_file_name("pid", file, sizeof(file))) 216 if (umid_file_name("pid", file, sizeof(file)))
210 return; 217 return;
211 218
212 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644); 219 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
213 if(fd < 0){ 220 if (fd < 0) {
214 printk("Open of machine pid file \"%s\" failed: %s\n", 221 printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
215 file, strerror(errno)); 222 "%s\n", file, strerror(errno));
216 return; 223 return;
217 } 224 }
218 225
219 snprintf(pid, sizeof(pid), "%d\n", getpid()); 226 snprintf(pid, sizeof(pid), "%d\n", getpid());
220 n = write(fd, pid, strlen(pid)); 227 n = write(fd, pid, strlen(pid));
221 if(n != strlen(pid)) 228 if (n != strlen(pid))
222 printk("Write of pid file failed - err = %d\n", errno); 229 printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
230 errno);
223 231
224 close(fd); 232 close(fd);
225} 233}
226 234
227int __init set_umid(char *name) 235int __init set_umid(char *name)
228{ 236{
229 if(strlen(name) > UMID_LEN - 1) 237 if (strlen(name) > UMID_LEN - 1)
230 return -E2BIG; 238 return -E2BIG;
231 239
232 strlcpy(umid, name, sizeof(umid)); 240 strlcpy(umid, name, sizeof(umid));
@@ -242,18 +250,18 @@ int __init make_umid(void)
242 int fd, err; 250 int fd, err;
243 char tmp[256]; 251 char tmp[256];
244 252
245 if(umid_setup) 253 if (umid_setup)
246 return 0; 254 return 0;
247 255
248 make_uml_dir(); 256 make_uml_dir();
249 257
250 if(*umid == '\0'){ 258 if (*umid == '\0') {
251 strlcpy(tmp, uml_dir, sizeof(tmp)); 259 strlcpy(tmp, uml_dir, sizeof(tmp));
252 strlcat(tmp, "XXXXXX", sizeof(tmp)); 260 strlcat(tmp, "XXXXXX", sizeof(tmp));
253 fd = mkstemp(tmp); 261 fd = mkstemp(tmp);
254 if(fd < 0){ 262 if (fd < 0) {
255 printk("make_umid - mkstemp(%s) failed: %s\n", 263 printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
256 tmp, strerror(errno)); 264 "%s\n", tmp, strerror(errno));
257 err = -errno; 265 err = -errno;
258 goto err; 266 goto err;
259 } 267 }
@@ -262,11 +270,12 @@ int __init make_umid(void)
262 270
263 set_umid(&tmp[strlen(uml_dir)]); 271 set_umid(&tmp[strlen(uml_dir)]);
264 272
265 /* There's a nice tiny little race between this unlink and 273 /*
274 * There's a nice tiny little race between this unlink and
266 * the mkdir below. It'd be nice if there were a mkstemp 275 * the mkdir below. It'd be nice if there were a mkstemp
267 * for directories. 276 * for directories.
268 */ 277 */
269 if(unlink(tmp)){ 278 if (unlink(tmp)) {
270 err = -errno; 279 err = -errno;
271 goto err; 280 goto err;
272 } 281 }
@@ -274,9 +283,9 @@ int __init make_umid(void)
274 283
275 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid); 284 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
276 err = mkdir(tmp, 0777); 285 err = mkdir(tmp, 0777);
277 if(err < 0){ 286 if (err < 0) {
278 err = -errno; 287 err = -errno;
279 if(err != -EEXIST) 288 if (err != -EEXIST)
280 goto err; 289 goto err;
281 290
282 if (umdir_take_if_dead(tmp) < 0) 291 if (umdir_take_if_dead(tmp) < 0)
@@ -284,9 +293,10 @@ int __init make_umid(void)
284 293
285 err = mkdir(tmp, 0777); 294 err = mkdir(tmp, 0777);
286 } 295 }
287 if(err){ 296 if (err) {
288 err = -errno; 297 err = -errno;
289 printk("Failed to create '%s' - err = %d\n", umid, -errno); 298 printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
299 errno);
290 goto err; 300 goto err;
291 } 301 }
292 302
@@ -301,14 +311,15 @@ int __init make_umid(void)
301 311
302static int __init make_umid_init(void) 312static int __init make_umid_init(void)
303{ 313{
304 if(!make_umid()) 314 if (!make_umid())
305 return 0; 315 return 0;
306 316
307 /* If initializing with the given umid failed, then try again with 317 /*
318 * If initializing with the given umid failed, then try again with
308 * a random one. 319 * a random one.
309 */ 320 */
310 printk("Failed to initialize umid \"%s\", trying with a random umid\n", 321 printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
311 umid); 322 "random umid\n", umid);
312 *umid = '\0'; 323 *umid = '\0';
313 make_umid(); 324 make_umid();
314 325
@@ -322,12 +333,12 @@ int __init umid_file_name(char *name, char *buf, int len)
322 int n, err; 333 int n, err;
323 334
324 err = make_umid(); 335 err = make_umid();
325 if(err) 336 if (err)
326 return err; 337 return err;
327 338
328 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name); 339 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
329 if(n >= len){ 340 if (n >= len) {
330 printk("umid_file_name : buffer too short\n"); 341 printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
331 return -E2BIG; 342 return -E2BIG;
332 } 343 }
333 344
@@ -341,21 +352,22 @@ char *get_umid(void)
341 352
342static int __init set_uml_dir(char *name, int *add) 353static int __init set_uml_dir(char *name, int *add)
343{ 354{
344 if(*name == '\0'){ 355 if (*name == '\0') {
345 printf("uml_dir can't be an empty string\n"); 356 printf("uml_dir can't be an empty string\n");
346 return 0; 357 return 0;
347 } 358 }
348 359
349 if(name[strlen(name) - 1] == '/'){ 360 if (name[strlen(name) - 1] == '/') {
350 uml_dir = name; 361 uml_dir = name;
351 return 0; 362 return 0;
352 } 363 }
353 364
354 uml_dir = malloc(strlen(name) + 2); 365 uml_dir = malloc(strlen(name) + 2);
355 if(uml_dir == NULL){ 366 if (uml_dir == NULL) {
356 printf("Failed to malloc uml_dir - error = %d\n", errno); 367 printf("Failed to malloc uml_dir - error = %d\n", errno);
357 368
358 /* Return 0 here because do_initcalls doesn't look at 369 /*
370 * Return 0 here because do_initcalls doesn't look at
359 * the return value. 371 * the return value.
360 */ 372 */
361 return 0; 373 return 0;
@@ -376,7 +388,7 @@ static void remove_umid_dir(void)
376 388
377 sprintf(dir, "%s%s", uml_dir, umid); 389 sprintf(dir, "%s%s", uml_dir, umid);
378 err = remove_files_and_dir(dir); 390 err = remove_files_and_dir(dir);
379 if(err) 391 if (err)
380 printf("remove_umid_dir - remove_files_and_dir failed with " 392 printf("remove_umid_dir - remove_files_and_dir failed with "
381 "err = %d\n", err); 393 "err = %d\n", err);
382} 394}