mem.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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/vfs.h>
  15. #include <linux/magic.h>
  16. #include <init.h>
  17. #include <os.h>
  18. /* Set by make_tempfile() during early boot. */
  19. static char *tempdir = NULL;
  20. /* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
  21. static int __init check_tmpfs(const char *dir)
  22. {
  23. struct statfs st;
  24. printf("Checking if %s is on tmpfs...", dir);
  25. if (statfs(dir, &st) < 0) {
  26. printf("%s\n", strerror(errno));
  27. } else if (st.f_type != TMPFS_MAGIC) {
  28. printf("no\n");
  29. } else {
  30. printf("OK\n");
  31. return 0;
  32. }
  33. return -1;
  34. }
  35. /*
  36. * Choose the tempdir to use. We want something on tmpfs so that our memory is
  37. * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
  38. * environment, we use that even if it's not on tmpfs, but we warn the user.
  39. * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
  40. * then we fall back to /tmp.
  41. */
  42. static char * __init choose_tempdir(void)
  43. {
  44. static const char * const vars[] = {
  45. "TMPDIR",
  46. "TMP",
  47. "TEMP",
  48. NULL
  49. };
  50. static const char fallback_dir[] = "/tmp";
  51. static const char * const tmpfs_dirs[] = {
  52. "/dev/shm",
  53. fallback_dir,
  54. NULL
  55. };
  56. int i;
  57. const char *dir;
  58. printf("Checking environment variables for a tempdir...");
  59. for (i = 0; vars[i]; i++) {
  60. dir = getenv(vars[i]);
  61. if ((dir != NULL) && (*dir != '\0')) {
  62. printf("%s\n", dir);
  63. if (check_tmpfs(dir) >= 0)
  64. goto done;
  65. else
  66. goto warn;
  67. }
  68. }
  69. printf("none found\n");
  70. for (i = 0; tmpfs_dirs[i]; i++) {
  71. dir = tmpfs_dirs[i];
  72. if (check_tmpfs(dir) >= 0)
  73. goto done;
  74. }
  75. dir = fallback_dir;
  76. warn:
  77. printf("Warning: tempdir %s is not on tmpfs\n", dir);
  78. done:
  79. /* Make a copy since getenv results may not remain valid forever. */
  80. return strdup(dir);
  81. }
  82. /*
  83. * Create an unlinked tempfile in a suitable tempdir. template must be the
  84. * basename part of the template with a leading '/'.
  85. */
  86. static int __init make_tempfile(const char *template)
  87. {
  88. char *tempname;
  89. int fd;
  90. if (tempdir == NULL) {
  91. tempdir = choose_tempdir();
  92. if (tempdir == NULL) {
  93. fprintf(stderr, "Failed to choose tempdir: %s\n",
  94. strerror(errno));
  95. return -1;
  96. }
  97. }
  98. tempname = malloc(strlen(tempdir) + strlen(template) + 1);
  99. if (tempname == NULL)
  100. return -1;
  101. strcpy(tempname, tempdir);
  102. strcat(tempname, template);
  103. fd = mkstemp(tempname);
  104. if (fd < 0) {
  105. fprintf(stderr, "open - cannot create %s: %s\n", tempname,
  106. strerror(errno));
  107. goto out;
  108. }
  109. if (unlink(tempname) < 0) {
  110. perror("unlink");
  111. goto close;
  112. }
  113. free(tempname);
  114. return fd;
  115. close:
  116. close(fd);
  117. out:
  118. free(tempname);
  119. return -1;
  120. }
  121. #define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
  122. static int __init create_tmp_file(unsigned long long len)
  123. {
  124. int fd, err;
  125. char zero;
  126. fd = make_tempfile(TEMPNAME_TEMPLATE);
  127. if (fd < 0)
  128. exit(1);
  129. err = fchmod(fd, 0777);
  130. if (err < 0) {
  131. perror("fchmod");
  132. exit(1);
  133. }
  134. /*
  135. * Seek to len - 1 because writing a character there will
  136. * increase the file size by one byte, to the desired length.
  137. */
  138. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  139. perror("lseek64");
  140. exit(1);
  141. }
  142. zero = 0;
  143. err = write(fd, &zero, 1);
  144. if (err != 1) {
  145. perror("write");
  146. exit(1);
  147. }
  148. return fd;
  149. }
  150. int __init create_mem_file(unsigned long long len)
  151. {
  152. int err, fd;
  153. fd = create_tmp_file(len);
  154. err = os_set_exec_close(fd);
  155. if (err < 0) {
  156. errno = -err;
  157. perror("exec_close");
  158. }
  159. return fd;
  160. }
  161. void __init check_tmpexec(void)
  162. {
  163. void *addr;
  164. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  165. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  166. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  167. printf("Checking PROT_EXEC mmap in %s...", tempdir);
  168. if (addr == MAP_FAILED) {
  169. err = errno;
  170. printf("%s\n", strerror(err));
  171. close(fd);
  172. if (err == EPERM)
  173. printf("%s must be not mounted noexec\n", tempdir);
  174. exit(1);
  175. }
  176. printf("OK\n");
  177. munmap(addr, UM_KERN_PAGE_SIZE);
  178. close(fd);
  179. }