umid.c 7.7 KB

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