From 0388142877654e7b0a420f8e63b4a3153644aee4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodrigo=20G=2E=20L=C3=B3pez?= Date: Thu, 14 Oct 2021 14:04:54 +0200 Subject: Initial commit. --- Makefile | 32 ++++++++++++++++++++++++++++++++ main.c | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 Makefile create mode 100644 main.c diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..373aef4 --- /dev/null +++ b/Makefile @@ -0,0 +1,32 @@ +CC=cc +CFLAGS=-Wall -Wno-missing-braces -Wno-parentheses -Wno-switch -Wno-pointer-to-int-cast -fno-diagnostics-color -ggdb -c -O2 +LDFLAGS=-static +O=o +BIN=$(HOME)/bin + +TARG=paramedic +OFILES=\ + main.$O\ + +.PHONY: all clean +all: $(TARG) + +%.$O: %.c + $(CC) $(CFLAGS) $< + +$(OFILES): $(HFILES) + +$(TARG): $(OFILES) + $(CC) -o $@ $(OFILES) $(LIBS) $(LDFLAGS) + +install: $(TARG) + cp $(TARG) $(BIN)/ + +uninstall: + rm -f $(BIN)/$(TARG) + +man: + groff -man $(TARG).8.man | ps2pdf - >$(TARG).8.pdf + +clean: + rm $(TARG) *.o *.pdf diff --git a/main.c b/main.c new file mode 100644 index 0000000..6b6e15a --- /dev/null +++ b/main.c @@ -0,0 +1,35 @@ +#define _GNU_SOURCE +#include +#include +#include +#include +#include +#include +#include + +int +main(int argc, char *argv[]) +{ + char buf[1024]; + int mfd, n, cs; + + mfd = memfd_create("", MFD_CLOEXEC); + if(mfd < 0) + exit(1); + + while((n = read(0, buf, sizeof buf)) > 0) + if(write(mfd, buf, n) != n) + exit(2); + + switch(fork()){ + case -1: + exit(3); + case 0: + snprintf(buf, sizeof buf, "/proc/self/fd/%d", mfd); + execl(buf, "", NULL); + exit(66); + default: + wait(&cs); + } + exit(cs); +} -- cgit v1.2.3