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