2008-02-08 20:22:08 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
2005-09-17 10:27:50 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
2008-02-08 20:22:08 +08:00
|
|
|
#include <stdlib.h>
|
2005-09-17 10:27:50 +08:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2008-02-08 20:22:08 +08:00
|
|
|
#include <string.h>
|
2010-06-30 06:05:40 +08:00
|
|
|
#include <sys/stat.h>
|
2005-09-17 10:27:50 +08:00
|
|
|
#include <sys/mman.h>
|
2013-11-12 02:03:06 +08:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
#include <linux/magic.h>
|
2012-10-08 10:27:32 +08:00
|
|
|
#include <init.h>
|
|
|
|
#include <os.h>
|
2005-09-17 10:27:50 +08:00
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
/* Set by make_tempfile() during early boot. */
|
2005-09-17 10:27:50 +08:00
|
|
|
static char *tempdir = NULL;
|
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
/* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
|
|
|
|
static int __init check_tmpfs(const char *dir)
|
2005-09-17 10:27:50 +08:00
|
|
|
{
|
2013-11-12 02:03:06 +08:00
|
|
|
struct statfs st;
|
2005-09-17 10:27:50 +08:00
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
printf("Checking if %s is on tmpfs...", dir);
|
|
|
|
if (statfs(dir, &st) < 0) {
|
|
|
|
printf("%s\n", strerror(errno));
|
|
|
|
} else if (st.f_type != TMPFS_MAGIC) {
|
|
|
|
printf("no\n");
|
|
|
|
} else {
|
|
|
|
printf("OK\n");
|
|
|
|
return 0;
|
2006-04-19 13:21:43 +08:00
|
|
|
}
|
2013-11-12 02:03:06 +08:00
|
|
|
return -1;
|
2013-07-09 04:19:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-12 02:03:06 +08:00
|
|
|
* Choose the tempdir to use. We want something on tmpfs so that our memory is
|
|
|
|
* not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
|
|
|
|
* environment, we use that even if it's not on tmpfs, but we warn the user.
|
|
|
|
* Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
|
|
|
|
* then we fall back to /tmp.
|
2013-07-09 04:19:49 +08:00
|
|
|
*/
|
2013-11-12 02:03:06 +08:00
|
|
|
static char * __init choose_tempdir(void)
|
2013-07-09 04:19:49 +08:00
|
|
|
{
|
2013-11-12 02:03:06 +08:00
|
|
|
static const char * const vars[] = {
|
|
|
|
"TMPDIR",
|
|
|
|
"TMP",
|
|
|
|
"TEMP",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
static const char fallback_dir[] = "/tmp";
|
|
|
|
static const char * const tmpfs_dirs[] = {
|
|
|
|
"/dev/shm",
|
|
|
|
fallback_dir,
|
|
|
|
NULL
|
|
|
|
};
|
2013-07-09 04:19:49 +08:00
|
|
|
int i;
|
2013-11-12 02:03:06 +08:00
|
|
|
const char *dir;
|
|
|
|
|
|
|
|
printf("Checking environment variables for a tempdir...");
|
|
|
|
for (i = 0; vars[i]; i++) {
|
|
|
|
dir = getenv(vars[i]);
|
|
|
|
if ((dir != NULL) && (*dir != '\0')) {
|
|
|
|
printf("%s\n", dir);
|
|
|
|
if (check_tmpfs(dir) >= 0)
|
|
|
|
goto done;
|
|
|
|
else
|
|
|
|
goto warn;
|
2013-07-09 04:19:49 +08:00
|
|
|
}
|
|
|
|
}
|
2013-11-12 02:03:06 +08:00
|
|
|
printf("none found\n");
|
2013-07-09 04:19:49 +08:00
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
for (i = 0; tmpfs_dirs[i]; i++) {
|
|
|
|
dir = tmpfs_dirs[i];
|
|
|
|
if (check_tmpfs(dir) >= 0)
|
|
|
|
goto done;
|
2013-07-09 04:19:49 +08:00
|
|
|
}
|
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
dir = fallback_dir;
|
|
|
|
warn:
|
|
|
|
printf("Warning: tempdir %s is not on tmpfs\n", dir);
|
|
|
|
done:
|
|
|
|
/* Make a copy since getenv results may not remain valid forever. */
|
|
|
|
return strdup(dir);
|
2006-04-19 13:21:43 +08:00
|
|
|
}
|
|
|
|
|
2008-02-08 20:22:08 +08:00
|
|
|
/*
|
2013-11-12 02:03:06 +08:00
|
|
|
* Create an unlinked tempfile in a suitable tempdir. template must be the
|
|
|
|
* basename part of the template with a leading '/'.
|
2006-04-19 13:21:43 +08:00
|
|
|
*/
|
2013-11-12 02:03:06 +08:00
|
|
|
static int __init make_tempfile(const char *template)
|
2006-04-19 13:21:43 +08:00
|
|
|
{
|
2013-11-12 02:03:06 +08:00
|
|
|
char *tempname;
|
2013-07-09 04:19:49 +08:00
|
|
|
int fd;
|
2006-04-19 13:21:43 +08:00
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
if (tempdir == NULL) {
|
|
|
|
tempdir = choose_tempdir();
|
|
|
|
if (tempdir == NULL) {
|
|
|
|
fprintf(stderr, "Failed to choose tempdir: %s\n",
|
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
2013-07-09 04:19:49 +08:00
|
|
|
}
|
2006-04-19 13:21:43 +08:00
|
|
|
}
|
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
tempname = malloc(strlen(tempdir) + strlen(template) + 1);
|
2008-02-08 20:22:09 +08:00
|
|
|
if (tempname == NULL)
|
|
|
|
return -1;
|
2006-04-11 13:53:39 +08:00
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
strcpy(tempname, tempdir);
|
|
|
|
strcat(tempname, template);
|
2005-09-17 10:27:50 +08:00
|
|
|
fd = mkstemp(tempname);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (fd < 0) {
|
2005-09-17 10:27:50 +08:00
|
|
|
fprintf(stderr, "open - cannot create %s: %s\n", tempname,
|
|
|
|
strerror(errno));
|
2006-04-11 13:53:39 +08:00
|
|
|
goto out;
|
2005-09-17 10:27:50 +08:00
|
|
|
}
|
2013-11-12 02:03:06 +08:00
|
|
|
if (unlink(tempname) < 0) {
|
2005-09-17 10:27:50 +08:00
|
|
|
perror("unlink");
|
2011-07-26 08:12:52 +08:00
|
|
|
goto close;
|
2005-09-17 10:27:50 +08:00
|
|
|
}
|
2013-11-12 02:03:06 +08:00
|
|
|
free(tempname);
|
2007-02-10 17:44:21 +08:00
|
|
|
return fd;
|
2011-07-26 08:12:52 +08:00
|
|
|
close:
|
|
|
|
close(fd);
|
2006-04-11 13:53:39 +08:00
|
|
|
out:
|
|
|
|
free(tempname);
|
|
|
|
return -1;
|
2005-09-17 10:27:50 +08:00
|
|
|
}
|
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
#define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
|
2005-09-17 10:27:50 +08:00
|
|
|
|
2008-02-08 20:22:08 +08:00
|
|
|
static int __init create_tmp_file(unsigned long long len)
|
2005-09-17 10:27:50 +08:00
|
|
|
{
|
|
|
|
int fd, err;
|
|
|
|
char zero;
|
|
|
|
|
2013-11-12 02:03:06 +08:00
|
|
|
fd = make_tempfile(TEMPNAME_TEMPLATE);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (fd < 0)
|
2005-09-17 10:27:50 +08:00
|
|
|
exit(1);
|
|
|
|
|
|
|
|
err = fchmod(fd, 0777);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (err < 0) {
|
2007-10-16 16:27:11 +08:00
|
|
|
perror("fchmod");
|
2005-09-17 10:27:50 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2008-02-08 20:22:08 +08:00
|
|
|
/*
|
|
|
|
* Seek to len - 1 because writing a character there will
|
2006-06-30 16:55:55 +08:00
|
|
|
* increase the file size by one byte, to the desired length.
|
|
|
|
*/
|
|
|
|
if (lseek64(fd, len - 1, SEEK_SET) < 0) {
|
2007-10-16 16:27:11 +08:00
|
|
|
perror("lseek64");
|
2005-09-17 10:27:50 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
zero = 0;
|
|
|
|
|
2007-05-07 05:51:35 +08:00
|
|
|
err = write(fd, &zero, 1);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (err != 1) {
|
2007-05-07 05:51:35 +08:00
|
|
|
perror("write");
|
2005-09-17 10:27:50 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-02-10 17:44:21 +08:00
|
|
|
return fd;
|
2005-09-17 10:27:50 +08:00
|
|
|
}
|
|
|
|
|
2007-05-07 05:51:11 +08:00
|
|
|
int __init create_mem_file(unsigned long long len)
|
2005-09-17 10:27:50 +08:00
|
|
|
{
|
|
|
|
int err, fd;
|
|
|
|
|
2006-03-31 18:30:08 +08:00
|
|
|
fd = create_tmp_file(len);
|
2005-09-17 10:27:50 +08:00
|
|
|
|
2007-10-16 16:27:11 +08:00
|
|
|
err = os_set_exec_close(fd);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (err < 0) {
|
2005-09-17 10:27:50 +08:00
|
|
|
errno = -err;
|
|
|
|
perror("exec_close");
|
|
|
|
}
|
2007-02-10 17:44:21 +08:00
|
|
|
return fd;
|
2005-09-17 10:27:50 +08:00
|
|
|
}
|
2006-04-19 13:21:43 +08:00
|
|
|
|
2007-05-07 05:51:11 +08:00
|
|
|
void __init check_tmpexec(void)
|
2006-04-19 13:21:43 +08:00
|
|
|
{
|
|
|
|
void *addr;
|
|
|
|
int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
|
|
|
|
|
|
|
|
addr = mmap(NULL, UM_KERN_PAGE_SIZE,
|
|
|
|
PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
|
2013-11-12 02:03:06 +08:00
|
|
|
printf("Checking PROT_EXEC mmap in %s...", tempdir);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (addr == MAP_FAILED) {
|
2006-04-19 13:21:43 +08:00
|
|
|
err = errno;
|
2013-11-12 02:03:06 +08:00
|
|
|
printf("%s\n", strerror(err));
|
2008-02-05 14:30:35 +08:00
|
|
|
close(fd);
|
2008-02-08 20:22:08 +08:00
|
|
|
if (err == EPERM)
|
2013-11-12 02:03:06 +08:00
|
|
|
printf("%s must be not mounted noexec\n", tempdir);
|
2006-04-19 13:21:43 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
printf("OK\n");
|
|
|
|
munmap(addr, UM_KERN_PAGE_SIZE);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|