xref: /linux/arch/um/os-Linux/mem.c (revision 81999a01c786e26c2058968d76b595df60d2f8da)
10f80bc85SJeff Dike #include <stdio.h>
20f80bc85SJeff Dike #include <stdlib.h>
30f80bc85SJeff Dike #include <stddef.h>
40f80bc85SJeff Dike #include <stdarg.h>
50f80bc85SJeff Dike #include <unistd.h>
60f80bc85SJeff Dike #include <errno.h>
70f80bc85SJeff Dike #include <string.h>
80f80bc85SJeff Dike #include <fcntl.h>
90f80bc85SJeff Dike #include <sys/types.h>
100f80bc85SJeff Dike #include <sys/mman.h>
11966a082fSRob Landley #include <sys/statfs.h>
120f80bc85SJeff Dike #include "kern_util.h"
130f80bc85SJeff Dike #include "user.h"
140f80bc85SJeff Dike #include "user_util.h"
150f80bc85SJeff Dike #include "mem_user.h"
160f80bc85SJeff Dike #include "init.h"
170f80bc85SJeff Dike #include "os.h"
180f80bc85SJeff Dike #include "tempfile.h"
190f80bc85SJeff Dike #include "kern_constants.h"
200f80bc85SJeff Dike 
210f80bc85SJeff Dike #include <sys/param.h>
220f80bc85SJeff Dike 
236bf79482SJeff Dike /* Modified by which_tmpdir, which is called during early boot */
24966a082fSRob Landley static char *default_tmpdir = "/tmp";
256bf79482SJeff Dike 
266bf79482SJeff Dike /*
276bf79482SJeff Dike  *  Modified when creating the physical memory file and when checking
286bf79482SJeff Dike  * the tmp filesystem for usability, both happening during early boot.
296bf79482SJeff Dike  */
300f80bc85SJeff Dike static char *tempdir = NULL;
310f80bc85SJeff Dike 
320f80bc85SJeff Dike static void __init find_tempdir(void)
330f80bc85SJeff Dike {
340f80bc85SJeff Dike 	char *dirs[] = { "TMP", "TEMP", "TMPDIR", NULL };
350f80bc85SJeff Dike 	int i;
360f80bc85SJeff Dike 	char *dir = NULL;
370f80bc85SJeff Dike 
38*81999a01SJeff Dike 	if(tempdir != NULL) /* We've already been called */
39*81999a01SJeff Dike 		return;
400f80bc85SJeff Dike 	for(i = 0; dirs[i]; i++){
410f80bc85SJeff Dike 		dir = getenv(dirs[i]);
420f80bc85SJeff Dike 		if((dir != NULL) && (*dir != '\0'))
430f80bc85SJeff Dike 			break;
440f80bc85SJeff Dike 	}
450f80bc85SJeff Dike 	if((dir == NULL) || (*dir == '\0'))
46966a082fSRob Landley 		dir = default_tmpdir;
470f80bc85SJeff Dike 
480f80bc85SJeff Dike 	tempdir = malloc(strlen(dir) + 2);
490f80bc85SJeff Dike 	if(tempdir == NULL){
500f80bc85SJeff Dike 		fprintf(stderr, "Failed to malloc tempdir, "
510f80bc85SJeff Dike 			"errno = %d\n", errno);
520f80bc85SJeff Dike 		return;
530f80bc85SJeff Dike 	}
540f80bc85SJeff Dike 	strcpy(tempdir, dir);
550f80bc85SJeff Dike 	strcat(tempdir, "/");
560f80bc85SJeff Dike }
570f80bc85SJeff Dike 
58966a082fSRob Landley /* This will return 1, with the first character in buf being the
59966a082fSRob Landley  * character following the next instance of c in the file.  This will
60966a082fSRob Landley  * read the file as needed.  If there's an error, -errno is returned;
61966a082fSRob Landley  * if the end of the file is reached, 0 is returned.
62966a082fSRob Landley  */
63966a082fSRob Landley static int next(int fd, char *buf, int size, char c)
64966a082fSRob Landley {
65c2b7a4bbSJeff Dike 	int n, len;
66966a082fSRob Landley 	char *ptr;
67966a082fSRob Landley 
68966a082fSRob Landley 	while((ptr = strchr(buf, c)) == NULL){
69966a082fSRob Landley 		n = read(fd, buf, size - 1);
70966a082fSRob Landley 		if(n == 0)
71966a082fSRob Landley 			return 0;
72966a082fSRob Landley 		else if(n < 0)
73966a082fSRob Landley 			return -errno;
74966a082fSRob Landley 
75966a082fSRob Landley 		buf[n] = '\0';
76966a082fSRob Landley 	}
77966a082fSRob Landley 
78966a082fSRob Landley 	ptr++;
79c2b7a4bbSJeff Dike 	len = strlen(ptr);
80c2b7a4bbSJeff Dike 	memmove(buf, ptr, len + 1);
81c2b7a4bbSJeff Dike 
82c2b7a4bbSJeff Dike 	/* Refill the buffer so that if there's a partial string that we care
83c2b7a4bbSJeff Dike 	 * about, it will be completed, and we can recognize it.
84c2b7a4bbSJeff Dike 	 */
85c2b7a4bbSJeff Dike 	n = read(fd, &buf[len], size - len - 1);
86c2b7a4bbSJeff Dike 	if(n < 0)
87c2b7a4bbSJeff Dike 		return -errno;
88c2b7a4bbSJeff Dike 
89c2b7a4bbSJeff Dike 	buf[len + n] = '\0';
90966a082fSRob Landley 	return 1;
91966a082fSRob Landley }
92966a082fSRob Landley 
936bf79482SJeff Dike /* which_tmpdir is called only during early boot */
94966a082fSRob Landley static int checked_tmpdir = 0;
95966a082fSRob Landley 
96966a082fSRob Landley /* Look for a tmpfs mounted at /dev/shm.  I couldn't find a cleaner
97966a082fSRob Landley  * way to do this than to parse /proc/mounts.  statfs will return the
98966a082fSRob Landley  * same filesystem magic number and fs id for both /dev and /dev/shm
99966a082fSRob Landley  * when they are both tmpfs, so you can't tell if they are different
100966a082fSRob Landley  * filesystems.  Also, there seems to be no other way of finding the
101966a082fSRob Landley  * mount point of a filesystem from within it.
102966a082fSRob Landley  *
103966a082fSRob Landley  * If a /dev/shm tmpfs entry is found, then we switch to using it.
104966a082fSRob Landley  * Otherwise, we stay with the default /tmp.
105966a082fSRob Landley  */
106966a082fSRob Landley static void which_tmpdir(void)
107966a082fSRob Landley {
108966a082fSRob Landley 	int fd, found;
109966a082fSRob Landley 	char buf[128] = { '\0' };
110966a082fSRob Landley 
111966a082fSRob Landley 	if(checked_tmpdir)
112966a082fSRob Landley 		return;
113966a082fSRob Landley 
114966a082fSRob Landley 	checked_tmpdir = 1;
115966a082fSRob Landley 
116966a082fSRob Landley 	printf("Checking for tmpfs mount on /dev/shm...");
117966a082fSRob Landley 
118966a082fSRob Landley 	fd = open("/proc/mounts", O_RDONLY);
119966a082fSRob Landley 	if(fd < 0){
120966a082fSRob Landley 		printf("failed to open /proc/mounts, errno = %d\n", errno);
121966a082fSRob Landley 		return;
122966a082fSRob Landley 	}
123966a082fSRob Landley 
124966a082fSRob Landley 	while(1){
12591b165c0SJeff Dike 		found = next(fd, buf, ARRAY_SIZE(buf), ' ');
126966a082fSRob Landley 		if(found != 1)
127966a082fSRob Landley 			break;
128966a082fSRob Landley 
129966a082fSRob Landley 		if(!strncmp(buf, "/dev/shm", strlen("/dev/shm")))
130966a082fSRob Landley 			goto found;
131966a082fSRob Landley 
13291b165c0SJeff Dike 		found = next(fd, buf, ARRAY_SIZE(buf), '\n');
133966a082fSRob Landley 		if(found != 1)
134966a082fSRob Landley 			break;
135966a082fSRob Landley 	}
136966a082fSRob Landley 
137966a082fSRob Landley err:
138966a082fSRob Landley 	if(found == 0)
139966a082fSRob Landley 		printf("nothing mounted on /dev/shm\n");
140966a082fSRob Landley 	else if(found < 0)
141966a082fSRob Landley 		printf("read returned errno %d\n", -found);
142966a082fSRob Landley 
14380c13749SJeff Dike out:
14480c13749SJeff Dike 	close(fd);
14580c13749SJeff Dike 
146966a082fSRob Landley 	return;
147966a082fSRob Landley 
148966a082fSRob Landley found:
14991b165c0SJeff Dike 	found = next(fd, buf, ARRAY_SIZE(buf), ' ');
150966a082fSRob Landley 	if(found != 1)
151966a082fSRob Landley 		goto err;
152966a082fSRob Landley 
153966a082fSRob Landley 	if(strncmp(buf, "tmpfs", strlen("tmpfs"))){
154966a082fSRob Landley 		printf("not tmpfs\n");
15580c13749SJeff Dike 		goto out;
156966a082fSRob Landley 	}
157966a082fSRob Landley 
158966a082fSRob Landley 	printf("OK\n");
159966a082fSRob Landley 	default_tmpdir = "/dev/shm";
16080c13749SJeff Dike 	goto out;
161966a082fSRob Landley }
162966a082fSRob Landley 
1630f80bc85SJeff Dike /*
1640f80bc85SJeff Dike  * This proc still used in tt-mode
1650f80bc85SJeff Dike  * (file: kernel/tt/ptproxy/proxy.c, proc: start_debugger).
1660f80bc85SJeff Dike  * So it isn't 'static' yet.
1670f80bc85SJeff Dike  */
1680f80bc85SJeff Dike int make_tempfile(const char *template, char **out_tempname, int do_unlink)
1690f80bc85SJeff Dike {
17087276f72SPaolo 'Blaisorblade' Giarrusso 	char *tempname;
1710f80bc85SJeff Dike 	int fd;
1720f80bc85SJeff Dike 
173966a082fSRob Landley 	which_tmpdir();
17487276f72SPaolo 'Blaisorblade' Giarrusso 	tempname = malloc(MAXPATHLEN);
17587276f72SPaolo 'Blaisorblade' Giarrusso 
1760f80bc85SJeff Dike 	find_tempdir();
17787276f72SPaolo 'Blaisorblade' Giarrusso 	if (template[0] != '/')
1780f80bc85SJeff Dike 		strcpy(tempname, tempdir);
1790f80bc85SJeff Dike 	else
18087276f72SPaolo 'Blaisorblade' Giarrusso 		tempname[0] = '\0';
1810f80bc85SJeff Dike 	strcat(tempname, template);
1820f80bc85SJeff Dike 	fd = mkstemp(tempname);
1830f80bc85SJeff Dike 	if(fd < 0){
1840f80bc85SJeff Dike 		fprintf(stderr, "open - cannot create %s: %s\n", tempname,
1850f80bc85SJeff Dike 			strerror(errno));
18687276f72SPaolo 'Blaisorblade' Giarrusso 		goto out;
1870f80bc85SJeff Dike 	}
1880f80bc85SJeff Dike 	if(do_unlink && (unlink(tempname) < 0)){
1890f80bc85SJeff Dike 		perror("unlink");
19087276f72SPaolo 'Blaisorblade' Giarrusso 		goto out;
1910f80bc85SJeff Dike 	}
1920f80bc85SJeff Dike 	if(out_tempname){
19387276f72SPaolo 'Blaisorblade' Giarrusso 		*out_tempname = tempname;
19487276f72SPaolo 'Blaisorblade' Giarrusso 	} else {
19587276f72SPaolo 'Blaisorblade' Giarrusso 		free(tempname);
1960f80bc85SJeff Dike 	}
197*81999a01SJeff Dike 	return fd;
19887276f72SPaolo 'Blaisorblade' Giarrusso out:
19987276f72SPaolo 'Blaisorblade' Giarrusso 	free(tempname);
20087276f72SPaolo 'Blaisorblade' Giarrusso 	return -1;
2010f80bc85SJeff Dike }
2020f80bc85SJeff Dike 
2030f80bc85SJeff Dike #define TEMPNAME_TEMPLATE "vm_file-XXXXXX"
2040f80bc85SJeff Dike 
2050f80bc85SJeff Dike /*
2060f80bc85SJeff Dike  * This proc is used in start_up.c
2070f80bc85SJeff Dike  * So it isn't 'static'.
2080f80bc85SJeff Dike  */
209ae173816SJeff Dike int create_tmp_file(unsigned long long len)
2100f80bc85SJeff Dike {
2110f80bc85SJeff Dike 	int fd, err;
2120f80bc85SJeff Dike 	char zero;
2130f80bc85SJeff Dike 
2140f80bc85SJeff Dike 	fd = make_tempfile(TEMPNAME_TEMPLATE, NULL, 1);
2150f80bc85SJeff Dike 	if(fd < 0) {
2160f80bc85SJeff Dike 		exit(1);
2170f80bc85SJeff Dike 	}
2180f80bc85SJeff Dike 
2190f80bc85SJeff Dike 	err = fchmod(fd, 0777);
2200f80bc85SJeff Dike 	if(err < 0){
2210f80bc85SJeff Dike 		perror("os_mode_fd");
2220f80bc85SJeff Dike 		exit(1);
2230f80bc85SJeff Dike 	}
2240f80bc85SJeff Dike 
225190f4939SJeff Dike 	/* Seek to len - 1 because writing a character there will
226190f4939SJeff Dike 	 * increase the file size by one byte, to the desired length.
227190f4939SJeff Dike 	 */
228190f4939SJeff Dike 	if (lseek64(fd, len - 1, SEEK_SET) < 0) {
2290f80bc85SJeff Dike 		perror("os_seek_file");
2300f80bc85SJeff Dike 		exit(1);
2310f80bc85SJeff Dike 	}
2320f80bc85SJeff Dike 
2330f80bc85SJeff Dike 	zero = 0;
2340f80bc85SJeff Dike 
2350f80bc85SJeff Dike 	err = os_write_file(fd, &zero, 1);
2360f80bc85SJeff Dike 	if(err != 1){
2370f80bc85SJeff Dike 		errno = -err;
2380f80bc85SJeff Dike 		perror("os_write_file");
2390f80bc85SJeff Dike 		exit(1);
2400f80bc85SJeff Dike 	}
2410f80bc85SJeff Dike 
242*81999a01SJeff Dike 	return fd;
2430f80bc85SJeff Dike }
2440f80bc85SJeff Dike 
245ae173816SJeff Dike int create_mem_file(unsigned long long len)
2460f80bc85SJeff Dike {
2470f80bc85SJeff Dike 	int err, fd;
2480f80bc85SJeff Dike 
24902dea087SJeff Dike 	fd = create_tmp_file(len);
2500f80bc85SJeff Dike 
2510f80bc85SJeff Dike 	err = os_set_exec_close(fd, 1);
2520f80bc85SJeff Dike 	if(err < 0){
2530f80bc85SJeff Dike 		errno = -err;
2540f80bc85SJeff Dike 		perror("exec_close");
2550f80bc85SJeff Dike 	}
256*81999a01SJeff Dike 	return fd;
2570f80bc85SJeff Dike }
258966a082fSRob Landley 
259966a082fSRob Landley 
260966a082fSRob Landley void check_tmpexec(void)
261966a082fSRob Landley {
262966a082fSRob Landley 	void *addr;
263966a082fSRob Landley 	int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
264966a082fSRob Landley 
265966a082fSRob Landley 	addr = mmap(NULL, UM_KERN_PAGE_SIZE,
266966a082fSRob Landley 		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
267966a082fSRob Landley 	printf("Checking PROT_EXEC mmap in %s...",tempdir);
268966a082fSRob Landley 	fflush(stdout);
269966a082fSRob Landley 	if(addr == MAP_FAILED){
270966a082fSRob Landley 		err = errno;
271966a082fSRob Landley 		perror("failed");
272966a082fSRob Landley 		if(err == EPERM)
273966a082fSRob Landley 			printf("%s must be not mounted noexec\n",tempdir);
274966a082fSRob Landley 		exit(1);
275966a082fSRob Landley 	}
276966a082fSRob Landley 	printf("OK\n");
277966a082fSRob Landley 	munmap(addr, UM_KERN_PAGE_SIZE);
278966a082fSRob Landley 
279966a082fSRob Landley 	close(fd);
280966a082fSRob Landley }
281