feat: Initial commit

This commit is contained in:
ptrcnull 2022-08-11 12:32:36 +02:00
commit 1b32f5da9f
6 changed files with 104 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
fdcreate
fdcreate.o

9
LICENSE Normal file
View file

@ -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.

17
Makefile Normal file
View file

@ -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

30
README.md Normal file
View file

@ -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
```

24
fdcreate.1 Normal file
View file

@ -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

22
fdcreate.c Normal file
View file

@ -0,0 +1,22 @@
#include <sys/mman.h>
#include <stdio.h>
#include <stdlib.h>
#include <skalibs/strerr2.h>
#include <skalibs/env.h>
#include <skalibs/exec.h>
#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);
}