18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci#include <stdio.h>
78c2ecf20Sopenharmony_ci#include <stddef.h>
88c2ecf20Sopenharmony_ci#include <stdlib.h>
98c2ecf20Sopenharmony_ci#include <unistd.h>
108c2ecf20Sopenharmony_ci#include <errno.h>
118c2ecf20Sopenharmony_ci#include <fcntl.h>
128c2ecf20Sopenharmony_ci#include <string.h>
138c2ecf20Sopenharmony_ci#include <sys/stat.h>
148c2ecf20Sopenharmony_ci#include <sys/mman.h>
158c2ecf20Sopenharmony_ci#include <sys/vfs.h>
168c2ecf20Sopenharmony_ci#include <linux/magic.h>
178c2ecf20Sopenharmony_ci#include <init.h>
188c2ecf20Sopenharmony_ci#include <os.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci/* Set by make_tempfile() during early boot. */
218c2ecf20Sopenharmony_cistatic char *tempdir = NULL;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci/* Check if dir is on tmpfs. Return 0 if yes, -1 if no or error. */
248c2ecf20Sopenharmony_cistatic int __init check_tmpfs(const char *dir)
258c2ecf20Sopenharmony_ci{
268c2ecf20Sopenharmony_ci	struct statfs st;
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	os_info("Checking if %s is on tmpfs...", dir);
298c2ecf20Sopenharmony_ci	if (statfs(dir, &st) < 0) {
308c2ecf20Sopenharmony_ci		os_info("%s\n", strerror(errno));
318c2ecf20Sopenharmony_ci	} else if (st.f_type != TMPFS_MAGIC) {
328c2ecf20Sopenharmony_ci		os_info("no\n");
338c2ecf20Sopenharmony_ci	} else {
348c2ecf20Sopenharmony_ci		os_info("OK\n");
358c2ecf20Sopenharmony_ci		return 0;
368c2ecf20Sopenharmony_ci	}
378c2ecf20Sopenharmony_ci	return -1;
388c2ecf20Sopenharmony_ci}
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/*
418c2ecf20Sopenharmony_ci * Choose the tempdir to use. We want something on tmpfs so that our memory is
428c2ecf20Sopenharmony_ci * not subject to the host's vm.dirty_ratio. If a tempdir is specified in the
438c2ecf20Sopenharmony_ci * environment, we use that even if it's not on tmpfs, but we warn the user.
448c2ecf20Sopenharmony_ci * Otherwise, we try common tmpfs locations, and if no tmpfs directory is found
458c2ecf20Sopenharmony_ci * then we fall back to /tmp.
468c2ecf20Sopenharmony_ci */
478c2ecf20Sopenharmony_cistatic char * __init choose_tempdir(void)
488c2ecf20Sopenharmony_ci{
498c2ecf20Sopenharmony_ci	static const char * const vars[] = {
508c2ecf20Sopenharmony_ci		"TMPDIR",
518c2ecf20Sopenharmony_ci		"TMP",
528c2ecf20Sopenharmony_ci		"TEMP",
538c2ecf20Sopenharmony_ci		NULL
548c2ecf20Sopenharmony_ci	};
558c2ecf20Sopenharmony_ci	static const char fallback_dir[] = "/tmp";
568c2ecf20Sopenharmony_ci	static const char * const tmpfs_dirs[] = {
578c2ecf20Sopenharmony_ci		"/dev/shm",
588c2ecf20Sopenharmony_ci		fallback_dir,
598c2ecf20Sopenharmony_ci		NULL
608c2ecf20Sopenharmony_ci	};
618c2ecf20Sopenharmony_ci	int i;
628c2ecf20Sopenharmony_ci	const char *dir;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	os_info("Checking environment variables for a tempdir...");
658c2ecf20Sopenharmony_ci	for (i = 0; vars[i]; i++) {
668c2ecf20Sopenharmony_ci		dir = getenv(vars[i]);
678c2ecf20Sopenharmony_ci		if ((dir != NULL) && (*dir != '\0')) {
688c2ecf20Sopenharmony_ci			os_info("%s\n", dir);
698c2ecf20Sopenharmony_ci			if (check_tmpfs(dir) >= 0)
708c2ecf20Sopenharmony_ci				goto done;
718c2ecf20Sopenharmony_ci			else
728c2ecf20Sopenharmony_ci				goto warn;
738c2ecf20Sopenharmony_ci		}
748c2ecf20Sopenharmony_ci	}
758c2ecf20Sopenharmony_ci	os_info("none found\n");
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	for (i = 0; tmpfs_dirs[i]; i++) {
788c2ecf20Sopenharmony_ci		dir = tmpfs_dirs[i];
798c2ecf20Sopenharmony_ci		if (check_tmpfs(dir) >= 0)
808c2ecf20Sopenharmony_ci			goto done;
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	dir = fallback_dir;
848c2ecf20Sopenharmony_ciwarn:
858c2ecf20Sopenharmony_ci	os_warn("Warning: tempdir %s is not on tmpfs\n", dir);
868c2ecf20Sopenharmony_cidone:
878c2ecf20Sopenharmony_ci	/* Make a copy since getenv results may not remain valid forever. */
888c2ecf20Sopenharmony_ci	return strdup(dir);
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci/*
928c2ecf20Sopenharmony_ci * Create an unlinked tempfile in a suitable tempdir. template must be the
938c2ecf20Sopenharmony_ci * basename part of the template with a leading '/'.
948c2ecf20Sopenharmony_ci */
958c2ecf20Sopenharmony_cistatic int __init make_tempfile(const char *template)
968c2ecf20Sopenharmony_ci{
978c2ecf20Sopenharmony_ci	char *tempname;
988c2ecf20Sopenharmony_ci	int fd;
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci	if (tempdir == NULL) {
1018c2ecf20Sopenharmony_ci		tempdir = choose_tempdir();
1028c2ecf20Sopenharmony_ci		if (tempdir == NULL) {
1038c2ecf20Sopenharmony_ci			os_warn("Failed to choose tempdir: %s\n",
1048c2ecf20Sopenharmony_ci				strerror(errno));
1058c2ecf20Sopenharmony_ci			return -1;
1068c2ecf20Sopenharmony_ci		}
1078c2ecf20Sopenharmony_ci	}
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci#ifdef O_TMPFILE
1108c2ecf20Sopenharmony_ci	fd = open(tempdir, O_CLOEXEC | O_RDWR | O_EXCL | O_TMPFILE, 0700);
1118c2ecf20Sopenharmony_ci	/*
1128c2ecf20Sopenharmony_ci	 * If the running system does not support O_TMPFILE flag then retry
1138c2ecf20Sopenharmony_ci	 * without it.
1148c2ecf20Sopenharmony_ci	 */
1158c2ecf20Sopenharmony_ci	if (fd != -1 || (errno != EINVAL && errno != EISDIR &&
1168c2ecf20Sopenharmony_ci			errno != EOPNOTSUPP))
1178c2ecf20Sopenharmony_ci		return fd;
1188c2ecf20Sopenharmony_ci#endif
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci	tempname = malloc(strlen(tempdir) + strlen(template) + 1);
1218c2ecf20Sopenharmony_ci	if (tempname == NULL)
1228c2ecf20Sopenharmony_ci		return -1;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	strcpy(tempname, tempdir);
1258c2ecf20Sopenharmony_ci	strcat(tempname, template);
1268c2ecf20Sopenharmony_ci	fd = mkstemp(tempname);
1278c2ecf20Sopenharmony_ci	if (fd < 0) {
1288c2ecf20Sopenharmony_ci		os_warn("open - cannot create %s: %s\n", tempname,
1298c2ecf20Sopenharmony_ci			strerror(errno));
1308c2ecf20Sopenharmony_ci		goto out;
1318c2ecf20Sopenharmony_ci	}
1328c2ecf20Sopenharmony_ci	if (unlink(tempname) < 0) {
1338c2ecf20Sopenharmony_ci		perror("unlink");
1348c2ecf20Sopenharmony_ci		goto close;
1358c2ecf20Sopenharmony_ci	}
1368c2ecf20Sopenharmony_ci	free(tempname);
1378c2ecf20Sopenharmony_ci	return fd;
1388c2ecf20Sopenharmony_ciclose:
1398c2ecf20Sopenharmony_ci	close(fd);
1408c2ecf20Sopenharmony_ciout:
1418c2ecf20Sopenharmony_ci	free(tempname);
1428c2ecf20Sopenharmony_ci	return -1;
1438c2ecf20Sopenharmony_ci}
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci#define TEMPNAME_TEMPLATE "/vm_file-XXXXXX"
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_cistatic int __init create_tmp_file(unsigned long long len)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	int fd, err;
1508c2ecf20Sopenharmony_ci	char zero;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	fd = make_tempfile(TEMPNAME_TEMPLATE);
1538c2ecf20Sopenharmony_ci	if (fd < 0)
1548c2ecf20Sopenharmony_ci		exit(1);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	/*
1578c2ecf20Sopenharmony_ci	 * Seek to len - 1 because writing a character there will
1588c2ecf20Sopenharmony_ci	 * increase the file size by one byte, to the desired length.
1598c2ecf20Sopenharmony_ci	 */
1608c2ecf20Sopenharmony_ci	if (lseek64(fd, len - 1, SEEK_SET) < 0) {
1618c2ecf20Sopenharmony_ci		perror("lseek64");
1628c2ecf20Sopenharmony_ci		exit(1);
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_ci	zero = 0;
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_ci	err = write(fd, &zero, 1);
1688c2ecf20Sopenharmony_ci	if (err != 1) {
1698c2ecf20Sopenharmony_ci		perror("write");
1708c2ecf20Sopenharmony_ci		exit(1);
1718c2ecf20Sopenharmony_ci	}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	return fd;
1748c2ecf20Sopenharmony_ci}
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ciint __init create_mem_file(unsigned long long len)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	int err, fd;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	fd = create_tmp_file(len);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	err = os_set_exec_close(fd);
1838c2ecf20Sopenharmony_ci	if (err < 0) {
1848c2ecf20Sopenharmony_ci		errno = -err;
1858c2ecf20Sopenharmony_ci		perror("exec_close");
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci	return fd;
1888c2ecf20Sopenharmony_ci}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_civoid __init check_tmpexec(void)
1918c2ecf20Sopenharmony_ci{
1928c2ecf20Sopenharmony_ci	void *addr;
1938c2ecf20Sopenharmony_ci	int err, fd = create_tmp_file(UM_KERN_PAGE_SIZE);
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_ci	addr = mmap(NULL, UM_KERN_PAGE_SIZE,
1968c2ecf20Sopenharmony_ci		    PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE, fd, 0);
1978c2ecf20Sopenharmony_ci	os_info("Checking PROT_EXEC mmap in %s...", tempdir);
1988c2ecf20Sopenharmony_ci	if (addr == MAP_FAILED) {
1998c2ecf20Sopenharmony_ci		err = errno;
2008c2ecf20Sopenharmony_ci		os_warn("%s\n", strerror(err));
2018c2ecf20Sopenharmony_ci		close(fd);
2028c2ecf20Sopenharmony_ci		if (err == EPERM)
2038c2ecf20Sopenharmony_ci			os_warn("%s must be not mounted noexec\n", tempdir);
2048c2ecf20Sopenharmony_ci		exit(1);
2058c2ecf20Sopenharmony_ci	}
2068c2ecf20Sopenharmony_ci	os_info("OK\n");
2078c2ecf20Sopenharmony_ci	munmap(addr, UM_KERN_PAGE_SIZE);
2088c2ecf20Sopenharmony_ci
2098c2ecf20Sopenharmony_ci	close(fd);
2108c2ecf20Sopenharmony_ci}
211