11 #include <sys/statfs.h>
12 #include "kern_util.h"
14 #include "user_util.h"
19 #include "kern_constants.h"
21 #include <sys/param.h>
23 static char *default_tmpdir = "/tmp";
24 static char *tempdir = NULL;
26 static void __init find_tempdir(void)
28 char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
32 if(tempdir != NULL) return; /* We've already been called */
33 for(i = 0; dirs[i]; i++){
34 dir = getenv(dirs[i]);
35 if((dir != NULL) && (*dir != '\0'))
38 if((dir == NULL) || (*dir == '\0'))
41 tempdir = malloc(strlen(dir) + 2);
43 fprintf(stderr, "Failed to malloc tempdir, "
44 "errno = %d\n", errno);
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.
56 static int next(int fd, char *buf, int size, char c)
61 while((ptr = strchr(buf, c)) == NULL){
62 n = read(fd, buf, size - 1);
73 memmove(buf, ptr, len + 1);
75 /* Refill the buffer so that if there's a partial string that we care
76 * about, it will be completed, and we can recognize it.
78 n = read(fd, &buf[len], size - len - 1);
86 static int checked_tmpdir = 0;
88 /* Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
89 * way to do this than to parse /proc/mounts. statfs will return the
90 * same filesystem magic number and fs id for both /dev and /dev/shm
91 * when they are both tmpfs, so you can't tell if they are different
92 * filesystems. Also, there seems to be no other way of finding the
93 * mount point of a filesystem from within it.
95 * If a /dev/shm tmpfs entry is found, then we switch to using it.
96 * Otherwise, we stay with the default /tmp.
98 static void which_tmpdir(void)
101 char buf[128] = { '\0' };
108 printf("Checking for tmpfs mount on /dev/shm...");
110 fd = open("/proc/mounts", O_RDONLY);
112 printf("failed to open /proc/mounts, errno = %d\n", errno);
117 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
121 if(!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
124 found = next(fd, buf, ARRAY_SIZE(buf), '\n');
131 printf("nothing mounted on /dev/shm\n");
133 printf("read returned errno %d\n", -found);
138 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
142 if(strncmp(buf, "tmpfs", strlen("tmpfs"))){
143 printf("not tmpfs\n");
148 default_tmpdir = "/dev/shm";
152 * This proc still used in tt-mode
153 * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
154 * So it isn't 'static' yet.
156 int make_tempfile(const char *template, char **out_tempname, int do_unlink)
162 tempname = malloc(MAXPATHLEN);
165 if (template[0] != '/')
166 strcpy(tempname, tempdir);
169 strcat(tempname, template);
170 fd = mkstemp(tempname);
172 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
176 if(do_unlink && (unlink(tempname) < 0)){
181 *out_tempname = tempname;
191 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
194 * This proc is used in start_up.c
195 * So it isn't 'static'.
197 int create_tmp_file(unsigned long long len)
202 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
207 err = fchmod(fd, 0777);
209 perror("os_mode_fd");
213 /* Seek to len - 1 because writing a character there will
214 * increase the file size by one byte, to the desired length.
216 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
217 perror("os_seek_file");
223 err = os_write_file(fd, &zero, 1);
226 perror("os_write_file");
233 int create_mem_file(unsigned long long len)
237 fd = create_tmp_file(len);
239 err = os_set_exec_close(fd, 1);
242 perror("exec_close");
248 void check_tmpexec(void)
251 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
253 addr = mmap(NULL, UM_KERN_PAGE_SIZE,
254 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
255 printf("Checking PROT_EXEC mmap in %s...",tempdir);
257 if(addr == MAP_FAILED){
261 printf("%s must be not mounted noexec\n",tempdir);
265 munmap(addr, UM_KERN_PAGE_SIZE);