-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathucspi-tee.c
157 lines (134 loc) · 3.6 KB
/
ucspi-tee.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
/*
* Copyright (c) 2014 Jan Klemkow <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <err.h>
#include <errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#ifndef MAX
#define MAX(a, b) ((a) < (b) ? (b) : (a))
#endif
/* ucspi */
#define READ_FD 6
#define WRITE_FD 7
/* enviroment */
char **environ;
static void
usage(void)
{
fprintf(stderr, "ucspi-tee program [args]\n");
exit(EXIT_FAILURE);
}
int
main(int argc, char *argv[], char *envp[])
{
int ch;
environ = envp;
/* pipes to communicate with the front end */
int in = -1;
int out = -1;
/* pipes to communicate with the back end */
int sin = 6;
int sout = 7;
char *in_file = NULL;
char *out_file = NULL;
int in_fd = STDERR_FILENO;
int out_fd = STDERR_FILENO;
while ((ch = getopt(argc, argv, "f:p:Nh")) != -1) {
switch (ch) {
case 'i':
if ((in_file = strdup(optarg)) == NULL) goto err;
break;
case 'o':
if ((out_file = strdup(optarg)) == NULL) goto err;
break;
case 'h':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc < 1)
usage();
/* fork front end program */
char *prog = argv[0];
# define PIPE_READ 0
# define PIPE_WRITE 1
int pi[2];
int po[2];
if (pipe(pi) < 0) goto err;
if (pipe(po) < 0) goto err;
switch (fork()) {
case -1:
err(EXIT_FAILURE, "fork");
case 0: /* start client program */
/* close unused pipe ends */
if (close(pi[PIPE_READ]) < 0) goto err;
if (close(po[PIPE_WRITE]) < 0) goto err;
/*
* We have to move one descriptor cause po[] may
* overlaps with descriptor 6 and 7.
*/
int po_read = 0;
if ((po_read = dup(po[PIPE_READ])) < 0) goto err;
if (close(po[PIPE_READ]) < 0) goto err;
if (dup2(pi[PIPE_WRITE], WRITE_FD) < 0) goto err;
if (dup2(po_read, READ_FD) < 0) goto err;
if (close(pi[PIPE_WRITE]) < 0) goto err;
if (close(po_read) < 0) goto err;
execvpe(prog, argv, environ);
err(EXIT_FAILURE, "execvpe: %s", prog);
default: break;
}
/* close unused pipe ends */
if (close(pi[PIPE_WRITE]) < 0) goto err;
if (close(po[PIPE_READ]) < 0) goto err;
in = pi[PIPE_READ];
out = po[PIPE_WRITE];
for (;;) {
char buf[BUFSIZ];
ssize_t n = 0;
fd_set readfds;
FD_ZERO(&readfds);
FD_SET(in, &readfds);
FD_SET(sin, &readfds);
int max_fd = MAX(in, sin);
if (select(max_fd+1, &readfds, NULL, NULL, NULL) == -1)
goto err;
if (FD_ISSET(sin, &readfds)) {
if ((n = read(sin, buf, BUFSIZ)) < 0) goto err;
if (n == 0) break;
if (write(out, buf, n) < n) goto err;
write(in_fd, "server:\n", 8);
if (write(in_fd, buf, n) < n) goto err;
} else if (FD_ISSET(in, &readfds)) {
if ((n = read(in, buf, BUFSIZ)) < 0) goto err;
if (n == 0) break;
if (write(sout, buf, n) < n) goto err;
write(in_fd, "client:\n", 8);
if (write(out_fd, buf, n) < n) goto err;
}
}
return EXIT_SUCCESS;
err:
perror("ucspi-tee");
return EXIT_FAILURE;
}