Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 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> |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 11 | #include <sys/statfs.h> |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 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 | |
| 21 | #include <sys/param.h> |
| 22 | |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 23 | static char *default_tmpdir = "/tmp"; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 24 | static char *tempdir = NULL; |
| 25 | |
| 26 | static void __init find_tempdir(void) |
| 27 | { |
| 28 | char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL }; |
| 29 | int i; |
| 30 | char *dir = NULL; |
| 31 | |
| 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')) |
| 36 | break; |
| 37 | } |
| 38 | if((dir == NULL) || (*dir == '\0')) |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 39 | dir = default_tmpdir; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 40 | |
| 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 | |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 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. |
| 55 | */ |
| 56 | static int next(int fd, char *buf, int size, char c) |
| 57 | { |
Jeff Dike | c2b7a4b | 2006-06-30 01:55:54 -0700 | [diff] [blame] | 58 | int n, len; |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 59 | char *ptr; |
| 60 | |
| 61 | while((ptr = strchr(buf, c)) == NULL){ |
| 62 | n = read(fd, buf, size - 1); |
| 63 | if(n == 0) |
| 64 | return 0; |
| 65 | else if(n < 0) |
| 66 | return -errno; |
| 67 | |
| 68 | buf[n] = '\0'; |
| 69 | } |
| 70 | |
| 71 | ptr++; |
Jeff Dike | c2b7a4b | 2006-06-30 01:55:54 -0700 | [diff] [blame] | 72 | len = strlen(ptr); |
| 73 | memmove(buf, ptr, len + 1); |
| 74 | |
| 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. |
| 77 | */ |
| 78 | n = read(fd, &buf[len], size - len - 1); |
| 79 | if(n < 0) |
| 80 | return -errno; |
| 81 | |
| 82 | buf[len + n] = '\0'; |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 83 | return 1; |
| 84 | } |
| 85 | |
| 86 | static int checked_tmpdir = 0; |
| 87 | |
| 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. |
| 94 | * |
| 95 | * If a /dev/shm tmpfs entry is found, then we switch to using it. |
| 96 | * Otherwise, we stay with the default /tmp. |
| 97 | */ |
| 98 | static void which_tmpdir(void) |
| 99 | { |
| 100 | int fd, found; |
| 101 | char buf[128] = { '\0' }; |
| 102 | |
| 103 | if(checked_tmpdir) |
| 104 | return; |
| 105 | |
| 106 | checked_tmpdir = 1; |
| 107 | |
| 108 | printf("Checking for tmpfs mount on /dev/shm..."); |
| 109 | |
| 110 | fd = open("/proc/mounts", O_RDONLY); |
| 111 | if(fd < 0){ |
| 112 | printf("failed to open /proc/mounts, errno = %d\n", errno); |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | while(1){ |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 117 | found = next(fd, buf, ARRAY_SIZE(buf), ' '); |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 118 | if(found != 1) |
| 119 | break; |
| 120 | |
| 121 | if(!strncmp(buf, "/dev/shm", strlen("/dev/shm"))) |
| 122 | goto found; |
| 123 | |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 124 | found = next(fd, buf, ARRAY_SIZE(buf), '\n'); |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 125 | if(found != 1) |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | err: |
| 130 | if(found == 0) |
| 131 | printf("nothing mounted on /dev/shm\n"); |
| 132 | else if(found < 0) |
| 133 | printf("read returned errno %d\n", -found); |
| 134 | |
Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame^] | 135 | out: |
| 136 | close(fd); |
| 137 | |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 138 | return; |
| 139 | |
| 140 | found: |
Jeff Dike | 91b165c | 2006-09-25 23:33:00 -0700 | [diff] [blame] | 141 | found = next(fd, buf, ARRAY_SIZE(buf), ' '); |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 142 | if(found != 1) |
| 143 | goto err; |
| 144 | |
| 145 | if(strncmp(buf, "tmpfs", strlen("tmpfs"))){ |
| 146 | printf("not tmpfs\n"); |
Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame^] | 147 | goto out; |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | printf("OK\n"); |
| 151 | default_tmpdir = "/dev/shm"; |
Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame^] | 152 | goto out; |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 153 | } |
| 154 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 155 | /* |
| 156 | * This proc still used in tt-mode |
| 157 | * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger). |
| 158 | * So it isn't 'static' yet. |
| 159 | */ |
| 160 | int make_tempfile(const char *template, char **out_tempname, int do_unlink) |
| 161 | { |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 162 | char *tempname; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 163 | int fd; |
| 164 | |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 165 | which_tmpdir(); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 166 | tempname = malloc(MAXPATHLEN); |
| 167 | |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 168 | find_tempdir(); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 169 | if (template[0] != '/') |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 170 | strcpy(tempname, tempdir); |
| 171 | else |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 172 | tempname[0] = '\0'; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 173 | strcat(tempname, template); |
| 174 | fd = mkstemp(tempname); |
| 175 | if(fd < 0){ |
| 176 | fprintf(stderr, "open - cannot create %s: %s\n", tempname, |
| 177 | strerror(errno)); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 178 | goto out; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 179 | } |
| 180 | if(do_unlink && (unlink(tempname) < 0)){ |
| 181 | perror("unlink"); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 182 | goto out; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 183 | } |
| 184 | if(out_tempname){ |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 185 | *out_tempname = tempname; |
| 186 | } else { |
| 187 | free(tempname); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 188 | } |
| 189 | return(fd); |
Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 190 | out: |
| 191 | free(tempname); |
| 192 | return -1; |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | #define TEMPNAME_TEMPLATE "vm_file-XXXXXX" |
| 196 | |
| 197 | /* |
| 198 | * This proc is used in start_up.c |
| 199 | * So it isn't 'static'. |
| 200 | */ |
Jeff Dike | ae17381 | 2005-11-07 00:58:57 -0800 | [diff] [blame] | 201 | int create_tmp_file(unsigned long long len) |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 202 | { |
| 203 | int fd, err; |
| 204 | char zero; |
| 205 | |
| 206 | fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1); |
| 207 | if(fd < 0) { |
| 208 | exit(1); |
| 209 | } |
| 210 | |
| 211 | err = fchmod(fd, 0777); |
| 212 | if(err < 0){ |
| 213 | perror("os_mode_fd"); |
| 214 | exit(1); |
| 215 | } |
| 216 | |
Jeff Dike | 190f493 | 2006-06-30 01:55:55 -0700 | [diff] [blame] | 217 | /* Seek to len - 1 because writing a character there will |
| 218 | * increase the file size by one byte, to the desired length. |
| 219 | */ |
| 220 | if (lseek64(fd, len - 1, SEEK_SET) < 0) { |
| 221 | perror("os_seek_file"); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 222 | exit(1); |
| 223 | } |
| 224 | |
| 225 | zero = 0; |
| 226 | |
| 227 | err = os_write_file(fd, &zero, 1); |
| 228 | if(err != 1){ |
| 229 | errno = -err; |
| 230 | perror("os_write_file"); |
| 231 | exit(1); |
| 232 | } |
| 233 | |
| 234 | return(fd); |
| 235 | } |
| 236 | |
Jeff Dike | ae17381 | 2005-11-07 00:58:57 -0800 | [diff] [blame] | 237 | int create_mem_file(unsigned long long len) |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 238 | { |
| 239 | int err, fd; |
| 240 | |
Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 241 | fd = create_tmp_file(len); |
Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 242 | |
| 243 | err = os_set_exec_close(fd, 1); |
| 244 | if(err < 0){ |
| 245 | errno = -err; |
| 246 | perror("exec_close"); |
| 247 | } |
| 248 | return(fd); |
| 249 | } |
Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 250 | |
| 251 | |
| 252 | void check_tmpexec(void) |
| 253 | { |
| 254 | void *addr; |
| 255 | int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE); |
| 256 | |
| 257 | addr = mmap(NULL, UM_KERN_PAGE_SIZE, |
| 258 | PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); |
| 259 | printf("Checking PROT_EXEC mmap in %s...",tempdir); |
| 260 | fflush(stdout); |
| 261 | if(addr == MAP_FAILED){ |
| 262 | err = errno; |
| 263 | perror("failed"); |
| 264 | if(err == EPERM) |
| 265 | printf("%s must be not mounted noexec\n",tempdir); |
| 266 | exit(1); |
| 267 | } |
| 268 | printf("OK\n"); |
| 269 | munmap(addr, UM_KERN_PAGE_SIZE); |
| 270 | |
| 271 | close(fd); |
| 272 | } |