mem.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 "mem_user.h"
  15. #include "init.h"
  16. #include "os.h"
  17. #include "tempfile.h"
  18. #include "kern_constants.h"
  19. #include <sys/param.h>
  20. /* Modified by which_tmpdir, which is called during early boot */
  21. static char *default_tmpdir = "/tmp";
  22. /*
  23. * Modified when creating the physical memory file and when checking
  24. * the tmp filesystem for usability, both happening during early boot.
  25. */
  26. static char *tempdir = NULL;
  27. static void __init find_tempdir(void)
  28. {
  29. char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
  30. int i;
  31. char *dir = NULL;
  32. if(tempdir != NULL) /* 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. /* 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, int size, char c)
  56. {
  57. int n, 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. 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 __init 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 = write(fd, &zero, 1);
  199. if(err != 1){
  200. perror("write");
  201. exit(1);
  202. }
  203. return fd;
  204. }
  205. int __init create_mem_file(unsigned long long len)
  206. {
  207. int err, fd;
  208. fd = create_tmp_file(len);
  209. err = os_set_exec_close(fd, 1);
  210. if(err < 0){
  211. errno = -err;
  212. perror("exec_close");
  213. }
  214. return fd;
  215. }
  216. void __init check_tmpexec(void)
  217. {
  218. void *addr;
  219. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  220. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  221. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  222. printf("Checking PROT_EXEC mmap in %s...",tempdir);
  223. fflush(stdout);
  224. if(addr == MAP_FAILED){
  225. err = errno;
  226. perror("failed");
  227. if(err == EPERM)
  228. printf("%s must be not mounted noexec\n",tempdir);
  229. exit(1);
  230. }
  231. printf("OK\n");
  232. munmap(addr, UM_KERN_PAGE_SIZE);
  233. close(fd);
  234. }