Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 6 | #include <stdio.h> |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 7 | #include <stdlib.h> |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 8 | #include <dirent.h> |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 9 | #include <errno.h> |
| 10 | #include <fcntl.h> |
| 11 | #include <signal.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 14 | #include <sys/stat.h> |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 15 | #include "init.h" |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 16 | #include "kern_constants.h" |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 17 | #include "os.h" |
| 18 | #include "user.h" |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 19 | |
| 20 | #define UML_DIR "~/.uml/" |
| 21 | |
| 22 | #define UMID_LEN 64 |
| 23 | |
| 24 | /* Changed by set_umid, which is run early in boot */ |
Jeff Dike | de5fe76 | 2007-02-10 01:44:25 -0800 | [diff] [blame] | 25 | static char umid[UMID_LEN] = { 0 }; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 26 | |
| 27 | /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */ |
| 28 | static char *uml_dir = UML_DIR; |
| 29 | |
| 30 | static int __init make_uml_dir(void) |
| 31 | { |
| 32 | char dir[512] = { '\0' }; |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 33 | int len, err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 34 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 35 | if (*uml_dir == '~') { |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 36 | char *home = getenv("HOME"); |
| 37 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 38 | err = -ENOENT; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 39 | if (home == NULL) { |
| 40 | printk(UM_KERN_ERR "make_uml_dir : no value in " |
| 41 | "environment for $HOME\n"); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 42 | goto err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 43 | } |
| 44 | strlcpy(dir, home, sizeof(dir)); |
| 45 | uml_dir++; |
| 46 | } |
| 47 | strlcat(dir, uml_dir, sizeof(dir)); |
| 48 | len = strlen(dir); |
| 49 | if (len > 0 && dir[len - 1] != '/') |
| 50 | strlcat(dir, "/", sizeof(dir)); |
| 51 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 52 | err = -ENOMEM; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 53 | uml_dir = malloc(strlen(dir) + 1); |
| 54 | if (uml_dir == NULL) { |
| 55 | printf("make_uml_dir : malloc failed, errno = %d\n", errno); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 56 | goto err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 57 | } |
| 58 | strcpy(uml_dir, dir); |
| 59 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 60 | if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) { |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 61 | printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno)); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 62 | err = -errno; |
| 63 | goto err_free; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 64 | } |
| 65 | return 0; |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 66 | |
| 67 | err_free: |
| 68 | free(uml_dir); |
| 69 | err: |
| 70 | uml_dir = NULL; |
| 71 | return err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 74 | /* |
| 75 | * Unlinks the files contained in @dir and then removes @dir. |
| 76 | * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 77 | * ignore ENOENT errors for anything (they happen, strangely enough - possibly |
| 78 | * due to races between multiple dying UML threads). |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 79 | */ |
| 80 | static int remove_files_and_dir(char *dir) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 81 | { |
| 82 | DIR *directory; |
| 83 | struct dirent *ent; |
| 84 | int len; |
| 85 | char file[256]; |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 86 | int ret; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 87 | |
| 88 | directory = opendir(dir); |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 89 | if (directory == NULL) { |
| 90 | if (errno != ENOENT) |
| 91 | return -errno; |
| 92 | else |
| 93 | return 0; |
| 94 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 95 | |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 96 | while ((ent = readdir(directory)) != NULL) { |
| 97 | if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..")) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 98 | continue; |
| 99 | len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1; |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 100 | if (len > sizeof(file)) { |
| 101 | ret = -E2BIG; |
| 102 | goto out; |
| 103 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 104 | |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 105 | sprintf(file, "%s/%s", dir, ent->d_name); |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 106 | if (unlink(file) < 0 && errno != ENOENT) { |
| 107 | ret = -errno; |
| 108 | goto out; |
| 109 | } |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 110 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 111 | |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 112 | if (rmdir(dir) < 0 && errno != ENOENT) { |
| 113 | ret = -errno; |
| 114 | goto out; |
| 115 | } |
| 116 | |
| 117 | ret = 0; |
| 118 | out: |
| 119 | closedir(directory); |
| 120 | return ret; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 121 | } |
| 122 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 123 | /* |
| 124 | * This says that there isn't already a user of the specified directory even if |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 125 | * there are errors during the checking. This is because if these errors |
| 126 | * happen, the directory is unusable by the pre-existing UML, so we might as |
| 127 | * well take it over. This could happen either by |
| 128 | * the existing UML somehow corrupting its umid directory |
| 129 | * something other than UML sticking stuff in the directory |
| 130 | * this boot racing with a shutdown of the other UML |
| 131 | * In any of these cases, the directory isn't useful for anything else. |
Paolo 'Blaisorblade' Giarrusso | 912ad92 | 2006-07-01 04:36:23 -0700 | [diff] [blame] | 132 | * |
| 133 | * Boolean return: 1 if in use, 0 otherwise. |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 134 | */ |
Paolo 'Blaisorblade' Giarrusso | 912ad92 | 2006-07-01 04:36:23 -0700 | [diff] [blame] | 135 | static inline int is_umdir_used(char *dir) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 136 | { |
| 137 | char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; |
| 138 | char pid[sizeof("nnnnn\0")], *end; |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 139 | int dead, fd, p, n, err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 140 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 141 | n = snprintf(file, sizeof(file), "%s/pid", dir); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 142 | if (n >= sizeof(file)) { |
| 143 | printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n"); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 144 | err = -E2BIG; |
| 145 | goto out; |
| 146 | } |
| 147 | |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 148 | dead = 0; |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 149 | fd = open(file, O_RDONLY); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 150 | if (fd < 0) { |
Paolo 'Blaisorblade' Giarrusso | d84a19c | 2006-04-10 22:53:38 -0700 | [diff] [blame] | 151 | fd = -errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 152 | if (fd != -ENOENT) { |
| 153 | printk(UM_KERN_ERR "is_umdir_used : couldn't open pid " |
| 154 | "file '%s', err = %d\n", file, -fd); |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 155 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 156 | goto out; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 157 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 158 | |
| 159 | err = 0; |
| 160 | n = read(fd, pid, sizeof(pid)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 161 | if (n < 0) { |
| 162 | printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file " |
| 163 | "'%s', err = %d\n", file, errno); |
Paolo 'Blaisorblade' Giarrusso | d84a19c | 2006-04-10 22:53:38 -0700 | [diff] [blame] | 164 | goto out_close; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 165 | } else if (n == 0) { |
| 166 | printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file " |
| 167 | "'%s', 0-byte read\n", file); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 168 | goto out_close; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 169 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 170 | |
| 171 | p = strtoul(pid, &end, 0); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 172 | if (end == pid) { |
| 173 | printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file " |
| 174 | "'%s', errno = %d\n", file, errno); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 175 | goto out_close; |
| 176 | } |
| 177 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 178 | if ((kill(p, 0) == 0) || (errno != ESRCH)) { |
| 179 | printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n", |
| 180 | umid, p); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 181 | return 1; |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 182 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 183 | |
Paolo 'Blaisorblade' Giarrusso | d84a19c | 2006-04-10 22:53:38 -0700 | [diff] [blame] | 184 | out_close: |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 185 | close(fd); |
Paolo 'Blaisorblade' Giarrusso | d84a19c | 2006-04-10 22:53:38 -0700 | [diff] [blame] | 186 | out: |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 187 | return 0; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Paolo 'Blaisorblade' Giarrusso | 912ad92 | 2006-07-01 04:36:23 -0700 | [diff] [blame] | 190 | /* |
| 191 | * Try to remove the directory @dir unless it's in use. |
| 192 | * Precondition: @dir exists. |
| 193 | * Returns 0 for success, < 0 for failure in removal or if the directory is in |
| 194 | * use. |
| 195 | */ |
| 196 | static int umdir_take_if_dead(char *dir) |
| 197 | { |
| 198 | int ret; |
| 199 | if (is_umdir_used(dir)) |
| 200 | return -EEXIST; |
| 201 | |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 202 | ret = remove_files_and_dir(dir); |
Paolo 'Blaisorblade' Giarrusso | 912ad92 | 2006-07-01 04:36:23 -0700 | [diff] [blame] | 203 | if (ret) { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 204 | printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir " |
| 205 | "failed with err = %d\n", ret); |
Paolo 'Blaisorblade' Giarrusso | 912ad92 | 2006-07-01 04:36:23 -0700 | [diff] [blame] | 206 | } |
| 207 | return ret; |
| 208 | } |
| 209 | |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 210 | static void __init create_pid_file(void) |
| 211 | { |
| 212 | char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")]; |
| 213 | char pid[sizeof("nnnnn\0")]; |
| 214 | int fd, n; |
| 215 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 216 | if (umid_file_name("pid", file, sizeof(file))) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 217 | return; |
| 218 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 219 | fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 220 | if (fd < 0) { |
| 221 | printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: " |
| 222 | "%s\n", file, strerror(errno)); |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 223 | return; |
| 224 | } |
| 225 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 226 | snprintf(pid, sizeof(pid), "%d\n", getpid()); |
| 227 | n = write(fd, pid, strlen(pid)); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 228 | if (n != strlen(pid)) |
| 229 | printk(UM_KERN_ERR "Write of pid file failed - err = %d\n", |
| 230 | errno); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 231 | |
| 232 | close(fd); |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 235 | int __init set_umid(char *name) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 236 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 237 | if (strlen(name) > UMID_LEN - 1) |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 238 | return -E2BIG; |
| 239 | |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 240 | strlcpy(umid, name, sizeof(umid)); |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
Jeff Dike | de5fe76 | 2007-02-10 01:44:25 -0800 | [diff] [blame] | 245 | /* Changed in make_umid, which is called during early boot */ |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 246 | static int umid_setup = 0; |
| 247 | |
WANG Cong | 99764fa | 2008-07-23 21:28:49 -0700 | [diff] [blame] | 248 | static int __init make_umid(void) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 249 | { |
| 250 | int fd, err; |
| 251 | char tmp[256]; |
| 252 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 253 | if (umid_setup) |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 254 | return 0; |
| 255 | |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 256 | make_uml_dir(); |
| 257 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 258 | if (*umid == '\0') { |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 259 | strlcpy(tmp, uml_dir, sizeof(tmp)); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 260 | strlcat(tmp, "XXXXXX", sizeof(tmp)); |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 261 | fd = mkstemp(tmp); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 262 | if (fd < 0) { |
| 263 | printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: " |
| 264 | "%s\n", tmp, strerror(errno)); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 265 | err = -errno; |
| 266 | goto err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 267 | } |
| 268 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 269 | close(fd); |
| 270 | |
| 271 | set_umid(&tmp[strlen(uml_dir)]); |
| 272 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 273 | /* |
| 274 | * There's a nice tiny little race between this unlink and |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 275 | * the mkdir below. It'd be nice if there were a mkstemp |
| 276 | * for directories. |
| 277 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 278 | if (unlink(tmp)) { |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 279 | err = -errno; |
| 280 | goto err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 281 | } |
| 282 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 283 | |
| 284 | snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid); |
| 285 | err = mkdir(tmp, 0777); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 286 | if (err < 0) { |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 287 | err = -errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 288 | if (err != -EEXIST) |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 289 | goto err; |
| 290 | |
Paolo 'Blaisorblade' Giarrusso | 912ad92 | 2006-07-01 04:36:23 -0700 | [diff] [blame] | 291 | if (umdir_take_if_dead(tmp) < 0) |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 292 | goto err; |
| 293 | |
| 294 | err = mkdir(tmp, 0777); |
| 295 | } |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 296 | if (err) { |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 297 | err = -errno; |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 298 | printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid, |
| 299 | errno); |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 300 | goto err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | umid_setup = 1; |
| 304 | |
| 305 | create_pid_file(); |
| 306 | |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 307 | err = 0; |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 308 | err: |
| 309 | return err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | static int __init make_umid_init(void) |
| 313 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 314 | if (!make_umid()) |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 315 | return 0; |
| 316 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 317 | /* |
| 318 | * If initializing with the given umid failed, then try again with |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 319 | * a random one. |
| 320 | */ |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 321 | printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a " |
| 322 | "random umid\n", umid); |
Jeff Dike | 1fbbd684 | 2006-03-27 01:14:39 -0800 | [diff] [blame] | 323 | *umid = '\0'; |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 324 | make_umid(); |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 325 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 326 | return 0; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | __initcall(make_umid_init); |
| 330 | |
| 331 | int __init umid_file_name(char *name, char *buf, int len) |
| 332 | { |
| 333 | int n, err; |
| 334 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 335 | err = make_umid(); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 336 | if (err) |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 337 | return err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 338 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 339 | n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 340 | if (n >= len) { |
| 341 | printk(UM_KERN_ERR "umid_file_name : buffer too short\n"); |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 342 | return -E2BIG; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 343 | } |
| 344 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 345 | return 0; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 346 | } |
| 347 | |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 348 | char *get_umid(void) |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 349 | { |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 350 | return umid; |
| 351 | } |
| 352 | |
| 353 | static int __init set_uml_dir(char *name, int *add) |
| 354 | { |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 355 | if (*name == '\0') { |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 356 | printf("uml_dir can't be an empty string\n"); |
| 357 | return 0; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 358 | } |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 359 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 360 | if (name[strlen(name) - 1] == '/') { |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 361 | uml_dir = name; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | uml_dir = malloc(strlen(name) + 2); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 366 | if (uml_dir == NULL) { |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 367 | printf("Failed to malloc uml_dir - error = %d\n", errno); |
| 368 | |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Return 0 here because do_initcalls doesn't look at |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 371 | * the return value. |
| 372 | */ |
| 373 | return 0; |
| 374 | } |
| 375 | sprintf(uml_dir, "%s/", name); |
| 376 | |
| 377 | return 0; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | __uml_setup("uml_dir=", set_uml_dir, |
| 381 | "uml_dir=<directory>\n" |
| 382 | " The location to place the pid and umid files.\n\n" |
| 383 | ); |
| 384 | |
| 385 | static void remove_umid_dir(void) |
| 386 | { |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 387 | char dir[strlen(uml_dir) + UMID_LEN + 1], err; |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 388 | |
| 389 | sprintf(dir, "%s%s", uml_dir, umid); |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 390 | err = remove_files_and_dir(dir); |
Jeff Dike | ba180fd | 2007-10-16 01:27:00 -0700 | [diff] [blame] | 391 | if (err) |
Paolo 'Blaisorblade' Giarrusso | eb28931 | 2006-07-01 04:36:24 -0700 | [diff] [blame] | 392 | printf("remove_umid_dir - remove_files_and_dir failed with " |
Jeff Dike | 7eebe8a | 2006-01-06 00:19:01 -0800 | [diff] [blame] | 393 | "err = %d\n", err); |
Jeff Dike | 2264c475 | 2006-01-06 00:18:59 -0800 | [diff] [blame] | 394 | } |
| 395 | |
| 396 | __uml_exitcall(remove_umid_dir); |