aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJason A. Donenfeld <Jason@zx2c4.com>2021-06-15 12:39:39 +0200
committerJason A. Donenfeld <Jason@zx2c4.com>2021-06-15 12:39:39 +0200
commit15e11e7230fdb37da824b8150ebe33025073df03 (patch)
treeac79d77ccae8f153d92f6372a6f9cbf2271cd4ea
parentexample: avoid multi-dimensional array (diff)
downloadcscript-15e11e7230fdb37da824b8150ebe33025073df03.tar.xz
cscript-15e11e7230fdb37da824b8150ebe33025073df03.zip
script: use memfd_create instead of O_TMPFILE
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
-rw-r--r--Makefile2
-rw-r--r--cscript.c20
2 files changed, 7 insertions, 15 deletions
diff --git a/Makefile b/Makefile
index 5e2eb7b..74383b6 100644
--- a/Makefile
+++ b/Makefile
@@ -4,7 +4,7 @@ BINDIR ?= $(PREFIX)/bin
MANDIR ?= $(PREFIX)/share/man
CFLAGS ?= -O3 -march=native
-CFLAGS += -std=c99 -Wall
+CFLAGS += -std=gnu99 -Wall -D_GNU_SOURCE
all: cscript
diff --git a/cscript.c b/cscript.c
index c7dbcf5..65ab92a 100644
--- a/cscript.c
+++ b/cscript.c
@@ -1,10 +1,8 @@
/* SPDX-License-Identifier: GPL-2.0
*
- * Copyright (C) 2018 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
+ * Copyright (C) 2018-2021 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
*/
-#define _GNU_SOURCE
-
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -12,11 +10,12 @@
#include <errno.h>
#include <sys/types.h>
#include <sys/wait.h>
+#include <sys/mman.h>
#include <fcntl.h>
int main(int argc, char *argv[], char *envp[])
{
- int fd, fd2, input = -1, pipes[2] = { 0 }, status;
+ int fd, input = -1, pipes[2] = { 0 }, status;
pid_t compiler_pid;
char *output_path;
@@ -34,9 +33,9 @@ int main(int argc, char *argv[], char *envp[])
}
}
- fd = open(getenv("HOME") ?: getenv("TMPDIR") ?: "/tmp", O_TMPFILE | O_EXCL | O_RDWR, S_IWUSR | S_IRUSR);
+ fd = memfd_create("cscript", 0);
if (fd < 0) {
- perror("Error: unable to create temporary inode");
+ perror("Error: unable to create memfd");
return 1;
}
if (asprintf(&output_path, "/proc/self/fd/%d", fd) < 0) {
@@ -94,14 +93,7 @@ int main(int argc, char *argv[], char *envp[])
return 1;
}
- fd2 = open(output_path, O_RDONLY);
- if (fd2 < 0) {
- perror("Error: unable to reopen temporary inode");
- return 1;
- }
- close(fd);
-
- if (fexecve(fd2, argv, envp) < 0) {
+ if (fexecve(fd, argv, envp) < 0) {
perror("Error: could not execute compiled program");
return 1;
}