Debian Codegraph @

lib/pipeline.c

raw
1 /* Copyright (C) 1989, 1990, 1991, 1992, 2000, 2001, 2002, 2003
2 * Free Software Foundation, Inc.
3 * Copyright (C) 2003-2017 Colin Watson.
4 * Written for groff by James Clark ([email protected])
5 * Heavily adapted and extended for man-db by Colin Watson.
6 *
7 * This file is part of libpipeline.
8 *
9 * libpipeline is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or (at
12 * your option) any later version.
13 *
14 * libpipeline is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with libpipeline; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
22 * USA.
23 */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30 #include <errno.h>
31 #include <fcntl.h>
32 #include <signal.h>
33 #include <stdarg.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/select.h>
38 #include <sys/time.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <unistd.h>
42
43 #include "attribute.h"
44 #include "dirname.h"
45 #include "full-write.h"
46 #include "safe-read.h"
47 #include "safe-write.h"
48 #include "xalloc.h"
49 #include "xstrndup.h"
50 #include "xvasprintf.h"
51
52 #include "error.h"
53 #include "fatal.h"
54 #include "pipeline-private.h"
55 #include "pipeline.h"
56
57 #ifdef USE_SOCKETPAIR_PIPE
58 # include <netdb.h>
59 # include <netinet/in.h>
60 # include <sys/socket.h>
61 # ifdef CORRECT_SOCKETPAIR_MODE
62 # include <sys/stat.h>
63 # endif
64 # ifndef SHUT_RD
65 # define SHUT_RD 0
66 # endif
67 # ifndef SHUT_WR
68 # define SHUT_WR 1
69 # endif
70 # ifdef CORRECT_SOCKETPAIR_MODE
71 # define pipe(p) \
72 (((socketpair (AF_UNIX, SOCK_STREAM, 0, p) < 0) || \
73 (shutdown ((p)[1], SHUT_RD) < 0) || \
74 (fchmod ((p)[1], S_IWUSR) < 0) || \
75 (shutdown ((p)[0], SHUT_WR) < 0) || \
76 (fchmod ((p)[0], S_IRUSR) < 0)) \
77 ? -1 \
78 : 0)
79 # else
80 # define pipe(p) \
81 (((socketpair (AF_UNIX, SOCK_STREAM, 0, p) < 0) || \
82 (shutdown ((p)[1], SHUT_RD) < 0) || \
83 (shutdown ((p)[0], SHUT_WR) < 0)) \
84 ? -1 \
85 : 0)
86 # endif
87 #endif
88
89 #if defined(HAVE_SETENV) && !defined(HAVE_CLEARENV)
90 int clearenv (void)
91 {
92 /* According to:
93 * https://github.com/dovecot/core/blob/2.3.14/src/lib/env-util.c#L63-L70
94 * simply setting "environ = NULL" crashes some systems. Creating a
95 * new environment consisting of just a terminator is indeed
96 * probably the best we can do.
97 */
98 environ = XCALLOC (1, char *);
99 return 0;
100 }
101 #endif
102
103 /* ---------------------------------------------------------------------- */
104
105 /* Functions to build individual commands. */
106
107 pipecmd *pipecmd_new (const char *name)
108 {
109 pipecmd *cmd = XMALLOC (pipecmd);
110 struct pipecmd_process *cmdp;
111 char *name_base;
112
113 cmd->tag = PIPECMD_PROCESS;
114 cmd->name = xstrdup (name);
115 cmd->nice = 0;
116 cmd->discard_err = 0;
117 cmd->cwd_fd = -1;
118 cmd->cwd = NULL;
119
120 cmd->nenv = 0;
121 cmd->env_max = 4;
122 cmd->env = xnmalloc (cmd->env_max, sizeof *cmd->env);
123
124 cmd->pre_exec_func = NULL;
125 cmd->pre_exec_free_func = NULL;
126 cmd->pre_exec_data = NULL;
127
128 cmdp = &cmd->u.process;
129
130 cmdp->argc = 0;
131 cmdp->argv_max = 4;
132 cmdp->argv = xnmalloc (cmdp->argv_max, sizeof *cmdp->argv);
133
134 /* argv[0] is the basename of the command name. */
135 name_base = base_name (name);
136 pipecmd_arg (cmd, name_base);
137 free (name_base);
138
139 return cmd;
140 }
141
142 pipecmd *pipecmd_new_argv (const char *name, va_list argv)
143 {
144 pipecmd *cmd = pipecmd_new (name);
145 pipecmd_argv (cmd, argv);
146 return cmd;
147 }
148
149 pipecmd *pipecmd_new_args (const char *name, ...)
150 {
151 va_list argv;
152 pipecmd *cmd;
153
154 va_start (argv, name);
155 cmd = pipecmd_new_argv (name, argv);
156 va_end (argv);
157
158 return cmd;
159 }
160
161 /* As suggested in the header file, this function (for pipecmd_new_argstr()
162 * and pipecmd_argstr()) is really a wart. If we didn't have to worry about
163 * old configuration files then it wouldn't be necessary. Worse, the
164 * definition for tr in man_db.conf currently contains single-quoting, and
165 * people probably took that as a licence to do similar things, so we're
166 * obliged to worry about quoting as well!
167 *
168 * However, we can mitigate this; shell quoting alone is safe though
169 * sometimes confusing, but it's other shell constructs that tend to cause
170 * real security holes. Therefore, rather than punting to 'sh -c' or
171 * whatever, we parse a safe subset manually. Environment variables are not
172 * currently handled because of tricky word splitting issues, but in
173 * principle they could be if there's demand for it.
174 *
175 * TODO: Support setting environment variables.
176 */
177 static char *argstr_get_word (const char **argstr)
178 {
179 char *out = NULL;
180 const char *litstart;
181 enum {
182 NONE,
183 SINGLE,
184 DOUBLE
185 } quotemode = NONE;
186
187 /* Skip over any leading whitespace. */
188 while (**argstr == ' ' || **argstr == '\t')
189 ++*argstr;
190
191 litstart = *argstr;
192 while (**argstr) {
193 char backslashed[2];
194
195 /* If it's just a literal character, go round again. */
196 if ((quotemode == NONE && !strchr (" \t'\"\\", **argstr)) ||
197 /* nothing is special in '; terminated by ' */
198 (quotemode == SINGLE && **argstr != '\'') ||
199 /* \ is special in "; terminated by " */
200 (quotemode == DOUBLE && !strchr ("\"\\", **argstr))) {
201 ++*argstr;
202 continue;
203 }
204
205 /* Within "", \ is only special when followed by $, `, ", or
206 * \ (or <newline> in a real shell, but we don't do that).
207 */
208 if (quotemode == DOUBLE && **argstr == '\\' &&
209 !strchr ("$`\"\\", *(*argstr + 1))) {
210 ++*argstr;
211 continue;
212 }
213
214 /* Copy any accumulated literal characters. */
215 if (litstart < *argstr) {
216 char *tmp = xstrndup (litstart, *argstr - litstart);
217 out = appendstr (out, tmp, nullptr);
218 free (tmp);
219 }
220
221 switch (**argstr) {
222 case ' ':
223 case '\t':
224 /* End of word. */
225 return out;
226
227 case '\'':
228 if (quotemode != NONE)
229 quotemode = NONE;
230 else
231 quotemode = SINGLE;
232 litstart = ++*argstr;
233 break;
234
235 case '"':
236 if (quotemode != NONE)
237 quotemode = NONE;
238 else
239 quotemode = DOUBLE;
240 litstart = ++*argstr;
241 break;
242
243 case '\\':
244 backslashed[0] = *++*argstr;
245 if (!backslashed[0]) {
246 /* Unterminated quoting; give up. */
247 free (out);
248 return NULL;
249 }
250 backslashed[1] = '\0';
251 out = appendstr (out, backslashed, nullptr);
252 litstart = ++*argstr;
253 break;
254
255 default:
256 assert (!"unexpected state parsing argstr");
257 }
258 }
259
260 if (quotemode != NONE) {
261 /* Unterminated quoting; give up. */
262 free (out);
263 return NULL;
264 }
265
266 /* Copy any accumulated literal characters. */
267 if (litstart < *argstr) {
268 char *tmp = xstrndup (litstart, *argstr - litstart);
269 out = appendstr (out, tmp, nullptr);
270 free (tmp);
271 }
272
273 return out;
274 }
275
276 pipecmd *pipecmd_new_argstr (const char *argstr)
277 {
278 pipecmd *cmd;
279 char *arg;
280
281 arg = argstr_get_word (&argstr);
282 if (!arg)
283 fatal (0, "badly formed configuration directive: '%s'",
284 argstr);
285 if (!strcmp (arg, "exec")) {
286 /* Some old configuration files have "exec command" rather
287 * than "command"; this worked fine when being evaluated by
288 * a shell, but since exec is a shell builtin it doesn't
289 * work when being executed directly. To work around this,
290 * we just drop "exec" if it appears at the start of argstr.
291 */
292 free (arg);
293 arg = argstr_get_word (&argstr);
294 if (!arg)
295 fatal (0, "badly formed configuration directive: '%s'",
296 argstr);
297 }
298 cmd = pipecmd_new (arg);
299 free (arg);
300
301 while ((arg = argstr_get_word (&argstr))) {
302 pipecmd_arg (cmd, arg);
303 free (arg);
304 }
305
306 return cmd;
307 }
308
309 pipecmd *pipecmd_new_function (const char *name, pipecmd_function_type *func,
310 pipecmd_function_free_type *free_func,
311 void *data)
312 {
313 pipecmd *cmd = XMALLOC (pipecmd);
314 struct pipecmd_function *cmdf;
315
316 cmd->tag = PIPECMD_FUNCTION;
317 cmd->name = xstrdup (name);
318 cmd->nice = 0;
319 cmd->discard_err = 0;
320 cmd->cwd_fd = -1;
321 cmd->cwd = NULL;
322
323 cmd->nenv = 0;
324 cmd->env_max = 4;
325 cmd->env = xnmalloc (cmd->env_max, sizeof *cmd->env);
326
327 cmd->pre_exec_func = NULL;
328 cmd->pre_exec_free_func = NULL;
329 cmd->pre_exec_data = NULL;
330
331 cmdf = &cmd->u.function;
332
333 cmdf->func = func;
334 cmdf->free_func = free_func;
335 cmdf->data = data;
336
337 return cmd;
338 }
339
340 pipecmd *pipecmd_new_sequencev (const char *name, va_list cmdv)
341 {
342 pipecmd *cmd = XMALLOC (pipecmd);
343 struct pipecmd_sequence *cmds;
344 pipecmd *child;
345
346 cmd->tag = PIPECMD_SEQUENCE;
347 cmd->name = xstrdup (name);
348 cmd->nice = 0;
349 cmd->discard_err = 0;
350 cmd->cwd_fd = -1;
351 cmd->cwd = NULL;
352
353 cmd->nenv = 0;
354 cmd->env_max = 4;
355 cmd->env = xnmalloc (cmd->env_max, sizeof *cmd->env);
356
357 cmd->pre_exec_func = NULL;
358 cmd->pre_exec_free_func = NULL;
359 cmd->pre_exec_data = NULL;
360
361 cmds = &cmd->u.sequence;
362
363 cmds->ncommands = 0;
364 cmds->commands_max = 4;
365 cmds->commands = xnmalloc (cmds->commands_max, sizeof *cmds->commands);
366
367 child = va_arg (cmdv, pipecmd *);
368 while (child) {
369 pipecmd_sequence_command (cmd, child);
370 child = va_arg (cmdv, pipecmd *);
371 }
372
373 return cmd;
374 }
375
376 pipecmd *pipecmd_new_sequence (const char *name, ...)
377 {
378 va_list cmdv;
379 pipecmd *cmd;
380
381 va_start (cmdv, name);
382 cmd = pipecmd_new_sequencev (name, cmdv);
383 va_end (cmdv);
384
385 return cmd;
386 }
387
388 static void passthrough (void *data _GL_UNUSED)
389 {
390 for (;;) {
391 char buffer[4096];
392 size_t r = safe_read (STDIN_FILENO, buffer, 4096);
393 if (r == SAFE_READ_ERROR)
394 /* TODO: Function commands have no way to signal
395 * errors.
396 */
397 break;
398 if (r == 0)
399 break;
400 if (full_write (STDOUT_FILENO, buffer, (size_t) r) <
401 (size_t) r)
402 break;
403 }
404
405 return;
406 }
407
408 pipecmd *pipecmd_new_passthrough (void)
409 {
410 return pipecmd_new_function ("cat", &passthrough, NULL, NULL);
411 }
412
413 pipecmd *pipecmd_dup (pipecmd *cmd)
414 {
415 pipecmd *newcmd = XMALLOC (pipecmd);
416 int i;
417
418 newcmd->tag = cmd->tag;
419 newcmd->name = xstrdup (cmd->name);
420 newcmd->nice = cmd->nice;
421 newcmd->discard_err = cmd->discard_err;
422 newcmd->cwd_fd = cmd->cwd_fd;
423 newcmd->cwd = cmd->cwd ? xstrdup (cmd->cwd) : NULL;
424
425 newcmd->nenv = cmd->nenv;
426 newcmd->env_max = cmd->env_max;
427 assert (newcmd->nenv <= newcmd->env_max);
428 newcmd->env = xmalloc (newcmd->env_max * sizeof *newcmd->env);
429
430 newcmd->pre_exec_func = cmd->pre_exec_func;
431 newcmd->pre_exec_free_func = cmd->pre_exec_free_func;
432 newcmd->pre_exec_data = cmd->pre_exec_data;
433
434 for (i = 0; i < cmd->nenv; ++i) {
435 newcmd->env[i].name =
436 cmd->env[i].name ? xstrdup (cmd->env[i].name) : NULL;
437 newcmd->env[i].value =
438 cmd->env[i].value ? xstrdup (cmd->env[i].value) : NULL;
439 }
440
441 switch (newcmd->tag) {
442 case PIPECMD_PROCESS: {
443 struct pipecmd_process *cmdp = &cmd->u.process;
444 struct pipecmd_process *newcmdp = &newcmd->u.process;
445
446 newcmdp->argc = cmdp->argc;
447 newcmdp->argv_max = cmdp->argv_max;
448 assert (newcmdp->argc < newcmdp->argv_max);
449 newcmdp->argv = xmalloc (newcmdp->argv_max *
450 sizeof *newcmdp->argv);
451
452 for (i = 0; i < cmdp->argc; ++i)
453 newcmdp->argv[i] = xstrdup (cmdp->argv[i]);
454 newcmdp->argv[cmdp->argc] = NULL;
455
456 break;
457 }
458
459 case PIPECMD_FUNCTION: {
460 struct pipecmd_function *cmdf = &cmd->u.function;
461 struct pipecmd_function *newcmdf = &newcmd->u.function;
462
463 newcmdf->func = cmdf->func;
464 newcmdf->free_func = cmdf->free_func;
465 newcmdf->data = cmdf->data;
466
467 break;
468 }
469
470 case PIPECMD_SEQUENCE: {
471 struct pipecmd_sequence *cmds = &cmd->u.sequence;
472 struct pipecmd_sequence *newcmds = &newcmd->u.sequence;
473
474 newcmds->ncommands = cmds->ncommands;
475 newcmds->commands_max = cmds->commands_max;
476 assert (newcmds->ncommands <= newcmds->commands_max);
477 newcmds->commands =
478 xmalloc (newcmds->commands_max *
479 sizeof *newcmds->commands);
480
481 for (i = 0; i < cmds->ncommands; ++i)
482 newcmds->commands[i] =
483 pipecmd_dup (cmds->commands[i]);
484
485 break;
486 }
487 }
488
489 return newcmd;
490 }
491
492 void pipecmd_arg (pipecmd *cmd, const char *arg)
493 {
494 struct pipecmd_process *cmdp;
495
496 assert (cmd->tag == PIPECMD_PROCESS);
497 cmdp = &cmd->u.process;
498
499 if (cmdp->argc + 1 >= cmdp->argv_max) {
500 cmdp->argv_max *= 2;
501 cmdp->argv = xrealloc (cmdp->argv,
502 cmdp->argv_max * sizeof *cmdp->argv);
503 }
504
505 cmdp->argv[cmdp->argc++] = xstrdup (arg);
506 assert (cmdp->argc < cmdp->argv_max);
507 cmdp->argv[cmdp->argc] = NULL;
508 }
509
510 void pipecmd_argf (pipecmd *cmd, const char *format, ...)
511 {
512 va_list argv;
513 char *arg;
514
515 va_start (argv, format);
516 arg = xvasprintf (format, argv);
517 pipecmd_arg (cmd, arg);
518 free (arg);
519 va_end (argv);
520 }
521
522 void pipecmd_argv (pipecmd *cmd, va_list argv)
523 {
524 const char *arg = va_arg (argv, const char *);
525
526 assert (cmd->tag == PIPECMD_PROCESS);
527
528 while (arg) {
529 pipecmd_arg (cmd, arg);
530 arg = va_arg (argv, const char *);
531 }
532 }
533
534 void pipecmd_args (pipecmd *cmd, ...)
535 {
536 va_list argv;
537
538 assert (cmd->tag == PIPECMD_PROCESS);
539
540 va_start (argv, cmd);
541 pipecmd_argv (cmd, argv);
542 va_end (argv);
543 }
544
545 void pipecmd_argstr (pipecmd *cmd, const char *argstr)
546 {
547 char *arg;
548
549 assert (cmd->tag == PIPECMD_PROCESS);
550
551 while ((arg = argstr_get_word (&argstr))) {
552 pipecmd_arg (cmd, arg);
553 free (arg);
554 }
555 }
556
557 int ATTRIBUTE_PURE pipecmd_get_nargs (pipecmd *cmd)
558 {
559 struct pipecmd_process *cmdp;
560
561 assert (cmd->tag == PIPECMD_PROCESS);
562 cmdp = &cmd->u.process;
563
564 return cmdp->argc;
565 }
566
567 void pipecmd_nice (pipecmd *cmd, int value)
568 {
569 cmd->nice = value;
570 }
571
572 void pipecmd_discard_err (pipecmd *cmd, int discard_err)
573 {
574 cmd->discard_err = discard_err;
575 }
576
577 void pipecmd_chdir (pipecmd *cmd, const char *directory)
578 {
579 free (cmd->cwd);
580 cmd->cwd = xstrdup (directory);
581 }
582
583 void pipecmd_fchdir (pipecmd *cmd, int directory_fd)
584 {
585 cmd->cwd_fd = directory_fd;
586 }
587
588 void pipecmd_setenv (pipecmd *cmd, const char *name, const char *value)
589 {
590 if (cmd->nenv >= cmd->env_max) {
591 cmd->env_max *= 2;
592 cmd->env =
593 xrealloc (cmd->env, cmd->env_max * sizeof *cmd->env);
594 }
595
596 cmd->env[cmd->nenv].name = xstrdup (name);
597 cmd->env[cmd->nenv].value = xstrdup (value);
598 ++cmd->nenv;
599 }
600
601 void pipecmd_unsetenv (pipecmd *cmd, const char *name)
602 {
603 if (cmd->nenv >= cmd->env_max) {
604 cmd->env_max *= 2;
605 cmd->env =
606 xrealloc (cmd->env, cmd->env_max * sizeof *cmd->env);
607 }
608
609 cmd->env[cmd->nenv].name = xstrdup (name);
610 cmd->env[cmd->nenv].value = NULL;
611 ++cmd->nenv;
612 }
613
614 void pipecmd_clearenv (pipecmd *cmd)
615 {
616 if (cmd->nenv >= cmd->env_max) {
617 cmd->env_max *= 2;
618 cmd->env =
619 xrealloc (cmd->env, cmd->env_max * sizeof *cmd->env);
620 }
621
622 cmd->env[cmd->nenv].name = NULL;
623 cmd->env[cmd->nenv].value = NULL;
624 ++cmd->nenv;
625 }
626
627 void pipecmd_pre_exec (pipecmd *cmd, pipecmd_function_type *func,
628 pipecmd_function_free_type *free_func, void *data)
629 {
630 cmd->pre_exec_func = func;
631 cmd->pre_exec_free_func = free_func;
632 cmd->pre_exec_data = data;
633 }
634
635 void pipecmd_sequence_command (pipecmd *cmd, pipecmd *child)
636 {
637 struct pipecmd_sequence *cmds;
638
639 assert (cmd->tag == PIPECMD_SEQUENCE);
640 cmds = &cmd->u.sequence;
641
642 if (cmds->ncommands >= cmds->commands_max) {
643 cmds->commands_max *= 2;
644 cmds->commands =
645 xrealloc (cmds->commands,
646 cmds->commands_max * sizeof *cmds->commands);
647 }
648
649 cmds->commands[cmds->ncommands++] = child;
650 }
651
652 void pipecmd_dump (pipecmd *cmd, FILE *stream)
653 {
654 int i;
655
656 if (cmd->cwd_fd >= 0)
657 fprintf (stream, "(cd <fd %d> && ", cmd->cwd_fd);
658 else if (cmd->cwd)
659 fprintf (stream, "(cd %s && ", cmd->cwd);
660
661 for (i = 0; i < cmd->nenv; ++i) {
662 if (cmd->env[i].name)
663 fprintf (stream, "%s=%s ", cmd->env[i].name,
664 cmd->env[i].value ? cmd->env[i].value
665 : "<unset>");
666 else
667 fputs ("env -i ", stream);
668 }
669
670 switch (cmd->tag) {
671 case PIPECMD_PROCESS: {
672 struct pipecmd_process *cmdp = &cmd->u.process;
673
674 fputs (cmd->name, stream);
675 for (i = 1; i < cmdp->argc; ++i) {
676 /* TODO: escape_shell()? */
677 putc (' ', stream);
678 fputs (cmdp->argv[i], stream);
679 }
680
681 break;
682 }
683
684 case PIPECMD_FUNCTION:
685 fputs (cmd->name, stream);
686 break;
687
688 case PIPECMD_SEQUENCE: {
689 struct pipecmd_sequence *cmds = &cmd->u.sequence;
690
691 putc ('(', stream);
692 for (i = 0; i < cmds->ncommands; ++i) {
693 pipecmd_dump (cmds->commands[i], stream);
694 if (i < cmds->ncommands - 1)
695 fputs (" && ", stream);
696 }
697 putc (')', stream);
698
699 break;
700 }
701 }
702
703 if (cmd->cwd_fd >= 0 || cmd->cwd)
704 putc (')', stream);
705 }
706
707 char *pipecmd_tostring (pipecmd *cmd)
708 {
709 char *out = NULL;
710 int i;
711
712 if (cmd->cwd_fd >= 0) {
713 char *cwd_fd_str = xasprintf ("%d", cmd->cwd_fd);
714 out = appendstr (out, "(cd <fd ", cwd_fd_str, "> && ",
715 nullptr);
716 free (cwd_fd_str);
717 } else if (cmd->cwd)
718 out = appendstr (out, "(cd ", cmd->cwd, " && ", nullptr);
719
720 for (i = 0; i < cmd->nenv; ++i) {
721 if (cmd->env[i].name)
722 out = appendstr (out, cmd->env[i].name, "=",
723 cmd->env[i].value ? cmd->env[i].value
724 : "<unset>",
725 " ", nullptr);
726 else
727 out = appendstr (out, "env -i ", nullptr);
728 }
729
730 switch (cmd->tag) {
731 case PIPECMD_PROCESS: {
732 struct pipecmd_process *cmdp = &cmd->u.process;
733
734 out = appendstr (out, cmd->name, nullptr);
735 for (i = 1; i < cmdp->argc; ++i)
736 /* TODO: escape_shell()? */
737 out = appendstr (out, " ", cmdp->argv[i],
738 nullptr);
739
740 break;
741 }
742
743 case PIPECMD_FUNCTION:
744 out = appendstr (out, cmd->name, nullptr);
745 break;
746
747 case PIPECMD_SEQUENCE: {
748 struct pipecmd_sequence *cmds = &cmd->u.sequence;
749
750 out = appendstr (out, "(", nullptr);
751 for (i = 0; i < cmds->ncommands; ++i) {
752 char *subout =
753 pipecmd_tostring (cmds->commands[i]);
754 out = appendstr (out, subout, nullptr);
755 free (subout);
756 if (i < cmds->ncommands - 1)
757 out = appendstr (out, " && ", nullptr);
758 }
759 out = appendstr (out, ")", nullptr);
760
761 break;
762 }
763 }
764
765 if (cmd->cwd_fd >= 0 || cmd->cwd)
766 out = appendstr (out, ")", nullptr);
767
768 return out;
769 }
770
771 /* Children exit with this status if execvp fails. */
772 #define EXEC_FAILED_EXIT_STATUS 0xff
773
774 #ifdef SIGPIPE
775 # define IS_SIGPIPE(sig) (sig == SIGPIPE)
776 #else
777 # define IS_SIGPIPE(sig) 0
778 #endif
779
780 /* When called internally during pipeline execution, this is called in the
781 * forked child process, with file descriptors already set up.
782 */
783 void pipecmd_exec (pipecmd *cmd)
784 {
785 int i;
786
787 if (cmd->nice)
788 if (nice (cmd->nice) < 0)
789 /* Don't worry too much. */
790 debug ("nice failed: %s\n", strerror (errno));
791
792 if (cmd->discard_err) {
793 int devnull = open ("/dev/null", O_WRONLY);
794 if (devnull != -1) {
795 dup2 (devnull, 2);
796 close (devnull);
797 }
798 }
799
800 if (cmd->cwd_fd >= 0) {
801 if (fchdir (cmd->cwd_fd) < 0)
802 error (EXEC_FAILED_EXIT_STATUS, errno,
803 "can't change directory to fd %d", cmd->cwd_fd);
804 } else if (cmd->cwd) {
805 if (chdir (cmd->cwd) < 0)
806 error (EXEC_FAILED_EXIT_STATUS, errno,
807 "can't change directory to '%s'", cmd->cwd);
808 }
809
810 for (i = 0; i < cmd->nenv; ++i) {
811 if (cmd->env[i].name) {
812 if (cmd->env[i].value)
813 setenv (cmd->env[i].name, cmd->env[i].value,
814 1);
815 else
816 unsetenv (cmd->env[i].name);
817 } else
818 clearenv ();
819 }
820
821 switch (cmd->tag) {
822 case PIPECMD_PROCESS: {
823 struct pipecmd_process *cmdp = &cmd->u.process;
824 if (cmd->pre_exec_func)
825 cmd->pre_exec_func (cmd->pre_exec_data);
826 execvp (cmd->name, cmdp->argv);
827 break;
828 }
829
830 /* TODO: ideally, could there be a facility
831 * to execute non-blocking functions without
832 * needing to fork?
833 */
834 case PIPECMD_FUNCTION: {
835 struct pipecmd_function *cmdf = &cmd->u.function;
836 if (cmd->pre_exec_func)
837 cmd->pre_exec_func (cmd->pre_exec_data);
838 cmdf->func (cmdf->data);
839 /* pacify valgrind et al */
840 if (cmdf->free_func)
841 cmdf->free_func (cmdf->data);
842 if (cmd->pre_exec_free_func)
843 cmd->pre_exec_free_func (cmd->pre_exec_data);
844 fflush (NULL);
845 _exit (0);
846 }
847
848 case PIPECMD_SEQUENCE: {
849 struct pipecmd_sequence *cmds = &cmd->u.sequence;
850 struct sigaction sa;
851
852 /* Flush all pending output so that subprocesses
853 * don't inherit it.
854 */
855 fflush (NULL);
856
857 /* pipeline_start will have blocked SIGCHLD. We like
858 * it that way. Lose the parent's signal handler,
859 * though.
860 */
861 memset (&sa, 0, sizeof sa);
862 sa.sa_handler = SIG_DFL;
863 sigemptyset (&sa.sa_mask);
864 sa.sa_flags = 0;
865 if (sigaction (SIGCHLD, &sa, NULL) == -1)
866 fatal (errno, "can't install SIGCHLD handler");
867
868 for (i = 0; i < cmds->ncommands; ++i) {
869 pipecmd *child = cmds->commands[i];
870 pid_t pid = fork ();
871 int status;
872
873 if (pid < 0)
874 fatal (errno, "fork failed");
875 if (pid == 0)
876 pipecmd_exec (child);
877 debug ("Started \"%s\", pid %d\n", child->name,
878 pid);
879
880 while (waitpid (pid, &status, 0) < 0) {
881 if (errno == EINTR)
882 continue;
883 fatal (errno, "waitpid failed");
884 }
885
886 debug (" \"%s\" (%d) -> %d\n", child->name,
887 pid, status);
888
889 if (WIFSIGNALED (status)) {
890 int sig = WTERMSIG (status);
891 if (IS_SIGPIPE (sig))
892 status = 0;
893 else if (getenv ("PIPELINE_QUIET"))
894 ;
895 else if (WCOREDUMP (status))
896 error (0, 0,
897 "%s: %s (core dumped)",
898 child->name,
899 strsignal (sig));
900 else
901 error (0, 0, "%s: %s",
902 child->name,
903 strsignal (sig));
904 } else if (!WIFEXITED (status))
905 error (0, 0, "unexpected status %d",
906 status);
907
908 if (child->tag == PIPECMD_FUNCTION) {
909 struct pipecmd_function *cmdf =
910 &child->u.function;
911 if (cmdf->free_func)
912 (*cmdf->free_func) (
913 cmdf->data);
914 }
915
916 if (WIFSIGNALED (status)) {
917 raise (WTERMSIG (status));
918 _exit (1); /* just to make sure */
919 } else if (status && WIFEXITED (status))
920 _exit (WEXITSTATUS (status));
921 }
922
923 _exit (0);
924 }
925 }
926
927 error (EXEC_FAILED_EXIT_STATUS, errno, "can't execute %s", cmd->name);
928 /* Never called, but gcc doesn't realise that error with non-zero
929 * status always exits.
930 */
931 _exit (EXEC_FAILED_EXIT_STATUS);
932 }
933
934 void pipecmd_free (pipecmd *cmd)
935 {
936 int i;
937
938 if (!cmd)
939 return;
940
941 free (cmd->name);
942 free (cmd->cwd);
943
944 for (i = 0; i < cmd->nenv; ++i) {
945 free (cmd->env[i].name);
946 free (cmd->env[i].value);
947 }
948 free (cmd->env);
949
950 switch (cmd->tag) {
951 case PIPECMD_PROCESS: {
952 struct pipecmd_process *cmdp = &cmd->u.process;
953
954 for (i = 0; i < cmdp->argc; ++i)
955 free (cmdp->argv[i]);
956 free (cmdp->argv);
957
958 break;
959 }
960
961 case PIPECMD_FUNCTION:
962 break;
963
964 case PIPECMD_SEQUENCE: {
965 struct pipecmd_sequence *cmds = &cmd->u.sequence;
966
967 for (i = 0; i < cmds->ncommands; ++i)
968 pipecmd_free (cmds->commands[i]);
969 free (cmds->commands);
970
971 break;
972 }
973 }
974
975 free (cmd);
976 }
977
978 /* ---------------------------------------------------------------------- */
979
980 /* Functions to build pipelines. */
981
982 pipeline *pipeline_new (void)
983 {
984 pipeline *p = XMALLOC (pipeline);
985 p->ncommands = 0;
986 p->commands_max = 4;
987 p->commands = xnmalloc (p->commands_max, sizeof *p->commands);
988 p->pids = NULL;
989 p->statuses = NULL;
990 p->redirect_in = p->redirect_out = REDIRECT_NONE;
991 p->want_in = p->want_out = 0;
992 p->want_infile = p->want_outfile = NULL;
993 p->infd = p->outfd = -1;
994 p->infile = p->outfile = NULL;
995 p->source = NULL;
996 p->buffer = NULL;
997 p->buflen = p->bufmax = 0;
998 p->line_cache = NULL;
999 p->peek_offset = 0;
1000 p->ignore_signals = 0;
1001 return p;
1002 }
1003
1004 pipeline *pipeline_new_commandv (pipecmd *cmd1, va_list cmdv)
1005 {
1006 pipeline *p = pipeline_new ();
1007 pipeline_command (p, cmd1);
1008 pipeline_commandv (p, cmdv);
1009 return p;
1010 }
1011
1012 pipeline *pipeline_new_commands (pipecmd *cmd1, ...)
1013 {
1014 va_list cmdv;
1015 pipeline *p;
1016
1017 va_start (cmdv, cmd1);
1018 p = pipeline_new_commandv (cmd1, cmdv);
1019 va_end (cmdv);
1020
1021 return p;
1022 }
1023
1024 pipeline *pipeline_new_command_argv (const char *name, va_list argv)
1025 {
1026 pipeline *p;
1027 pipecmd *cmd;
1028
1029 p = pipeline_new ();
1030 cmd = pipecmd_new_argv (name, argv);
1031 pipeline_command (p, cmd);
1032
1033 return p;
1034 }
1035
1036 pipeline *pipeline_new_command_args (const char *name, ...)
1037 {
1038 va_list argv;
1039 pipeline *p;
1040
1041 va_start (argv, name);
1042 p = pipeline_new_command_argv (name, argv);
1043 va_end (argv);
1044
1045 return p;
1046 }
1047
1048 pipeline *pipeline_join (pipeline *p1, pipeline *p2)
1049 {
1050 pipeline *p = XMALLOC (pipeline);
1051 int i;
1052
1053 assert (!p1->pids);
1054 assert (!p2->pids);
1055 assert (!p1->statuses);
1056 assert (!p2->statuses);
1057
1058 p->ncommands = p1->ncommands + p2->ncommands;
1059 p->commands_max = p1->ncommands + p2->ncommands;
1060 p->commands = xnmalloc (p->commands_max, sizeof *p->commands);
1061 p->pids = NULL;
1062 p->statuses = NULL;
1063 p->redirect_in = p1->redirect_in;
1064 p->want_in = p1->want_in;
1065 p->want_infile = p1->want_infile ? xstrdup (p1->want_infile) : NULL;
1066 p->redirect_out = p2->redirect_out;
1067 p->want_out = p2->want_out;
1068 p->want_outfile = p2->want_outfile ? xstrdup (p2->want_outfile) : NULL;
1069 p->infd = p1->infd;
1070 p->outfd = p2->outfd;
1071 p->infile = p1->infile;
1072 p->outfile = p2->outfile;
1073 p->source = NULL;
1074 p->buffer = NULL;
1075 p->buflen = p->bufmax = 0;
1076 p->line_cache = NULL;
1077 p->peek_offset = 0;
1078 p->ignore_signals = (p1->ignore_signals || p2->ignore_signals);
1079
1080 for (i = 0; i < p1->ncommands; ++i)
1081 p->commands[i] = pipecmd_dup (p1->commands[i]);
1082 for (i = 0; i < p2->ncommands; ++i)
1083 p->commands[p1->ncommands + i] = pipecmd_dup (p2->commands[i]);
1084
1085 return p;
1086 }
1087
1088 void pipeline_connect (pipeline *source, pipeline *sink, ...)
1089 {
1090 va_list argv;
1091 pipeline *arg;
1092
1093 /* We must be in control of output from the source pipeline. If the
1094 * source isn't started, we can force this.
1095 */
1096 if (!source->pids)
1097 pipeline_want_out (source, -1);
1098 assert (source->redirect_out == REDIRECT_FD);
1099 assert (source->want_out < 0);
1100
1101 va_start (argv, sink);
1102 for (arg = sink; arg; arg = va_arg (argv, pipeline *)) {
1103 assert (!arg->pids); /* not started */
1104 arg->source = source;
1105 pipeline_want_in (arg, -1);
1106
1107 /* Zero-command sinks should represent data being passed
1108 * straight through from the input to the output.
1109 * Unfortunately pipeline_start and pipeline_pump don't
1110 * handle this very well between them; a zero-command
1111 * pipeline has the write end of its input pipe wrongly
1112 * stashed in outfd and then pipeline_pump can't handle it
1113 * because it has nowhere to send output. Until this is
1114 * fixed, this kludge is necessary.
1115 */
1116 if (arg->ncommands == 0)
1117 pipeline_command (arg, pipecmd_new_passthrough ());
1118 }
1119 va_end (argv);
1120 }
1121
1122 void pipeline_command (pipeline *p, pipecmd *cmd)
1123 {
1124 if (p->ncommands >= p->commands_max) {
1125 p->commands_max *= 2;
1126 p->commands = xrealloc (p->commands,
1127 p->commands_max * sizeof *p->commands);
1128 }
1129
1130 p->commands[p->ncommands++] = cmd;
1131 }
1132
1133 void pipeline_command_argv (pipeline *p, const char *name, va_list argv)
1134 {
1135 pipecmd *cmd;
1136
1137 cmd = pipecmd_new_argv (name, argv);
1138 pipeline_command (p, cmd);
1139 }
1140
1141 void pipeline_command_args (pipeline *p, const char *name, ...)
1142 {
1143 va_list argv;
1144
1145 va_start (argv, name);
1146 pipeline_command_argv (p, name, argv);
1147 va_end (argv);
1148 }
1149
1150 void pipeline_command_argstr (pipeline *p, const char *argstr)
1151 {
1152 pipeline_command (p, pipecmd_new_argstr (argstr));
1153 }
1154
1155 void pipeline_commandv (pipeline *p, va_list cmdv)
1156 {
1157 pipecmd *cmd = va_arg (cmdv, pipecmd *);
1158
1159 while (cmd) {
1160 pipeline_command (p, cmd);
1161 cmd = va_arg (cmdv, pipecmd *);
1162 }
1163 }
1164
1165 void pipeline_commands (pipeline *p, ...)
1166 {
1167 va_list cmdv;
1168
1169 va_start (cmdv, p);
1170 pipeline_commandv (p, cmdv);
1171 va_end (cmdv);
1172 }
1173
1174 int ATTRIBUTE_PURE pipeline_get_ncommands (pipeline *p)
1175 {
1176 return p->ncommands;
1177 }
1178
1179 pipecmd *ATTRIBUTE_PURE pipeline_get_command (pipeline *p, int n)
1180 {
1181 if (n < 0 || n >= p->ncommands)
1182 return NULL;
1183 return p->commands[n];
1184 }
1185
1186 pipecmd *pipeline_set_command (pipeline *p, int n, pipecmd *cmd)
1187 {
1188 pipecmd *prev;
1189 if (n < 0 || n >= p->ncommands)
1190 return NULL;
1191 prev = p->commands[n];
1192 p->commands[n] = cmd;
1193 return prev;
1194 }
1195
1196 pid_t ATTRIBUTE_PURE pipeline_get_pid (pipeline *p, int n)
1197 {
1198 assert (p->pids); /* pipeline started */
1199 if (n < 0 || n >= p->ncommands)
1200 return -1;
1201 return p->pids[n];
1202 }
1203
1204 void pipeline_want_in (pipeline *p, int fd)
1205 {
1206 p->redirect_in = REDIRECT_FD;
1207 p->want_in = fd;
1208 p->want_infile = NULL;
1209 }
1210
1211 void pipeline_want_out (pipeline *p, int fd)
1212 {
1213 p->redirect_out = REDIRECT_FD;
1214 p->want_out = fd;
1215 p->want_outfile = NULL;
1216 }
1217
1218 void pipeline_want_infile (pipeline *p, const char *file)
1219 {
1220 p->redirect_in = (file != NULL) ? REDIRECT_FILE_NAME : REDIRECT_NONE;
1221 p->want_in = 0;
1222 p->want_infile = file ? xstrdup (file) : NULL;
1223 }
1224
1225 void pipeline_want_outfile (pipeline *p, const char *file)
1226 {
1227 p->redirect_out = (file != NULL) ? REDIRECT_FILE_NAME : REDIRECT_NONE;
1228 p->want_out = 0;
1229 p->want_outfile = file ? xstrdup (file) : NULL;
1230 }
1231
1232 void pipeline_ignore_signals (pipeline *p, int ignore_signals)
1233 {
1234 p->ignore_signals = ignore_signals;
1235 }
1236
1237 FILE *pipeline_get_infile (pipeline *p)
1238 {
1239 assert (p->pids); /* pipeline started */
1240 assert (p->statuses);
1241 if (p->infile)
1242 return p->infile;
1243 else if (p->infd == -1) {
1244 error (0, 0, "pipeline input not open");
1245 return NULL;
1246 } else
1247 return p->infile = fdopen (p->infd, "w");
1248 }
1249
1250 FILE *pipeline_get_outfile (pipeline *p)
1251 {
1252 assert (p->pids); /* pipeline started */
1253 assert (p->statuses);
1254 if (p->outfile)
1255 return p->outfile;
1256 else if (p->outfd == -1) {
1257 error (0, 0, "pipeline output not open");
1258 return NULL;
1259 } else
1260 return p->outfile = fdopen (p->outfd, "r");
1261 }
1262
1263 void pipeline_dump (pipeline *p, FILE *stream)
1264 {
1265 int i;
1266
1267 for (i = 0; i < p->ncommands; ++i) {
1268 pipecmd_dump (p->commands[i], stream);
1269 if (i < p->ncommands - 1)
1270 fputs (" | ", stream);
1271 }
1272 /* clang-format off */
1273 fprintf (stream, " [input: {%d, %s}, output: {%d, %s}]\n",
1274 p->want_in, p->want_infile ? p->want_infile : "NULL",
1275 p->want_out, p->want_outfile ? p->want_outfile : "NULL");
1276 /* clang-format on */
1277 }
1278
1279 char *pipeline_tostring (pipeline *p)
1280 {
1281 char *out = NULL;
1282 int i;
1283
1284 for (i = 0; i < p->ncommands; ++i) {
1285 char *cmdout = pipecmd_tostring (p->commands[i]);
1286 out = appendstr (out, cmdout, nullptr);
1287 free (cmdout);
1288 if (i < p->ncommands - 1)
1289 out = appendstr (out, " | ", nullptr);
1290 }
1291
1292 return out;
1293 }
1294
1295 void pipeline_free (pipeline *p)
1296 {
1297 int i;
1298
1299 if (!p)
1300 return;
1301 if (p->pids)
1302 pipeline_wait (p);
1303
1304 for (i = 0; i < p->ncommands; ++i)
1305 pipecmd_free (p->commands[i]);
1306 free (p->commands);
1307 free (p->pids);
1308 free (p->statuses);
1309 free (p->want_infile);
1310 free (p->want_outfile);
1311 free (p->buffer);
1312 free (p->line_cache);
1313 free (p);
1314 }
1315
1316 /* ---------------------------------------------------------------------- */
1317
1318 /* Functions to run pipelines and handle signals. */
1319
1320 static pipeline **active_pipelines = NULL;
1321 static int n_active_pipelines = 0, max_active_pipelines = 0;
1322
1323 static int sigchld = 0;
1324 static int queue_sigchld = 0;
1325
1326 static int reap_children (int block)
1327 {
1328 pid_t pid;
1329 int status;
1330 int collected = 0;
1331
1332 do {
1333 int i;
1334
1335 if (sigchld) {
1336 /* Deal with a SIGCHLD delivery. */
1337 pid = waitpid (-1, &status, WNOHANG);
1338 --sigchld;
1339 } else
1340 pid = waitpid (-1, &status, block ? 0 : WNOHANG);
1341
1342 if (pid < 0 && errno == EINTR) {
1343 /* Try again. */
1344 pid = 0;
1345 continue;
1346 }
1347
1348 if (pid <= 0)
1349 /* We've run out of children to reap. */
1350 break;
1351
1352 ++collected;
1353
1354 /* Deliver the command status if possible. */
1355 for (i = 0; i < n_active_pipelines; ++i) {
1356 pipeline *p = active_pipelines[i];
1357 int j;
1358
1359 if (!p || !p->pids || !p->statuses)
1360 continue;
1361
1362 for (j = 0; j < p->ncommands; ++j) {
1363 if (p->pids[j] == pid) {
1364 p->statuses[j] = status;
1365 i = n_active_pipelines;
1366 break;
1367 }
1368 }
1369 }
1370 } while ((sigchld || block == 0) && pid >= 0);
1371
1372 if (collected)
1373 return collected;
1374 else
1375 return -1;
1376 }
1377
1378 static void pipeline_sigchld (int signum)
1379 {
1380 /* really an assert, but that's not async-signal-safe */
1381 if (signum == SIGCHLD) {
1382 ++sigchld;
1383
1384 if (!queue_sigchld) {
1385 int save_errno = errno;
1386 reap_children (0);
1387 errno = save_errno;
1388 }
1389 }
1390 }
1391
1392 static void pipeline_install_sigchld (void)
1393 {
1394 struct sigaction act;
1395 static int installed = 0;
1396
1397 if (installed)
1398 return;
1399
1400 memset (&act, 0, sizeof act);
1401 act.sa_handler = &pipeline_sigchld;
1402 sigemptyset (&act.sa_mask);
1403 sigaddset (&act.sa_mask, SIGINT);
1404 sigaddset (&act.sa_mask, SIGTERM);
1405 sigaddset (&act.sa_mask, SIGHUP);
1406 sigaddset (&act.sa_mask, SIGCHLD);
1407 act.sa_flags = 0;
1408 #ifdef SA_NOCLDSTOP
1409 act.sa_flags |= SA_NOCLDSTOP;
1410 #endif
1411 #ifdef SA_RESTART
1412 act.sa_flags |= SA_RESTART;
1413 #endif
1414 if (sigaction (SIGCHLD, &act, NULL) == -1)
1415 fatal (errno, "can't install SIGCHLD handler");
1416
1417 installed = 1;
1418 }
1419
1420 static pipeline_post_fork_fn *post_fork = NULL;
1421
1422 void pipeline_install_post_fork (pipeline_post_fork_fn *fn)
1423 {
1424 post_fork = fn;
1425 }
1426
1427 static int ignored_signals = 0;
1428 static struct sigaction osa_sigint, osa_sigquit;
1429
1430 void pipeline_start (pipeline *p)
1431 {
1432 int i, j;
1433 int last_input = -1;
1434 int infd[2];
1435 sigset_t set, oset;
1436
1437 /* Make sure our SIGCHLD handler is installed. */
1438 pipeline_install_sigchld ();
1439
1440 assert (!p->pids); /* pipeline not started already */
1441 assert (!p->statuses);
1442
1443 init_debug ();
1444 if (debug_level) {
1445 debug ("Starting pipeline: ");
1446 pipeline_dump (p, stderr);
1447 }
1448
1449 /* Flush all pending output so that subprocesses don't inherit it. */
1450 fflush (NULL);
1451
1452 if (p->ignore_signals && !ignored_signals++) {
1453 struct sigaction sa;
1454
1455 /* Ignore SIGINT and SIGQUIT while subprocesses are running,
1456 * just like system().
1457 */
1458 memset (&sa, 0, sizeof sa);
1459 sa.sa_handler = SIG_IGN;
1460 sigemptyset (&sa.sa_mask);
1461 sa.sa_flags = 0;
1462 if (sigaction (SIGINT, &sa, &osa_sigint) < 0)
1463 fatal (errno, "Couldn't ignore SIGINT");
1464 if (sigaction (SIGQUIT, &sa, &osa_sigquit) < 0)
1465 fatal (errno, "Couldn't ignore SIGQUIT");
1466 }
1467
1468 /* Add to the table of active pipelines, so that signal handlers
1469 * know what to do with exit statuses. Block SIGCHLD so that we can
1470 * do this safely.
1471 */
1472 sigemptyset (&set);
1473 sigaddset (&set, SIGCHLD);
1474 sigemptyset (&oset);
1475 while (sigprocmask (SIG_BLOCK, &set, &oset) == -1 && errno == EINTR)
1476 ;
1477
1478 /* Grow the table if necessary. */
1479 if (n_active_pipelines >= max_active_pipelines) {
1480 int filled = max_active_pipelines;
1481 if (max_active_pipelines)
1482 max_active_pipelines *= 2;
1483 else
1484 max_active_pipelines = 4;
1485 /* reduces to xmalloc (...) if active_pipelines == NULL */
1486 active_pipelines = xrealloc (active_pipelines,
1487 max_active_pipelines *
1488 sizeof *active_pipelines);
1489 memset (active_pipelines + filled, 0,
1490 (max_active_pipelines - filled) *
1491 sizeof *active_pipelines);
1492 }
1493
1494 for (i = 0; i < max_active_pipelines; ++i)
1495 if (!active_pipelines[i]) {
1496 active_pipelines[i] = p;
1497 break;
1498 }
1499 assert (i < max_active_pipelines);
1500 ++n_active_pipelines;
1501
1502 p->pids = xcalloc (p->ncommands, sizeof *p->pids);
1503 p->statuses = xcalloc (p->ncommands, sizeof *p->statuses);
1504
1505 /* Unblock SIGCHLD. */
1506 while (sigprocmask (SIG_SETMASK, &oset, NULL) == -1 && errno == EINTR)
1507 ;
1508
1509 if (p->redirect_in == REDIRECT_FD && p->want_in < 0) {
1510 if (pipe (infd) < 0)
1511 fatal (errno, "pipe failed");
1512 last_input = infd[0];
1513 p->infd = infd[1];
1514 } else if (p->redirect_in == REDIRECT_FD)
1515 last_input = p->want_in;
1516 else if (p->redirect_in == REDIRECT_FILE_NAME) {
1517 assert (p->want_infile);
1518 last_input = open (p->want_infile, O_RDONLY);
1519 if (last_input < 0)
1520 fatal (errno, "can't open %s", p->want_infile);
1521 }
1522
1523 for (i = 0; i < p->ncommands; i++) {
1524 int pdes[2];
1525 pid_t pid;
1526 int output_read = -1, output_write = -1;
1527
1528 if (i != p->ncommands - 1 ||
1529 (p->redirect_out == REDIRECT_FD && p->want_out < 0)) {
1530 if (pipe (pdes) < 0)
1531 fatal (errno, "pipe failed");
1532 if (i == p->ncommands - 1)
1533 p->outfd = pdes[0];
1534 output_read = pdes[0];
1535 output_write = pdes[1];
1536 } else {
1537 if (p->redirect_out == REDIRECT_FD)
1538 output_write = p->want_out;
1539 else if (p->redirect_out == REDIRECT_FILE_NAME) {
1540 assert (p->want_outfile);
1541 output_write = open (
1542 p->want_outfile,
1543 O_WRONLY | O_CREAT | O_TRUNC, 0666);
1544 if (output_write < 0)
1545 fatal (errno, "can't open %s",
1546 p->want_outfile);
1547 }
1548 }
1549
1550 /* Block SIGCHLD so that the signal handler doesn't collect
1551 * the exit status before we've filled in the pids array.
1552 */
1553 sigemptyset (&set);
1554 sigaddset (&set, SIGCHLD);
1555 sigemptyset (&oset);
1556 while (sigprocmask (SIG_BLOCK, &set, &oset) == -1 &&
1557 errno == EINTR)
1558 ;
1559
1560 pid = fork ();
1561 if (pid < 0)
1562 fatal (errno, "fork failed");
1563 if (pid == 0) {
1564 /* child */
1565 if (post_fork)
1566 post_fork ();
1567
1568 /* input, reading side */
1569 if (last_input != -1) {
1570 if (dup2 (last_input, 0) < 0)
1571 fatal (errno, "dup2 failed");
1572 if (close (last_input) < 0)
1573 fatal (errno, "close failed");
1574 }
1575
1576 /* output, writing side */
1577 if (output_write != -1) {
1578 if (dup2 (output_write, 1) < 0)
1579 fatal (errno, "dup2 failed");
1580 if (close (output_write) < 0)
1581 fatal (errno, "close failed");
1582 }
1583
1584 /* output, reading side */
1585 if (output_read != -1)
1586 if (close (output_read))
1587 fatal (errno, "close failed");
1588
1589 /* input from first command, writing side; must close
1590 * it in every child because it has to be created
1591 * before forking anything
1592 */
1593 if (p->infd != -1)
1594 if (close (p->infd))
1595 fatal (errno, "close failed");
1596
1597 /* inputs and outputs from other active pipelines */
1598 for (j = 0; j < n_active_pipelines; ++j) {
1599 pipeline *active = active_pipelines[j];
1600 if (!active || active == p)
1601 continue;
1602 /* ignore failures */
1603 if (active->infd != -1)
1604 close (active->infd);
1605 if (active->outfd != -1)
1606 close (active->outfd);
1607 }
1608
1609 /* Restore signals. */
1610 if (p->ignore_signals) {
1611 sigaction (SIGINT, &osa_sigint, NULL);
1612 sigaction (SIGQUIT, &osa_sigquit, NULL);
1613 }
1614
1615 pipecmd_exec (p->commands[i]);
1616 /* never returns */
1617 }
1618
1619 /* in the parent */
1620 if (last_input != -1) {
1621 if (close (last_input) < 0)
1622 fatal (errno, "close failed");
1623 }
1624 if (output_write != -1) {
1625 if (close (output_write) < 0)
1626 fatal (errno, "close failed");
1627 }
1628 if (output_read != -1)
1629 last_input = output_read;
1630 p->pids[i] = pid;
1631 p->statuses[i] = -1;
1632
1633 /* Unblock SIGCHLD. */
1634 while (sigprocmask (SIG_SETMASK, &oset, NULL) == -1 &&
1635 errno == EINTR)
1636 ;
1637
1638 debug ("Started \"%s\", pid %d\n", p->commands[i]->name, pid);
1639 }
1640
1641 if (p->ncommands == 0)
1642 p->outfd = last_input;
1643 }
1644
1645 int pipeline_wait_all (pipeline *p, int **statuses, int *n_statuses)
1646 {
1647 int ret = 0;
1648 int proc_count = p->ncommands;
1649 int i;
1650 int raise_signal = 0;
1651
1652 init_debug ();
1653 if (debug_level) {
1654 debug ("Waiting for pipeline: ");
1655 pipeline_dump (p, stderr);
1656 }
1657
1658 assert (p->pids); /* pipeline started */
1659 assert (p->statuses);
1660
1661 if (p->infile) {
1662 if (fclose (p->infile))
1663 error (0, errno,
1664 "closing pipeline input stream failed");
1665 p->infile = NULL;
1666 p->infd = -1;
1667 } else if (p->infd != -1) {
1668 if (close (p->infd))
1669 error (0, errno, "closing pipeline input failed");
1670 p->infd = -1;
1671 }
1672
1673 if (p->outfile) {
1674 if (fclose (p->outfile)) {
1675 error (0, errno,
1676 "closing pipeline output stream failed");
1677 ret = 127;
1678 }
1679 p->outfile = NULL;
1680 p->outfd = -1;
1681 } else if (p->outfd != -1) {
1682 if (close (p->outfd)) {
1683 error (0, errno, "closing pipeline output failed");
1684 ret = 127;
1685 }
1686 p->outfd = -1;
1687 }
1688
1689 /* Tell the SIGCHLD handler not to get in our way. */
1690 queue_sigchld = 1;
1691
1692 while (proc_count > 0) {
1693 int r;
1694
1695 debug ("Active processes (%d):\n", proc_count);
1696
1697 /* Check for any statuses already collected by SIGCHLD
1698 * handlers or the previous iteration before calling
1699 * reap_children() again.
1700 */
1701 for (i = 0; i < p->ncommands; ++i) {
1702 int status;
1703
1704 if (p->pids[i] == -1)
1705 continue;
1706
1707 debug (" \"%s\" (%d) -> %d\n", p->commands[i]->name,
1708 p->pids[i], p->statuses[i]);
1709
1710 if (p->statuses[i] == -1)
1711 continue;
1712
1713 status = p->statuses[i];
1714 p->pids[i] = -1;
1715 --proc_count;
1716 if (WIFSIGNALED (status)) {
1717 int sig = WTERMSIG (status);
1718 if (IS_SIGPIPE (sig))
1719 status = 0;
1720 else if (sig == SIGINT || sig == SIGQUIT)
1721 /* signals currently blocked,
1722 * re-raise later
1723 */
1724 raise_signal = sig;
1725 else if (getenv ("PIPELINE_QUIET"))
1726 ;
1727 else if (WCOREDUMP (status))
1728 error (0, 0, "%s: %s (core dumped)",
1729 p->commands[i]->name,
1730 strsignal (sig));
1731 else
1732 error (0, 0, "%s: %s",
1733 p->commands[i]->name,
1734 strsignal (sig));
1735 } else if (!WIFEXITED (status))
1736 error (0, 0, "unexpected status %d", status);
1737
1738 if (p->commands[i]->tag == PIPECMD_FUNCTION) {
1739 struct pipecmd_function *cmdf =
1740 &p->commands[i]->u.function;
1741 if (cmdf->free_func)
1742 (*cmdf->free_func) (cmdf->data);
1743 }
1744
1745 if (i == p->ncommands - 1) {
1746 if (WIFSIGNALED (status))
1747 ret = 128 + WTERMSIG (status);
1748 else if (WEXITSTATUS (status))
1749 ret = WEXITSTATUS (status);
1750 } else if (!ret && (WIFSIGNALED (status) ||
1751 WEXITSTATUS (status)))
1752 ret = 127;
1753 }
1754
1755 assert (proc_count >= 0);
1756 if (proc_count == 0)
1757 break;
1758
1759 errno = 0;
1760 r = reap_children (1);
1761
1762 if (r == -1 && errno == ECHILD)
1763 /* Eh? The pipeline was allegedly still running, so
1764 * we shouldn't have got ECHILD.
1765 */
1766 fatal (errno, "waitpid failed");
1767 }
1768
1769 queue_sigchld = 0;
1770
1771 for (i = 0; i < n_active_pipelines; ++i)
1772 if (active_pipelines[i] == p)
1773 active_pipelines[i] = NULL;
1774
1775 /* It isn't normally safe to compactify active_pipelines as we go,
1776 * because it's used by a signal handler. However, if it's entirely
1777 * empty, we can safely clean it up now. This prevents the table
1778 * growing without bound, not to mention pacifying valgrind.
1779 */
1780 for (i = 0; i < n_active_pipelines; ++i)
1781 if (active_pipelines[i])
1782 break;
1783 if (i == n_active_pipelines) {
1784 n_active_pipelines = 0;
1785 max_active_pipelines = 0;
1786 free (active_pipelines);
1787 active_pipelines = NULL;
1788 }
1789
1790 if (statuses && n_statuses) {
1791 *statuses = xnmalloc (p->ncommands, sizeof **statuses);
1792 *n_statuses = p->ncommands;
1793 for (i = 0; i < p->ncommands; ++i)
1794 (*statuses)[i] = p->statuses[i];
1795 }
1796
1797 free (p->pids);
1798 p->pids = NULL;
1799 free (p->statuses);
1800 p->statuses = NULL;
1801
1802 if (p->ignore_signals && !--ignored_signals) {
1803 /* Restore signals. */
1804 sigaction (SIGINT, &osa_sigint, NULL);
1805 sigaction (SIGQUIT, &osa_sigquit, NULL);
1806 }
1807
1808 if (raise_signal)
1809 raise (raise_signal);
1810
1811 return ret;
1812 }
1813
1814 int pipeline_wait (pipeline *p)
1815 {
1816 return pipeline_wait_all (p, NULL, NULL);
1817 }
1818
1819 int pipeline_run (pipeline *p)
1820 {
1821 int status;
1822
1823 pipeline_start (p);
1824 status = pipeline_wait (p);
1825 pipeline_free (p);
1826
1827 return status;
1828 }
1829
1830 void pipeline_pump (pipeline *p, ...)
1831 {
1832 va_list argv;
1833 int argc, i, j;
1834 pipeline *arg, **pieces;
1835 size_t *pos;
1836 int *known_source, *blocking_in, *blocking_out, *dying_source,
1837 *waiting, *write_error;
1838 struct sigaction sa, osa_sigpipe;
1839
1840 /* Count pipelines and allocate space for arrays. */
1841 va_start (argv, p);
1842 argc = 0;
1843 for (arg = p; arg; arg = va_arg (argv, pipeline *))
1844 ++argc;
1845 va_end (argv);
1846 pieces = xnmalloc (argc, sizeof *pieces);
1847 pos = xnmalloc (argc, sizeof *pos);
1848 known_source = xcalloc (argc, sizeof *known_source);
1849 blocking_in = xcalloc (argc, sizeof *blocking_in);
1850 blocking_out = xcalloc (argc, sizeof *blocking_out);
1851 dying_source = xcalloc (argc, sizeof *dying_source);
1852 waiting = xcalloc (argc, sizeof *waiting);
1853 write_error = xcalloc (argc, sizeof *write_error);
1854
1855 /* Set up arrays of pipelines and their read positions. Start all
1856 * pipelines if necessary.
1857 */
1858 va_start (argv, p);
1859 for (arg = p, i = 0; i < argc; arg = va_arg (argv, pipeline *), ++i) {
1860 pieces[i] = arg;
1861 pos[i] = 0;
1862 if (!pieces[i]->pids)
1863 pipeline_start (pieces[i]);
1864 }
1865 assert (arg == NULL);
1866 va_end (argv);
1867
1868 /* All source pipelines must be supplied as arguments. */
1869 for (i = 0; i < argc; ++i) {
1870 int found = 0;
1871 if (!pieces[i]->source)
1872 continue;
1873 for (j = 0; j < argc; ++j) {
1874 if (pieces[i]->source == pieces[j]) {
1875 known_source[j] = found = 1;
1876 break;
1877 }
1878 }
1879 assert (found);
1880 }
1881
1882 for (i = 0; i < argc; ++i) {
1883 int flags;
1884 if (pieces[i]->infd != -1) {
1885 flags = fcntl (pieces[i]->infd, F_GETFL);
1886 if (!(flags & O_NONBLOCK)) {
1887 blocking_in[i] = 1;
1888 fcntl (pieces[i]->infd, F_SETFL,
1889 flags | O_NONBLOCK);
1890 }
1891 }
1892 if (pieces[i]->outfd != -1) {
1893 flags = fcntl (pieces[i]->outfd, F_GETFL);
1894 if (!(flags & O_NONBLOCK)) {
1895 blocking_out[i] = 1;
1896 fcntl (pieces[i]->outfd, F_SETFL,
1897 flags | O_NONBLOCK);
1898 }
1899 }
1900 }
1901
1902 #ifdef SIGPIPE
1903 memset (&sa, 0, sizeof sa);
1904 sa.sa_handler = SIG_IGN;
1905 sigemptyset (&sa.sa_mask);
1906 sa.sa_flags = 0;
1907 sigaction (SIGPIPE, &sa, &osa_sigpipe);
1908 #endif
1909
1910 #ifdef SA_RESTART
1911 /* We rely on getting EINTR from select. */
1912 sigaction (SIGCHLD, NULL, &sa);
1913 sa.sa_flags &= ~SA_RESTART;
1914 sigaction (SIGCHLD, &sa, NULL);
1915 #endif
1916
1917 for (;;) {
1918 fd_set rfds, wfds;
1919 int maxfd = -1;
1920 int ret;
1921
1922 /* If a source dies and all data from it has been written to
1923 * all sinks, close the writing end of the pipe to each of
1924 * its sinks.
1925 */
1926 for (i = 0; i < argc; ++i) {
1927 if (!known_source[i] || pieces[i]->outfd != -1 ||
1928 pipeline_peek_size (pieces[i]))
1929 continue;
1930 for (j = 0; j < argc; ++j) {
1931 if (pieces[j]->source == pieces[i] &&
1932 pieces[j]->infd != -1) {
1933 if (close (pieces[j]->infd))
1934 error (0, errno,
1935 "closing pipeline "
1936 "input failed");
1937 pieces[j]->infd = -1;
1938 }
1939 }
1940 }
1941
1942 /* If all sinks on a source have died, close the reading end
1943 * of the pipe from that source.
1944 */
1945 for (i = 0; i < argc; ++i) {
1946 int got_sink = 0;
1947 if (!known_source[i] || pieces[i]->outfd == -1)
1948 continue;
1949 for (j = 0; j < argc; ++j) {
1950 if (pieces[j]->source == pieces[i] &&
1951 pieces[j]->infd != -1) {
1952 got_sink = 1;
1953 break;
1954 }
1955 }
1956 if (got_sink)
1957 continue;
1958 if (close (pieces[i]->outfd))
1959 error (0, errno,
1960 "closing pipeline output failed");
1961 pieces[i]->outfd = -1;
1962 }
1963
1964 FD_ZERO (&rfds);
1965 FD_ZERO (&wfds);
1966 for (i = 0; i < argc; ++i) {
1967 /* Input to sink pipeline. */
1968 if (pieces[i]->source && pieces[i]->infd != -1 &&
1969 !waiting[i]) {
1970 FD_SET (pieces[i]->infd, &wfds);
1971 if (pieces[i]->infd > maxfd)
1972 maxfd = pieces[i]->infd;
1973 }
1974 /* Output from source pipeline. */
1975 if (known_source[i] && pieces[i]->outfd != -1) {
1976 FD_SET (pieces[i]->outfd, &rfds);
1977 if (pieces[i]->outfd > maxfd)
1978 maxfd = pieces[i]->outfd;
1979 }
1980 }
1981 if (maxfd == -1)
1982 break; /* nothing meaningful left to do */
1983
1984 ret = select (maxfd + 1, &rfds, &wfds, NULL, NULL);
1985 if (ret < 0 && errno == EINTR) {
1986 /* Did a source or sink pipeline die? */
1987 for (i = 0; i < argc; ++i) {
1988 if (pieces[i]->ncommands == 0)
1989 continue;
1990 if (known_source[i] && !dying_source[i] &&
1991 pieces[i]->outfd != -1) {
1992 int last = pieces[i]->ncommands - 1;
1993 assert (pieces[i]->statuses);
1994 if (pieces[i]->statuses[last] != -1) {
1995 debug ("source pipeline %d "
1996 "died\n",
1997 i);
1998 dying_source[i] = 1;
1999 }
2000 }
2001 if (pieces[i]->source &&
2002 pieces[i]->infd != -1) {
2003 assert (pieces[i]->statuses);
2004 if (pieces[i]->statuses[0] != -1) {
2005 debug ("sink pipeline %d "
2006 "died\n",
2007 i);
2008 close (pieces[i]->infd);
2009 pieces[i]->infd = -1;
2010 }
2011 }
2012 }
2013 continue;
2014 } else if (ret < 0)
2015 fatal (errno, "select");
2016
2017 /* Read a block of data from each available source pipeline. */
2018 for (i = 0; i < argc; ++i) {
2019 size_t peek_size, len;
2020
2021 if (!known_source[i] || pieces[i]->outfd == -1)
2022 continue;
2023 if (!FD_ISSET (pieces[i]->outfd, &rfds))
2024 continue;
2025
2026 peek_size = pipeline_peek_size (pieces[i]);
2027 len = peek_size + 4096;
2028 if (!pipeline_peek (pieces[i], &len) ||
2029 len == peek_size) {
2030 /* Error or end-of-file; skip this pipeline
2031 * from now on.
2032 */
2033 debug ("source pipeline %d returned error "
2034 "or EOF\n",
2035 i);
2036 close (pieces[i]->outfd);
2037 pieces[i]->outfd = -1;
2038 } else
2039 /* This is rather a large hammer. Whenever
2040 * any data is read from any source
2041 * pipeline, we go through and retry all
2042 * sink pipelines, even if they aren't
2043 * receiving data from the source in
2044 * question. This probably results in a few
2045 * more passes around the select() loop, but
2046 * it eliminates some annoyingly fiddly
2047 * bookkeeping.
2048 */
2049 memset (waiting, 0, argc * sizeof *waiting);
2050 }
2051
2052 /* Write as much data as we can to each available sink
2053 * pipeline.
2054 */
2055 for (i = 0; i < argc; ++i) {
2056 const char *block;
2057 size_t peek_size;
2058 size_t w;
2059 size_t minpos;
2060
2061 if (!pieces[i]->source || pieces[i]->infd == -1)
2062 continue;
2063 if (!FD_ISSET (pieces[i]->infd, &wfds))
2064 continue;
2065 peek_size = pipeline_peek_size (pieces[i]->source);
2066 if (peek_size <= pos[i]) {
2067 /* Disable reading until data is read from a
2068 * source fd or a child process exits, so
2069 * that we neither spin nor block if the
2070 * source is slow.
2071 */
2072 waiting[i] = 1;
2073 continue;
2074 }
2075
2076 /* peek a block from the source */
2077 block = pipeline_peek (pieces[i]->source, &peek_size);
2078 /* should all already be in the peek cache */
2079 assert (block);
2080 assert (peek_size);
2081
2082 /* write as much of it as will fit to the sink */
2083 for (;;) {
2084 w = safe_write (pieces[i]->infd,
2085 block + pos[i],
2086 peek_size - pos[i]);
2087 if (w != SAFE_WRITE_ERROR)
2088 break;
2089 if (errno == EAGAIN) {
2090 w = 0;
2091 break;
2092 }
2093 /* It may be useful for other processes to
2094 * continue even though this one fails, so
2095 * don't FATAL yet.
2096 */
2097 if (errno != EPIPE)
2098 write_error[i] = errno;
2099 close (pieces[i]->infd);
2100 pieces[i]->infd = -1;
2101 goto next_sink;
2102 }
2103 pos[i] += w;
2104 minpos = pos[i];
2105
2106 /* check other sinks on the same source, and update
2107 * the source's read position if earlier data is no
2108 * longer needed by any sink
2109 */
2110 for (j = 0; j < argc; ++j) {
2111 if (pieces[i]->source != pieces[j]->source ||
2112 pieces[j]->infd == -1)
2113 continue;
2114 if (pos[j] < minpos)
2115 minpos = pos[j];
2116 /* If the source is dead and all data has
2117 * been written to this sink, close the
2118 * writing end of the pipe to the sink.
2119 */
2120 if (pieces[j]->source->outfd == -1 &&
2121 pos[j] >= peek_size) {
2122 close (pieces[j]->infd);
2123 pieces[j]->infd = -1;
2124 }
2125 }
2126
2127 /* If some data has been written to all sinks,
2128 * discard it from the source's peek cache.
2129 */
2130 pipeline_peek_skip (pieces[i]->source, minpos);
2131 for (j = 0; j < argc; ++j) {
2132 if (pieces[i]->source == pieces[j]->source)
2133 pos[j] -= minpos;
2134 }
2135 next_sink:;
2136 }
2137 }
2138
2139 #ifdef SA_RESTART
2140 sigaction (SIGCHLD, NULL, &sa);
2141 sa.sa_flags |= SA_RESTART;
2142 sigaction (SIGCHLD, &sa, NULL);
2143 #endif
2144
2145 #ifdef SIGPIPE
2146 sigaction (SIGPIPE, &osa_sigpipe, NULL);
2147 #endif
2148
2149 for (i = 0; i < argc; ++i) {
2150 int flags;
2151 if (blocking_in[i] && pieces[i]->infd != -1) {
2152 flags = fcntl (pieces[i]->infd, F_GETFL);
2153 fcntl (pieces[i]->infd, F_SETFL, flags & ~O_NONBLOCK);
2154 }
2155 if (blocking_out[i] && pieces[i]->outfd != -1) {
2156 flags = fcntl (pieces[i]->outfd, F_GETFL);
2157 fcntl (pieces[i]->outfd, F_SETFL, flags & ~O_NONBLOCK);
2158 }
2159 }
2160
2161 for (i = 0; i < argc; ++i) {
2162 if (write_error[i])
2163 fatal (write_error[i], "write to sink %d", i);
2164 }
2165
2166 free (write_error);
2167 free (waiting);
2168 free (dying_source);
2169 free (blocking_out);
2170 free (blocking_in);
2171 free (known_source);
2172 free (pieces);
2173 free (pos);
2174 }
2175
2176 /* ---------------------------------------------------------------------- */
2177
2178 /* Functions to read output from pipelines. */
2179
2180 static const char *get_block (pipeline *p, size_t *len, int peek)
2181 {
2182 size_t readstart = 0, retstart = 0;
2183 size_t space = p->bufmax;
2184 size_t toread = *len;
2185 size_t r;
2186
2187 if (p->buffer && p->peek_offset) {
2188 if (p->peek_offset >= toread) {
2189 /* We've got the whole thing in the peek cache; just
2190 * return it.
2191 */
2192 const char *buffer;
2193 assert (p->peek_offset <= p->buflen);
2194 buffer = p->buffer + p->buflen - p->peek_offset;
2195 if (!peek)
2196 p->peek_offset -= toread;
2197 return buffer;
2198 } else {
2199 readstart = p->buflen;
2200 retstart = p->buflen - p->peek_offset;
2201 space -= p->buflen;
2202 toread -= p->peek_offset;
2203 }
2204 }
2205
2206 if (toread > space) {
2207 if (p->buffer)
2208 p->bufmax = readstart + toread;
2209 else
2210 p->bufmax = toread;
2211 p->buffer = xrealloc (p->buffer, p->bufmax + 1);
2212 }
2213
2214 if (!peek)
2215 p->peek_offset = 0;
2216
2217 assert (p->outfd != -1);
2218 r = safe_read (p->outfd, p->buffer + readstart, toread);
2219 if (r == SAFE_READ_ERROR)
2220 return NULL;
2221 p->buflen = readstart + r;
2222 if (peek)
2223 p->peek_offset += r;
2224 *len -= (toread - r);
2225
2226 return p->buffer + retstart;
2227 }
2228
2229 const char *pipeline_read (pipeline *p, size_t *len)
2230 {
2231 return get_block (p, len, 0);
2232 }
2233
2234 const char *pipeline_peek (pipeline *p, size_t *len)
2235 {
2236 return get_block (p, len, 1);
2237 }
2238
2239 size_t ATTRIBUTE_PURE pipeline_peek_size (pipeline *p)
2240 {
2241 if (!p->buffer)
2242 return 0;
2243 return p->peek_offset;
2244 }
2245
2246 void pipeline_peek_skip (pipeline *p, size_t len)
2247 {
2248 if (len > 0) {
2249 assert (p->buffer);
2250 assert (len <= p->peek_offset);
2251 p->peek_offset -= len;
2252 }
2253 }
2254
2255 /* readline and peekline repeatedly peek larger and larger buffers until
2256 * they find a newline or they fail. readline then adjusts the peek offset.
2257 */
2258
2259 static const char *get_line (pipeline *p, size_t *outlen)
2260 {
2261 const size_t block = 4096;
2262 const char *buffer = NULL, *end = NULL;
2263 int i;
2264 size_t previous_len = 0;
2265
2266 if (p->line_cache) {
2267 free (p->line_cache);
2268 p->line_cache = NULL;
2269 }
2270
2271 if (outlen)
2272 *outlen = 0;
2273
2274 for (i = 0;; ++i) {
2275 size_t len = block * (i + 1);
2276
2277 buffer = get_block (p, &len, 1);
2278 if (!buffer || len == 0)
2279 return NULL;
2280
2281 if (len == previous_len)
2282 /* end of file, no newline found */
2283 end = buffer + len - 1;
2284 else
2285 end = memchr (buffer + previous_len, '\n',
2286 len - previous_len);
2287 if (end)
2288 break;
2289 previous_len = len;
2290 }
2291
2292 if (end) {
2293 p->line_cache = xstrndup (buffer, end - buffer + 1);
2294 if (outlen)
2295 *outlen = end - buffer + 1;
2296 return p->line_cache;
2297 } else
2298 return NULL;
2299 }
2300
2301 const char *pipeline_readline (pipeline *p)
2302 {
2303 size_t buflen;
2304 const char *buffer = get_line (p, &buflen);
2305 if (buffer)
2306 p->peek_offset -= buflen;
2307 return buffer;
2308 }
2309
2310 const char *pipeline_peekline (pipeline *p)
2311 {
2312 return get_line (p, NULL);
2313 }