about summary refs log tree commit diff
path: root/filter.c
blob: 480cd521a46b8dddd9a4c2e92357b01db69bec6d (plain) (blame)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include <sys/wait.h>

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

int
filter(char *input, size_t inlen, char *cmd, char **outputo, size_t *outleno)
{
	char *output;
	ssize_t outlen;
	ssize_t outalloc = 4096;
	pid_t pid;
	sigset_t mask, orig_mask;
	int r;

	sigemptyset(&mask);
	sigaddset(&mask, SIGPIPE);
	sigprocmask(SIG_BLOCK, &mask, &orig_mask);

	outlen = 0;
	output = malloc(outalloc);
	if (!output)
		goto fail;

	int pipe0[2];
	int pipe1[2];

	if (pipe(pipe0) != 0 || pipe(pipe1) != 0)
		goto fail;

	int got = fcntl(pipe0[1], F_GETFL);
	if (got > 0)
		fcntl(pipe0[1], F_SETFL, got | O_NONBLOCK);

	char *argv[] = { "/bin/sh", "-c", cmd, (char *)0 };

	if (!(pid = fork())) {
		dup2(pipe0[0], 0);
		close(pipe0[1]);
		close(pipe0[0]);

		dup2(pipe1[1], 1);
		close(pipe1[0]);
		close(pipe1[1]);

		execvp(argv[0], argv);
		exit(-1);
	}
	close(pipe0[0]);
	close(pipe1[1]);

	if (pid < 0) {
		close(pipe0[1]);
		close(pipe1[0]);
		goto fail;
	}

	struct pollfd fds[2];

	fds[0].fd = pipe1[0];
	fds[0].events = POLLIN | POLLHUP;
	fds[1].fd = pipe0[1];
	fds[1].events = POLLOUT;

	while ((fds[0].fd >= 0 || fds[1].fd >= 0) &&
	    poll(fds, sizeof fds / sizeof fds[0], -1) >= 0) {
		if (fds[0].revents & POLLIN) {
			if (outlen + 512 > outalloc) {
				outalloc *= 2;
				if (outalloc < 0)
					exit(-1);
				output = realloc(output, outalloc);
				if (!output)
					exit(-1);
			}
			ssize_t ret = read(fds[0].fd, output + outlen, 512);
			if (ret > 0)
				outlen += ret;
			else
				close(fds[0].fd);
		} else if (fds[0].revents & (POLLERR | POLLHUP | POLLNVAL)) {
			fds[0].fd = -1;
		}

		if (fds[1].revents & POLLOUT) {
			ssize_t ret = write(fds[1].fd, input, inlen);
			if (ret > 0) {
				input += ret;
				inlen -= ret;
			}
			if (ret <= 0 && errno == EAGAIN) {
				/* ignore */
			} else if (ret <= 0 || inlen == 0)
				close(fds[1].fd);
		} else if (fds[1].revents & (POLLERR | POLLHUP | POLLNVAL)) {
			fds[1].fd = -1;
		}
	}

	// ok to fail when closed already
	close(pipe0[1]);
	close(pipe1[0]);

	int status;
	waitpid(pid, &status, 0);
	r = WEXITSTATUS(status);

	*outputo = output;
	*outleno = outlen;

	if (0) {
fail:
		*outputo = 0;
		*outleno = 0;
		free(output);
		r = -1;
	}

	sigpending(&mask);
	if (sigismember(&mask, SIGPIPE)) {
		int sig;
		sigwait(&mask, &sig);
	}
	sigprocmask(SIG_SETMASK, &orig_mask, 0);

	return r;
}

#ifdef TEST
int
main()
{
	char *input = "foo\nbar\nbaz";
	int e;

	char *output;
	size_t outlen;

	e = filter(input, strlen(input), "rev;exit 2", &output, &outlen);

	fwrite(output, 1, outlen, stdout);
	printf("%ld -> %d\n", outlen, e);

	return 0;
}
#endif