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