2019-08-25 17:49:18 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2008-02-08 20:22:08 +08:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("Checking if %s is on tmpfs...", dir);
|
2013-11-12 02:03:06 +08:00
|
|
|
if (statfs(dir, &st) < 0) {
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("%s\n", strerror(errno));
|
2013-11-12 02:03:06 +08:00
|
|
|
} else if (st.f_type != TMPFS_MAGIC) {
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("no\n");
|
2013-11-12 02:03:06 +08:00
|
|
|
} else {
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("OK\n");
|
2013-11-12 02:03:06 +08:00
|
|
|
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;
|
|
|
|
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("Checking environment variables for a tempdir...");
|
2013-11-12 02:03:06 +08:00
|
|
|
for (i = 0; vars[i]; i++) {
|
|
|
|
dir = getenv(vars[i]);
|
|
|
|
if ((dir != NULL) && (*dir != '\0')) {
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("%s\n", dir);
|
2013-11-12 02:03:06 +08:00
|
|
|
if (check_tmpfs(dir) >= 0)
|
|
|
|
goto done;
|
|
|
|
else
|
|
|
|
goto warn;
|
2013-07-09 04:19:49 +08:00
|
|
|
}
|
|
|
|
}
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("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:
|
2017-05-18 01:19:31 +08:00
|
|
|
os_warn("Warning: tempdir %s is not on tmpfs\n", dir);
|
2013-11-12 02:03:06 +08:00
|
|
|
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) {
|
2017-05-18 01:19:31 +08:00
|
|
|
os_warn("Failed to choose tempdir: %s\n",
|
2013-11-12 02:03:06 +08:00
|
|
|
strerror(errno));
|
|
|
|
return -1;
|
2013-07-09 04:19:49 +08:00
|
|
|
}
|
2006-04-19 13:21:43 +08:00
|
|
|
}
|
|
|
|
|
2015-12-23 05:15:10 +08: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-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) {
|
2017-05-18 01:19:31 +08:00
|
|
|
os_warn("open - cannot create %s: %s\n", tempname,
|
2005-09-17 10:27:50 +08:00
|
|
|
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);
|
|
|
|
|
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);
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("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;
|
2017-05-18 01:19:31 +08:00
|
|
|
os_warn("%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)
|
2017-05-18 01:19:31 +08:00
|
|
|
os_warn("%s must be not mounted noexec\n", tempdir);
|
2006-04-19 13:21:43 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
2017-05-18 01:17:14 +08:00
|
|
|
os_info("OK\n");
|
2006-04-19 13:21:43 +08:00
|
|
|
munmap(addr, UM_KERN_PAGE_SIZE);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|