umid.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. strscpy(dir, home);
  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;
  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. n = read(fd, pid, sizeof(pid));
  137. if (n < 0) {
  138. printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
  139. "'%s', err = %d\n", file, errno);
  140. goto out_close;
  141. } else if (n == 0) {
  142. printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
  143. "'%s', 0-byte read\n", file);
  144. goto out_close;
  145. }
  146. p = strtoul(pid, &end, 0);
  147. if (end == pid) {
  148. printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
  149. "'%s', errno = %d\n", file, errno);
  150. goto out_close;
  151. }
  152. if ((kill(p, 0) == 0) || (errno != ESRCH)) {
  153. printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
  154. umid, p);
  155. return 1;
  156. }
  157. out_close:
  158. close(fd);
  159. out:
  160. free(file);
  161. return 0;
  162. }
  163. /*
  164. * Try to remove the directory @dir unless it's in use.
  165. * Precondition: @dir exists.
  166. * Returns 0 for success, < 0 for failure in removal or if the directory is in
  167. * use.
  168. */
  169. static int umdir_take_if_dead(char *dir)
  170. {
  171. int ret;
  172. if (is_umdir_used(dir))
  173. return -EEXIST;
  174. ret = remove_files_and_dir(dir);
  175. if (ret) {
  176. printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
  177. "failed with err = %d\n", ret);
  178. }
  179. return ret;
  180. }
  181. static void __init create_pid_file(void)
  182. {
  183. char pid[sizeof("nnnnnnnnn")], *file;
  184. int fd, n;
  185. n = strlen(uml_dir) + UMID_LEN + sizeof("/pid");
  186. file = malloc(n);
  187. if (!file)
  188. return;
  189. if (umid_file_name("pid", file, n))
  190. goto out;
  191. fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
  192. if (fd < 0) {
  193. printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
  194. "%s\n", file, strerror(errno));
  195. goto out;
  196. }
  197. snprintf(pid, sizeof(pid), "%d\n", getpid());
  198. n = write(fd, pid, strlen(pid));
  199. if (n != strlen(pid))
  200. printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
  201. errno);
  202. close(fd);
  203. out:
  204. free(file);
  205. }
  206. int __init set_umid(char *name)
  207. {
  208. if (strlen(name) > UMID_LEN - 1)
  209. return -E2BIG;
  210. strscpy(umid, name);
  211. return 0;
  212. }
  213. /* Changed in make_umid, which is called during early boot */
  214. static int umid_setup = 0;
  215. static int __init make_umid(void)
  216. {
  217. int fd, err;
  218. char tmp[256];
  219. if (umid_setup)
  220. return 0;
  221. make_uml_dir();
  222. if (*umid == '\0') {
  223. strscpy(tmp, uml_dir);
  224. strlcat(tmp, "XXXXXX", sizeof(tmp));
  225. fd = mkstemp(tmp);
  226. if (fd < 0) {
  227. printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
  228. "%s\n", tmp, strerror(errno));
  229. err = -errno;
  230. goto err;
  231. }
  232. close(fd);
  233. set_umid(&tmp[strlen(uml_dir)]);
  234. /*
  235. * There's a nice tiny little race between this unlink and
  236. * the mkdir below. It'd be nice if there were a mkstemp
  237. * for directories.
  238. */
  239. if (unlink(tmp)) {
  240. err = -errno;
  241. goto err;
  242. }
  243. }
  244. snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
  245. err = mkdir(tmp, 0777);
  246. if (err < 0) {
  247. err = -errno;
  248. if (err != -EEXIST)
  249. goto err;
  250. if (umdir_take_if_dead(tmp) < 0)
  251. goto err;
  252. err = mkdir(tmp, 0777);
  253. }
  254. if (err) {
  255. err = -errno;
  256. printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
  257. errno);
  258. goto err;
  259. }
  260. umid_setup = 1;
  261. create_pid_file();
  262. err = 0;
  263. err:
  264. return err;
  265. }
  266. static int __init make_umid_init(void)
  267. {
  268. if (!make_umid())
  269. return 0;
  270. /*
  271. * If initializing with the given umid failed, then try again with
  272. * a random one.
  273. */
  274. printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
  275. "random umid\n", umid);
  276. *umid = '\0';
  277. make_umid();
  278. return 0;
  279. }
  280. __initcall(make_umid_init);
  281. int __init umid_file_name(char *name, char *buf, int len)
  282. {
  283. int n, err;
  284. err = make_umid();
  285. if (err)
  286. return err;
  287. n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
  288. if (n >= len) {
  289. printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
  290. return -E2BIG;
  291. }
  292. return 0;
  293. }
  294. char *get_umid(void)
  295. {
  296. return umid;
  297. }
  298. static int __init set_uml_dir(char *name, int *add)
  299. {
  300. *add = 0;
  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);