blob: 38742c21def52499788ad6175674d1b6db0162fa [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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-04-18 22:21:43 -070063{
WANG Congc0a92902008-02-04 22:30:41 -080064 ssize_t n;
65 size_t len;
Rob Landley966a082f2006-04-18 22:21:43 -070066 char *ptr;
67
Jeff Dike5134d8f2008-02-08 04:22:08 -080068 while ((ptr = strchr(buf, c)) == NULL) {
Rob Landley966a082f2006-04-18 22:21:43 -070069 n = read(fd, buf, size - 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -080070 if (n == 0)
Rob Landley966a082f2006-04-18 22:21:43 -070071 return 0;
Jeff Dike5134d8f2008-02-08 04:22:08 -080072 else if (n < 0)
Rob Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-04-18 22:21:43 -0700136 break;
137 }
138
139err:
Jeff Dike5134d8f2008-02-08 04:22:08 -0800140 if (found == 0)
Rob Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-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 Landley966a082f2006-04-18 22:21:43 -0700153 goto err;
154
Jeff Dike5134d8f2008-02-08 04:22:08 -0800155 if (strncmp(buf, "tmpfs", strlen("tmpfs"))) {
Rob Landley966a082f2006-04-18 22:21:43 -0700156 printf("not tmpfs\n");
Jeff Dike80c13742006-09-29 01:58:51 -0700157 goto out;
Rob Landley966a082f2006-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 Landley966a082f2006-04-18 22:21:43 -0700163}
164
Jeff Dike0f80bc82005-09-16 19:27:50 -0700165/*
166 * This proc still used in tt-mode
167 * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
168 * So it isn't 'static' yet.
169 */
Jeff Dike5134d8f2008-02-08 04:22:08 -0800170static int __init make_tempfile(const char *template, char **out_tempname,
171 int do_unlink)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700172{
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700173 char *tempname;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700174 int fd;
175
Rob Landley966a082f2006-04-18 22:21:43 -0700176 which_tmpdir();
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700177 tempname = malloc(MAXPATHLEN);
WANG Congc9a30722008-02-04 22:30:35 -0800178 if (!tempname)
179 goto out;
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700180
Jeff Dike0f80bc82005-09-16 19:27:50 -0700181 find_tempdir();
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700182 if (template[0] != '/')
Jeff Dike0f80bc82005-09-16 19:27:50 -0700183 strcpy(tempname, tempdir);
184 else
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700185 tempname[0] = '\0';
WANG Congc9a30722008-02-04 22:30:35 -0800186 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
Jeff Dike0f80bc82005-09-16 19:27:50 -0700187 fd = mkstemp(tempname);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800188 if (fd < 0) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700189 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
190 strerror(errno));
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700191 goto out;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700192 }
Jeff Dike5134d8f2008-02-08 04:22:08 -0800193 if (do_unlink && (unlink(tempname) < 0)) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700194 perror("unlink");
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700195 goto out;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700196 }
Jeff Dike5134d8f2008-02-08 04:22:08 -0800197 if (out_tempname) {
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700198 *out_tempname = tempname;
199 } else {
200 free(tempname);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700201 }
Jeff Dike81999a02007-02-10 01:44:21 -0800202 return fd;
Paolo 'Blaisorblade' Giarrusso87276f72006-04-10 22:53:39 -0700203out:
204 free(tempname);
205 return -1;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700206}
207
208#define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
209
Jeff Dike5134d8f2008-02-08 04:22:08 -0800210static int __init create_tmp_file(unsigned long long len)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700211{
212 int fd, err;
213 char zero;
214
215 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800216 if (fd < 0)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700217 exit(1);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700218
219 err = fchmod(fd, 0777);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800220 if (err < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700221 perror("fchmod");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700222 exit(1);
223 }
224
Jeff Dike5134d8f2008-02-08 04:22:08 -0800225 /*
226 * Seek to len - 1 because writing a character there will
Jeff Dike190f4932006-06-30 01:55:55 -0700227 * increase the file size by one byte, to the desired length.
228 */
229 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
Jeff Dike512b6fb2007-10-16 01:27:11 -0700230 perror("lseek64");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700231 exit(1);
232 }
233
234 zero = 0;
235
Jeff Dikea61f3342007-05-06 14:51:35 -0700236 err = write(fd, &zero, 1);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800237 if (err != 1) {
Jeff Dikea61f3342007-05-06 14:51:35 -0700238 perror("write");
Jeff Dike0f80bc82005-09-16 19:27:50 -0700239 exit(1);
240 }
241
Jeff Dike81999a02007-02-10 01:44:21 -0800242 return fd;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700243}
244
Jeff Dike36e45462007-05-06 14:51:11 -0700245int __init create_mem_file(unsigned long long len)
Jeff Dike0f80bc82005-09-16 19:27:50 -0700246{
247 int err, fd;
248
Jeff Dike02dea082006-03-31 02:30:08 -0800249 fd = create_tmp_file(len);
Jeff Dike0f80bc82005-09-16 19:27:50 -0700250
Jeff Dike512b6fb2007-10-16 01:27:11 -0700251 err = os_set_exec_close(fd);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800252 if (err < 0) {
Jeff Dike0f80bc82005-09-16 19:27:50 -0700253 errno = -err;
254 perror("exec_close");
255 }
Jeff Dike81999a02007-02-10 01:44:21 -0800256 return fd;
Jeff Dike0f80bc82005-09-16 19:27:50 -0700257}
Rob Landley966a082f2006-04-18 22:21:43 -0700258
259
Jeff Dike36e45462007-05-06 14:51:11 -0700260void __init check_tmpexec(void)
Rob Landley966a082f2006-04-18 22:21:43 -0700261{
262 void *addr;
263 int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
264
265 addr = mmap(NULL, UM_KERN_PAGE_SIZE,
266 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
267 printf("Checking PROT_EXEC mmap in %s...",tempdir);
268 fflush(stdout);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800269 if (addr == MAP_FAILED) {
Rob Landley966a082f2006-04-18 22:21:43 -0700270 err = errno;
271 perror("failed");
WANG Congc9a30722008-02-04 22:30:35 -0800272 close(fd);
Jeff Dike5134d8f2008-02-08 04:22:08 -0800273 if (err == EPERM)
Rob Landley966a082f2006-04-18 22:21:43 -0700274 printf("%s must be not mounted noexec\n",tempdir);
275 exit(1);
276 }
277 printf("OK\n");
278 munmap(addr, UM_KERN_PAGE_SIZE);
279
280 close(fd);
281}