1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
18 #define UML_DIR "~/.uml/"
22 /* Changed by set_umid, which is run early in boot */
23 static char umid[UMID_LEN] = { 0 };
25 /* Changed by set_uml_dir and make_uml_dir, which are run early in boot */
26 static char *uml_dir = UML_DIR;
28 static int __init make_uml_dir(void)
30 char dir[512] = { '\0' };
33 if (*uml_dir == '~') {
34 char *home = getenv("HOME");
39 "%s: no value in environment for $HOME\n",
43 strlcpy(dir, home, sizeof(dir));
46 strlcat(dir, uml_dir, sizeof(dir));
48 if (len > 0 && dir[len - 1] != '/')
49 strlcat(dir, "/", sizeof(dir));
52 uml_dir = malloc(strlen(dir) + 1);
53 if (uml_dir == NULL) {
54 printk(UM_KERN_ERR "%s : malloc failed, errno = %d\n",
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));
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).
81 static int remove_files_and_dir(char *dir)
89 directory = opendir(dir);
90 if (directory == NULL) {
97 while ((ent = readdir(directory)) != NULL) {
98 if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
100 len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
101 if (len > sizeof(file)) {
106 sprintf(file, "%s/%s", dir, ent->d_name);
107 if (unlink(file) < 0 && errno != ENOENT) {
113 if (rmdir(dir) < 0 && errno != ENOENT) {
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.
134 * Boolean return: 1 if in use, 0 otherwise.
136 static inline int is_umdir_used(char *dir)
138 char pid[sizeof("nnnnn\0")], *end, *file;
139 int dead, fd, p, n, err;
142 err = asprintf(&file, "%s/pid", dir);
146 filelen = strlen(file);
148 n = snprintf(file, filelen, "%s/pid", dir);
150 printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n");
156 fd = open(file, O_RDONLY);
160 printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
161 "file '%s', err = %d\n", file, -fd);
167 n = read(fd, pid, sizeof(pid));
169 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
170 "'%s', err = %d\n", file, errno);
173 printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
174 "'%s', 0-byte read\n", file);
178 p = strtoul(pid, &end, 0);
180 printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
181 "'%s', errno = %d\n", file, errno);
185 if ((kill(p, 0) == 0) || (errno != ESRCH)) {
186 printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
199 * Try to remove the directory @dir unless it's in use.
200 * Precondition: @dir exists.
201 * Returns 0 for success, < 0 for failure in removal or if the directory is in
204 static int umdir_take_if_dead(char *dir)
207 if (is_umdir_used(dir))
210 ret = remove_files_and_dir(dir);
212 printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
213 "failed with err = %d\n", ret);
218 static void __init create_pid_file(void)
220 char pid[sizeof("nnnnn\0")], *file;
223 n = strlen(uml_dir) + UMID_LEN + sizeof("/pid\0");
228 if (umid_file_name("pid", file, n))
231 fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
233 printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
234 "%s\n", file, strerror(errno));
238 snprintf(pid, sizeof(pid), "%d\n", getpid());
239 n = write(fd, pid, strlen(pid));
240 if (n != strlen(pid))
241 printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
249 int __init set_umid(char *name)
251 if (strlen(name) > UMID_LEN - 1)
254 strlcpy(umid, name, sizeof(umid));
259 /* Changed in make_umid, which is called during early boot */
260 static int umid_setup = 0;
262 static int __init make_umid(void)
273 strlcpy(tmp, uml_dir, sizeof(tmp));
274 strlcat(tmp, "XXXXXX", sizeof(tmp));
277 printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
278 "%s\n", tmp, strerror(errno));
285 set_umid(&tmp[strlen(uml_dir)]);
288 * There's a nice tiny little race between this unlink and
289 * the mkdir below. It'd be nice if there were a mkstemp
298 snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
299 err = mkdir(tmp, 0777);
305 if (umdir_take_if_dead(tmp) < 0)
308 err = mkdir(tmp, 0777);
312 printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
326 static int __init make_umid_init(void)
332 * If initializing with the given umid failed, then try again with
335 printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
336 "random umid\n", umid);
343 __initcall(make_umid_init);
345 int __init umid_file_name(char *name, char *buf, int len)
353 n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
355 printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
367 static int __init set_uml_dir(char *name, int *add)
370 os_warn("uml_dir can't be an empty string\n");
374 if (name[strlen(name) - 1] == '/') {
379 uml_dir = malloc(strlen(name) + 2);
380 if (uml_dir == NULL) {
381 os_warn("Failed to malloc uml_dir - error = %d\n", errno);
384 * Return 0 here because do_initcalls doesn't look at
389 sprintf(uml_dir, "%s/", name);
394 __uml_setup("uml_dir=", set_uml_dir,
395 "uml_dir=<directory>\n"
396 " The location to place the pid and umid files.\n\n"
399 static void remove_umid_dir(void)
403 dir = malloc(strlen(uml_dir) + UMID_LEN + 1);
407 sprintf(dir, "%s%s", uml_dir, umid);
408 err = remove_files_and_dir(dir);
410 os_warn("%s - remove_files_and_dir failed with err = %d\n",
416 __uml_exitcall(remove_umid_dir);