-
Notifications
You must be signed in to change notification settings - Fork 40
/
r_wrap.c
240 lines (212 loc) · 8.08 KB
/
r_wrap.c
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "r_split.h"
#include <unistd.h>
#include <sys/wait.h>
#define READ_END 0
#define WRITE_END 1
/*
Batch sizes in and out should be around the same size or smaller ideally.
The code was not tested in cases were the fork would output significantly more lines
than the input but that would definitly lead to incread memory cosumption in the whole pipeline.
*/
void processCmd(char *args[])
{
size_t bufLen = BUFLEN, stdoutBlockBufLen = BUFLEN, writeBufLen = 2*BUFLEN; //buffer length, would be resized as needed
int64_t id;
size_t blockSize;
bool isLast;
char *buffer = malloc(bufLen + 1);
char *readBuffer = malloc(bufLen + 1);
char *writebuffer = malloc(writeBufLen + 1);
char *stdoutBlock = malloc(stdoutBlockBufLen+1);
//select
fd_set readFds;
fd_set writeFds;
int maxFd;
readHeader(stdin, &id, &blockSize, &isLast);
while (!feof(stdin))
{
int fdIn[2];
int fdOut[2];
if (pipe(fdIn) < 0)
{
perror("pipe failed");
exit(1);
}
if (pipe(fdOut) < 0)
{
perror("pipe failed");
exit(1);
}
int pid = fork();
if (pid == -1) {
err(2, "fork failed");
}
if (pid == 0)
{
dup2(fdOut[WRITE_END], STDOUT_FILENO);
dup2(fdIn[READ_END], STDIN_FILENO);
close(fdOut[READ_END]);
close(fdOut[WRITE_END]);
close(fdIn[READ_END]);
close(fdIn[WRITE_END]);
// free(buffer); copy-on-write fork optimize this better(freeing is more harm than good here)
execvp(args[0], args);
//shouldn't get here
perror("Exec failed");
exit(1);
}
else
{
close(fdIn[READ_END]);
close(fdOut[WRITE_END]);
int inputFd = fdOut[READ_END];
int outputFd = fdIn[WRITE_END];
// FILE *execOutFile = fdopen(inputFd, "rb");
// FILE *execInFile = fdopen(outputFd, "wb");
non_block_fd(inputFd, "r-wrap-fork-read");
non_block_fd(outputFd, "r-wrap-fork-write");
size_t currReadLen = 0;
size_t len = 0, currWriteLen = 0;
//select prep
maxFd = MAX(inputFd, outputFd);
//Read batch
size_t tot_read = 0, readSize = 0;
while (tot_read < blockSize || currWriteLen > 0 || !isLast)
{
readSize = MIN(bufLen, blockSize - tot_read);
handle_reading(buffer, readSize, stdin);
if ((currWriteLen + readSize) > writeBufLen) {
writeBufLen = 2*(currWriteLen + readSize);
writebuffer = realloc(writebuffer, writeBufLen+1);
}
memcpy(writebuffer + currWriteLen, buffer, readSize);
currWriteLen += readSize;
FD_ZERO(&writeFds); // Clear FD set for select
while (!FD_ISSET(outputFd, &writeFds))
{
FD_ZERO(&readFds); // Clear FD set for select
FD_ZERO(&writeFds); // Clear FD set for select
FD_SET(inputFd, &readFds);
FD_SET(outputFd, &writeFds);
// Theoretically we don't need select because both pipes are blocking
// but it saves cpu cycles
if (select(maxFd + 1, &readFds, &writeFds, NULL, NULL) < 0) {
if (errno == EINTR)
continue;
perror("select");
}
if (FD_ISSET(inputFd, &readFds))
{
//Try reading from forked processs, nonblocking
if ((len = read(inputFd, readBuffer, bufLen)) > 0) {
if ((currReadLen + len) > stdoutBlockBufLen)
{
stdoutBlockBufLen = 2*(currReadLen + len);
stdoutBlock = realloc(stdoutBlock, stdoutBlockBufLen + 1);
}
memcpy(stdoutBlock + currReadLen, readBuffer, len);
currReadLen += len;
} else if (len < 0) {
switch (errno) {
case EAGAIN:
break;
default:
err(2, "Failed reading from fork, error %d", errno);
}
}
}
}
// Write to forked process
if ((len = write(outputFd, writebuffer, currWriteLen)) > 0) {
currWriteLen -= len;
memmove(writebuffer, writebuffer + len, currWriteLen);
} else if (len < 0) {
switch (errno) {
case EAGAIN:
len = 0;
break;
// TODO handle broken pipe if needed
case EPIPE:
default:
err(2, "Error writing to fork, error %d", errno);
}
}
tot_read += readSize;
if (currReadLen > CHUNKSIZE) {
// write block to stdout
writeHeader(stdout, id, currReadLen, 0);
safeWriteWithFlush(stdoutBlock, 1, currReadLen, stdout);
currReadLen = 0;
}
if (!isLast && tot_read == blockSize && currWriteLen == 0) {
readHeader(stdin, &id, &blockSize, &isLast);
currWriteLen = 0;
tot_read = 0;
}
}
close(outputFd);
assert(tot_read == blockSize);
// read output of forked process
// block to make sure you read until the process exits and to save cpu cycles
block_fd(inputFd, "r-wrap-fork-read");
bool reached_eof = 0;
while (!reached_eof) {
if ((len = read(inputFd, buffer, bufLen)) > 0) {
if ((currReadLen + len) > stdoutBlockBufLen)
{
stdoutBlockBufLen = currReadLen + len + CHUNKSIZE;
stdoutBlock = realloc(stdoutBlock, stdoutBlockBufLen + 1);
}
memcpy(stdoutBlock + currReadLen, buffer, len);
currReadLen += len;
} else {
if (len == 0)
reached_eof = 1;
else {
switch (errno) {
case EAGAIN:
break;
default:
err(2, "Error writing to fork, error %d", errno);
}
}
}
}
close(inputFd);
// write block to stdout
writeHeader(stdout, id, currReadLen, 1);
safeWriteWithFlush(stdoutBlock, 1, currReadLen, stdout);
// update header (ordered at the end so !feof works) and cleanup
readHeader(stdin, &id, &blockSize, &isLast);
// wait is necessary to reclaim process
// Do I need to do something with return status?
waitpid(pid, NULL, 0);
}
}
free(buffer);
free(readBuffer);
free(stdoutBlock);
free(writebuffer);
}
int main(int argc, char *argv[])
{
//arg1: command
//args 2.. : arguments for command
//input is from stdin, out to stdout
char **args = NULL;
if (argc < 2)
{
/* default behavior is to echo all the filenames */
fprintf(stderr, "missing input!\n");
exit(1);
}
//process arguments
args = malloc(sizeof(char *) * (argc));
for (int i = 1; i < argc; i++)
{
args[i - 1] = malloc(strlen(argv[i]) + 1);
strcpy(args[i - 1], argv[i]);
}
args[argc - 1] = '\0';
processCmd(args);
}