2008-02-08 15:22:08 +03:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
|
|
|
* Licensed under the GPL
|
|
|
|
*/
|
|
|
|
|
2005-09-17 06:27:50 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stddef.h>
|
2008-02-08 15:22:08 +03:00
|
|
|
#include <stdlib.h>
|
2005-09-17 06:27:50 +04:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2008-02-08 15:22:08 +03:00
|
|
|
#include <string.h>
|
2010-06-30 02:05:40 +04:00
|
|
|
#include <sys/stat.h>
|
2005-09-17 06:27:50 +04:00
|
|
|
#include <sys/mman.h>
|
2013-11-11 22:03:06 +04:00
|
|
|
#include <sys/vfs.h>
|
|
|
|
#include <linux/magic.h>
|
2012-10-08 06:27:32 +04:00
|
|
|
#include <init.h>
|
|
|
|
#include <os.h>
|
2005-09-17 06:27:50 +04:00
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
/* Set by make_tempfile() during early boot. */
|
2005-09-17 06:27:50 +04:00
|
|
|
static char *tempdir = NULL;
|
|
|
|
|
2013-11-11 22:03:06 +04: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 06:27:50 +04:00
|
|
|
{
|
2013-11-11 22:03:06 +04:00
|
|
|
struct statfs st;
|
2005-09-17 06:27:50 +04:00
|
|
|
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("Checking if %s is on tmpfs...", dir);
|
2013-11-11 22:03:06 +04:00
|
|
|
if (statfs(dir, &st) < 0) {
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("%s\n", strerror(errno));
|
2013-11-11 22:03:06 +04:00
|
|
|
} else if (st.f_type != TMPFS_MAGIC) {
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("no\n");
|
2013-11-11 22:03:06 +04:00
|
|
|
} else {
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("OK\n");
|
2013-11-11 22:03:06 +04:00
|
|
|
return 0;
|
2006-04-19 09:21:43 +04:00
|
|
|
}
|
2013-11-11 22:03:06 +04:00
|
|
|
return -1;
|
2013-07-09 00:19:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-11-11 22:03:06 +04: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 00:19:49 +04:00
|
|
|
*/
|
2013-11-11 22:03:06 +04:00
|
|
|
static char * __init choose_tempdir(void)
|
2013-07-09 00:19:49 +04:00
|
|
|
{
|
2013-11-11 22:03:06 +04: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 00:19:49 +04:00
|
|
|
int i;
|
2013-11-11 22:03:06 +04:00
|
|
|
const char *dir;
|
|
|
|
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("Checking environment variables for a tempdir...");
|
2013-11-11 22:03:06 +04:00
|
|
|
for (i = 0; vars[i]; i++) {
|
|
|
|
dir = getenv(vars[i]);
|
|
|
|
if ((dir != NULL) && (*dir != '\0')) {
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("%s\n", dir);
|
2013-11-11 22:03:06 +04:00
|
|
|
if (check_tmpfs(dir) >= 0)
|
|
|
|
goto done;
|
|
|
|
else
|
|
|
|
goto warn;
|
2013-07-09 00:19:49 +04:00
|
|
|
}
|
|
|
|
}
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("none found\n");
|
2013-07-09 00:19:49 +04:00
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
for (i = 0; tmpfs_dirs[i]; i++) {
|
|
|
|
dir = tmpfs_dirs[i];
|
|
|
|
if (check_tmpfs(dir) >= 0)
|
|
|
|
goto done;
|
2013-07-09 00:19:49 +04:00
|
|
|
}
|
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
dir = fallback_dir;
|
|
|
|
warn:
|
2017-05-17 20:19:31 +03:00
|
|
|
os_warn("Warning: tempdir %s is not on tmpfs\n", dir);
|
2013-11-11 22:03:06 +04:00
|
|
|
done:
|
|
|
|
/* Make a copy since getenv results may not remain valid forever. */
|
|
|
|
return strdup(dir);
|
2006-04-19 09:21:43 +04:00
|
|
|
}
|
|
|
|
|
2008-02-08 15:22:08 +03:00
|
|
|
/*
|
2013-11-11 22:03:06 +04:00
|
|
|
* Create an unlinked tempfile in a suitable tempdir. template must be the
|
|
|
|
* basename part of the template with a leading '/'.
|
2006-04-19 09:21:43 +04:00
|
|
|
*/
|
2013-11-11 22:03:06 +04:00
|
|
|
static int __init make_tempfile(const char *template)
|
2006-04-19 09:21:43 +04:00
|
|
|
{
|
2013-11-11 22:03:06 +04:00
|
|
|
char *tempname;
|
2013-07-09 00:19:49 +04:00
|
|
|
int fd;
|
2006-04-19 09:21:43 +04:00
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
if (tempdir == NULL) {
|
|
|
|
tempdir = choose_tempdir();
|
|
|
|
if (tempdir == NULL) {
|
2017-05-17 20:19:31 +03:00
|
|
|
os_warn("Failed to choose tempdir: %s\n",
|
2013-11-11 22:03:06 +04:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
2013-07-09 00:19:49 +04:00
|
|
|
}
|
2006-04-19 09:21:43 +04:00
|
|
|
}
|
|
|
|
|
2015-12-23 00:15:10 +03:00
|
|
|
#ifdef O_TMPFILE
|
|
|
|
fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700);
|
|
|
|
/*
|
|
|
|
* If the running system does not support O_TMPFILE flag then retry
|
|
|
|
* without it.
|
|
|
|
*/
|
|
|
|
if (fd != -1 || (errno != EINVAL && errno != EISDIR &&
|
|
|
|
errno != EOPNOTSUPP))
|
|
|
|
return fd;
|
|
|
|
#endif
|
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
tempname = malloc(strlen(tempdir) + strlen(template) + 1);
|
2008-02-08 15:22:09 +03:00
|
|
|
if (tempname == NULL)
|
|
|
|
return -1;
|
2006-04-11 09:53:39 +04:00
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
strcpy(tempname, tempdir);
|
|
|
|
strcat(tempname, template);
|
2005-09-17 06:27:50 +04:00
|
|
|
fd = mkstemp(tempname);
|
2008-02-08 15:22:08 +03:00
|
|
|
if (fd < 0) {
|
2017-05-17 20:19:31 +03:00
|
|
|
os_warn("open - cannot create %s: %s\n", tempname,
|
2005-09-17 06:27:50 +04:00
|
|
|
strerror(errno));
|
2006-04-11 09:53:39 +04:00
|
|
|
goto out;
|
2005-09-17 06:27:50 +04:00
|
|
|
}
|
2013-11-11 22:03:06 +04:00
|
|
|
if (unlink(tempname) < 0) {
|
2005-09-17 06:27:50 +04:00
|
|
|
perror("unlink");
|
2011-07-26 04:12:52 +04:00
|
|
|
goto close;
|
2005-09-17 06:27:50 +04:00
|
|
|
}
|
2013-11-11 22:03:06 +04:00
|
|
|
free(tempname);
|
2007-02-10 12:44:21 +03:00
|
|
|
return fd;
|
2011-07-26 04:12:52 +04:00
|
|
|
close:
|
|
|
|
close(fd);
|
2006-04-11 09:53:39 +04:00
|
|
|
out:
|
|
|
|
free(tempname);
|
|
|
|
return -1;
|
2005-09-17 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
#define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
|
2005-09-17 06:27:50 +04:00
|
|
|
|
2008-02-08 15:22:08 +03:00
|
|
|
static int __init create_tmp_file(unsigned long long len)
|
2005-09-17 06:27:50 +04:00
|
|
|
{
|
|
|
|
int fd, err;
|
|
|
|
char zero;
|
|
|
|
|
2013-11-11 22:03:06 +04:00
|
|
|
fd = make_tempfile(TEMPNAME_TEMPLATE);
|
2008-02-08 15:22:08 +03:00
|
|
|
if (fd < 0)
|
2005-09-17 06:27:50 +04:00
|
|
|
exit(1);
|
|
|
|
|
2008-02-08 15:22:08 +03:00
|
|
|
/*
|
|
|
|
* Seek to len - 1 because writing a character there will
|
2006-06-30 12:55:55 +04:00
|
|
|
* increase the file size by one byte, to the desired length.
|
|
|
|
*/
|
|
|
|
if (lseek64(fd, len - 1, SEEK_SET) < 0) {
|
2007-10-16 12:27:11 +04:00
|
|
|
perror("lseek64");
|
2005-09-17 06:27:50 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
zero = 0;
|
|
|
|
|
2007-05-07 01:51:35 +04:00
|
|
|
err = write(fd, &zero, 1);
|
2008-02-08 15:22:08 +03:00
|
|
|
if (err != 1) {
|
2007-05-07 01:51:35 +04:00
|
|
|
perror("write");
|
2005-09-17 06:27:50 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2007-02-10 12:44:21 +03:00
|
|
|
return fd;
|
2005-09-17 06:27:50 +04:00
|
|
|
}
|
|
|
|
|
2007-05-07 01:51:11 +04:00
|
|
|
int __init create_mem_file(unsigned long long len)
|
2005-09-17 06:27:50 +04:00
|
|
|
{
|
|
|
|
int err, fd;
|
|
|
|
|
2006-03-31 14:30:08 +04:00
|
|
|
fd = create_tmp_file(len);
|
2005-09-17 06:27:50 +04:00
|
|
|
|
2007-10-16 12:27:11 +04:00
|
|
|
err = os_set_exec_close(fd);
|
2008-02-08 15:22:08 +03:00
|
|
|
if (err < 0) {
|
2005-09-17 06:27:50 +04:00
|
|
|
errno = -err;
|
|
|
|
perror("exec_close");
|
|
|
|
}
|
2007-02-10 12:44:21 +03:00
|
|
|
return fd;
|
2005-09-17 06:27:50 +04:00
|
|
|
}
|
2006-04-19 09:21:43 +04:00
|
|
|
|
2007-05-07 01:51:11 +04:00
|
|
|
void __init check_tmpexec(void)
|
2006-04-19 09:21:43 +04: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);
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("Checking PROT_EXEC mmap in %s...", tempdir);
|
2008-02-08 15:22:08 +03:00
|
|
|
if (addr == MAP_FAILED) {
|
2006-04-19 09:21:43 +04:00
|
|
|
err = errno;
|
2017-05-17 20:19:31 +03:00
|
|
|
os_warn("%s\n", strerror(err));
|
2008-02-05 09:30:35 +03:00
|
|
|
close(fd);
|
2008-02-08 15:22:08 +03:00
|
|
|
if (err == EPERM)
|
2017-05-17 20:19:31 +03:00
|
|
|
os_warn("%s must be not mounted noexec\n", tempdir);
|
2006-04-19 09:21:43 +04:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-17 20:17:14 +03:00
|
|
|
os_info("OK\n");
|
2006-04-19 09:21:43 +04:00
|
|
|
munmap(addr, UM_KERN_PAGE_SIZE);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|