2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 Roland Illig <roland.illig@gmx.de>, 2004.
27 #include <string.h> /* On Solaris, FD_SET invokes memset(3) */
29 #include <sys/types.h>
32 #ifdef HAVE_SYS_SELECT_H
33 # include <sys/select.h>
37 #include "pipethrough.h"
42 /* Internal data types *************************************************/
44 struct writer_buffer
{
45 /*@temp@*/ const void *data
;
52 struct reader_buffer
{
53 /*@null@*/ /*@only@*/ void *data
;
60 struct select_request
{
66 /* Helper functions ****************************************************/
68 static int closeref(int *fd
)
69 /*@globals internalState, fileSystem, errno; @*/
70 /*@modifies internalState, fileSystem, errno, *fd; @*/
72 int result
= close(*fd
);
77 static void propagate(int *dest
, int src
)
78 /*@modifies *dest; @*/
85 /* reader_buffer -- implementation *************************************/
87 static void reader_buffer_init(/*@out@*/ struct reader_buffer
*buf
, int fd
)
97 /* SPlint cannot describe realloc(3) correctly. */
98 /*@-usereleased@*/ /*@-compdef@*/ /*@-branchstate@*/ /*@-compmempass@*/
99 static ssize_t
reader_buffer_read(struct reader_buffer
*buf
)
100 /*@globals internalState, errno; @*/
101 /*@modifies internalState, errno, *buf; @*/
105 if (buf
->size
== buf
->maxsize
) {
106 size_t newsize
= buf
->maxsize
+ 4096;
107 /*@only@*/ void *newdata
;
108 if (buf
->data
== NULL
) {
109 newdata
= malloc(newsize
);
111 newdata
= realloc(buf
->data
, newsize
);
113 if (newdata
== NULL
) {
118 buf
->maxsize
= newsize
;
120 res
= read(buf
->fd
, &(((char *) buf
->data
)[buf
->size
]),
121 buf
->maxsize
- buf
->size
);
122 if (res
== (ssize_t
) -1) {
125 buf
->size
+= (ssize_t
) res
;
128 /*@=usereleased@*/ /*@=compdef@*/ /*@=branchstate@*/ /*@=compmempass@*/
130 static void reader_buffer_handle(struct reader_buffer
*buf
)
131 /*@globals internalState, fileSystem, errno; @*/
132 /*@modifies internalState, fileSystem, errno, *buf; @*/
134 ssize_t rd
= reader_buffer_read(buf
);
135 if (rd
== (ssize_t
) -1) {
136 propagate(&buf
->lasterror
, errno
);
139 if (closeref(&(buf
->fd
)) == -1) {
140 propagate(&buf
->lasterror
, errno
);
146 static void reader_buffer_finalize(/*@special@*/ struct reader_buffer
*buf
)
147 /*@modifies *buf; @*/
148 /*@releases buf->data; @*/
150 if (buf
->data
!= NULL
) {
160 /* writer_buffer -- implementation *************************************/
162 static void writer_buffer_init(/*@out@*/ struct writer_buffer
*buf
, int fd
,
163 const void *data
, size_t size
)
164 /*@globals internalState, fileSystem, errno; @*/
165 /*@modifies internalState, fileSystem, errno, *buf; @*/
173 if (closeref(&(buf
->fd
)) == -1) {
174 propagate(&buf
->lasterror
, errno
);
179 static void writer_buffer_handle(struct writer_buffer
*buf
)
180 /*@globals internalState, fileSystem, errno; @*/
181 /*@modifies internalState, fileSystem, errno, *buf; @*/
183 typedef void (*my_sighandler_fn
) (int);
184 my_sighandler_fn old_sigpipe
= signal(SIGPIPE
, SIG_IGN
);
185 ssize_t wr
= write(buf
->fd
, &(((const char *) buf
->data
)[buf
->pos
]),
186 buf
->size
- buf
->pos
);
187 if (wr
== (ssize_t
) -1) {
188 propagate(&buf
->lasterror
, errno
);
192 if (wr
== (ssize_t
) -1 || buf
->pos
== buf
->size
) {
193 if (closeref(&(buf
->fd
)) == -1) {
194 propagate(&buf
->lasterror
, errno
);
197 (void) signal(SIGPIPE
, old_sigpipe
);
200 static void writer_buffer_finalize(/*@special@*/ struct writer_buffer
*buf
)
201 /*@modifies *buf; @*/
209 /* select_request -- implementation ************************************/
211 static void select_request_init(/*@out@*/ struct select_request
*sr
)
214 FD_ZERO(&(sr
->readfds
));
215 FD_ZERO(&(sr
->writefds
));
219 static void select_request_add_reader(struct select_request
*sr
,
220 const struct reader_buffer
*buf
)
224 FD_SET(buf
->fd
, &(sr
->readfds
));
225 if (buf
->fd
+ 1 > sr
->n
) {
231 static void select_request_add_writer(struct select_request
*sr
,
232 const struct writer_buffer
*buf
)
236 FD_SET(buf
->fd
, &(sr
->writefds
));
237 if (buf
->fd
+ 1 > sr
->n
) {
243 static void select_request_handle_writer(const struct select_request
*sr
,
244 struct writer_buffer
*buf
)
245 /*@globals internalState, fileSystem, errno; @*/
246 /*@modifies internalState, fileSystem, errno, *buf; @*/
248 if (buf
->fd
!= -1 && FD_ISSET(buf
->fd
, &(sr
->writefds
))) {
249 writer_buffer_handle(buf
);
253 static void select_request_handle_reader(const struct select_request
*sr
,
254 struct reader_buffer
*buf
)
255 /*@globals internalState, fileSystem, errno; @*/
256 /*@modifies internalState, fileSystem, errno, *buf; @*/
258 if (buf
->fd
!= -1 && FD_ISSET(buf
->fd
, &(sr
->readfds
))) {
259 reader_buffer_handle(buf
);
263 /* pipethrough -- helper functions *************************************/
265 static void pipethrough_child(const char *command
,
266 const int *stdin_pipe
,
267 const int *stdout_pipe
,
268 const int *stderr_pipe
)
269 /*@globals internalState, fileSystem, errno, stderr; @*/
270 /*@modifies internalState, fileSystem, errno, *stderr; @*/
272 const char *shell
= getenv("SHELL");
277 if (close(STDIN_FILENO
) == -1
278 || dup2(stdin_pipe
[PIPE_RD
], STDIN_FILENO
) == -1
279 || close(stdin_pipe
[PIPE_RD
]) == -1
280 || close(stdin_pipe
[PIPE_WR
]) == -1
281 || close(STDOUT_FILENO
) == -1
282 || dup2(stdout_pipe
[PIPE_WR
], STDOUT_FILENO
) == -1
283 || close(stdout_pipe
[PIPE_RD
]) == -1
284 || close(stdout_pipe
[PIPE_WR
]) == -1
285 || close(STDERR_FILENO
) == -1
286 || dup2(stderr_pipe
[PIPE_WR
], STDERR_FILENO
) == -1
287 || close(stderr_pipe
[PIPE_RD
]) == -1
288 || close(stderr_pipe
[PIPE_WR
]) == -1
289 || execl(shell
, shell
, "-c", command
, NULL
) == -1) {
290 perror("pipethrough_child");
291 (void) fflush(stderr
);
296 static int pipethrough_parent(int stdin_fd
,
299 const struct pipe_inbuffer
*stdin_buf
,
300 /*@out@*/ struct pipe_outbuffer
*stdout_buf
,
301 /*@out@*/ struct pipe_outbuffer
*stderr_buf
)
302 /*@globals internalState, fileSystem, errno; @*/
303 /*@modifies internalState, fileSystem, errno,
304 *stdout_buf, *stderr_buf; @*/
306 struct writer_buffer stdin_wbuf
;
307 struct reader_buffer stdout_rbuf
;
308 struct reader_buffer stderr_rbuf
;
309 struct select_request sr
;
314 writer_buffer_init(&stdin_wbuf
, stdin_fd
, stdin_buf
->data
,
316 propagate(&firsterror
, stdin_wbuf
.lasterror
);
317 reader_buffer_init(&stdout_rbuf
, stdout_fd
);
318 propagate(&firsterror
, stdout_rbuf
.lasterror
);
319 reader_buffer_init(&stderr_rbuf
, stderr_fd
);
320 propagate(&firsterror
, stdout_rbuf
.lasterror
);
322 while (stdin_wbuf
.fd
!= -1 || stdout_rbuf
.fd
!= -1
323 || stderr_rbuf
.fd
!= -1) {
324 select_request_init(&sr
);
327 select_request_add_writer(&sr
, &stdin_wbuf
);
328 select_request_add_reader(&sr
, &stdout_rbuf
);
329 select_request_add_reader(&sr
, &stderr_rbuf
);
330 select_res
= select(sr
.n
, &(sr
.readfds
), &(sr
.writefds
),
332 if (select_res
== -1) {
333 if (errno
== EINTR
) {
336 propagate(&firsterror
, errno
);
340 select_request_handle_writer(&sr
, &stdin_wbuf
);
341 propagate(&firsterror
, stdin_wbuf
.lasterror
);
342 select_request_handle_reader(&sr
, &stdout_rbuf
);
343 propagate(&firsterror
, stdout_rbuf
.lasterror
);
344 select_request_handle_reader(&sr
, &stderr_rbuf
);
345 propagate(&firsterror
, stdout_rbuf
.lasterror
);
347 stdout_buf
->data
= stdout_rbuf
.data
;
348 stdout_rbuf
.data
= NULL
;
349 stdout_buf
->size
= stdout_rbuf
.size
;
350 stderr_buf
->data
= stderr_rbuf
.data
;
351 stderr_rbuf
.data
= NULL
;
352 stderr_buf
->size
= stderr_rbuf
.size
;
356 writer_buffer_finalize(&stdin_wbuf
);
357 reader_buffer_finalize(&stdout_rbuf
);
358 reader_buffer_finalize(&stderr_rbuf
);
363 /* pipethrough -- implementation ***************************************/
365 extern int pipethrough(const char *command
,
366 const struct pipe_inbuffer
*stdin_buf
,
367 /*@out@*/ struct pipe_outbuffer
*stdout_buf
,
368 /*@out@*/ struct pipe_outbuffer
*stderr_buf
,
369 /*@out@*/ int *status
)
370 /*@globals internalState, fileSystem, errno, stderr; @*/
371 /*@modifies internalState, fileSystem, errno, *stderr,
372 *stdout_buf, *stderr_buf, *status; @*/
376 int stdin_pipe
[2] = { -1, -1 };
377 int stdout_pipe
[2] = { -1, -1 };
378 int stderr_pipe
[2] = { -1, -1 };
381 if (pipe(stdin_pipe
) == -1
382 || pipe(stdout_pipe
) == -1
383 || pipe(stderr_pipe
) == -1) {
384 propagate(&firsterror
, errno
);
388 if ((pid
= fork()) == (pid_t
) -1) {
389 propagate(&firsterror
, errno
);
394 pipethrough_child(command
, stdin_pipe
, stdout_pipe
,
399 if (closeref(&stdin_pipe
[PIPE_RD
]) == -1
400 || closeref(&stdout_pipe
[PIPE_WR
]) == -1
401 || closeref(&stderr_pipe
[PIPE_WR
]) == -1) {
402 propagate(&firsterror
, errno
);
406 if (pipethrough_parent(stdin_pipe
[PIPE_WR
], stdout_pipe
[PIPE_RD
],
407 stderr_pipe
[PIPE_RD
], stdin_buf
,
408 stdout_buf
, stderr_buf
) == -1) {
409 propagate(&firsterror
, errno
);
415 if (waitpid(pid
, status
, 0) == (pid_t
) -1) {
416 propagate(&firsterror
, errno
);
417 pipe_outbuffer_finalize(stdout_buf
);
418 pipe_outbuffer_finalize(stderr_buf
);
424 (void) close(stderr_pipe
[PIPE_RD
]);
425 (void) close(stderr_pipe
[PIPE_WR
]);
426 (void) close(stdout_pipe
[PIPE_RD
]);
427 (void) close(stdout_pipe
[PIPE_WR
]);
428 (void) close(stdin_pipe
[PIPE_RD
]);
429 (void) close(stdin_pipe
[PIPE_WR
]);
436 /* pipe_outbuffer -- implementation ************************************/
438 /*@-mustfreeonly@*/ /*@-nullstate@*/
439 extern void pipe_outbuffer_finalize(struct pipe_outbuffer
*buf
)
440 /*@modifies *buf; @*/
441 /*@releases buf->data; @*/
442 /*@ensures isnull buf->data; @*/
444 if (buf
->data
!= NULL
) {
450 /*@=mustfreeonly@*/ /*@=nullstate@*/