mem.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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, sizeof(buf) / sizeof(buf[0]), ' ');
  100. if(found != 1)
  101. break;
  102. if(!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
  103. goto found;
  104. found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), '\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. return;
  114. found:
  115. found = next(fd, buf, sizeof(buf) / sizeof(buf[0]), ' ');
  116. if(found != 1)
  117. goto err;
  118. if(strncmp(buf, "tmpfs", strlen("tmpfs"))){
  119. printf("not tmpfs\n");
  120. return;
  121. }
  122. printf("OK\n");
  123. default_tmpdir = "/dev/shm";
  124. }
  125. /*
  126. * This proc still used in tt-mode
  127. * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
  128. * So it isn't 'static' yet.
  129. */
  130. int make_tempfile(const char *template, char **out_tempname, int do_unlink)
  131. {
  132. char *tempname;
  133. int fd;
  134. which_tmpdir();
  135. tempname = malloc(MAXPATHLEN);
  136. find_tempdir();
  137. if (template[0] != '/')
  138. strcpy(tempname, tempdir);
  139. else
  140. tempname[0] = '\0';
  141. strcat(tempname, template);
  142. fd = mkstemp(tempname);
  143. if(fd < 0){
  144. fprintf(stderr, "open - cannot create %s: %s\n", tempname,
  145. strerror(errno));
  146. goto out;
  147. }
  148. if(do_unlink && (unlink(tempname) < 0)){
  149. perror("unlink");
  150. goto out;
  151. }
  152. if(out_tempname){
  153. *out_tempname = tempname;
  154. } else {
  155. free(tempname);
  156. }
  157. return(fd);
  158. out:
  159. free(tempname);
  160. return -1;
  161. }
  162. #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
  163. /*
  164. * This proc is used in start_up.c
  165. * So it isn't 'static'.
  166. */
  167. int create_tmp_file(unsigned long long len)
  168. {
  169. int fd, err;
  170. char zero;
  171. fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
  172. if(fd < 0) {
  173. exit(1);
  174. }
  175. err = fchmod(fd, 0777);
  176. if(err < 0){
  177. perror("os_mode_fd");
  178. exit(1);
  179. }
  180. /* Seek to len - 1 because writing a character there will
  181. * increase the file size by one byte, to the desired length.
  182. */
  183. if (lseek64(fd, len - 1, SEEK_SET) < 0) {
  184. perror("os_seek_file");
  185. exit(1);
  186. }
  187. zero = 0;
  188. err = os_write_file(fd, &zero, 1);
  189. if(err != 1){
  190. errno = -err;
  191. perror("os_write_file");
  192. exit(1);
  193. }
  194. return(fd);
  195. }
  196. int create_mem_file(unsigned long long len)
  197. {
  198. int err, fd;
  199. fd = create_tmp_file(len);
  200. err = os_set_exec_close(fd, 1);
  201. if(err < 0){
  202. errno = -err;
  203. perror("exec_close");
  204. }
  205. return(fd);
  206. }
  207. void check_tmpexec(void)
  208. {
  209. void *addr;
  210. int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
  211. addr = mmap(NULL, UM_KERN_PAGE_SIZE,
  212. PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
  213. printf("Checking PROT_EXEC mmap in %s...",tempdir);
  214. fflush(stdout);
  215. if(addr == MAP_FAILED){
  216. err = errno;
  217. perror("failed");
  218. if(err == EPERM)
  219. printf("%s must be not mounted noexec\n",tempdir);
  220. exit(1);
  221. }
  222. printf("OK\n");
  223. munmap(addr, UM_KERN_PAGE_SIZE);
  224. close(fd);
  225. }