aboutsummaryrefslogtreecommitdiffstats
path: root/arch/um/os-Linux/mem.c
diff options
context:
space:
mode:
Diffstat (limited to 'arch/um/os-Linux/mem.c')
-rw-r--r--arch/um/os-Linux/mem.c101
1 files changed, 50 insertions, 51 deletions
diff --git a/arch/um/os-Linux/mem.c b/arch/um/os-Linux/mem.c
index eedc2d88ef8a..38742c21def5 100644
--- a/arch/um/os-Linux/mem.c
+++ b/arch/um/os-Linux/mem.c
@@ -1,22 +1,21 @@
1/*
2 * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
5
1#include <stdio.h> 6#include <stdio.h>
2#include <stdlib.h>
3#include <stddef.h> 7#include <stddef.h>
4#include <stdarg.h> 8#include <stdlib.h>
5#include <unistd.h> 9#include <unistd.h>
6#include <errno.h> 10#include <errno.h>
7#include <string.h>
8#include <fcntl.h> 11#include <fcntl.h>
9#include <sys/types.h> 12#include <string.h>
10#include <sys/mman.h> 13#include <sys/mman.h>
11#include <sys/statfs.h> 14#include <sys/param.h>
12#include "user.h"
13#include "mem_user.h"
14#include "init.h" 15#include "init.h"
15#include "os.h"
16#include "tempfile.h"
17#include "kern_constants.h" 16#include "kern_constants.h"
18 17#include "os.h"
19#include <sys/param.h> 18#include "user.h"
20 19
21/* Modified by which_tmpdir, which is called during early boot */ 20/* Modified by which_tmpdir, which is called during early boot */
22static char *default_tmpdir = "/tmp"; 21static char *default_tmpdir = "/tmp";
@@ -33,18 +32,19 @@ static void __init find_tempdir(void)
33 int i; 32 int i;
34 char *dir = NULL; 33 char *dir = NULL;
35 34
36 if(tempdir != NULL) /* We've already been called */ 35 if (tempdir != NULL)
36 /* We've already been called */
37 return; 37 return;
38 for(i = 0; dirs[i]; i++){ 38 for (i = 0; dirs[i]; i++) {
39 dir = getenv(dirs[i]); 39 dir = getenv(dirs[i]);
40 if((dir != NULL) && (*dir != '\0')) 40 if ((dir != NULL) && (*dir != '\0'))
41 break; 41 break;
42 } 42 }
43 if((dir == NULL) || (*dir == '\0')) 43 if ((dir == NULL) || (*dir == '\0'))
44 dir = default_tmpdir; 44 dir = default_tmpdir;
45 45
46 tempdir = malloc(strlen(dir) + 2); 46 tempdir = malloc(strlen(dir) + 2);
47 if(tempdir == NULL){ 47 if (tempdir == NULL) {
48 fprintf(stderr, "Failed to malloc tempdir, " 48 fprintf(stderr, "Failed to malloc tempdir, "
49 "errno = %d\n", errno); 49 "errno = %d\n", errno);
50 return; 50 return;
@@ -53,7 +53,8 @@ static void __init find_tempdir(void)
53 strcat(tempdir, "/"); 53 strcat(tempdir, "/");
54} 54}
55 55
56/* This will return 1, with the first character in buf being the 56/*
57 * This will return 1, with the first character in buf being the
57 * character following the next instance of c in the file. This will 58 * character following the next instance of c in the file. This will
58 * read the file as needed. If there's an error, -errno is returned; 59 * read the file as needed. If there's an error, -errno is returned;
59 * if the end of the file is reached, 0 is returned. 60 * if the end of the file is reached, 0 is returned.
@@ -64,11 +65,11 @@ static int next(int fd, char *buf, size_t size, char c)
64 size_t len; 65 size_t len;
65 char *ptr; 66 char *ptr;
66 67
67 while((ptr = strchr(buf, c)) == NULL){ 68 while ((ptr = strchr(buf, c)) == NULL) {
68 n = read(fd, buf, size - 1); 69 n = read(fd, buf, size - 1);
69 if(n == 0) 70 if (n == 0)
70 return 0; 71 return 0;
71 else if(n < 0) 72 else if (n < 0)
72 return -errno; 73 return -errno;
73 74
74 buf[n] = '\0'; 75 buf[n] = '\0';
@@ -78,11 +79,12 @@ static int next(int fd, char *buf, size_t size, char c)
78 len = strlen(ptr); 79 len = strlen(ptr);
79 memmove(buf, ptr, len + 1); 80 memmove(buf, ptr, len + 1);
80 81
81 /* Refill the buffer so that if there's a partial string that we care 82 /*
83 * Refill the buffer so that if there's a partial string that we care
82 * about, it will be completed, and we can recognize it. 84 * about, it will be completed, and we can recognize it.
83 */ 85 */
84 n = read(fd, &buf[len], size - len - 1); 86 n = read(fd, &buf[len], size - len - 1);
85 if(n < 0) 87 if (n < 0)
86 return -errno; 88 return -errno;
87 89
88 buf[len + n] = '\0'; 90 buf[len + n] = '\0';
@@ -92,7 +94,8 @@ static int next(int fd, char *buf, size_t size, char c)
92/* which_tmpdir is called only during early boot */ 94/* which_tmpdir is called only during early boot */
93static int checked_tmpdir = 0; 95static int checked_tmpdir = 0;
94 96
95/* Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner 97/*
98 * Look for a tmpfs mounted at /dev/shm. I couldn't find a cleaner
96 * way to do this than to parse /proc/mounts. statfs will return the 99 * way to do this than to parse /proc/mounts. statfs will return the
97 * same filesystem magic number and fs id for both /dev and /dev/shm 100 * same filesystem magic number and fs id for both /dev and /dev/shm
98 * when they are both tmpfs, so you can't tell if they are different 101 * when they are both tmpfs, so you can't tell if they are different
@@ -107,7 +110,7 @@ static void which_tmpdir(void)
107 int fd, found; 110 int fd, found;
108 char buf[128] = { '\0' }; 111 char buf[128] = { '\0' };
109 112
110 if(checked_tmpdir) 113 if (checked_tmpdir)
111 return; 114 return;
112 115
113 checked_tmpdir = 1; 116 checked_tmpdir = 1;
@@ -115,28 +118,28 @@ static void which_tmpdir(void)
115 printf("Checking for tmpfs mount on /dev/shm..."); 118 printf("Checking for tmpfs mount on /dev/shm...");
116 119
117 fd = open("/proc/mounts", O_RDONLY); 120 fd = open("/proc/mounts", O_RDONLY);
118 if(fd < 0){ 121 if (fd < 0) {
119 printf("failed to open /proc/mounts, errno = %d\n", errno); 122 printf("failed to open /proc/mounts, errno = %d\n", errno);
120 return; 123 return;
121 } 124 }
122 125
123 while(1){ 126 while (1) {
124 found = next(fd, buf, ARRAY_SIZE(buf), ' '); 127 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
125 if(found != 1) 128 if (found != 1)
126 break; 129 break;
127 130
128 if(!strncmp(buf, "/dev/shm", strlen("/dev/shm"))) 131 if (!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
129 goto found; 132 goto found;
130 133
131 found = next(fd, buf, ARRAY_SIZE(buf), '\n'); 134 found = next(fd, buf, ARRAY_SIZE(buf), '\n');
132 if(found != 1) 135 if (found != 1)
133 break; 136 break;
134 } 137 }
135 138
136err: 139err:
137 if(found == 0) 140 if (found == 0)
138 printf("nothing mounted on /dev/shm\n"); 141 printf("nothing mounted on /dev/shm\n");
139 else if(found < 0) 142 else if (found < 0)
140 printf("read returned errno %d\n", -found); 143 printf("read returned errno %d\n", -found);
141 144
142out: 145out:
@@ -146,10 +149,10 @@ out:
146 149
147found: 150found:
148 found = next(fd, buf, ARRAY_SIZE(buf), ' '); 151 found = next(fd, buf, ARRAY_SIZE(buf), ' ');
149 if(found != 1) 152 if (found != 1)
150 goto err; 153 goto err;
151 154
152 if(strncmp(buf, "tmpfs", strlen("tmpfs"))){ 155 if (strncmp(buf, "tmpfs", strlen("tmpfs"))) {
153 printf("not tmpfs\n"); 156 printf("not tmpfs\n");
154 goto out; 157 goto out;
155 } 158 }
@@ -164,8 +167,8 @@ found:
164 * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger). 167 * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
165 * So it isn't 'static' yet. 168 * So it isn't 'static' yet.
166 */ 169 */
167int __init make_tempfile(const char *template, char **out_tempname, 170static int __init make_tempfile(const char *template, char **out_tempname,
168 int do_unlink) 171 int do_unlink)
169{ 172{
170 char *tempname; 173 char *tempname;
171 int fd; 174 int fd;
@@ -182,16 +185,16 @@ int __init make_tempfile(const char *template, char **out_tempname,
182 tempname[0] = '\0'; 185 tempname[0] = '\0';
183 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname)); 186 strncat(tempname, template, MAXPATHLEN-1-strlen(tempname));
184 fd = mkstemp(tempname); 187 fd = mkstemp(tempname);
185 if(fd < 0){ 188 if (fd < 0) {
186 fprintf(stderr, "open - cannot create %s: %s\n", tempname, 189 fprintf(stderr, "open - cannot create %s: %s\n", tempname,
187 strerror(errno)); 190 strerror(errno));
188 goto out; 191 goto out;
189 } 192 }
190 if(do_unlink && (unlink(tempname) < 0)){ 193 if (do_unlink && (unlink(tempname) < 0)) {
191 perror("unlink"); 194 perror("unlink");
192 goto out; 195 goto out;
193 } 196 }
194 if(out_tempname){ 197 if (out_tempname) {
195 *out_tempname = tempname; 198 *out_tempname = tempname;
196 } else { 199 } else {
197 free(tempname); 200 free(tempname);
@@ -204,27 +207,23 @@ out:
204 207
205#define TEMPNAME_TEMPLATE "vm_file-XXXXXX" 208#define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
206 209
207/* 210static int __init create_tmp_file(unsigned long long len)
208 * This proc is used in start_up.c
209 * So it isn't 'static'.
210 */
211int __init create_tmp_file(unsigned long long len)
212{ 211{
213 int fd, err; 212 int fd, err;
214 char zero; 213 char zero;
215 214
216 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1); 215 fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
217 if(fd < 0) { 216 if (fd < 0)
218 exit(1); 217 exit(1);
219 }
220 218
221 err = fchmod(fd, 0777); 219 err = fchmod(fd, 0777);
222 if(err < 0){ 220 if (err < 0) {
223 perror("fchmod"); 221 perror("fchmod");
224 exit(1); 222 exit(1);
225 } 223 }
226 224
227 /* Seek to len - 1 because writing a character there will 225 /*
226 * Seek to len - 1 because writing a character there will
228 * increase the file size by one byte, to the desired length. 227 * increase the file size by one byte, to the desired length.
229 */ 228 */
230 if (lseek64(fd, len - 1, SEEK_SET) < 0) { 229 if (lseek64(fd, len - 1, SEEK_SET) < 0) {
@@ -235,7 +234,7 @@ int __init create_tmp_file(unsigned long long len)
235 zero = 0; 234 zero = 0;
236 235
237 err = write(fd, &zero, 1); 236 err = write(fd, &zero, 1);
238 if(err != 1){ 237 if (err != 1) {
239 perror("write"); 238 perror("write");
240 exit(1); 239 exit(1);
241 } 240 }
@@ -250,7 +249,7 @@ int __init create_mem_file(unsigned long long len)
250 fd = create_tmp_file(len); 249 fd = create_tmp_file(len);
251 250
252 err = os_set_exec_close(fd); 251 err = os_set_exec_close(fd);
253 if(err < 0){ 252 if (err < 0) {
254 errno = -err; 253 errno = -err;
255 perror("exec_close"); 254 perror("exec_close");
256 } 255 }
@@ -267,11 +266,11 @@ void __init check_tmpexec(void)
267 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0); 266 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
268 printf("Checking PROT_EXEC mmap in %s...",tempdir); 267 printf("Checking PROT_EXEC mmap in %s...",tempdir);
269 fflush(stdout); 268 fflush(stdout);
270 if(addr == MAP_FAILED){ 269 if (addr == MAP_FAILED) {
271 err = errno; 270 err = errno;
272 perror("failed"); 271 perror("failed");
273 close(fd); 272 close(fd);
274 if(err == EPERM) 273 if (err == EPERM)
275 printf("%s must be not mounted noexec\n",tempdir); 274 printf("%s must be not mounted noexec\n",tempdir);
276 exit(1); 275 exit(1);
277 } 276 }