mem.c 5.5 KB

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