umid.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * Copyright (C) 2002 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  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) + sizeof("/") + 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 file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  121. char pid[sizeof("nnnnn\0")], *end;
  122. int dead, fd, p, n, err;
  123. n = snprintf(file, sizeof(file), "%s/pid", dir);
  124. if (n >= sizeof(file)) {
  125. printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n");
  126. err = -E2BIG;
  127. goto out;
  128. }
  129. dead = 0;
  130. fd = open(file, O_RDONLY);
  131. if (fd < 0) {
  132. fd = -errno;
  133. if (fd != -ENOENT) {
  134. printk(UM_KERN_ERR "is_umdir_used : couldn't open pid "
  135. "file '%s', err = %d\n", file, -fd);
  136. }
  137. goto out;
  138. }
  139. err = 0;
  140. n = read(fd, pid, sizeof(pid));
  141. if (n < 0) {
  142. printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
  143. "'%s', err = %d\n", file, errno);
  144. goto out_close;
  145. } else if (n == 0) {
  146. printk(UM_KERN_ERR "is_umdir_used : couldn't read pid file "
  147. "'%s', 0-byte read\n", file);
  148. goto out_close;
  149. }
  150. p = strtoul(pid, &end, 0);
  151. if (end == pid) {
  152. printk(UM_KERN_ERR "is_umdir_used : couldn't parse pid file "
  153. "'%s', errno = %d\n", file, errno);
  154. goto out_close;
  155. }
  156. if ((kill(p, 0) == 0) || (errno != ESRCH)) {
  157. printk(UM_KERN_ERR "umid \"%s\" is already in use by pid %d\n",
  158. umid, p);
  159. return 1;
  160. }
  161. out_close:
  162. close(fd);
  163. out:
  164. return 0;
  165. }
  166. /*
  167. * Try to remove the directory @dir unless it's in use.
  168. * Precondition: @dir exists.
  169. * Returns 0 for success, < 0 for failure in removal or if the directory is in
  170. * use.
  171. */
  172. static int umdir_take_if_dead(char *dir)
  173. {
  174. int ret;
  175. if (is_umdir_used(dir))
  176. return -EEXIST;
  177. ret = remove_files_and_dir(dir);
  178. if (ret) {
  179. printk(UM_KERN_ERR "is_umdir_used - remove_files_and_dir "
  180. "failed with err = %d\n", ret);
  181. }
  182. return ret;
  183. }
  184. static void __init create_pid_file(void)
  185. {
  186. char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  187. char pid[sizeof("nnnnn\0")];
  188. int fd, n;
  189. if (umid_file_name("pid", file, sizeof(file)))
  190. return;
  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. return;
  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. }
  204. int __init set_umid(char *name)
  205. {
  206. if (strlen(name) > UMID_LEN - 1)
  207. return -E2BIG;
  208. strlcpy(umid, name, sizeof(umid));
  209. return 0;
  210. }
  211. /* Changed in make_umid, which is called during early boot */
  212. static int umid_setup = 0;
  213. static int __init make_umid(void)
  214. {
  215. int fd, err;
  216. char tmp[256];
  217. if (umid_setup)
  218. return 0;
  219. make_uml_dir();
  220. if (*umid == '\0') {
  221. strlcpy(tmp, uml_dir, sizeof(tmp));
  222. strlcat(tmp, "XXXXXX", sizeof(tmp));
  223. fd = mkstemp(tmp);
  224. if (fd < 0) {
  225. printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
  226. "%s\n", tmp, strerror(errno));
  227. err = -errno;
  228. goto err;
  229. }
  230. close(fd);
  231. set_umid(&tmp[strlen(uml_dir)]);
  232. /*
  233. * There's a nice tiny little race between this unlink and
  234. * the mkdir below. It'd be nice if there were a mkstemp
  235. * for directories.
  236. */
  237. if (unlink(tmp)) {
  238. err = -errno;
  239. goto err;
  240. }
  241. }
  242. snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
  243. err = mkdir(tmp, 0777);
  244. if (err < 0) {
  245. err = -errno;
  246. if (err != -EEXIST)
  247. goto err;
  248. if (umdir_take_if_dead(tmp) < 0)
  249. goto err;
  250. err = mkdir(tmp, 0777);
  251. }
  252. if (err) {
  253. err = -errno;
  254. printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
  255. errno);
  256. goto err;
  257. }
  258. umid_setup = 1;
  259. create_pid_file();
  260. err = 0;
  261. err:
  262. return err;
  263. }
  264. static int __init make_umid_init(void)
  265. {
  266. if (!make_umid())
  267. return 0;
  268. /*
  269. * If initializing with the given umid failed, then try again with
  270. * a random one.
  271. */
  272. printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
  273. "random umid\n", umid);
  274. *umid = '\0';
  275. make_umid();
  276. return 0;
  277. }
  278. __initcall(make_umid_init);
  279. int __init umid_file_name(char *name, char *buf, int len)
  280. {
  281. int n, err;
  282. err = make_umid();
  283. if (err)
  284. return err;
  285. n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
  286. if (n >= len) {
  287. printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
  288. return -E2BIG;
  289. }
  290. return 0;
  291. }
  292. char *get_umid(void)
  293. {
  294. return umid;
  295. }
  296. static int __init set_uml_dir(char *name, int *add)
  297. {
  298. if (*name == '\0') {
  299. os_warn("uml_dir can't be an empty string\n");
  300. return 0;
  301. }
  302. if (name[strlen(name) - 1] == '/') {
  303. uml_dir = name;
  304. return 0;
  305. }
  306. uml_dir = malloc(strlen(name) + 2);
  307. if (uml_dir == NULL) {
  308. os_warn("Failed to malloc uml_dir - error = %d\n", errno);
  309. /*
  310. * Return 0 here because do_initcalls doesn't look at
  311. * the return value.
  312. */
  313. return 0;
  314. }
  315. sprintf(uml_dir, "%s/", name);
  316. return 0;
  317. }
  318. __uml_setup("uml_dir=", set_uml_dir,
  319. "uml_dir=<directory>\n"
  320. " The location to place the pid and umid files.\n\n"
  321. );
  322. static void remove_umid_dir(void)
  323. {
  324. char dir[strlen(uml_dir) + UMID_LEN + 1], err;
  325. sprintf(dir, "%s%s", uml_dir, umid);
  326. err = remove_files_and_dir(dir);
  327. if (err)
  328. os_warn("%s - remove_files_and_dir failed with err = %d\n",
  329. __func__, err);
  330. }
  331. __uml_exitcall(remove_umid_dir);