diff options
author | Jeff Dike <jdike@addtoit.com> | 2006-01-06 03:19:01 -0500 |
---|---|---|
committer | Linus Torvalds <torvalds@g5.osdl.org> | 2006-01-06 11:33:47 -0500 |
commit | 7eebe8a9c51686927709a57b1f2725d371014abc (patch) | |
tree | 6b1543982c8656c76c25baa7e44497d5d9de307a /arch/um/os-Linux | |
parent | 2264c475e4bf7427e59921953c89a5693ecb506f (diff) |
[PATCH] uml: umid cleanup
This patch cleans up the umid code:
- The only_if_set argument to get_umid is gone.
- get_umid returns an empty string rather than NULL if there is no umid.
- umid_is_random is gone since its users went away.
- Some printfs were turned into printks because the code runs late enough
that printk is working.
- Error paths were cleaned up.
- Some functions now return an error and let the caller print the error
message rather than printing it themselves. This eliminates the practice of
passing a pointer to printf or printk in, depending on where in the boot
process we are.
- Major tidying of not_dead_yet - mostly error path cleanup, plus a comment
explaining why it doesn't react to errors the way you might expect.
- Calls to os_* interfaces that were moved under os are changed back to
their native libc forms.
- snprintf, strlcpy, and their bounds-checking friends are used more often,
replacing by-hand bounds checking in some places.
Signed-off-by: Jeff Dike <jdike@addtoit.com>
Cc: Paolo 'Blaisorblade' Giarrusso <blaisorblade@yahoo.it>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'arch/um/os-Linux')
-rw-r--r-- | arch/um/os-Linux/umid.c | 265 |
1 files changed, 154 insertions, 111 deletions
diff --git a/arch/um/os-Linux/umid.c b/arch/um/os-Linux/umid.c index 77d69a348cf4..ecf107ae5ac8 100644 --- a/arch/um/os-Linux/umid.c +++ b/arch/um/os-Linux/umid.c | |||
@@ -5,6 +5,7 @@ | |||
5 | #include <errno.h> | 5 | #include <errno.h> |
6 | #include <signal.h> | 6 | #include <signal.h> |
7 | #include <dirent.h> | 7 | #include <dirent.h> |
8 | #include <sys/fcntl.h> | ||
8 | #include <sys/stat.h> | 9 | #include <sys/stat.h> |
9 | #include <sys/param.h> | 10 | #include <sys/param.h> |
10 | #include "init.h" | 11 | #include "init.h" |
@@ -25,15 +26,16 @@ static char *uml_dir = UML_DIR; | |||
25 | static int __init make_uml_dir(void) | 26 | static int __init make_uml_dir(void) |
26 | { | 27 | { |
27 | char dir[512] = { '\0' }; | 28 | char dir[512] = { '\0' }; |
28 | int len; | 29 | int len, err; |
29 | 30 | ||
30 | if(*uml_dir == '~'){ | 31 | if(*uml_dir == '~'){ |
31 | char *home = getenv("HOME"); | 32 | char *home = getenv("HOME"); |
32 | 33 | ||
34 | err = -ENOENT; | ||
33 | if(home == NULL){ | 35 | if(home == NULL){ |
34 | printf("make_uml_dir : no value in environment for " | 36 | printk("make_uml_dir : no value in environment for " |
35 | "$HOME\n"); | 37 | "$HOME\n"); |
36 | exit(1); | 38 | goto err; |
37 | } | 39 | } |
38 | strlcpy(dir, home, sizeof(dir)); | 40 | strlcpy(dir, home, sizeof(dir)); |
39 | uml_dir++; | 41 | uml_dir++; |
@@ -43,18 +45,26 @@ static int __init make_uml_dir(void) | |||
43 | if (len > 0 && dir[len - 1] != '/') | 45 | if (len > 0 && dir[len - 1] != '/') |
44 | strlcat(dir, "/", sizeof(dir)); | 46 | strlcat(dir, "/", sizeof(dir)); |
45 | 47 | ||
48 | err = -ENOMEM; | ||
46 | uml_dir = malloc(strlen(dir) + 1); | 49 | uml_dir = malloc(strlen(dir) + 1); |
47 | if (uml_dir == NULL) { | 50 | if (uml_dir == NULL) { |
48 | printf("make_uml_dir : malloc failed, errno = %d\n", errno); | 51 | printf("make_uml_dir : malloc failed, errno = %d\n", errno); |
49 | exit(1); | 52 | goto err; |
50 | } | 53 | } |
51 | strcpy(uml_dir, dir); | 54 | strcpy(uml_dir, dir); |
52 | 55 | ||
53 | if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){ | 56 | if((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)){ |
54 | printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno)); | 57 | printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno)); |
55 | return(-1); | 58 | err = -errno; |
59 | goto err_free; | ||
56 | } | 60 | } |
57 | return 0; | 61 | return 0; |
62 | |||
63 | err_free: | ||
64 | free(uml_dir); | ||
65 | err: | ||
66 | uml_dir = NULL; | ||
67 | return err; | ||
58 | } | 68 | } |
59 | 69 | ||
60 | static int actually_do_remove(char *dir) | 70 | static int actually_do_remove(char *dir) |
@@ -65,75 +75,88 @@ static int actually_do_remove(char *dir) | |||
65 | char file[256]; | 75 | char file[256]; |
66 | 76 | ||
67 | directory = opendir(dir); | 77 | directory = opendir(dir); |
68 | if(directory == NULL){ | 78 | if(directory == NULL) |
69 | printk("actually_do_remove : couldn't open directory '%s', " | 79 | return -errno; |
70 | "errno = %d\n", dir, errno); | 80 | |
71 | return(1); | ||
72 | } | ||
73 | while((ent = readdir(directory)) != NULL){ | 81 | while((ent = readdir(directory)) != NULL){ |
74 | if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) | 82 | if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
75 | continue; | 83 | continue; |
76 | len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; | 84 | len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; |
77 | if(len > sizeof(file)){ | 85 | if(len > sizeof(file)) |
78 | printk("Not deleting '%s' from '%s' - name too long\n", | 86 | return -E2BIG; |
79 | ent->d_name, dir); | 87 | |
80 | continue; | ||
81 | } | ||
82 | sprintf(file, "%s/%s", dir, ent->d_name); | 88 | sprintf(file, "%s/%s", dir, ent->d_name); |
83 | if(unlink(file) < 0){ | 89 | if(unlink(file) < 0) |
84 | printk("actually_do_remove : couldn't remove '%s' " | 90 | return -errno; |
85 | "from '%s', errno = %d\n", ent->d_name, dir, | ||
86 | errno); | ||
87 | return(1); | ||
88 | } | ||
89 | } | ||
90 | if(rmdir(dir) < 0){ | ||
91 | printk("actually_do_remove : couldn't rmdir '%s', " | ||
92 | "errno = %d\n", dir, errno); | ||
93 | return(1); | ||
94 | } | 91 | } |
95 | return(0); | 92 | if(rmdir(dir) < 0) |
93 | return -errno; | ||
94 | |||
95 | return 0; | ||
96 | } | 96 | } |
97 | 97 | ||
98 | extern int tracing_pid; | 98 | /* This says that there isn't already a user of the specified directory even if |
99 | * there are errors during the checking. This is because if these errors | ||
100 | * happen, the directory is unusable by the pre-existing UML, so we might as | ||
101 | * well take it over. This could happen either by | ||
102 | * the existing UML somehow corrupting its umid directory | ||
103 | * something other than UML sticking stuff in the directory | ||
104 | * this boot racing with a shutdown of the other UML | ||
105 | * In any of these cases, the directory isn't useful for anything else. | ||
106 | */ | ||
99 | 107 | ||
100 | static int not_dead_yet(char *dir) | 108 | static int not_dead_yet(char *dir) |
101 | { | 109 | { |
102 | char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; | 110 | char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; |
103 | char pid[sizeof("nnnnn\0")], *end; | 111 | char pid[sizeof("nnnnn\0")], *end; |
104 | int dead, fd, p, n; | 112 | int dead, fd, p, n, err; |
113 | |||
114 | n = snprintf(file, sizeof(file), "%s/pid", dir); | ||
115 | if(n >= sizeof(file)){ | ||
116 | printk("not_dead_yet - pid filename too long\n"); | ||
117 | err = -E2BIG; | ||
118 | goto out; | ||
119 | } | ||
105 | 120 | ||
106 | sprintf(file, "%s/pid", dir); | ||
107 | dead = 0; | 121 | dead = 0; |
108 | fd = os_open_file(file, of_read(OPENFLAGS()), 0); | 122 | fd = open(file, O_RDONLY); |
109 | if(fd < 0){ | 123 | if(fd < 0){ |
110 | if(fd != -ENOENT){ | 124 | if(fd != -ENOENT){ |
111 | printk("not_dead_yet : couldn't open pid file '%s', " | 125 | printk("not_dead_yet : couldn't open pid file '%s', " |
112 | "err = %d\n", file, -fd); | 126 | "err = %d\n", file, -fd); |
113 | return(1); | ||
114 | } | 127 | } |
115 | dead = 1; | 128 | goto out; |
116 | } | 129 | } |
117 | if(fd > 0){ | 130 | |
118 | n = os_read_file(fd, pid, sizeof(pid)); | 131 | err = 0; |
119 | if(n < 0){ | 132 | n = read(fd, pid, sizeof(pid)); |
120 | printk("not_dead_yet : couldn't read pid file '%s', " | 133 | if(n <= 0){ |
121 | "err = %d\n", file, -n); | 134 | printk("not_dead_yet : couldn't read pid file '%s', " |
122 | return(1); | 135 | "err = %d\n", file, -n); |
123 | } | 136 | goto out_close; |
124 | p = strtoul(pid, &end, 0); | 137 | } |
125 | if(end == pid){ | 138 | |
126 | printk("not_dead_yet : couldn't parse pid file '%s', " | 139 | p = strtoul(pid, &end, 0); |
127 | "errno = %d\n", file, errno); | 140 | if(end == pid){ |
128 | dead = 1; | 141 | printk("not_dead_yet : couldn't parse pid file '%s', " |
129 | } | 142 | "errno = %d\n", file, errno); |
130 | if(((kill(p, 0) < 0) && (errno == ESRCH)) || | 143 | goto out_close; |
131 | (p == CHOOSE_MODE(tracing_pid, os_getpid()))) | ||
132 | dead = 1; | ||
133 | } | 144 | } |
134 | if(!dead) | 145 | |
135 | return(1); | 146 | if((kill(p, 0) == 0) || (errno != ESRCH)) |
136 | return(actually_do_remove(dir)); | 147 | return 1; |
148 | |||
149 | err = actually_do_remove(dir); | ||
150 | if(err) | ||
151 | printk("not_dead_yet - actually_do_remove failed with " | ||
152 | "err = %d\n", err); | ||
153 | |||
154 | return err; | ||
155 | |||
156 | out_close: | ||
157 | close(fd); | ||
158 | out: | ||
159 | return 0; | ||
137 | } | 160 | } |
138 | 161 | ||
139 | static void __init create_pid_file(void) | 162 | static void __init create_pid_file(void) |
@@ -145,26 +168,26 @@ static void __init create_pid_file(void) | |||
145 | if(umid_file_name("pid", file, sizeof(file))) | 168 | if(umid_file_name("pid", file, sizeof(file))) |
146 | return; | 169 | return; |
147 | 170 | ||
148 | fd = os_open_file(file, of_create(of_excl(of_rdwr(OPENFLAGS()))), | 171 | fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644); |
149 | 0644); | ||
150 | if(fd < 0){ | 172 | if(fd < 0){ |
151 | printf("Open of machine pid file \"%s\" failed: %s\n", | 173 | printk("Open of machine pid file \"%s\" failed: %s\n", |
152 | file, strerror(-fd)); | 174 | file, strerror(-fd)); |
153 | return; | 175 | return; |
154 | } | 176 | } |
155 | 177 | ||
156 | sprintf(pid, "%d\n", os_getpid()); | 178 | snprintf(pid, sizeof(pid), "%d\n", getpid()); |
157 | n = os_write_file(fd, pid, strlen(pid)); | 179 | n = write(fd, pid, strlen(pid)); |
158 | if(n != strlen(pid)) | 180 | if(n != strlen(pid)) |
159 | printf("Write of pid file failed - err = %d\n", -n); | 181 | printk("Write of pid file failed - err = %d\n", -n); |
160 | os_close_file(fd); | 182 | |
183 | close(fd); | ||
161 | } | 184 | } |
162 | 185 | ||
163 | int __init set_umid(char *name, int (*printer)(const char *fmt, ...)) | 186 | int __init set_umid(char *name) |
164 | { | 187 | { |
165 | if(strlen(name) > UMID_LEN - 1) | 188 | if(strlen(name) > UMID_LEN - 1) |
166 | (*printer)("Unique machine name is being truncated to %d " | 189 | return -E2BIG; |
167 | "characters\n", UMID_LEN); | 190 | |
168 | strlcpy(umid, name, sizeof(umid)); | 191 | strlcpy(umid, name, sizeof(umid)); |
169 | 192 | ||
170 | return 0; | 193 | return 0; |
@@ -172,44 +195,56 @@ int __init set_umid(char *name, int (*printer)(const char *fmt, ...)) | |||
172 | 195 | ||
173 | static int umid_setup = 0; | 196 | static int umid_setup = 0; |
174 | 197 | ||
175 | int __init make_umid(int (*printer)(const char *fmt, ...)) | 198 | int __init make_umid(void) |
176 | { | 199 | { |
177 | int fd, err; | 200 | int fd, err; |
178 | char tmp[256]; | 201 | char tmp[256]; |
179 | 202 | ||
203 | if(umid_setup) | ||
204 | return 0; | ||
205 | |||
180 | make_uml_dir(); | 206 | make_uml_dir(); |
181 | 207 | ||
182 | if(*umid == '\0'){ | 208 | if(*umid == '\0'){ |
183 | strlcpy(tmp, uml_dir, sizeof(tmp)); | 209 | strlcpy(tmp, uml_dir, sizeof(tmp)); |
184 | strcat(tmp, "XXXXXX"); | 210 | strlcat(tmp, "XXXXXX", sizeof(tmp)); |
185 | fd = mkstemp(tmp); | 211 | fd = mkstemp(tmp); |
186 | if(fd < 0){ | 212 | if(fd < 0){ |
187 | (*printer)("make_umid - mkstemp(%s) failed: %s\n", | 213 | printk("make_umid - mkstemp(%s) failed: %s\n", |
188 | tmp,strerror(errno)); | 214 | tmp, strerror(errno)); |
189 | return(1); | 215 | err = -errno; |
216 | goto err; | ||
190 | } | 217 | } |
191 | 218 | ||
192 | os_close_file(fd); | 219 | close(fd); |
220 | |||
221 | set_umid(&tmp[strlen(uml_dir)]); | ||
222 | |||
193 | /* There's a nice tiny little race between this unlink and | 223 | /* There's a nice tiny little race between this unlink and |
194 | * the mkdir below. It'd be nice if there were a mkstemp | 224 | * the mkdir below. It'd be nice if there were a mkstemp |
195 | * for directories. | 225 | * for directories. |
196 | */ | 226 | */ |
197 | unlink(tmp); | 227 | if(unlink(tmp)){ |
198 | set_umid(&tmp[strlen(uml_dir)], printer); | 228 | err = -errno; |
229 | goto err; | ||
230 | } | ||
199 | } | 231 | } |
200 | 232 | ||
201 | sprintf(tmp, "%s%s", uml_dir, umid); | 233 | snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid); |
202 | err = mkdir(tmp, 0777); | 234 | err = mkdir(tmp, 0777); |
203 | if(err < 0){ | 235 | if(err < 0){ |
204 | if(errno == EEXIST){ | 236 | err = -errno; |
205 | if(not_dead_yet(tmp)) | 237 | if(errno != EEXIST) |
206 | return -EEXIST; | 238 | goto err; |
207 | err = mkdir(tmp, 0777); | 239 | |
208 | } | 240 | if(not_dead_yet(tmp) < 0) |
241 | goto err; | ||
242 | |||
243 | err = mkdir(tmp, 0777); | ||
209 | } | 244 | } |
210 | if(err < 0){ | 245 | if(err < 0){ |
211 | (*printer)("Failed to create %s - errno = %d\n", umid, errno); | 246 | printk("Failed to create '%s' - err = %d\n", umid, err); |
212 | return(-1); | 247 | goto err_rmdir; |
213 | } | 248 | } |
214 | 249 | ||
215 | umid_setup = 1; | 250 | umid_setup = 1; |
@@ -217,13 +252,18 @@ int __init make_umid(int (*printer)(const char *fmt, ...)) | |||
217 | create_pid_file(); | 252 | create_pid_file(); |
218 | 253 | ||
219 | return 0; | 254 | return 0; |
255 | |||
256 | err_rmdir: | ||
257 | rmdir(tmp); | ||
258 | err: | ||
259 | return err; | ||
220 | } | 260 | } |
221 | 261 | ||
222 | static int __init make_umid_init(void) | 262 | static int __init make_umid_init(void) |
223 | { | 263 | { |
224 | make_umid(printk); | 264 | make_umid(); |
225 | 265 | ||
226 | return(0); | 266 | return 0; |
227 | } | 267 | } |
228 | 268 | ||
229 | __initcall(make_umid_init); | 269 | __initcall(make_umid_init); |
@@ -232,48 +272,48 @@ int __init umid_file_name(char *name, char *buf, int len) | |||
232 | { | 272 | { |
233 | int n, err; | 273 | int n, err; |
234 | 274 | ||
235 | if(!umid_setup){ | 275 | err = make_umid(); |
236 | err = make_umid(printk); | 276 | if(err) |
237 | if(err) | 277 | return err; |
238 | return err; | ||
239 | } | ||
240 | 278 | ||
241 | n = strlen(uml_dir) + strlen(umid) + strlen("/") + strlen(name) + 1; | 279 | n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name); |
242 | if(n > len){ | 280 | if(n >= len){ |
243 | printk("umid_file_name : buffer too short\n"); | 281 | printk("umid_file_name : buffer too short\n"); |
244 | return(-1); | 282 | return -E2BIG; |
245 | } | 283 | } |
246 | 284 | ||
247 | sprintf(buf, "%s%s/%s", uml_dir, umid, name); | 285 | return 0; |
248 | return(0); | ||
249 | } | 286 | } |
250 | 287 | ||
251 | extern int umid_is_random; | 288 | char *get_umid(void) |
252 | |||
253 | char *get_umid(int only_if_set) | ||
254 | { | 289 | { |
255 | if(only_if_set && umid_is_random) | ||
256 | return NULL; | ||
257 | return umid; | 290 | return umid; |
258 | } | 291 | } |
259 | 292 | ||
260 | static int __init set_uml_dir(char *name, int *add) | 293 | static int __init set_uml_dir(char *name, int *add) |
261 | { | 294 | { |
262 | if((strlen(name) > 0) && (name[strlen(name) - 1] != '/')){ | 295 | if(*name == '\0'){ |
263 | uml_dir = malloc(strlen(name) + 2); | 296 | printf("uml_dir can't be an empty string\n"); |
264 | if(uml_dir == NULL){ | 297 | return 0; |
265 | printf("Failed to malloc uml_dir - error = %d\n", | ||
266 | errno); | ||
267 | uml_dir = name; | ||
268 | /* Return 0 here because do_initcalls doesn't look at | ||
269 | * the return value. | ||
270 | */ | ||
271 | return(0); | ||
272 | } | ||
273 | sprintf(uml_dir, "%s/", name); | ||
274 | } | 298 | } |
275 | else uml_dir = name; | 299 | |
276 | return(0); | 300 | if(name[strlen(name) - 1] == '/'){ |
301 | uml_dir = name; | ||
302 | return 0; | ||
303 | } | ||
304 | |||
305 | uml_dir = malloc(strlen(name) + 2); | ||
306 | if(uml_dir == NULL){ | ||
307 | printf("Failed to malloc uml_dir - error = %d\n", errno); | ||
308 | |||
309 | /* Return 0 here because do_initcalls doesn't look at | ||
310 | * the return value. | ||
311 | */ | ||
312 | return 0; | ||
313 | } | ||
314 | sprintf(uml_dir, "%s/", name); | ||
315 | |||
316 | return 0; | ||
277 | } | 317 | } |
278 | 318 | ||
279 | __uml_setup("uml_dir=", set_uml_dir, | 319 | __uml_setup("uml_dir=", set_uml_dir, |
@@ -283,10 +323,13 @@ __uml_setup("uml_dir=", set_uml_dir, | |||
283 | 323 | ||
284 | static void remove_umid_dir(void) | 324 | static void remove_umid_dir(void) |
285 | { | 325 | { |
286 | char dir[strlen(uml_dir) + UMID_LEN + 1]; | 326 | char dir[strlen(uml_dir) + UMID_LEN + 1], err; |
287 | 327 | ||
288 | sprintf(dir, "%s%s", uml_dir, umid); | 328 | sprintf(dir, "%s%s", uml_dir, umid); |
289 | actually_do_remove(dir); | 329 | err = actually_do_remove(dir); |
330 | if(err) | ||
331 | printf("remove_umid_dir - actually_do_remove failed with " | ||
332 | "err = %d\n", err); | ||
290 | } | 333 | } |
291 | 334 | ||
292 | __uml_exitcall(remove_umid_dir); | 335 | __uml_exitcall(remove_umid_dir); |