From 1b32f5da9fa95fa2e42a2eb07f1fa0ec8171d44d Mon Sep 17 00:00:00 2001 From: ptrcnull Date: Thu, 11 Aug 2022 12:32:36 +0200 Subject: [PATCH] feat: Initial commit --- .gitignore | 2 ++ LICENSE | 9 +++++++++ Makefile | 17 +++++++++++++++++ README.md | 30 ++++++++++++++++++++++++++++++ fdcreate.1 | 24 ++++++++++++++++++++++++ fdcreate.c | 22 ++++++++++++++++++++++ 6 files changed, 104 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 fdcreate.1 create mode 100644 fdcreate.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5a3b2ce --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +fdcreate +fdcreate.o diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7a18f9e --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +Copyright 2022 ptrcnull + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..464d9bb --- /dev/null +++ b/Makefile @@ -0,0 +1,17 @@ +CC=gcc +CFLAGS+=-D_GNU_SOURCE +LIBS+=-lskarnet +BINDIR=/usr/bin +MANDIR=/usr/share/man + +all: fdcreate + +fdcreate: fdcreate.o + $(CC) -o fdcreate fdcreate.o $(LIBS) + +check: fdcreate + ./fdcreate test [ -f /proc/self/fd/3 ] + +install: fdcreate + install -Dm755 fdcreate -t $(DESTDIR)$(BINDIR) + install -Dm644 fdcreate.1 -t $(DESTDIR)$(MANDIR)/man1 diff --git a/README.md b/README.md new file mode 100644 index 0000000..8b7b5a3 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# fdcreate + +create an anonymous file, then execute a program + +### usage + +``` +fdcreate name prog... +``` + +### description + +fdcreate creates an anonymous file (memfd) with memfd_create(3) and +exports the resulting file descriptor as name, then exec(3)s into prog +with its arguments. + +### examples + +``` +fdcreate tmate +importas -ui tmate_fd tmate +foreground { + pipeline { + curl -L https://github.com/tmate-io/tmate/releases/download/2.4.0/tmate-2.4.0-static-linux-amd64.tar.xz + } + fdmove -c 1 $tmate_fd + tar xOJf - tmate-2.4.0-static-linux-amd64/tmate +} +/proc/self/fd/$tmate_fd +``` diff --git a/fdcreate.1 b/fdcreate.1 new file mode 100644 index 0000000..dec4fb3 --- /dev/null +++ b/fdcreate.1 @@ -0,0 +1,24 @@ +.Dd August 11, 2022 +.Dt FDCREATE 1 +.Os +.Sh NAME +.Nm fdcreate +.Nd +create an anonymous file, then execute a program +.Sh SYNOPSIS +.Nm +.Ar name +.Ar prog... +.Sh DESCRIPTION +.Nm +creates an anonymous file (memfd) with +.Xr memfd_create 3 +and exports the resulting file descriptor as +.Ar name , +then +.Xr exec 3 Ns +s into +.Ar prog +with its arguments. +.Sh AUTHORS +.An Patrycja Rosa diff --git a/fdcreate.c b/fdcreate.c new file mode 100644 index 0000000..089bf97 --- /dev/null +++ b/fdcreate.c @@ -0,0 +1,22 @@ +#include +#include +#include + +#include +#include +#include + +#define USAGE "fdcreate name prog..." + +int main(int argc, char const *const *argv) { + PROG = "fdcreate"; + + if (argc < 3) strerr_dieusage(100, USAGE); + int fd = memfd_create(argv[1], 0); + if (fd < 0) strerr_diefu1sys(111, "memfd_create"); + char fd_str[10]; + sprintf(fd_str, "%d", fd); + setenv(argv[1], fd_str, 1); + + xexec(argv+2); +}