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