mem.c 5.5 KB

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