umid.c 8.1 KB

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