mem.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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 "user.h"
  13. #include "mem_user.h"
  14. #include "init.h"
  15. #include "os.h"
  16. #include "tempfile.h"
  17. #include "kern_constants.h"
  18. #include <sys/param.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) /* 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. /* This will return 1, with the first character in buf being the
  50. * character following the next instance of c in the file. This will
  51. * read the file as needed. If there's an error, -errno is returned;
  52. * if the end of the file is reached, 0 is returned.
  53. */
  54. static int next(int fd, char *buf, size_t size, char c)
  55. {
  56. ssize_t n;
  57. size_t len;
  58. char *ptr;
  59. while((ptr = strchr(buf, c)) == NULL){
  60. n = read(fd, buf, size - 1);
  61. if(n == 0)
  62. return 0;
  63. else if(n < 0)
  64. return -errno;
  65. buf[n] = '\0';
  66. }
  67. ptr++;
  68. len = strlen(ptr);
  69. memmove(buf, ptr, len + 1);
  70. /* Refill the buffer so that if there's a partial string that we care
  71. * about, it will be completed, and we can recognize it.
  72. */
  73. n = read(fd, &buf[len], size - len - 1);
  74. if(n < 0)
  75. return -errno;
  76. buf[len + n] = '\0';
  77. return 1;
  78. }
  79. /* which_tmpdir is called only during early boot */
  80. static int checked_tmpdir = 0;
  81. /* Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
  82. * way to do this than to parse /proc/mounts. statfs will return the
  83. * same filesystem magic number and fs id for both /dev and /dev/shm
  84. * when they are both tmpfs, so you can't tell if they are different
  85. * filesystems. Also, there seems to be no other way of finding the
  86. * mount point of a filesystem from within it.
  87. *
  88. * If a /dev/shm tmpfs entry is found, then we switch to using it.
  89. * Otherwise, we stay with the default /tmp.
  90. */
  91. static void which_tmpdir(void)
  92. {
  93. int fd, found;
  94. char buf[128] = { '\0' };
  95. if(checked_tmpdir)
  96. return;
  97. checked_tmpdir = 1;
  98. printf("Checking for tmpfs mount on /dev/shm...");
  99. fd = open("/proc/mounts", O_RDONLY);
  100. if(fd < 0){
  101. printf("failed to open /proc/mounts, errno = %d\n", errno);
  102. return;
  103. }
  104. while(1){
  105. found = next(fd, buf, ARRAY_SIZE(buf), ' ');
  106. if(found != 1)
  107. break;
  108. if(!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
  109. goto found;
  110. found = next(fd, buf, ARRAY_SIZE(buf), '\n');
  111. if(found != 1)
  112. break;
  113. }
  114. err:
  115. if(found == 0)
  116. printf("nothing mounted on /dev/shm\n");
  117. else if(found < 0)
  118. printf("read returned errno %d\n", -found);
  119. out:
  120. close(fd);
  121. return;
  122. found:
  123. found = next(fd, buf, ARRAY_SIZE(buf), ' ');
  124. if(found != 1)
  125. goto err;
  126. if(strncmp(buf, "tmpfs", strlen("tmpfs"))){
  127. printf("not tmpfs\n");
  128. goto out;
  129. }
  130. printf("OK\n");
  131. default_tmpdir = "/dev/shm";
  132. goto out;
  133. }
  134. /*
  135. * This proc still used in tt-mode
  136. * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
  137. * So it isn't 'static' yet.
  138. */
  139. int __init make_tempfile(const char *template, char **out_tempname,
  140. int do_unlink)
  141. {
  142. char *tempname;
  143. int fd;
  144. which_tmpdir();
  145. tempname = malloc(MAXPATHLEN);
  146. if (!tempname)
  147. goto out;
  148. find_tempdir();
  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. }
  169. return fd;
  170. out:
  171. free(tempname);
  172. return -1;
  173. }
  174. #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
  175. /*
  176. * This proc is used in start_up.c
  177. * So it isn't 'static'.
  178. */
  179. int __init create_tmp_file(unsigned long long len)
  180. {
  181. int fd, err;
  182. char zero;
  183. fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
  184. if(fd < 0) {
  185. exit(1);
  186. }
  187. err = fchmod(fd, 0777);
  188. if(err < 0){
  189. perror("fchmod");
  190. exit(1);
  191. }
  192. /* Seek to len - 1 because writing a character there will
  193. * increase the file size by one byte, to the desired length.
  194. */
  195. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  196. perror("lseek64");
  197. exit(1);
  198. }
  199. zero = 0;
  200. err = write(fd, &zero, 1);
  201. if(err != 1){
  202. perror("write");
  203. exit(1);
  204. }
  205. return fd;
  206. }
  207. int __init create_mem_file(unsigned long long len)
  208. {
  209. int err, fd;
  210. fd = create_tmp_file(len);
  211. err = os_set_exec_close(fd);
  212. if(err < 0){
  213. errno = -err;
  214. perror("exec_close");
  215. }
  216. return fd;
  217. }
  218. void __init check_tmpexec(void)
  219. {
  220. void *addr;
  221. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  222. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  223. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  224. printf("Checking PROT_EXEC mmap in %s...",tempdir);
  225. fflush(stdout);
  226. if(addr == MAP_FAILED){
  227. err = errno;
  228. perror("failed");
  229. close(fd);
  230. if(err == EPERM)
  231. printf("%s must be not mounted noexec\n",tempdir);
  232. exit(1);
  233. }
  234. printf("OK\n");
  235. munmap(addr, UM_KERN_PAGE_SIZE);
  236. close(fd);
  237. }