blob: 93a11d7edfa0505a7acfd9bd99a0266aec97f933 [file] [log] [blame]
Jeff Dike5134d8f2008-02-08 04:22:08 -08001/*
2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
Jeff Dike0f80bc82005-09-16 19:27:50 -07006#include <stdio.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07007#include <stddef.h>
Jeff Dike5134d8f2008-02-08 04:22:08 -08008#include <stdlib.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -07009#include <unistd.h>
10#include <errno.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070011#include <fcntl.h>
Jeff Dike5134d8f2008-02-08 04:22:08 -080012#include <string.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070013#include <sys/mman.h>
Jeff Dike0f80bc82005-09-16 19:27:50 -070014#include <sys/param.h>
Jeff Dike5134d8f2008-02-08 04:22:08 -080015#include "init.h"
16#include "kern_constants.h"
17#include "os.h"
18#include "user.h"
Jeff Dike0f80bc82005-09-16 19:27:50 -070019
Jeff Dike6bf79482007-02-10 01:44:18 -080020/* Modified by which_tmpdir, which is called during early boot */
Rob Landley966a0822006-04-18 22:21:43 -070021static char *default_tmpdir = "/tmp";
Jeff Dike6bf79482007-02-10 01:44:18 -080022
23/*
24 * Modified when creating the physical memory file and when checking
25 * the tmp filesystem for usability, both happening during early boot.
26 */
Jeff Dike0f80bc82005-09-16 19:27:50 -070027static char *tempdir = NULL;
28
29static void __init find_tempdir(void)
30{
WANG Congc0a92902008-02-04 22:30:41 -080031 const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
Jeff Dike0f80bc82005-09-16 19:27:50 -070032 int i;
33 char *dir = NULL;
34
Jeff Dike5134d8f2008-02-08 04:22:08 -080035 if (tempdir != NULL)
36 /* We've already been called */
Jeff Dike81999a02007-02-10 01:44:21 -080037 return;
Jeff Dike5134d8f2008-02-08 04:22:08 -080038 for (i = 0; dirs[i]; i++) {
Jeff Dike0f80bc82005-09-16 19:27:50 -070039 dir = getenv(dirs[i]);
Jeff Dike5134d8f2008-02-08 04:22:08 -080040 if ((dir != NULL) && (*dir != '\0'))
Jeff Dike0f80bc82005-09-16 19:27:50 -070041 break;
42 }
Jeff Dike5134d8f2008-02-08 04:22:08 -080043 if ((dir == NULL) || (*dir == '\0'))
Rob Landley966a0822006-04-18 22:21:43 -070044 dir = default_tmpdir;
Jeff Dike0f80bc82005-09-16 19:27:50 -070045
46 tempdir = malloc(strlen(dir) + 2);
Jeff Dike5134d8f2008-02-08 04:22:08 -080047 if (tempdir == NULL) {
Jeff Dike0f80bc82005-09-16 19:27:50 -070048 fprintf(stderr, "Failed to malloc tempdir, "
49 "errno = %d\n", errno);
50 return;
51 }
52 strcpy(tempdir, dir);
53 strcat(tempdir, "/");
54}
55
Jeff Dike5134d8f2008-02-08 04:22:08 -080056/*
57 * This will return 1, with the first character in buf being the
Rob Landley966a0822006-04-18 22:21:43 -070058 * 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.
61 */
WANG Congc0a92902008-02-04 22:30:41 -080062static int next(int fd, char *buf, size_t size, char c)
Rob Landley966a0822006-04-18 22:21:43 -070063{
WANG Congc0a92902008-02-04 22:30:41 -080064 ssize_t n;
65 size_t len;
Rob Landley966a0822006-04-18 22:21:43 -070066 char *ptr;
67
Jeff Dike5134d8f2008-02-08 04:22:08 -080068 while ((ptr = strchr(buf, c)) == NULL) {
Rob Landley966a0822006-04-18 22:21:43 -070069 n = read(fd, buf, size - 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -080070 if (n == 0)
Rob Landley966a0822006-04-18 22:21:43 -070071 return 0;
Jeff Dike5134d8f2008-02-08 04:22:08 -080072 else if (n < 0)
Rob Landley966a0822006-04-18 22:21:43 -070073 return -errno;
74
75 buf[n] = '\0';
76 }
77
78 ptr++;
Jeff Dikec2b7a4b2006-06-30 01:55:54 -070079 len = strlen(ptr);
80 memmove(buf, ptr, len + 1);
81
Jeff Dike5134d8f2008-02-08 04:22:08 -080082 /*
83 * Refill the buffer so that if there's a partial string that we care
Jeff Dikec2b7a4b2006-06-30 01:55:54 -070084 * about, it will be completed, and we can recognize it.
85 */
86 n = read(fd, &buf[len], size - len - 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -080087 if (n < 0)
Jeff Dikec2b7a4b2006-06-30 01:55:54 -070088 return -errno;
89
90 buf[len + n] = '\0';
Rob Landley966a0822006-04-18 22:21:43 -070091 return 1;
92}
93
Jeff Dike6bf79482007-02-10 01:44:18 -080094/* which_tmpdir is called only during early boot */
Rob Landley966a0822006-04-18 22:21:43 -070095static int checked_tmpdir = 0;
96
Jeff Dike5134d8f2008-02-08 04:22:08 -080097/*
98 * Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
Rob Landley966a0822006-04-18 22:21:43 -070099 * way to do this than to parse /proc/mounts. statfs will return the
100 * same filesystem magic number and fs id for both /dev and /dev/shm
101 * when they are both tmpfs, so you can't tell if they are different
102 * filesystems. Also, there seems to be no other way of finding the
103 * mount point of a filesystem from within it.
104 *
105 * If a /dev/shm tmpfs entry is found, then we switch to using it.
106 * Otherwise, we stay with the default /tmp.
107 */
108static void which_tmpdir(void)
109{
110 int fd, found;
111 char buf[128] = { '\0' };
112
Jeff Dike5134d8f2008-02-08 04:22:08 -0800113 if (checked_tmpdir)
Rob Landley966a0822006-04-18 22:21:43 -0700114 return;
115
116 checked_tmpdir = 1;
117
118 printf("Checking for tmpfs mount on /dev/shm...");
119
120 fd = open("/proc/mounts", O_RDONLY);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800121 if (fd < 0) {
Rob Landley966a0822006-04-18 22:21:43 -0700122 printf("failed to open /proc/mounts, errno = %d\n", errno);
123 return;
124 }
125
Jeff Dike5134d8f2008-02-08 04:22:08 -0800126 while (1) {
Jeff Dike91b165c2006-09-25 23:33:00 -0700127 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
Jeff Dike5134d8f2008-02-08 04:22:08 -0800128 if (found != 1)
Rob Landley966a0822006-04-18 22:21:43 -0700129 break;
130
Jeff Dike5134d8f2008-02-08 04:22:08 -0800131 if (!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
Rob Landley966a0822006-04-18 22:21:43 -0700132 goto found;
133
Jeff Dike91b165c2006-09-25 23:33:00 -0700134 found = next(fd, buf, ARRAY_SIZE(buf), '\n');
Jeff Dike5134d8f2008-02-08 04:22:08 -0800135 if (found != 1)
Rob Landley966a0822006-04-18 22:21:43 -0700136 break;
137 }
138
139err:
Jeff Dike5134d8f2008-02-08 04:22:08 -0800140 if (found == 0)
Rob Landley966a0822006-04-18 22:21:43 -0700141 printf("nothing mounted on /dev/shm\n");
Jeff Dike5134d8f2008-02-08 04:22:08 -0800142 else if (found < 0)
Rob Landley966a0822006-04-18 22:21:43 -0700143 printf("read returned errno %d\n", -found);
144
Jeff Dike80c13742006-09-29 01:58:51 -0700145out:
146 close(fd);
147
Rob Landley966a0822006-04-18 22:21:43 -0700148 return;
149
150found:
Jeff Dike91b165c2006-09-25 23:33:00 -0700151 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
Jeff Dike5134d8f2008-02-08 04:22:08 -0800152 if (found != 1)
Rob Landley966a0822006-04-18 22:21:43 -0700153 goto err;
154
Jeff Dike5134d8f2008-02-08 04:22:08 -0800155 if (strncmp(buf, "tmpfs", strlen("tmpfs"))) {
Rob Landley966a0822006-04-18 22:21:43 -0700156 printf("not tmpfs\n");
Jeff Dike80c13742006-09-29 01:58:51 -0700157 goto out;
Rob Landley966a0822006-04-18 22:21:43 -0700158 }
159
160 printf("OK\n");
161 default_tmpdir = "/dev/shm";
Jeff Dike80c13742006-09-29 01:58:51 -0700162 goto out;
Rob Landley966a0822006-04-18 22:21:43 -0700163}
164
Jeff Dike5134d8f2008-02-08 04:22:08 -0800165static int __init make_tempfile(const char *template, char **out_tempname,
166 int do_unlink)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700167{
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700168 char *tempname;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700169 int fd;
170
Rob Landley966a0822006-04-18 22:21:43 -0700171 which_tmpdir();
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700172 tempname = malloc(MAXPATHLEN);
Jim Meyering11a7ac22008-02-08 04:22:09 -0800173 if (tempname == NULL)
174 return -1;
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700175
Jeff Dike0f80bc82005-09-16 19:27:50 -0700176 find_tempdir();
Jim Meyering11a7ac22008-02-08 04:22:09 -0800177 if ((tempdir == NULL) || (strlen(tempdir) >= MAXPATHLEN))
178 return -1;
179
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700180 if (template[0] != '/')
Jeff Dike0f80bc82005-09-16 19:27:50 -0700181 strcpy(tempname, tempdir);
182 else
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700183 tempname[0] = '\0';
WANG Congc9a30722008-02-04 22:30:35 -0800184 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
Jeff Dike0f80bc82005-09-16 19:27:50 -0700185 fd = mkstemp(tempname);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800186 if (fd < 0) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700187 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
188 strerror(errno));
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700189 goto out;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700190 }
Jeff Dike5134d8f2008-02-08 04:22:08 -0800191 if (do_unlink && (unlink(tempname) < 0)) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700192 perror("unlink");
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700193 goto out;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700194 }
Jeff Dike5134d8f2008-02-08 04:22:08 -0800195 if (out_tempname) {
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700196 *out_tempname = tempname;
Jim Meyering11a7ac22008-02-08 04:22:09 -0800197 } else
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700198 free(tempname);
Jeff Dike81999a02007-02-10 01:44:21 -0800199 return fd;
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700200out:
201 free(tempname);
202 return -1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700203}
204
205#define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
206
Jeff Dike5134d8f2008-02-08 04:22:08 -0800207static int __init create_tmp_file(unsigned long long len)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700208{
209 int fd, err;
210 char zero;
211
212 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800213 if (fd < 0)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700214 exit(1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700215
216 err = fchmod(fd, 0777);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800217 if (err < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700218 perror("fchmod");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700219 exit(1);
220 }
221
Jeff Dike5134d8f2008-02-08 04:22:08 -0800222 /*
223 * Seek to len - 1 because writing a character there will
Jeff Dike190f4932006-06-30 01:55:55 -0700224 * increase the file size by one byte, to the desired length.
225 */
226 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700227 perror("lseek64");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700228 exit(1);
229 }
230
231 zero = 0;
232
Jeff Dikea61f3342007-05-06 14:51:35 -0700233 err = write(fd, &zero, 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800234 if (err != 1) {
Jeff Dikea61f3342007-05-06 14:51:35 -0700235 perror("write");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700236 exit(1);
237 }
238
Jeff Dike81999a02007-02-10 01:44:21 -0800239 return fd;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700240}
241
Jeff Dike36e45462007-05-06 14:51:11 -0700242int __init create_mem_file(unsigned long long len)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700243{
244 int err, fd;
245
Jeff Dike02dea082006-03-31 02:30:08 -0800246 fd = create_tmp_file(len);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700247
Jeff Dike512b6fb2007-10-16 01:27:11 -0700248 err = os_set_exec_close(fd);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800249 if (err < 0) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700250 errno = -err;
251 perror("exec_close");
252 }
Jeff Dike81999a02007-02-10 01:44:21 -0800253 return fd;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700254}
Rob Landley966a0822006-04-18 22:21:43 -0700255
256
Jeff Dike36e45462007-05-06 14:51:11 -0700257void __init check_tmpexec(void)
Rob Landley966a0822006-04-18 22:21:43 -0700258{
259 void *addr;
260 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
261
262 addr = mmap(NULL, UM_KERN_PAGE_SIZE,
263 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
264 printf("Checking PROT_EXEC mmap in %s...",tempdir);
265 fflush(stdout);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800266 if (addr == MAP_FAILED) {
Rob Landley966a0822006-04-18 22:21:43 -0700267 err = errno;
268 perror("failed");
WANG Congc9a30722008-02-04 22:30:35 -0800269 close(fd);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800270 if (err == EPERM)
Rob Landley966a0822006-04-18 22:21:43 -0700271 printf("%s must be not mounted noexec\n",tempdir);
272 exit(1);
273 }
274 printf("OK\n");
275 munmap(addr, UM_KERN_PAGE_SIZE);
276
277 close(fd);
278}