1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Handling of pipes, child processes, temporary files, file enwrapping.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 #ifndef HAVE_AMALGAMATION
52 struct termios
*fp_tios
;
65 /* TODO FP_UNLINK: should be in a separated process so that unlinking
66 * TODO the temporary "garbage" is "safe"(r than it is like that) */
80 static struct fp
*fp_head
;
81 static struct child
*_popen_child
;
83 /* TODO Rather temporary: deal with job control with FD_PASS */
84 static struct termios a_popen_tios
;
85 static sighandler_type a_popen_otstp
, a_popen_ottin
, a_popen_ottou
;
86 static volatile int a_popen_hadsig
;
88 static int scan_mode(char const *mode
, int *omode
);
89 static void register_file(FILE *fp
, int omode
, int pid
,
90 int flags
, char const *realfile
, long offset
,
91 char const *save_cmd
, struct termios
*tiosp
);
92 static enum okay
_file_save(struct fp
*fpp
);
93 static int _file_load(int flags
, int infd
, int outfd
,
94 char const *load_cmd
);
95 static enum okay
unregister_file(FILE *fp
, struct termios
**tiosp
);
96 static int file_pid(FILE *fp
);
98 /* TODO Rather temporary: deal with job control with FD_PASS */
99 static void a_popen_jobsigs_up(void);
100 static void a_popen_jobsigs_down(void);
101 static void a_popen_jobsig(int sig
);
104 static void _sigchld(int signo
);
106 static struct child
*_findchild(int pid
, bool_t create
);
107 static void _delchild(struct child
*cp
);
110 scan_mode(char const *mode
, int *omode
)
117 {"w", O_WRONLY
| O_CREAT
| O_TRUNC
},
118 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
119 {"a", O_WRONLY
| O_APPEND
| O_CREAT
},
120 {"a+", O_RDWR
| O_APPEND
},
122 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
128 for (i
= 0; UICMP(z
, i
, <, NELEM(maps
)); ++i
)
129 if (!strcmp(maps
[i
].mode
, mode
)) {
130 *omode
= maps
[i
].omode
;
135 n_alert(_("Internal error: bad stdio open mode %s"), mode
);
137 *omode
= 0; /* (silence CC) */
145 register_file(FILE *fp
, int omode
, int pid
, int flags
,
146 char const *realfile
, long offset
, char const *save_cmd
,
147 struct termios
*tiosp
)
152 assert(!(flags
& FP_UNLINK
) || realfile
!= NULL
);
153 assert(!(flags
& FP_TERMIOS
) || tiosp
!= NULL
);
155 fpp
= smalloc(sizeof *fpp
);
161 fpp
->realfile
= (realfile
!= NULL
) ? sstrdup(realfile
) : NULL
;
162 fpp
->save_cmd
= (save_cmd
!= NULL
) ? sstrdup(save_cmd
) : NULL
;
163 fpp
->fp_tios
= tiosp
;
164 fpp
->offset
= offset
;
170 _file_save(struct fp
*fpp
)
177 if (fpp
->omode
== O_RDONLY
) {
186 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
187 if (fseek(fpp
->fp
, fpp
->offset
, SEEK_SET
) == -1) {
189 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
190 n_shell_quote_cp(fpp
->realfile
, FAL0
), strerror(outfd
));
193 rv
= maildir_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
197 /* Ensure the I/O library doesn't optimize the fseek(3) away! */
198 if(lseek(infd
= fileno(fpp
->fp
), fpp
->offset
, SEEK_SET
) == -1){
200 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
201 n_shell_quote_cp(fpp
->realfile
, FAL0
), strerror(outfd
));
205 outfd
= open(fpp
->realfile
,
206 ((fpp
->omode
| O_CREAT
| (fpp
->omode
& O_APPEND
? 0 : O_TRUNC
))
210 n_err(_("Fatal: cannot create %s: %s\n"),
211 n_shell_quote_cp(fpp
->realfile
, FAL0
), strerror(outfd
));
216 switch (fpp
->flags
& FP_MASK
) {
218 cmd
[0] = "gzip"; cmd
[1] = "-c"; break;
220 cmd
[0] = "bzip2"; cmd
[1] = "-c"; break;
222 cmd
[0] = "xz"; cmd
[1] = "-c"; break;
224 cmd
[0] = "cat"; cmd
[1] = NULL
; break;
226 cmd
[0] = ok_vlook(SHELL
);
228 cmd
[2] = fpp
->save_cmd
;
230 if (run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
) >= 0)
240 _file_load(int flags
, int infd
, int outfd
, char const *load_cmd
)
247 switch (flags
& FP_MASK
) {
248 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
249 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
250 case FP_XZ
: cmd
[0] = "xz"; cmd
[1] = "-cd"; break;
251 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
253 cmd
[0] = ok_vlook(SHELL
);
262 rv
= run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
);
269 unregister_file(FILE *fp
, struct termios
**tiosp
)
278 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
280 switch (p
->flags
& FP_MASK
) {
288 if ((p
->flags
& FP_UNLINK
) && unlink(p
->realfile
))
292 if (p
->save_cmd
!= NULL
)
294 if (p
->realfile
!= NULL
)
296 if (p
->flags
& FP_TERMIOS
) {
305 DBGOR(n_panic
, n_alert
)(_("Invalid file pointer"));
320 for (p
= fp_head
; p
; p
= p
->link
)
330 a_popen_jobsigs_up(void){
336 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
337 a_popen_otstp
= safe_signal(SIGTSTP
, &a_popen_jobsig
);
338 a_popen_ottin
= safe_signal(SIGTTIN
, &a_popen_jobsig
);
339 a_popen_ottou
= safe_signal(SIGTTOU
, &a_popen_jobsig
);
341 /* This assumes oset contains nothing but SIGCHLD, so to say */
342 sigdelset(&oset
, SIGTSTP
);
343 sigdelset(&oset
, SIGTTIN
);
344 sigdelset(&oset
, SIGTTOU
);
345 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
350 a_popen_jobsigs_down(void){
356 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
357 safe_signal(SIGTSTP
, a_popen_otstp
);
358 safe_signal(SIGTTIN
, a_popen_ottin
);
359 safe_signal(SIGTTOU
, a_popen_ottou
);
361 sigaddset(&oset
, SIGTSTP
);
362 sigaddset(&oset
, SIGTTIN
);
363 sigaddset(&oset
, SIGTTOU
);
364 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
369 a_popen_jobsig(int sig
){
370 sighandler_type oldact
;
373 NYD_X
; /* Signal handler */
375 hadsig
= (a_popen_hadsig
!= 0);
378 n_TERMCAP_SUSPEND(TRU1
);
380 oldact
= safe_signal(sig
, SIG_DFL
);
383 sigaddset(&nset
, sig
);
384 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
386 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
388 safe_signal(sig
, oldact
);
397 NYD_X
; /* Signal handler */
401 pid
= waitpid(-1, &status
, WNOHANG
);
403 if (pid
== -1 && errno
== EINTR
)
408 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
410 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
419 static struct child
*
420 _findchild(int pid
, bool_t create
)
425 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
429 if (*cpp
== NULL
&& create
) {
430 *cpp
= smalloc(sizeof **cpp
);
432 (*cpp
)->done
= (*cpp
)->free
= 0;
440 _delchild(struct child
*cp
)
452 if (*(cpp
= &(*cpp
)->link
) == NULL
) {
453 DBG( n_err("! popen.c:_delchild(): implementation error\n"); )
461 command_manager_start(void)
463 struct sigaction nact
, oact
;
466 nact
.sa_handler
= &_sigchld
;
467 sigemptyset(&nact
.sa_mask
);
468 nact
.sa_flags
= SA_RESTART
473 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
474 n_panic(_("Cannot install signal handler for child process management"));
479 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
483 NYD2_ENTER
; /* (only for Fopen() and once in lex.c) */
485 if (scan_mode(oflags
, &osflags
) < 0)
487 osflags
|= _O_CLOEXEC
;
491 if ((fd
= open(file
, osflags
, 0666)) == -1)
495 fp
= fdopen(fd
, oflags
);
502 Fopen(char const *file
, char const *oflags
)
508 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
509 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
);
515 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
521 scan_mode(oflags
, &osflags
);
523 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
525 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
526 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
);
537 if (unregister_file(fp
, NULL
) == OKAY
)
542 return (i
== 3 ? 0 : EOF
);
546 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
549 char const *cload
= NULL
, *csave
= NULL
;
550 int flags
, osflags
, mode
, infd
;
556 if (scan_mode(oflags
, &osflags
) < 0)
560 rof
= OF_RDWR
| OF_UNLINK
;
561 if (osflags
& O_APPEND
)
563 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
565 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_MAILDIR
)) {
567 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
572 if ((ext
= strrchr(file
, '.')) != NULL
) {
573 if (!asccasecmp(ext
, ".gz"))
575 else if (!asccasecmp(ext
, ".xz")) {
577 osflags
&= ~O_APPEND
;
579 } else if (!asccasecmp(ext
, ".bz2")) {
581 osflags
&= ~O_APPEND
;
585 #define _X1 "file-hook-load-"
587 #define _X2 "file-hook-save-"
588 size_t l
= strlen(++ext
);
589 char *vbuf
= ac_alloc(l
+ MAX(sizeof(_X1
), sizeof(_X2
)));
591 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
592 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
593 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
594 cload
= vok_vlook(vbuf
);
595 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
596 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
597 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
598 csave
= vok_vlook(vbuf
);
603 if ((csave
!= NULL
) && (cload
!= NULL
)) {
605 osflags
&= ~O_APPEND
;
607 } else if ((csave
!= NULL
) | (cload
!= NULL
)) {
608 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
609 "Treating as plain text!"), ext
);
617 rv
= Fopen(file
, oflags
);
621 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
622 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
626 /* Note rv is not yet register_file()d, fclose() it in error path! */
627 if ((rv
= Ftmp(NULL
, "zopen", rof
)) == NULL
) {
628 n_perr(_("tmpfile"), 0);
632 if (flags
& FP_MAILDIR
)
634 else if (infd
>= 0) {
635 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
645 if ((infd
= creat(file
, 0666)) == -1) {
656 if (!(osflags
& O_APPEND
))
658 if ((offset
= ftell(rv
)) == -1) {
664 register_file(rv
, osflags
, 0, flags
, file
, offset
, csave
, NULL
);
671 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
673 /* The 8 is arbitrary but leaves room for a six character suffix (the
674 * POSIX minimum path length is 14, though we don't check that XXX).
675 * 8 should be more than sufficient given that we use base64url encoding
676 * for our random string */
677 enum {_RANDCHARS
= 8};
679 size_t maxname
, xlen
, i
;
686 assert(namehint
!= NULL
);
687 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
688 assert(!(oflags
& OF_RDONLY
));
689 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
698 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
699 maxname
= (size_t)pc
;
703 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
704 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
705 errno
= ENAMETOOLONG
;
711 /* Prepare the template string once, then iterate over the random range */
713 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
714 cp
= sstpcpy(cp
, tempdir
);
717 char *x
= sstpcpy(cp
, UAGENT
);
719 if (!(oflags
& OF_SUFFIX
))
720 x
= sstpcpy(x
, namehint
);
722 i
= PTR2SIZE(x
- cp
);
723 if (i
> maxname
- xlen
- _RANDCHARS
) {
724 size_t j
= maxname
- xlen
- _RANDCHARS
;
729 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
730 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
732 x
[xlen
+ _RANDCHARS
] = '\0';
736 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
737 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
738 if (oflags
& OF_APPEND
)
739 osoflags
|= O_APPEND
;
742 memcpy(cp
, getrandstring(_RANDCHARS
), _RANDCHARS
);
747 if ((fd
= open(cp_base
, osoflags
, 0600)) != -1) {
751 if (i
>= FTMP_OPEN_TRIES
) {
759 if (oflags
& OF_REGISTER
) {
760 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
763 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
764 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
765 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0,
766 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
767 cp_base
, 0L, NULL
, NULL
);
769 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
771 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
782 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
789 if ((cp
= cp_base
) != NULL
)
795 Ftmp_release(char **fn
)
811 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
824 pipe_cloexec(int fd
[2])
830 if (pipe2(fd
, O_CLOEXEC
) == -1)
835 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
836 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
845 Popen(char const *cmd
, char const *mode
, char const *sh
,
846 char const **env_addon
, int newfd1
)
848 struct termios
*tiosp
;
849 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
850 char mod
[2] = {'0', '\0'};
855 /* First clean up child structures */
857 struct child
**cpp
, *cp
;
860 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
862 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
863 if ((*cpp
)->pid
== -1) {
871 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
874 if (!pipe_cloexec(p
))
879 fd0
= COMMAND_FD_PASS
;
880 hisside
= fd1
= p
[WRITE
];
882 } else if (*mode
== 'W') {
884 hisside
= fd0
= p
[READ
];
889 hisside
= fd0
= p
[READ
];
890 fd1
= COMMAND_FD_PASS
;
894 /* In interactive mode both STDIN and STDOUT point to the terminal. If we
895 * pass through the TTY restore terminal attributes after pipe returns.
896 * XXX It shouldn't matter which FD we actually use in this case */
897 if ((options
& OPT_INTERACTIVE
) && (fd0
== COMMAND_FD_PASS
||
898 fd1
== COMMAND_FD_PASS
)) {
899 tiosp
= smalloc(sizeof *tiosp
);
900 tcgetattr(STDIN_FILENO
, tiosp
);
901 n_TERMCAP_SUSPEND(TRU1
);
907 if (cmd
== (char*)-1) {
908 if ((pid
= fork_child()) == -1)
909 n_perr(_("fork"), 0);
911 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
912 prepare_child(&nset
, fd0
, fd1
);
919 } else if (sh
== NULL
) {
920 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
922 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
930 if ((rv
= fdopen(myside
, mod
)) != NULL
)
931 register_file(rv
, 0, pid
,
932 (tiosp
== NULL
? FP_PIPE
: FP_PIPE
| FP_TERMIOS
),
933 NULL
, 0L, NULL
, tiosp
);
942 Pclose(FILE *ptr
, bool_t dowait
)
944 struct termios
*tiosp
;
954 unregister_file(ptr
, &tiosp
);
959 sigaddset(&nset
, SIGINT
);
960 sigaddset(&nset
, SIGHUP
);
961 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
962 rv
= wait_child(pid
, NULL
);
964 n_TERMCAP_RESUME(TRU1
);
965 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
967 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
982 char const *env_add
[2], *pager
;
986 assert(options
& OPT_INTERACTIVE
);
988 pager
= n_pager_get(env_add
+ 0);
991 if ((rv
= Popen(pager
, "w", NULL
, env_add
, COMMAND_FD_PASS
)) == NULL
)
998 n_pager_close(FILE *fp
)
1004 sh
= safe_signal(SIGPIPE
, SIG_IGN
);
1005 rv
= Pclose(fp
, TRU1
);
1006 safe_signal(SIGPIPE
, sh
);
1012 close_all_files(void)
1015 while (fp_head
!= NULL
)
1016 if ((fp_head
->flags
& FP_MASK
) == FP_PIPE
)
1017 Pclose(fp_head
->fp
, TRU1
);
1019 Fclose(fp_head
->fp
);
1030 cp
= _findchild(0, TRU1
);
1032 if ((cp
->pid
= pid
= fork()) == -1) {
1034 n_perr(_("fork"), 0);
1041 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1042 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
1044 sigset_t nset
, oset
;
1049 /* TODO Of course this is a joke given that during a "p*" the PAGER may
1050 * TODO be up and running while we play around like this... but i guess
1051 * TODO this can't be helped at all unless we perform complete and true
1052 * TODO process group separation and ensure we don't deadlock us out
1053 * TODO via TTY jobcontrol signal storms (could this really happen?).
1054 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
1055 * TODO any action, and shall we find we need to run programs dump it
1056 * TODO all into a temporary file which is then passed through to the
1057 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
1058 if ((tio_set
= ((options
& OPT_INTERACTIVE
) &&
1059 (infd
== COMMAND_FD_PASS
|| outfd
== COMMAND_FD_PASS
)))) {
1060 tcgetattr((options
& OPT_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
), &a_popen_tios
);
1061 n_TERMCAP_SUSPEND(FAL0
);
1063 sigdelset(&nset
, SIGCHLD
);
1064 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
1065 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1067 a_popen_jobsigs_up();
1070 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1073 if (wait_child(rv
, NULL
))
1076 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1077 n_err(_("Fatal error in process\n"));
1083 a_popen_jobsigs_down();
1084 tio_set
= ((options
& OPT_TTYIN
) != 0);
1085 n_TERMCAP_RESUME(a_popen_hadsig
? TRU1
: FAL0
);
1086 tcsetattr((tio_set
? STDIN_FILENO
: STDOUT_FILENO
),
1087 (tio_set
? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1088 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1095 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1096 char const *a0
, char const *a1
, char const *a2
,
1097 char const **env_addon
)
1102 if ((rv
= fork_child()) == -1) {
1103 n_perr(_("fork"), 0);
1105 } else if (rv
== 0) {
1109 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1110 extern char **environ
;
1111 size_t ei
, ei_orig
, ai
, ai_orig
;
1114 /* TODO note we don't check the POSIX limit:
1115 * the total space used to store the environment and the arguments to
1116 * the process is limited to {ARG_MAX} bytes */
1117 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1120 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1123 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1124 memcpy(env
, environ
, sizeof(*env
) * ei
);
1126 /* Replace all those keys that yet exist */
1128 char const *ee
, *kvs
;
1132 kvs
= strchr(ee
, '=');
1133 assert(kvs
!= NULL
);
1134 kl
= PTR2SIZE(kvs
- ee
);
1136 for (ei
= ei_orig
; ei
-- > 0;) {
1137 char const *ekvs
= strchr(env
[ei
], '=');
1138 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1139 !memcmp(ee
, env
[ei
], kl
)) {
1140 env
[ei
] = UNCONST(ee
);
1141 env_addon
[ai
] = NULL
;
1147 /* And append the rest */
1148 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1149 if (env_addon
[ai
] != NULL
)
1150 env
[ei
++] = UNCONST(env_addon
[ai
]);
1156 i
= (int)getrawlist(FAL0
, argv
, NELEM(argv
), cmd
, strlen(cmd
));
1158 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
1159 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
1160 (argv
[i
++] = UNCONST(a2
)) != NULL
)
1162 prepare_child(mask
, infd
, outfd
);
1163 execvp(argv
[0], argv
);
1172 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
1178 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1179 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1180 if ((i
= (infd
== COMMAND_FD_NULL
)))
1181 infd
= open("/dev/null", O_RDONLY
);
1183 dup2(infd
, STDIN_FILENO
);
1188 if ((i
= (outfd
== COMMAND_FD_NULL
)))
1189 outfd
= open("/dev/null", O_WRONLY
);
1191 dup2(outfd
, STDOUT_FILENO
);
1197 for (i
= 1; i
< NSIG
; ++i
)
1198 if (sigismember(nset
, i
))
1199 safe_signal(i
, SIG_IGN
);
1200 if (!sigismember(nset
, SIGINT
))
1201 safe_signal(SIGINT
, SIG_DFL
);
1205 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1212 sigset_t nset
, oset
;
1217 sigaddset(&nset
, SIGCHLD
);
1218 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1220 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1227 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1232 wait_child(int pid
, int *wait_status
)
1234 sigset_t nset
, oset
;
1241 sigaddset(&nset
, SIGCHLD
);
1242 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1244 cp
= _findchild(pid
, FAL0
);
1253 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1255 if (wait_status
!= NULL
)
1257 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);