| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 1 | /* | 
|  | 2 | * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com) | 
|  | 3 | * Licensed under the GPL | 
|  | 4 | */ | 
|  | 5 |  | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 6 | #include <stdio.h> | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 7 | #include <stddef.h> | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 8 | #include <stdlib.h> | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 9 | #include <unistd.h> | 
|  | 10 | #include <errno.h> | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 11 | #include <fcntl.h> | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 12 | #include <string.h> | 
| Liu Aleaxander | fb967ec | 2010-06-29 15:05:40 -0700 | [diff] [blame] | 13 | #include <sys/stat.h> | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 14 | #include <sys/mman.h> | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 15 | #include <sys/param.h> | 
| Al Viro | 37185b3 | 2012-10-08 03:27:32 +0100 | [diff] [blame] | 16 | #include <init.h> | 
|  | 17 | #include <os.h> | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 18 |  | 
| Jeff Dike | 6bf7948 | 2007-02-10 01:44:18 -0800 | [diff] [blame] | 19 | /* Modified by which_tmpdir, which is called during early boot */ | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 20 | static char *default_tmpdir = "/tmp"; | 
| Jeff Dike | 6bf7948 | 2007-02-10 01:44:18 -0800 | [diff] [blame] | 21 |  | 
|  | 22 | /* | 
|  | 23 | *  Modified when creating the physical memory file and when checking | 
|  | 24 | * the tmp filesystem for usability, both happening during early boot. | 
|  | 25 | */ | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 26 | static char *tempdir = NULL; | 
|  | 27 |  | 
|  | 28 | static void __init find_tempdir(void) | 
|  | 29 | { | 
| WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 30 | const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL }; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 31 | int i; | 
|  | 32 | char *dir = NULL; | 
|  | 33 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 34 | if (tempdir != NULL) | 
|  | 35 | /* We've already been called */ | 
| Jeff Dike | 81999a0 | 2007-02-10 01:44:21 -0800 | [diff] [blame] | 36 | return; | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 37 | for (i = 0; dirs[i]; i++) { | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 38 | dir = getenv(dirs[i]); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 39 | if ((dir != NULL) && (*dir != '\0')) | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 40 | break; | 
|  | 41 | } | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 42 | if ((dir == NULL) || (*dir == '\0')) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 43 | dir = default_tmpdir; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 44 |  | 
|  | 45 | tempdir = malloc(strlen(dir) + 2); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 46 | if (tempdir == NULL) { | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 47 | fprintf(stderr, "Failed to malloc tempdir, " | 
|  | 48 | "errno = %d\n", errno); | 
|  | 49 | return; | 
|  | 50 | } | 
|  | 51 | strcpy(tempdir, dir); | 
|  | 52 | strcat(tempdir, "/"); | 
|  | 53 | } | 
|  | 54 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 55 | /* | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 56 | * Remove bytes from the front of the buffer and refill it so that if there's a | 
|  | 57 | * partial string that we care about, it will be completed, and we can recognize | 
|  | 58 | * it. | 
|  | 59 | */ | 
|  | 60 | static int pop(int fd, char *buf, size_t size, size_t npop) | 
|  | 61 | { | 
|  | 62 | ssize_t n; | 
|  | 63 | size_t len = strlen(&buf[npop]); | 
|  | 64 |  | 
|  | 65 | memmove(buf, &buf[npop], len + 1); | 
|  | 66 | n = read(fd, &buf[len], size - len - 1); | 
|  | 67 | if (n < 0) | 
|  | 68 | return -errno; | 
|  | 69 |  | 
|  | 70 | buf[len + n] = '\0'; | 
|  | 71 | return 1; | 
|  | 72 | } | 
|  | 73 |  | 
|  | 74 | /* | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 75 | * This will return 1, with the first character in buf being the | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 76 | * character following the next instance of c in the file.  This will | 
|  | 77 | * read the file as needed.  If there's an error, -errno is returned; | 
|  | 78 | * if the end of the file is reached, 0 is returned. | 
|  | 79 | */ | 
| WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 80 | static int next(int fd, char *buf, size_t size, char c) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 81 | { | 
| WANG Cong | c0a9290 | 2008-02-04 22:30:41 -0800 | [diff] [blame] | 82 | ssize_t n; | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 83 | char *ptr; | 
|  | 84 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 85 | while ((ptr = strchr(buf, c)) == NULL) { | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 86 | n = read(fd, buf, size - 1); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 87 | if (n == 0) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 88 | return 0; | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 89 | else if (n < 0) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 90 | return -errno; | 
|  | 91 |  | 
|  | 92 | buf[n] = '\0'; | 
|  | 93 | } | 
|  | 94 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 95 | return pop(fd, buf, size, ptr - buf + 1); | 
|  | 96 | } | 
|  | 97 |  | 
|  | 98 | /* | 
|  | 99 | * Decode an octal-escaped and space-terminated path of the form used by | 
|  | 100 | * /proc/mounts. May be used to decode a path in-place. "out" must be at least | 
|  | 101 | * as large as the input. The output is always null-terminated. "len" gets the | 
|  | 102 | * length of the output, excluding the trailing null. Returns 0 if a full path | 
|  | 103 | * was successfully decoded, otherwise an error. | 
|  | 104 | */ | 
|  | 105 | static int decode_path(const char *in, char *out, size_t *len) | 
|  | 106 | { | 
|  | 107 | char *first = out; | 
|  | 108 | int c; | 
|  | 109 | int i; | 
|  | 110 | int ret = -EINVAL; | 
|  | 111 | while (1) { | 
|  | 112 | switch (*in) { | 
|  | 113 | case '\0': | 
|  | 114 | goto out; | 
|  | 115 |  | 
|  | 116 | case ' ': | 
|  | 117 | ret = 0; | 
|  | 118 | goto out; | 
|  | 119 |  | 
|  | 120 | case '\\': | 
|  | 121 | in++; | 
|  | 122 | c = 0; | 
|  | 123 | for (i = 0; i < 3; i++) { | 
|  | 124 | if (*in < '0' || *in > '7') | 
|  | 125 | goto out; | 
|  | 126 | c = (c << 3) | (*in++ - '0'); | 
|  | 127 | } | 
|  | 128 | *(unsigned char *)out++ = (unsigned char) c; | 
|  | 129 | break; | 
|  | 130 |  | 
|  | 131 | default: | 
|  | 132 | *out++ = *in++; | 
|  | 133 | break; | 
|  | 134 | } | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | out: | 
|  | 138 | *out = '\0'; | 
|  | 139 | *len = out - first; | 
|  | 140 | return ret; | 
|  | 141 | } | 
|  | 142 |  | 
|  | 143 | /* | 
|  | 144 | * Computes the length of s when encoded with three-digit octal escape sequences | 
|  | 145 | * for the characters in chars. | 
|  | 146 | */ | 
|  | 147 | static size_t octal_encoded_length(const char *s, const char *chars) | 
|  | 148 | { | 
|  | 149 | size_t len = strlen(s); | 
|  | 150 | while ((s = strpbrk(s, chars)) != NULL) { | 
|  | 151 | len += 3; | 
|  | 152 | s++; | 
|  | 153 | } | 
|  | 154 |  | 
|  | 155 | return len; | 
|  | 156 | } | 
|  | 157 |  | 
|  | 158 | enum { | 
|  | 159 | OUTCOME_NOTHING_MOUNTED, | 
|  | 160 | OUTCOME_TMPFS_MOUNT, | 
|  | 161 | OUTCOME_NON_TMPFS_MOUNT, | 
|  | 162 | }; | 
|  | 163 |  | 
|  | 164 | /* Read a line of /proc/mounts data looking for a tmpfs mount at "path". */ | 
|  | 165 | static int read_mount(int fd, char *buf, size_t bufsize, const char *path, | 
|  | 166 | int *outcome) | 
|  | 167 | { | 
|  | 168 | int found; | 
|  | 169 | int match; | 
|  | 170 | char *space; | 
|  | 171 | size_t len; | 
|  | 172 |  | 
|  | 173 | enum { | 
|  | 174 | MATCH_NONE, | 
|  | 175 | MATCH_EXACT, | 
|  | 176 | MATCH_PARENT, | 
|  | 177 | }; | 
|  | 178 |  | 
|  | 179 | found = next(fd, buf, bufsize, ' '); | 
|  | 180 | if (found != 1) | 
|  | 181 | return found; | 
| Jeff Dike | c2b7a4b | 2006-06-30 01:55:54 -0700 | [diff] [blame] | 182 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 183 | /* | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 184 | * If there's no following space in the buffer, then this path is | 
|  | 185 | * truncated, so it can't be the one we're looking for. | 
| Jeff Dike | c2b7a4b | 2006-06-30 01:55:54 -0700 | [diff] [blame] | 186 | */ | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 187 | space = strchr(buf, ' '); | 
|  | 188 | if (space) { | 
|  | 189 | match = MATCH_NONE; | 
|  | 190 | if (!decode_path(buf, buf, &len)) { | 
|  | 191 | if (!strcmp(buf, path)) | 
|  | 192 | match = MATCH_EXACT; | 
|  | 193 | else if (!strncmp(buf, path, len) | 
|  | 194 | && (path[len] == '/' || !strcmp(buf, "/"))) | 
|  | 195 | match = MATCH_PARENT; | 
|  | 196 | } | 
| Jeff Dike | c2b7a4b | 2006-06-30 01:55:54 -0700 | [diff] [blame] | 197 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 198 | found = pop(fd, buf, bufsize, space - buf + 1); | 
|  | 199 | if (found != 1) | 
|  | 200 | return found; | 
|  | 201 |  | 
|  | 202 | switch (match) { | 
|  | 203 | case MATCH_EXACT: | 
|  | 204 | if (!strncmp(buf, "tmpfs", strlen("tmpfs"))) | 
|  | 205 | *outcome = OUTCOME_TMPFS_MOUNT; | 
|  | 206 | else | 
|  | 207 | *outcome = OUTCOME_NON_TMPFS_MOUNT; | 
|  | 208 | break; | 
|  | 209 |  | 
|  | 210 | case MATCH_PARENT: | 
|  | 211 | /* This mount obscures any previous ones. */ | 
|  | 212 | *outcome = OUTCOME_NOTHING_MOUNTED; | 
|  | 213 | break; | 
|  | 214 | } | 
|  | 215 | } | 
|  | 216 |  | 
|  | 217 | return next(fd, buf, bufsize, '\n'); | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 218 | } | 
|  | 219 |  | 
| Jeff Dike | 6bf7948 | 2007-02-10 01:44:18 -0800 | [diff] [blame] | 220 | /* which_tmpdir is called only during early boot */ | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 221 | static int checked_tmpdir = 0; | 
|  | 222 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 223 | /* | 
|  | 224 | * Look for a tmpfs mounted at /dev/shm.  I couldn't find a cleaner | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 225 | * way to do this than to parse /proc/mounts.  statfs will return the | 
|  | 226 | * same filesystem magic number and fs id for both /dev and /dev/shm | 
|  | 227 | * when they are both tmpfs, so you can't tell if they are different | 
|  | 228 | * filesystems.  Also, there seems to be no other way of finding the | 
|  | 229 | * mount point of a filesystem from within it. | 
|  | 230 | * | 
|  | 231 | * If a /dev/shm tmpfs entry is found, then we switch to using it. | 
|  | 232 | * Otherwise, we stay with the default /tmp. | 
|  | 233 | */ | 
|  | 234 | static void which_tmpdir(void) | 
|  | 235 | { | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 236 | int fd; | 
|  | 237 | int found; | 
|  | 238 | int outcome; | 
|  | 239 | char *path; | 
|  | 240 | char *buf; | 
|  | 241 | size_t bufsize; | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 242 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 243 | if (checked_tmpdir) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 244 | return; | 
|  | 245 |  | 
|  | 246 | checked_tmpdir = 1; | 
|  | 247 |  | 
|  | 248 | printf("Checking for tmpfs mount on /dev/shm..."); | 
|  | 249 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 250 | path = realpath("/dev/shm", NULL); | 
|  | 251 | if (!path) { | 
|  | 252 | printf("failed to check real path, errno = %d\n", errno); | 
|  | 253 | return; | 
|  | 254 | } | 
|  | 255 | printf("%s...", path); | 
|  | 256 |  | 
|  | 257 | /* | 
|  | 258 | * The buffer needs to be able to fit the full octal-escaped path, a | 
|  | 259 | * space, and a trailing null in order to successfully decode it. | 
|  | 260 | */ | 
|  | 261 | bufsize = octal_encoded_length(path, " \t\n\\") + 2; | 
|  | 262 |  | 
|  | 263 | if (bufsize < 128) | 
|  | 264 | bufsize = 128; | 
|  | 265 |  | 
|  | 266 | buf = malloc(bufsize); | 
|  | 267 | if (!buf) { | 
|  | 268 | printf("malloc failed, errno = %d\n", errno); | 
|  | 269 | goto out; | 
|  | 270 | } | 
|  | 271 | buf[0] = '\0'; | 
|  | 272 |  | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 273 | fd = open("/proc/mounts", O_RDONLY); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 274 | if (fd < 0) { | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 275 | printf("failed to open /proc/mounts, errno = %d\n", errno); | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 276 | goto out1; | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 277 | } | 
|  | 278 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 279 | outcome = OUTCOME_NOTHING_MOUNTED; | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 280 | while (1) { | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 281 | found = read_mount(fd, buf, bufsize, path, &outcome); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 282 | if (found != 1) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 283 | break; | 
|  | 284 | } | 
|  | 285 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 286 | if (found < 0) { | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 287 | printf("read returned errno %d\n", -found); | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 288 | } else { | 
|  | 289 | switch (outcome) { | 
|  | 290 | case OUTCOME_TMPFS_MOUNT: | 
|  | 291 | printf("OK\n"); | 
|  | 292 | default_tmpdir = "/dev/shm"; | 
|  | 293 | break; | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 294 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 295 | case OUTCOME_NON_TMPFS_MOUNT: | 
|  | 296 | printf("not tmpfs\n"); | 
|  | 297 | break; | 
| Jeff Dike | 80c1374 | 2006-09-29 01:58:51 -0700 | [diff] [blame] | 298 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 299 | default: | 
|  | 300 | printf("nothing mounted on /dev/shm\n"); | 
|  | 301 | break; | 
|  | 302 | } | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 303 | } | 
|  | 304 |  | 
| Tristan Schmelcher | 7473534 | 2013-07-08 16:19:49 -0400 | [diff] [blame] | 305 | close(fd); | 
|  | 306 | out1: | 
|  | 307 | free(buf); | 
|  | 308 | out: | 
|  | 309 | free(path); | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 310 | } | 
|  | 311 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 312 | static int __init make_tempfile(const char *template, char **out_tempname, | 
|  | 313 | int do_unlink) | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 314 | { | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 315 | char *tempname; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 316 | int fd; | 
|  | 317 |  | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 318 | which_tmpdir(); | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 319 | tempname = malloc(MAXPATHLEN); | 
| Jim Meyering | 11a7ac2 | 2008-02-08 04:22:09 -0800 | [diff] [blame] | 320 | if (tempname == NULL) | 
|  | 321 | return -1; | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 322 |  | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 323 | find_tempdir(); | 
| Jim Meyering | 11a7ac2 | 2008-02-08 04:22:09 -0800 | [diff] [blame] | 324 | if ((tempdir == NULL) || (strlen(tempdir) >= MAXPATHLEN)) | 
| Davidlohr Bueso | 2a6d0ac | 2011-07-25 17:12:52 -0700 | [diff] [blame] | 325 | goto out; | 
| Jim Meyering | 11a7ac2 | 2008-02-08 04:22:09 -0800 | [diff] [blame] | 326 |  | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 327 | if (template[0] != '/') | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 328 | strcpy(tempname, tempdir); | 
|  | 329 | else | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 330 | tempname[0] = '\0'; | 
| WANG Cong | c9a3072 | 2008-02-04 22:30:35 -0800 | [diff] [blame] | 331 | strncat(tempname, template, MAXPATHLEN-1-strlen(tempname)); | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 332 | fd = mkstemp(tempname); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 333 | if (fd < 0) { | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 334 | fprintf(stderr, "open - cannot create %s: %s\n", tempname, | 
|  | 335 | strerror(errno)); | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 336 | goto out; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 337 | } | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 338 | if (do_unlink && (unlink(tempname) < 0)) { | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 339 | perror("unlink"); | 
| Davidlohr Bueso | 2a6d0ac | 2011-07-25 17:12:52 -0700 | [diff] [blame] | 340 | goto close; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 341 | } | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 342 | if (out_tempname) { | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 343 | *out_tempname = tempname; | 
| Jim Meyering | 11a7ac2 | 2008-02-08 04:22:09 -0800 | [diff] [blame] | 344 | } else | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 345 | free(tempname); | 
| Jeff Dike | 81999a0 | 2007-02-10 01:44:21 -0800 | [diff] [blame] | 346 | return fd; | 
| Davidlohr Bueso | 2a6d0ac | 2011-07-25 17:12:52 -0700 | [diff] [blame] | 347 | close: | 
|  | 348 | close(fd); | 
| Paolo 'Blaisorblade' Giarrusso | 87276f7 | 2006-04-10 22:53:39 -0700 | [diff] [blame] | 349 | out: | 
|  | 350 | free(tempname); | 
|  | 351 | return -1; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 352 | } | 
|  | 353 |  | 
|  | 354 | #define TEMPNAME_TEMPLATE "vm_file-XXXXXX" | 
|  | 355 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 356 | static int __init create_tmp_file(unsigned long long len) | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 357 | { | 
|  | 358 | int fd, err; | 
|  | 359 | char zero; | 
|  | 360 |  | 
|  | 361 | fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 362 | if (fd < 0) | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 363 | exit(1); | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 364 |  | 
|  | 365 | err = fchmod(fd, 0777); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 366 | if (err < 0) { | 
| Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 367 | perror("fchmod"); | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 368 | exit(1); | 
|  | 369 | } | 
|  | 370 |  | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 371 | /* | 
|  | 372 | * Seek to len - 1 because writing a character there will | 
| Jeff Dike | 190f493 | 2006-06-30 01:55:55 -0700 | [diff] [blame] | 373 | * increase the file size by one byte, to the desired length. | 
|  | 374 | */ | 
|  | 375 | if (lseek64(fd, len - 1, SEEK_SET) < 0) { | 
| Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 376 | perror("lseek64"); | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 377 | exit(1); | 
|  | 378 | } | 
|  | 379 |  | 
|  | 380 | zero = 0; | 
|  | 381 |  | 
| Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 382 | err = write(fd, &zero, 1); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 383 | if (err != 1) { | 
| Jeff Dike | a61f334 | 2007-05-06 14:51:35 -0700 | [diff] [blame] | 384 | perror("write"); | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 385 | exit(1); | 
|  | 386 | } | 
|  | 387 |  | 
| Jeff Dike | 81999a0 | 2007-02-10 01:44:21 -0800 | [diff] [blame] | 388 | return fd; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 389 | } | 
|  | 390 |  | 
| Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 391 | int __init create_mem_file(unsigned long long len) | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 392 | { | 
|  | 393 | int err, fd; | 
|  | 394 |  | 
| Jeff Dike | 02dea08 | 2006-03-31 02:30:08 -0800 | [diff] [blame] | 395 | fd = create_tmp_file(len); | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 396 |  | 
| Jeff Dike | 512b6fb | 2007-10-16 01:27:11 -0700 | [diff] [blame] | 397 | err = os_set_exec_close(fd); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 398 | if (err < 0) { | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 399 | errno = -err; | 
|  | 400 | perror("exec_close"); | 
|  | 401 | } | 
| Jeff Dike | 81999a0 | 2007-02-10 01:44:21 -0800 | [diff] [blame] | 402 | return fd; | 
| Jeff Dike | 0f80bc8 | 2005-09-16 19:27:50 -0700 | [diff] [blame] | 403 | } | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 404 |  | 
|  | 405 |  | 
| Jeff Dike | 36e4546 | 2007-05-06 14:51:11 -0700 | [diff] [blame] | 406 | void __init check_tmpexec(void) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 407 | { | 
|  | 408 | void *addr; | 
|  | 409 | int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE); | 
|  | 410 |  | 
|  | 411 | addr = mmap(NULL, UM_KERN_PAGE_SIZE, | 
|  | 412 | PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); | 
|  | 413 | printf("Checking PROT_EXEC mmap in %s...",tempdir); | 
|  | 414 | fflush(stdout); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 415 | if (addr == MAP_FAILED) { | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 416 | err = errno; | 
|  | 417 | perror("failed"); | 
| WANG Cong | c9a3072 | 2008-02-04 22:30:35 -0800 | [diff] [blame] | 418 | close(fd); | 
| Jeff Dike | 5134d8f | 2008-02-08 04:22:08 -0800 | [diff] [blame] | 419 | if (err == EPERM) | 
| Rob Landley | 966a082f | 2006-04-18 22:21:43 -0700 | [diff] [blame] | 420 | printf("%s must be not mounted noexec\n",tempdir); | 
|  | 421 | exit(1); | 
|  | 422 | } | 
|  | 423 | printf("OK\n"); | 
|  | 424 | munmap(addr, UM_KERN_PAGE_SIZE); | 
|  | 425 |  | 
|  | 426 | close(fd); | 
|  | 427 | } |