mem.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. /*
  138. * This proc still used in tt-mode
  139. * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
  140. * So it isn't 'static' yet.
  141. */
  142. static int __init make_tempfile(const char *template, char **out_tempname,
  143. int do_unlink)
  144. {
  145. char *tempname;
  146. int fd;
  147. which_tmpdir();
  148. tempname = malloc(MAXPATHLEN);
  149. if (!tempname)
  150. goto out;
  151. find_tempdir();
  152. if (template[0] != '/')
  153. strcpy(tempname, tempdir);
  154. else
  155. tempname[0] = '\0';
  156. strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
  157. fd = mkstemp(tempname);
  158. if (fd < 0) {
  159. fprintf(stderr, "open - cannot create %s: %s\n", tempname,
  160. strerror(errno));
  161. goto out;
  162. }
  163. if (do_unlink && (unlink(tempname) < 0)) {
  164. perror("unlink");
  165. goto out;
  166. }
  167. if (out_tempname) {
  168. *out_tempname = tempname;
  169. } else {
  170. free(tempname);
  171. }
  172. return fd;
  173. out:
  174. free(tempname);
  175. return -1;
  176. }
  177. #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
  178. static int __init create_tmp_file(unsigned long long len)
  179. {
  180. int fd, err;
  181. char zero;
  182. fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
  183. if (fd < 0)
  184. exit(1);
  185. err = fchmod(fd, 0777);
  186. if (err < 0) {
  187. perror("fchmod");
  188. exit(1);
  189. }
  190. /*
  191. * Seek to len - 1 because writing a character there will
  192. * increase the file size by one byte, to the desired length.
  193. */
  194. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  195. perror("lseek64");
  196. exit(1);
  197. }
  198. zero = 0;
  199. err = write(fd, &zero, 1);
  200. if (err != 1) {
  201. perror("write");
  202. exit(1);
  203. }
  204. return fd;
  205. }
  206. int __init create_mem_file(unsigned long long len)
  207. {
  208. int err, fd;
  209. fd = create_tmp_file(len);
  210. err = os_set_exec_close(fd);
  211. if (err < 0) {
  212. errno = -err;
  213. perror("exec_close");
  214. }
  215. return fd;
  216. }
  217. void __init check_tmpexec(void)
  218. {
  219. void *addr;
  220. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  221. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  222. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  223. printf("Checking PROT_EXEC mmap in %s...",tempdir);
  224. fflush(stdout);
  225. if (addr == MAP_FAILED) {
  226. err = errno;
  227. perror("failed");
  228. close(fd);
  229. if (err == EPERM)
  230. printf("%s must be not mounted noexec\n",tempdir);
  231. exit(1);
  232. }
  233. printf("OK\n");
  234. munmap(addr, UM_KERN_PAGE_SIZE);
  235. close(fd);
  236. }