11 #include <sys/statfs.h>
12 #include "kern_util.h"
18 #include "kern_constants.h"
20 #include <sys/param.h>
22 /* Modified by which_tmpdir, which is called during early boot */
23 static char *default_tmpdir = "/tmp";
26 * Modified when creating the physical memory file and when checking
27 * the tmp filesystem for usability, both happening during early boot.
29 static char *tempdir = NULL;
31 static void __init find_tempdir(void)
33 const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
37 if(tempdir != NULL) /* We've already been called */
39 for(i = 0; dirs[i]; i++){
40 dir = getenv(dirs[i]);
41 if((dir != NULL) && (*dir != '\0'))
44 if((dir == NULL) || (*dir == '\0'))
47 tempdir = malloc(strlen(dir) + 2);
49 fprintf(stderr, "Failed to malloc tempdir, "
50 "errno = %d\n", errno);
57 /* This will return 1, with the first character in buf being the
58 * character following the next instance of c in the file. This will
59 * read the file as needed. If there's an error, -errno is returned;
60 * if the end of the file is reached, 0 is returned.
62 static int next(int fd, char *buf, size_t size, char c)
68 while((ptr = strchr(buf, c)) == NULL){
69 n = read(fd, buf, size - 1);
80 memmove(buf, ptr, len + 1);
82 /* Refill the buffer so that if there's a partial string that we care
83 * about, it will be completed, and we can recognize it.
85 n = read(fd, &buf[len], size - len - 1);
93 /* which_tmpdir is called only during early boot */
94 static int checked_tmpdir = 0;
96 /* Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
97 * way to do this than to parse /proc/mounts. statfs will return the
98 * same filesystem magic number and fs id for both /dev and /dev/shm
99 * when they are both tmpfs, so you can't tell if they are different
100 * filesystems. Also, there seems to be no other way of finding the
101 * mount point of a filesystem from within it.
103 * If a /dev/shm tmpfs entry is found, then we switch to using it.
104 * Otherwise, we stay with the default /tmp.
106 static void which_tmpdir(void)
109 char buf[128] = { '\0' };
116 printf("Checking for tmpfs mount on /dev/shm...");
118 fd = open("/proc/mounts", O_RDONLY);
120 printf("failed to open /proc/mounts, errno = %d\n", errno);
125 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
129 if(!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
132 found = next(fd, buf, ARRAY_SIZE(buf), '\n');
139 printf("nothing mounted on /dev/shm\n");
141 printf("read returned errno %d\n", -found);
149 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
153 if(strncmp(buf, "tmpfs", strlen("tmpfs"))){
154 printf("not tmpfs\n");
159 default_tmpdir = "/dev/shm";
164 * This proc still used in tt-mode
165 * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
166 * So it isn't 'static' yet.
168 int __init make_tempfile(const char *template, char **out_tempname,
175 tempname = malloc(MAXPATHLEN);
180 if (template[0] != '/')
181 strcpy(tempname, tempdir);
184 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
185 fd = mkstemp(tempname);
187 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
191 if(do_unlink && (unlink(tempname) < 0)){
196 *out_tempname = tempname;
206 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
209 * This proc is used in start_up.c
210 * So it isn't 'static'.
212 int __init create_tmp_file(unsigned long long len)
217 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
222 err = fchmod(fd, 0777);
228 /* Seek to len - 1 because writing a character there will
229 * increase the file size by one byte, to the desired length.
231 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
238 err = write(fd, &zero, 1);
247 int __init create_mem_file(unsigned long long len)
251 fd = create_tmp_file(len);
253 err = os_set_exec_close(fd);
256 perror("exec_close");
262 void __init check_tmpexec(void)
265 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
267 addr = mmap(NULL, UM_KERN_PAGE_SIZE,
268 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
269 printf("Checking PROT_EXEC mmap in %s...",tempdir);
271 if(addr == MAP_FAILED){
276 printf("%s must be not mounted noexec\n",tempdir);
280 munmap(addr, UM_KERN_PAGE_SIZE);