umid.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. 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. static int actually_do_remove(char *dir)
  60. {
  61. DIR *directory;
  62. struct dirent *ent;
  63. int len;
  64. char file[256];
  65. directory = opendir(dir);
  66. if(directory == NULL)
  67. return -errno;
  68. while((ent = readdir(directory)) != NULL){
  69. if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, ".."))
  70. continue;
  71. len = strlen(dir) + sizeof("/") + strlen(ent->d_name) + 1;
  72. if(len > sizeof(file))
  73. return -E2BIG;
  74. sprintf(file, "%s/%s", dir, ent->d_name);
  75. if(unlink(file) < 0)
  76. return -errno;
  77. }
  78. if(rmdir(dir) < 0)
  79. return -errno;
  80. return 0;
  81. }
  82. /* This says that there isn't already a user of the specified directory even if
  83. * there are errors during the checking. This is because if these errors
  84. * happen, the directory is unusable by the pre-existing UML, so we might as
  85. * well take it over. This could happen either by
  86. * the existing UML somehow corrupting its umid directory
  87. * something other than UML sticking stuff in the directory
  88. * this boot racing with a shutdown of the other UML
  89. * In any of these cases, the directory isn't useful for anything else.
  90. */
  91. static int not_dead_yet(char *dir)
  92. {
  93. char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  94. char pid[sizeof("nnnnn\0")], *end;
  95. int dead, fd, p, n, err;
  96. n = snprintf(file, sizeof(file), "%s/pid", dir);
  97. if(n >= sizeof(file)){
  98. printk("not_dead_yet - pid filename too long\n");
  99. err = -E2BIG;
  100. goto out;
  101. }
  102. dead = 0;
  103. fd = open(file, O_RDONLY);
  104. if(fd < 0){
  105. if(fd != -ENOENT){
  106. printk("not_dead_yet : couldn't open pid file '%s', "
  107. "err = %d\n", file, -fd);
  108. }
  109. goto out;
  110. }
  111. err = 0;
  112. n = read(fd, pid, sizeof(pid));
  113. if(n <= 0){
  114. printk("not_dead_yet : couldn't read pid file '%s', "
  115. "err = %d\n", file, -n);
  116. goto out_close;
  117. }
  118. p = strtoul(pid, &end, 0);
  119. if(end == pid){
  120. printk("not_dead_yet : couldn't parse pid file '%s', "
  121. "errno = %d\n", file, errno);
  122. goto out_close;
  123. }
  124. if((kill(p, 0) == 0) || (errno != ESRCH)){
  125. printk("umid \"%s\" is already in use by pid %d\n", umid, p);
  126. return 1;
  127. }
  128. err = actually_do_remove(dir);
  129. if(err)
  130. printk("not_dead_yet - actually_do_remove failed with "
  131. "err = %d\n", err);
  132. return err;
  133. out_close:
  134. close(fd);
  135. out:
  136. return 0;
  137. }
  138. static void __init create_pid_file(void)
  139. {
  140. char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  141. char pid[sizeof("nnnnn\0")];
  142. int fd, n;
  143. if(umid_file_name("pid", file, sizeof(file)))
  144. return;
  145. fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
  146. if(fd < 0){
  147. printk("Open of machine pid file \"%s\" failed: %s\n",
  148. file, strerror(-fd));
  149. return;
  150. }
  151. snprintf(pid, sizeof(pid), "%d\n", getpid());
  152. n = write(fd, pid, strlen(pid));
  153. if(n != strlen(pid))
  154. printk("Write of pid file failed - err = %d\n", -n);
  155. close(fd);
  156. }
  157. int __init set_umid(char *name)
  158. {
  159. if(strlen(name) > UMID_LEN - 1)
  160. return -E2BIG;
  161. strlcpy(umid, name, sizeof(umid));
  162. return 0;
  163. }
  164. static int umid_setup = 0;
  165. int __init make_umid(void)
  166. {
  167. int fd, err;
  168. char tmp[256];
  169. if(umid_setup)
  170. return 0;
  171. make_uml_dir();
  172. if(*umid == '\0'){
  173. strlcpy(tmp, uml_dir, sizeof(tmp));
  174. strlcat(tmp, "XXXXXX", sizeof(tmp));
  175. fd = mkstemp(tmp);
  176. if(fd < 0){
  177. printk("make_umid - mkstemp(%s) failed: %s\n",
  178. tmp, strerror(errno));
  179. err = -errno;
  180. goto err;
  181. }
  182. close(fd);
  183. set_umid(&tmp[strlen(uml_dir)]);
  184. /* There's a nice tiny little race between this unlink and
  185. * the mkdir below. It'd be nice if there were a mkstemp
  186. * for directories.
  187. */
  188. if(unlink(tmp)){
  189. err = -errno;
  190. goto err;
  191. }
  192. }
  193. snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
  194. err = mkdir(tmp, 0777);
  195. if(err < 0){
  196. err = -errno;
  197. if(err != -EEXIST)
  198. goto err;
  199. /* 1 -> this umid is already in use
  200. * < 0 -> we couldn't remove the umid directory
  201. * In either case, we can't use this umid, so return -EEXIST.
  202. */
  203. if(not_dead_yet(tmp) != 0)
  204. goto err;
  205. err = mkdir(tmp, 0777);
  206. }
  207. if(err){
  208. err = -errno;
  209. printk("Failed to create '%s' - err = %d\n", umid, -errno);
  210. goto err;
  211. }
  212. umid_setup = 1;
  213. create_pid_file();
  214. err = 0;
  215. err:
  216. return err;
  217. }
  218. static int __init make_umid_init(void)
  219. {
  220. if(!make_umid())
  221. return 0;
  222. /* If initializing with the given umid failed, then try again with
  223. * a random one.
  224. */
  225. printk("Failed to initialize umid \"%s\", trying with a random umid\n",
  226. umid);
  227. *umid = '\0';
  228. make_umid();
  229. return 0;
  230. }
  231. __initcall(make_umid_init);
  232. int __init umid_file_name(char *name, char *buf, int len)
  233. {
  234. int n, err;
  235. err = make_umid();
  236. if(err)
  237. return err;
  238. n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
  239. if(n >= len){
  240. printk("umid_file_name : buffer too short\n");
  241. return -E2BIG;
  242. }
  243. return 0;
  244. }
  245. char *get_umid(void)
  246. {
  247. return umid;
  248. }
  249. static int __init set_uml_dir(char *name, int *add)
  250. {
  251. if(*name == '\0'){
  252. printf("uml_dir can't be an empty string\n");
  253. return 0;
  254. }
  255. if(name[strlen(name) - 1] == '/'){
  256. uml_dir = name;
  257. return 0;
  258. }
  259. uml_dir = malloc(strlen(name) + 2);
  260. if(uml_dir == NULL){
  261. printf("Failed to malloc uml_dir - error = %d\n", errno);
  262. /* Return 0 here because do_initcalls doesn't look at
  263. * the return value.
  264. */
  265. return 0;
  266. }
  267. sprintf(uml_dir, "%s/", name);
  268. return 0;
  269. }
  270. __uml_setup("uml_dir=", set_uml_dir,
  271. "uml_dir=<directory>\n"
  272. " The location to place the pid and umid files.\n\n"
  273. );
  274. static void remove_umid_dir(void)
  275. {
  276. char dir[strlen(uml_dir) + UMID_LEN + 1], err;
  277. sprintf(dir, "%s%s", uml_dir, umid);
  278. err = actually_do_remove(dir);
  279. if(err)
  280. printf("remove_umid_dir - actually_do_remove failed with "
  281. "err = %d\n", err);
  282. }
  283. __uml_exitcall(remove_umid_dir);