mem.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. */
  5. #include <stdio.h>
  6. #include <stddef.h>
  7. #include <stdlib.h>
  8. #include <unistd.h>
  9. #include <errno.h>
  10. #include <fcntl.h>
  11. #include <string.h>
  12. #include <sys/mman.h>
  13. #include <sys/param.h>
  14. #include "init.h"
  15. #include "kern_constants.h"
  16. #include "os.h"
  17. #include "user.h"
  18. /* Modified by which_tmpdir, which is called during early boot */
  19. static char *default_tmpdir = "/tmp";
  20. /*
  21. * Modified when creating the physical memory file and when checking
  22. * the tmp filesystem for usability, both happening during early boot.
  23. */
  24. static char *tempdir = NULL;
  25. static void __init find_tempdir(void)
  26. {
  27. const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
  28. int i;
  29. char *dir = NULL;
  30. if (tempdir != NULL)
  31. /* We've already been called */
  32. return;
  33. for (i = 0; dirs[i]; i++) {
  34. dir = getenv(dirs[i]);
  35. if ((dir != NULL) && (*dir != '\0'))
  36. break;
  37. }
  38. if ((dir == NULL) || (*dir == '\0'))
  39. dir = default_tmpdir;
  40. tempdir = malloc(strlen(dir) + 2);
  41. if (tempdir == NULL) {
  42. fprintf(stderr, "Failed to malloc tempdir, "
  43. "errno = %d\n", errno);
  44. return;
  45. }
  46. strcpy(tempdir, dir);
  47. strcat(tempdir, "/");
  48. }
  49. /*
  50. * This will return 1, with the first character in buf being the
  51. * character following the next instance of c in the file. This will
  52. * read the file as needed. If there's an error, -errno is returned;
  53. * if the end of the file is reached, 0 is returned.
  54. */
  55. static int next(int fd, char *buf, size_t size, char c)
  56. {
  57. ssize_t n;
  58. size_t len;
  59. char *ptr;
  60. while ((ptr = strchr(buf, c)) == NULL) {
  61. n = read(fd, buf, size - 1);
  62. if (n == 0)
  63. return 0;
  64. else if (n < 0)
  65. return -errno;
  66. buf[n] = '\0';
  67. }
  68. ptr++;
  69. len = strlen(ptr);
  70. memmove(buf, ptr, len + 1);
  71. /*
  72. * Refill the buffer so that if there's a partial string that we care
  73. * about, it will be completed, and we can recognize it.
  74. */
  75. n = read(fd, &buf[len], size - len - 1);
  76. if (n < 0)
  77. return -errno;
  78. buf[len + n] = '\0';
  79. return 1;
  80. }
  81. /* which_tmpdir is called only during early boot */
  82. static int checked_tmpdir = 0;
  83. /*
  84. * Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
  85. * way to do this than to parse /proc/mounts. statfs will return the
  86. * same filesystem magic number and fs id for both /dev and /dev/shm
  87. * when they are both tmpfs, so you can't tell if they are different
  88. * filesystems. Also, there seems to be no other way of finding the
  89. * mount point of a filesystem from within it.
  90. *
  91. * If a /dev/shm tmpfs entry is found, then we switch to using it.
  92. * Otherwise, we stay with the default /tmp.
  93. */
  94. static void which_tmpdir(void)
  95. {
  96. int fd, found;
  97. char buf[128] = { '\0' };
  98. if (checked_tmpdir)
  99. return;
  100. checked_tmpdir = 1;
  101. printf("Checking for tmpfs mount on /dev/shm...");
  102. fd = open("/proc/mounts", O_RDONLY);
  103. if (fd < 0) {
  104. printf("failed to open /proc/mounts, errno = %d\n", errno);
  105. return;
  106. }
  107. while (1) {
  108. found = next(fd, buf, ARRAY_SIZE(buf), ' ');
  109. if (found != 1)
  110. break;
  111. if (!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
  112. goto found;
  113. found = next(fd, buf, ARRAY_SIZE(buf), '\n');
  114. if (found != 1)
  115. break;
  116. }
  117. err:
  118. if (found == 0)
  119. printf("nothing mounted on /dev/shm\n");
  120. else if (found < 0)
  121. printf("read returned errno %d\n", -found);
  122. out:
  123. close(fd);
  124. return;
  125. found:
  126. found = next(fd, buf, ARRAY_SIZE(buf), ' ');
  127. if (found != 1)
  128. goto err;
  129. if (strncmp(buf, "tmpfs", strlen("tmpfs"))) {
  130. printf("not tmpfs\n");
  131. goto out;
  132. }
  133. printf("OK\n");
  134. default_tmpdir = "/dev/shm";
  135. goto out;
  136. }
  137. static int __init make_tempfile(const char *template, char **out_tempname,
  138. int do_unlink)
  139. {
  140. char *tempname;
  141. int fd;
  142. which_tmpdir();
  143. tempname = malloc(MAXPATHLEN);
  144. if (tempname == NULL)
  145. return -1;
  146. find_tempdir();
  147. if ((tempdir == NULL) || (strlen(tempdir) >= MAXPATHLEN))
  148. return -1;
  149. if (template[0] != '/')
  150. strcpy(tempname, tempdir);
  151. else
  152. tempname[0] = '\0';
  153. strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
  154. fd = mkstemp(tempname);
  155. if (fd < 0) {
  156. fprintf(stderr, "open - cannot create %s: %s\n", tempname,
  157. strerror(errno));
  158. goto out;
  159. }
  160. if (do_unlink && (unlink(tempname) < 0)) {
  161. perror("unlink");
  162. goto out;
  163. }
  164. if (out_tempname) {
  165. *out_tempname = tempname;
  166. } else
  167. free(tempname);
  168. return fd;
  169. out:
  170. free(tempname);
  171. return -1;
  172. }
  173. #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
  174. static int __init create_tmp_file(unsigned long long len)
  175. {
  176. int fd, err;
  177. char zero;
  178. fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
  179. if (fd < 0)
  180. exit(1);
  181. err = fchmod(fd, 0777);
  182. if (err < 0) {
  183. perror("fchmod");
  184. exit(1);
  185. }
  186. /*
  187. * Seek to len - 1 because writing a character there will
  188. * increase the file size by one byte, to the desired length.
  189. */
  190. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  191. perror("lseek64");
  192. exit(1);
  193. }
  194. zero = 0;
  195. err = write(fd, &zero, 1);
  196. if (err != 1) {
  197. perror("write");
  198. exit(1);
  199. }
  200. return fd;
  201. }
  202. int __init create_mem_file(unsigned long long len)
  203. {
  204. int err, fd;
  205. fd = create_tmp_file(len);
  206. err = os_set_exec_close(fd);
  207. if (err < 0) {
  208. errno = -err;
  209. perror("exec_close");
  210. }
  211. return fd;
  212. }
  213. void __init check_tmpexec(void)
  214. {
  215. void *addr;
  216. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  217. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  218. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  219. printf("Checking PROT_EXEC mmap in %s...",tempdir);
  220. fflush(stdout);
  221. if (addr == MAP_FAILED) {
  222. err = errno;
  223. perror("failed");
  224. close(fd);
  225. if (err == EPERM)
  226. printf("%s must be not mounted noexec\n",tempdir);
  227. exit(1);
  228. }
  229. printf("OK\n");
  230. munmap(addr, UM_KERN_PAGE_SIZE);
  231. close(fd);
  232. }