uml: style fixes in arch/um/os-Linux
[deliverable/linux.git] / arch / um / os-Linux / mem.c
1 /*
2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
6 #include <stdio.h>
7 #include <stddef.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <sys/mman.h>
14 #include <sys/param.h>
15 #include "init.h"
16 #include "kern_constants.h"
17 #include "os.h"
18 #include "user.h"
19
20 /* Modified by which_tmpdir, which is called during early boot */
21 static char *default_tmpdir = "/tmp";
22
23 /*
24 * Modified when creating the physical memory file and when checking
25 * the tmp filesystem for usability, both happening during early boot.
26 */
27 static char *tempdir = NULL;
28
29 static void __init find_tempdir(void)
30 {
31 const char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
32 int i;
33 char *dir = NULL;
34
35 if (tempdir != NULL)
36 /* We've already been called */
37 return;
38 for (i = 0; dirs[i]; i++) {
39 dir = getenv(dirs[i]);
40 if ((dir != NULL) && (*dir != '\0'))
41 break;
42 }
43 if ((dir == NULL) || (*dir == '\0'))
44 dir = default_tmpdir;
45
46 tempdir = malloc(strlen(dir) + 2);
47 if (tempdir == NULL) {
48 fprintf(stderr, "Failed to malloc tempdir, "
49 "errno = %d\n", errno);
50 return;
51 }
52 strcpy(tempdir, dir);
53 strcat(tempdir, "/");
54 }
55
56 /*
57 * This will return 1, with the first character in buf being the
58 * 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 */
62 static int next(int fd, char *buf, size_t size, char c)
63 {
64 ssize_t n;
65 size_t len;
66 char *ptr;
67
68 while ((ptr = strchr(buf, c)) == NULL) {
69 n = read(fd, buf, size - 1);
70 if (n == 0)
71 return 0;
72 else if (n < 0)
73 return -errno;
74
75 buf[n] = '\0';
76 }
77
78 ptr++;
79 len = strlen(ptr);
80 memmove(buf, ptr, len + 1);
81
82 /*
83 * Refill the buffer so that if there's a partial string that we care
84 * about, it will be completed, and we can recognize it.
85 */
86 n = read(fd, &buf[len], size - len - 1);
87 if (n < 0)
88 return -errno;
89
90 buf[len + n] = '\0';
91 return 1;
92 }
93
94 /* which_tmpdir is called only during early boot */
95 static int checked_tmpdir = 0;
96
97 /*
98 * Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
99 * 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 */
108 static void which_tmpdir(void)
109 {
110 int fd, found;
111 char buf[128] = { '\0' };
112
113 if (checked_tmpdir)
114 return;
115
116 checked_tmpdir = 1;
117
118 printf("Checking for tmpfs mount on /dev/shm...");
119
120 fd = open("/proc/mounts", O_RDONLY);
121 if (fd < 0) {
122 printf("failed to open /proc/mounts, errno = %d\n", errno);
123 return;
124 }
125
126 while (1) {
127 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
128 if (found != 1)
129 break;
130
131 if (!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
132 goto found;
133
134 found = next(fd, buf, ARRAY_SIZE(buf), '\n');
135 if (found != 1)
136 break;
137 }
138
139 err:
140 if (found == 0)
141 printf("nothing mounted on /dev/shm\n");
142 else if (found < 0)
143 printf("read returned errno %d\n", -found);
144
145 out:
146 close(fd);
147
148 return;
149
150 found:
151 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
152 if (found != 1)
153 goto err;
154
155 if (strncmp(buf, "tmpfs", strlen("tmpfs"))) {
156 printf("not tmpfs\n");
157 goto out;
158 }
159
160 printf("OK\n");
161 default_tmpdir = "/dev/shm";
162 goto out;
163 }
164
165 /*
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 */
170 static int __init make_tempfile(const char *template, char **out_tempname,
171 int do_unlink)
172 {
173 char *tempname;
174 int fd;
175
176 which_tmpdir();
177 tempname = malloc(MAXPATHLEN);
178 if (!tempname)
179 goto out;
180
181 find_tempdir();
182 if (template[0] != '/')
183 strcpy(tempname, tempdir);
184 else
185 tempname[0] = '\0';
186 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
187 fd = mkstemp(tempname);
188 if (fd < 0) {
189 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
190 strerror(errno));
191 goto out;
192 }
193 if (do_unlink && (unlink(tempname) < 0)) {
194 perror("unlink");
195 goto out;
196 }
197 if (out_tempname) {
198 *out_tempname = tempname;
199 } else {
200 free(tempname);
201 }
202 return fd;
203 out:
204 free(tempname);
205 return -1;
206 }
207
208 #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
209
210 static int __init create_tmp_file(unsigned long long len)
211 {
212 int fd, err;
213 char zero;
214
215 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
216 if (fd < 0)
217 exit(1);
218
219 err = fchmod(fd, 0777);
220 if (err < 0) {
221 perror("fchmod");
222 exit(1);
223 }
224
225 /*
226 * Seek to len - 1 because writing a character there will
227 * increase the file size by one byte, to the desired length.
228 */
229 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
230 perror("lseek64");
231 exit(1);
232 }
233
234 zero = 0;
235
236 err = write(fd, &zero, 1);
237 if (err != 1) {
238 perror("write");
239 exit(1);
240 }
241
242 return fd;
243 }
244
245 int __init create_mem_file(unsigned long long len)
246 {
247 int err, fd;
248
249 fd = create_tmp_file(len);
250
251 err = os_set_exec_close(fd);
252 if (err < 0) {
253 errno = -err;
254 perror("exec_close");
255 }
256 return fd;
257 }
258
259
260 void __init check_tmpexec(void)
261 {
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);
269 if (addr == MAP_FAILED) {
270 err = errno;
271 perror("failed");
272 close(fd);
273 if (err == EPERM)
274 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 }
This page took 0.036661 seconds and 5 git commands to generate.