aboutsummaryrefslogtreecommitdiff
path: root/src/rsvr.c
blob: 01fa84587b26e3c6a5b9bc95d59f080cf1ebcdc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#define _GNU_SOURCE
#include <sys/mman.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>

char buf[128*1024];

int main(int argc, char *argv[])
{
	if( 2 > argc ){
		fprintf(stderr,
			"Read stdin into a memfd reservoir file and use this as stdin for a subsequently executed command.\n"
			"usage: %s <command and argument>\n",
			(argc ? argv[0] : "rsvr") );
		return EXIT_FAILURE;
	}

	int rsvr_fd = memfd_create("reservoiur", MFD_CLOEXEC);
	if( 0 > rsvr_fd ){
		perror("memfd_create('reservoiur', MFD_CLOEXEC)"); 
		return EXIT_FAILURE;
	}

	ssize_t rb = 0, wb = 0;
	do {
		rb = read(0, buf, sizeof(buf));
		if( 0 >= rb ){ continue; }

		for( ssize_t cb = 0
		   ; 0 <= wb && (cb < rb)
		   ; cb += ((0 < wb) ? wb : 0)
		){
			wb = write(rsvr_fd, (buf+cb), (rb-cb));
			if( (0 > wb) && (EINTR == errno) ){ wb = 0; }
		}
	} while( (0 < rb)
	      || (0 > rb) && EINTR == errno );

	if( 0 > rb ){ perror("read(stdin)"); }
	if( 0 > wb ){ perror("write(reservoir)"); }
	if( 0 > rb || 0 > wb ){ return EXIT_FAILURE; }

	if( 0 > dup2(rsvr_fd, 0) ){
		perror("dup2(reservoir, stdin)");
		return EXIT_FAILURE;
	}
	close(rsvr_fd);

	memmove(argv, argv+1, sizeof(*argv)*(argc-1));
	argv[argc-1] = NULL;
	if( 0 > execvp(argv[0], argv) ){
		perror("exec(...)");
	}

	return EXIT_FAILURE;
}