umid.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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 "make_uml_dir : no value in "
  31. "environment for $HOME\n");
  32. goto err;
  33. }
  34. strlcpy(dir, home, sizeof(dir));
  35. uml_dir++;
  36. }
  37. strlcat(dir, uml_dir, sizeof(dir));
  38. len = strlen(dir);
  39. if (len > 0 && dir[len - 1] != '/')
  40. strlcat(dir, "/", sizeof(dir));
  41. err = -ENOMEM;
  42. uml_dir = malloc(strlen(dir) + 1);
  43. if (uml_dir == NULL) {
  44. printf("make_uml_dir : malloc failed, errno = %d\n", errno);
  45. goto err;
  46. }
  47. strcpy(uml_dir, dir);
  48. if ((mkdir(uml_dir, 0777) < 0) && (errno != EEXIST)) {
  49. printf("Failed to mkdir '%s': %s\n", uml_dir, strerror(errno));
  50. err = -errno;
  51. goto err_free;
  52. }
  53. return 0;
  54. err_free:
  55. free(uml_dir);
  56. err:
  57. uml_dir = NULL;
  58. return err;
  59. }
  60. /*
  61. * Unlinks the files contained in @dir and then removes @dir.
  62. * Doesn't handle directory trees, so it's not like rm -rf, but almost such. We
  63. * ignore ENOENT errors for anything (they happen, strangely enough - possibly
  64. * due to races between multiple dying UML threads).
  65. */
  66. static int remove_files_and_dir(char *dir)
  67. {
  68. DIR *directory;
  69. struct dirent *ent;
  70. int len;
  71. char file[256];
  72. int ret;
  73. directory = opendir(dir);
  74. if (directory == NULL) {
  75. if (errno != ENOENT)
  76. return -errno;
  77. else
  78. return 0;
  79. }
  80. while ((ent = readdir(directory)) != NULL) {
  81. if (!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
  82. continue;
  83. len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
  84. if (len > sizeof(file)) {
  85. ret = -E2BIG;
  86. goto out;
  87. }
  88. sprintf(file, "%s/%s", dir, ent->d_name);
  89. if (unlink(file) < 0 && errno != ENOENT) {
  90. ret = -errno;
  91. goto out;
  92. }
  93. }
  94. if (rmdir(dir) < 0 && errno != ENOENT) {
  95. ret = -errno;
  96. goto out;
  97. }
  98. ret = 0;
  99. out:
  100. closedir(directory);
  101. return ret;
  102. }
  103. /*
  104. * This says that there isn't already a user of the specified directory even if
  105. * there are errors during the checking. This is because if these errors
  106. * happen, the directory is unusable by the pre-existing UML, so we might as
  107. * well take it over. This could happen either by
  108. * the existing UML somehow corrupting its umid directory
  109. * something other than UML sticking stuff in the directory
  110. * this boot racing with a shutdown of the other UML
  111. * In any of these cases, the directory isn't useful for anything else.
  112. *
  113. * Boolean return: 1 if in use, 0 otherwise.
  114. */
  115. static inline int is_umdir_used(char *dir)
  116. {
  117. char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  118. char pid[sizeof("nnnnn\0")], *end;
  119. int dead, fd, p, n, err;
  120. n = snprintf(file, sizeof(file), "%s/pid", dir);
  121. if (n >= sizeof(file)) {
  122. printk(UM_KERN_ERR "is_umdir_used - pid filename too long\n");
  123. err = -E2BIG;
  124. goto out;
  125. }
  126. dead = 0;
  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. 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 file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  184. char pid[sizeof("nnnnn\0")];
  185. int fd, n;
  186. if (umid_file_name("pid", file, sizeof(file)))
  187. return;
  188. fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
  189. if (fd < 0) {
  190. printk(UM_KERN_ERR "Open of machine pid file \"%s\" failed: "
  191. "%s\n", file, strerror(errno));
  192. return;
  193. }
  194. snprintf(pid, sizeof(pid), "%d\n", getpid());
  195. n = write(fd, pid, strlen(pid));
  196. if (n != strlen(pid))
  197. printk(UM_KERN_ERR "Write of pid file failed - err = %d\n",
  198. errno);
  199. close(fd);
  200. }
  201. int __init set_umid(char *name)
  202. {
  203. if (strlen(name) > UMID_LEN - 1)
  204. return -E2BIG;
  205. strlcpy(umid, name, sizeof(umid));
  206. return 0;
  207. }
  208. /* Changed in make_umid, which is called during early boot */
  209. static int umid_setup = 0;
  210. static int __init make_umid(void)
  211. {
  212. int fd, err;
  213. char tmp[256];
  214. if (umid_setup)
  215. return 0;
  216. make_uml_dir();
  217. if (*umid == '\0') {
  218. strlcpy(tmp, uml_dir, sizeof(tmp));
  219. strlcat(tmp, "XXXXXX", sizeof(tmp));
  220. fd = mkstemp(tmp);
  221. if (fd < 0) {
  222. printk(UM_KERN_ERR "make_umid - mkstemp(%s) failed: "
  223. "%s\n", tmp, strerror(errno));
  224. err = -errno;
  225. goto err;
  226. }
  227. close(fd);
  228. set_umid(&tmp[strlen(uml_dir)]);
  229. /*
  230. * There's a nice tiny little race between this unlink and
  231. * the mkdir below. It'd be nice if there were a mkstemp
  232. * for directories.
  233. */
  234. if (unlink(tmp)) {
  235. err = -errno;
  236. goto err;
  237. }
  238. }
  239. snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
  240. err = mkdir(tmp, 0777);
  241. if (err < 0) {
  242. err = -errno;
  243. if (err != -EEXIST)
  244. goto err;
  245. if (umdir_take_if_dead(tmp) < 0)
  246. goto err;
  247. err = mkdir(tmp, 0777);
  248. }
  249. if (err) {
  250. err = -errno;
  251. printk(UM_KERN_ERR "Failed to create '%s' - err = %d\n", umid,
  252. errno);
  253. goto err;
  254. }
  255. umid_setup = 1;
  256. create_pid_file();
  257. err = 0;
  258. err:
  259. return err;
  260. }
  261. static int __init make_umid_init(void)
  262. {
  263. if (!make_umid())
  264. return 0;
  265. /*
  266. * If initializing with the given umid failed, then try again with
  267. * a random one.
  268. */
  269. printk(UM_KERN_ERR "Failed to initialize umid \"%s\", trying with a "
  270. "random umid\n", umid);
  271. *umid = '\0';
  272. make_umid();
  273. return 0;
  274. }
  275. __initcall(make_umid_init);
  276. int __init umid_file_name(char *name, char *buf, int len)
  277. {
  278. int n, err;
  279. err = make_umid();
  280. if (err)
  281. return err;
  282. n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
  283. if (n >= len) {
  284. printk(UM_KERN_ERR "umid_file_name : buffer too short\n");
  285. return -E2BIG;
  286. }
  287. return 0;
  288. }
  289. char *get_umid(void)
  290. {
  291. return umid;
  292. }
  293. static int __init set_uml_dir(char *name, int *add)
  294. {
  295. if (*name == '\0') {
  296. printf("uml_dir can't be an empty string\n");
  297. return 0;
  298. }
  299. if (name[strlen(name) - 1] == '/') {
  300. uml_dir = name;
  301. return 0;
  302. }
  303. uml_dir = malloc(strlen(name) + 2);
  304. if (uml_dir == NULL) {
  305. printf("Failed to malloc uml_dir - error = %d\n", errno);
  306. /*
  307. * Return 0 here because do_initcalls doesn't look at
  308. * the return value.
  309. */
  310. return 0;
  311. }
  312. sprintf(uml_dir, "%s/", name);
  313. return 0;
  314. }
  315. __uml_setup("uml_dir=", set_uml_dir,
  316. "uml_dir=<directory>\n"
  317. " The location to place the pid and umid files.\n\n"
  318. );
  319. static void remove_umid_dir(void)
  320. {
  321. char dir[strlen(uml_dir) + UMID_LEN + 1], err;
  322. sprintf(dir, "%s%s", uml_dir, umid);
  323. err = remove_files_and_dir(dir);
  324. if (err)
  325. printf("remove_umid_dir - remove_files_and_dir failed with "
  326. "err = %d\n", err);
  327. }
  328. __uml_exitcall(remove_umid_dir);