mem.c 5.3 KB

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