umid.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. return 1;
  126. err = actually_do_remove(dir);
  127. if(err)
  128. printk("not_dead_yet - actually_do_remove failed with "
  129. "err = %d\n", err);
  130. return err;
  131. out_close:
  132. close(fd);
  133. out:
  134. return 0;
  135. }
  136. static void __init create_pid_file(void)
  137. {
  138. char file[strlen(uml_dir) + UMID_LEN + sizeof("/pid\0")];
  139. char pid[sizeof("nnnnn\0")];
  140. int fd, n;
  141. if(umid_file_name("pid", file, sizeof(file)))
  142. return;
  143. fd = open(file, O_RDWR | O_CREAT | O_EXCL, 0644);
  144. if(fd < 0){
  145. printk("Open of machine pid file \"%s\" failed: %s\n",
  146. file, strerror(-fd));
  147. return;
  148. }
  149. snprintf(pid, sizeof(pid), "%d\n", getpid());
  150. n = write(fd, pid, strlen(pid));
  151. if(n != strlen(pid))
  152. printk("Write of pid file failed - err = %d\n", -n);
  153. close(fd);
  154. }
  155. int __init set_umid(char *name)
  156. {
  157. if(strlen(name) > UMID_LEN - 1)
  158. return -E2BIG;
  159. strlcpy(umid, name, sizeof(umid));
  160. return 0;
  161. }
  162. static int umid_setup = 0;
  163. int __init make_umid(void)
  164. {
  165. int fd, err;
  166. char tmp[256];
  167. if(umid_setup)
  168. return 0;
  169. make_uml_dir();
  170. if(*umid == '\0'){
  171. strlcpy(tmp, uml_dir, sizeof(tmp));
  172. strlcat(tmp, "XXXXXX", sizeof(tmp));
  173. fd = mkstemp(tmp);
  174. if(fd < 0){
  175. printk("make_umid - mkstemp(%s) failed: %s\n",
  176. tmp, strerror(errno));
  177. err = -errno;
  178. goto err;
  179. }
  180. close(fd);
  181. set_umid(&tmp[strlen(uml_dir)]);
  182. /* There's a nice tiny little race between this unlink and
  183. * the mkdir below. It'd be nice if there were a mkstemp
  184. * for directories.
  185. */
  186. if(unlink(tmp)){
  187. err = -errno;
  188. goto err;
  189. }
  190. }
  191. snprintf(tmp, sizeof(tmp), "%s%s", uml_dir, umid);
  192. err = mkdir(tmp, 0777);
  193. if(err < 0){
  194. err = -errno;
  195. if(errno != EEXIST)
  196. goto err;
  197. if(not_dead_yet(tmp) < 0)
  198. goto err;
  199. err = mkdir(tmp, 0777);
  200. }
  201. if(err < 0){
  202. printk("Failed to create '%s' - err = %d\n", umid, err);
  203. goto err_rmdir;
  204. }
  205. umid_setup = 1;
  206. create_pid_file();
  207. return 0;
  208. err_rmdir:
  209. rmdir(tmp);
  210. err:
  211. return err;
  212. }
  213. static int __init make_umid_init(void)
  214. {
  215. make_umid();
  216. return 0;
  217. }
  218. __initcall(make_umid_init);
  219. int __init umid_file_name(char *name, char *buf, int len)
  220. {
  221. int n, err;
  222. err = make_umid();
  223. if(err)
  224. return err;
  225. n = snprintf(buf, len, "%s%s/%s", uml_dir, umid, name);
  226. if(n >= len){
  227. printk("umid_file_name : buffer too short\n");
  228. return -E2BIG;
  229. }
  230. return 0;
  231. }
  232. char *get_umid(void)
  233. {
  234. return umid;
  235. }
  236. static int __init set_uml_dir(char *name, int *add)
  237. {
  238. if(*name == '\0'){
  239. printf("uml_dir can't be an empty string\n");
  240. return 0;
  241. }
  242. if(name[strlen(name) - 1] == '/'){
  243. uml_dir = name;
  244. return 0;
  245. }
  246. uml_dir = malloc(strlen(name) + 2);
  247. if(uml_dir == NULL){
  248. printf("Failed to malloc uml_dir - error = %d\n", errno);
  249. /* Return 0 here because do_initcalls doesn't look at
  250. * the return value.
  251. */
  252. return 0;
  253. }
  254. sprintf(uml_dir, "%s/", name);
  255. return 0;
  256. }
  257. __uml_setup("uml_dir=", set_uml_dir,
  258. "uml_dir=<directory>\n"
  259. " The location to place the pid and umid files.\n\n"
  260. );
  261. static void remove_umid_dir(void)
  262. {
  263. char dir[strlen(uml_dir) + UMID_LEN + 1], err;
  264. sprintf(dir, "%s%s", uml_dir, umid);
  265. err = actually_do_remove(dir);
  266. if(err)
  267. printf("remove_umid_dir - actually_do_remove failed with "
  268. "err = %d\n", err);
  269. }
  270. __uml_exitcall(remove_umid_dir);