umid.c 8.1 KB

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