2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
15 #include <sys/param.h>
19 /* Modified by which_tmpdir, which is called during early boot */
20 static char *default_tmpdir = "/tmp";
23 * Modified when creating the physical memory file and when checking
24 * the tmp filesystem for usability, both happening during early boot.
26 static char *tempdir = NULL;
28 static void __init find_tempdir(void)
30 const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
35 /* We've already been called */
37 for (i = 0; dirs[i]; i++) {
38 dir = getenv(dirs[i]);
39 if ((dir != NULL) && (*dir != '\0'))
42 if ((dir == NULL) || (*dir == '\0'))
45 tempdir = malloc(strlen(dir) + 2);
46 if (tempdir == NULL) {
47 fprintf(stderr, "Failed to malloc tempdir, "
48 "errno = %d\n", errno);
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
60 static int pop(int fd, char *buf, size_t size, size_t npop)
63 size_t len = strlen(&buf[npop]);
65 memmove(buf, &buf[npop], len + 1);
66 n = read(fd, &buf[len], size - len - 1);
75 * This will return 1, with the first character in buf being the
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.
80 static int next(int fd, char *buf, size_t size, char c)
85 while ((ptr = strchr(buf, c)) == NULL) {
86 n = read(fd, buf, size - 1);
95 return pop(fd, buf, size, ptr - buf + 1);
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.
105 static int decode_path(const char *in, char *out, size_t *len)
123 for (i = 0; i < 3; i++) {
124 if (*in < '0' || *in > '7')
126 c = (c << 3) | (*in++ - '0');
128 *(unsigned char *)out++ = (unsigned char) c;
144 * Computes the length of s when encoded with three-digit octal escape sequences
145 * for the characters in chars.
147 static size_t octal_encoded_length(const char *s, const char *chars)
149 size_t len = strlen(s);
150 while ((s = strpbrk(s, chars)) != NULL) {
159 OUTCOME_NOTHING_MOUNTED,
161 OUTCOME_NON_TMPFS_MOUNT,
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,
179 found = next(fd, buf, bufsize, ' ');
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.
187 space = strchr(buf, ' ');
190 if (!decode_path(buf, buf, &len)) {
191 if (!strcmp(buf, path))
193 else if (!strncmp(buf, path, len)
194 && (path[len] == '/' || !strcmp(buf, "/")))
195 match = MATCH_PARENT;
198 found = pop(fd, buf, bufsize, space - buf + 1);
204 if (!strncmp(buf, "tmpfs", strlen("tmpfs")))
205 *outcome = OUTCOME_TMPFS_MOUNT;
207 *outcome = OUTCOME_NON_TMPFS_MOUNT;
211 /* This mount obscures any previous ones. */
212 *outcome = OUTCOME_NOTHING_MOUNTED;
217 return next(fd, buf, bufsize, '\n');
220 /* which_tmpdir is called only during early boot */
221 static int checked_tmpdir = 0;
224 * Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
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.
231 * If a /dev/shm tmpfs entry is found, then we switch to using it.
232 * Otherwise, we stay with the default /tmp.
234 static void which_tmpdir(void)
248 printf("Checking for tmpfs mount on /dev/shm...");
250 path = realpath("/dev/shm", NULL);
252 printf("failed to check real path, errno = %d\n", errno);
255 printf("%s...", path);
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.
261 bufsize = octal_encoded_length(path, " \t\n\\") + 2;
266 buf = malloc(bufsize);
268 printf("malloc failed, errno = %d\n", errno);
273 fd = open("/proc/mounts", O_RDONLY);
275 printf("failed to open /proc/mounts, errno = %d\n", errno);
279 outcome = OUTCOME_NOTHING_MOUNTED;
281 found = read_mount(fd, buf, bufsize, path, &outcome);
287 printf("read returned errno %d\n", -found);
290 case OUTCOME_TMPFS_MOUNT:
292 default_tmpdir = "/dev/shm";
295 case OUTCOME_NON_TMPFS_MOUNT:
296 printf("not tmpfs\n");
300 printf("nothing mounted on /dev/shm\n");
312 static int __init make_tempfile(const char *template, char **out_tempname,
319 tempname = malloc(MAXPATHLEN);
320 if (tempname == NULL)
324 if ((tempdir == NULL) || (strlen(tempdir) >= MAXPATHLEN))
327 if (template[0] != '/')
328 strcpy(tempname, tempdir);
331 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
332 fd = mkstemp(tempname);
334 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
338 if (do_unlink && (unlink(tempname) < 0)) {
343 *out_tempname = tempname;
354 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
356 static int __init create_tmp_file(unsigned long long len)
361 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
365 err = fchmod(fd, 0777);
372 * Seek to len - 1 because writing a character there will
373 * increase the file size by one byte, to the desired length.
375 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
382 err = write(fd, &zero, 1);
391 int __init create_mem_file(unsigned long long len)
395 fd = create_tmp_file(len);
397 err = os_set_exec_close(fd);
400 perror("exec_close");
406 void __init check_tmpexec(void)
409 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
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);
415 if (addr == MAP_FAILED) {
420 printf("%s must be not mounted noexec\n",tempdir);
424 munmap(addr, UM_KERN_PAGE_SIZE);