umid.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. fd = -errno;
  106. if(fd != -ENOENT){
  107. printk("not_dead_yet : couldn't open pid file '%s', "
  108. "err = %d\n", file, -fd);
  109. }
  110. goto out;
  111. }
  112. err = 0;
  113. n = read(fd, pid, sizeof(pid));
  114. if(n < 0){
  115. printk("not_dead_yet : couldn't read pid file '%s', "
  116. "err = %d\n", file, errno);
  117. goto out_close;
  118. } else if(n == 0){
  119. printk("not_dead_yet : couldn't read pid file '%s', "
  120. "0-byte read\n", file);
  121. goto out_close;
  122. }
  123. p = strtoul(pid, &end, 0);
  124. if(end == pid){
  125. printk("not_dead_yet : couldn't parse pid file '%s', "
  126. "errno = %d\n", file, errno);
  127. goto out_close;
  128. }
  129. if((kill(p, 0) == 0) || (errno != ESRCH)){
  130. printk("umid \"%s\" is already in use by pid %d\n", umid, p);
  131. return 1;
  132. }
  133. err = actually_do_remove(dir);
  134. if(err)
  135. printk("not_dead_yet - actually_do_remove failed with "
  136. "err = %d\n", err);
  137. return err;
  138. out_close:
  139. close(fd);
  140. out:
  141. return 0;
  142. }
  143. static void __init create_pid_file(void)
  144. {
  145. char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  146. char pid[sizeof("nnnnn\0")];
  147. int fd, n;
  148. if(umid_file_name("pid", file, sizeof(file)))
  149. return;
  150. fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
  151. if(fd < 0){
  152. printk("Open of machine pid file \"%s\" failed: %s\n",
  153. file, strerror(-fd));
  154. return;
  155. }
  156. snprintf(pid, sizeof(pid), "%d\n", getpid());
  157. n = write(fd, pid, strlen(pid));
  158. if(n != strlen(pid))
  159. printk("Write of pid file failed - err = %d\n", -n);
  160. close(fd);
  161. }
  162. int __init set_umid(char *name)
  163. {
  164. if(strlen(name) > UMID_LEN - 1)
  165. return -E2BIG;
  166. strlcpy(umid, name, sizeof(umid));
  167. return 0;
  168. }
  169. static int umid_setup = 0;
  170. int __init make_umid(void)
  171. {
  172. int fd, err;
  173. char tmp[256];
  174. if(umid_setup)
  175. return 0;
  176. make_uml_dir();
  177. if(*umid == '\0'){
  178. strlcpy(tmp, uml_dir, sizeof(tmp));
  179. strlcat(tmp, "XXXXXX", sizeof(tmp));
  180. fd = mkstemp(tmp);
  181. if(fd < 0){
  182. printk("make_umid - mkstemp(%s) failed: %s\n",
  183. tmp, strerror(errno));
  184. err = -errno;
  185. goto err;
  186. }
  187. close(fd);
  188. set_umid(&tmp[strlen(uml_dir)]);
  189. /* There's a nice tiny little race between this unlink and
  190. * the mkdir below. It'd be nice if there were a mkstemp
  191. * for directories.
  192. */
  193. if(unlink(tmp)){
  194. err = -errno;
  195. goto err;
  196. }
  197. }
  198. snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
  199. err = mkdir(tmp, 0777);
  200. if(err < 0){
  201. err = -errno;
  202. if(err != -EEXIST)
  203. goto err;
  204. /* 1 -> this umid is already in use
  205. * < 0 -> we couldn't remove the umid directory
  206. * In either case, we can't use this umid, so return -EEXIST.
  207. */
  208. if(not_dead_yet(tmp) != 0)
  209. goto err;
  210. err = mkdir(tmp, 0777);
  211. }
  212. if(err){
  213. err = -errno;
  214. printk("Failed to create '%s' - err = %d\n", umid, -errno);
  215. goto err;
  216. }
  217. umid_setup = 1;
  218. create_pid_file();
  219. err = 0;
  220. err:
  221. return err;
  222. }
  223. static int __init make_umid_init(void)
  224. {
  225. if(!make_umid())
  226. return 0;
  227. /* If initializing with the given umid failed, then try again with
  228. * a random one.
  229. */
  230. printk("Failed to initialize umid \"%s\", trying with a random umid\n",
  231. umid);
  232. *umid = '\0';
  233. make_umid();
  234. return 0;
  235. }
  236. __initcall(make_umid_init);
  237. int __init umid_file_name(char *name, char *buf, int len)
  238. {
  239. int n, err;
  240. err = make_umid();
  241. if(err)
  242. return err;
  243. n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
  244. if(n >= len){
  245. printk("umid_file_name : buffer too short\n");
  246. return -E2BIG;
  247. }
  248. return 0;
  249. }
  250. char *get_umid(void)
  251. {
  252. return umid;
  253. }
  254. static int __init set_uml_dir(char *name, int *add)
  255. {
  256. if(*name == '\0'){
  257. printf("uml_dir can't be an empty string\n");
  258. return 0;
  259. }
  260. if(name[strlen(name) - 1] == '/'){
  261. uml_dir = name;
  262. return 0;
  263. }
  264. uml_dir = malloc(strlen(name) + 2);
  265. if(uml_dir == NULL){
  266. printf("Failed to malloc uml_dir - error = %d\n", errno);
  267. /* Return 0 here because do_initcalls doesn't look at
  268. * the return value.
  269. */
  270. return 0;
  271. }
  272. sprintf(uml_dir, "%s/", name);
  273. return 0;
  274. }
  275. __uml_setup("uml_dir=", set_uml_dir,
  276. "uml_dir=<directory>\n"
  277. " The location to place the pid and umid files.\n\n"
  278. );
  279. static void remove_umid_dir(void)
  280. {
  281. char dir[strlen(uml_dir) + UMID_LEN + 1], err;
  282. sprintf(dir, "%s%s", uml_dir, umid);
  283. err = actually_do_remove(dir);
  284. if(err)
  285. printf("remove_umid_dir - actually_do_remove failed with "
  286. "err = %d\n", err);
  287. }
  288. __uml_exitcall(remove_umid_dir);