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 fpp
->realfile
, 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 fpp
->realfile
, 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 fpp
->realfile
, 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 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
229 cmd
[2] = fpp
->save_cmd
;
231 if (run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
) >= 0)
241 _file_load(int flags
, int infd
, int outfd
, char const *load_cmd
)
248 switch (flags
& FP_MASK
) {
249 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
250 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
251 case FP_XZ
: cmd
[0] = "xz"; cmd
[1] = "-cd"; break;
252 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
254 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
264 rv
= run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
);
271 unregister_file(FILE *fp
, struct termios
**tiosp
)
280 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
282 switch (p
->flags
& FP_MASK
) {
290 if ((p
->flags
& FP_UNLINK
) && unlink(p
->realfile
))
294 if (p
->save_cmd
!= NULL
)
296 if (p
->realfile
!= NULL
)
298 if (p
->flags
& FP_TERMIOS
) {
307 DBGOR(n_panic
, n_alert
)(_("Invalid file pointer"));
322 for (p
= fp_head
; p
; p
= p
->link
)
332 a_popen_jobsigs_up(void){
338 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
339 a_popen_otstp
= safe_signal(SIGTSTP
, &a_popen_jobsig
);
340 a_popen_ottin
= safe_signal(SIGTTIN
, &a_popen_jobsig
);
341 a_popen_ottou
= safe_signal(SIGTTOU
, &a_popen_jobsig
);
343 /* This assumes oset contains nothing but SIGCHLD, so to say */
344 sigdelset(&oset
, SIGTSTP
);
345 sigdelset(&oset
, SIGTTIN
);
346 sigdelset(&oset
, SIGTTOU
);
347 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
352 a_popen_jobsigs_down(void){
358 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
359 safe_signal(SIGTSTP
, a_popen_otstp
);
360 safe_signal(SIGTTIN
, a_popen_ottin
);
361 safe_signal(SIGTTOU
, a_popen_ottou
);
363 sigaddset(&oset
, SIGTSTP
);
364 sigaddset(&oset
, SIGTTIN
);
365 sigaddset(&oset
, SIGTTOU
);
366 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
371 a_popen_jobsig(int sig
){
372 sighandler_type oldact
;
375 NYD_X
; /* Signal handler */
377 hadsig
= (a_popen_hadsig
!= 0);
380 n_TERMCAP_SUSPEND(TRU1
);
382 oldact
= safe_signal(sig
, SIG_DFL
);
385 sigaddset(&nset
, sig
);
386 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
388 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
390 safe_signal(sig
, oldact
);
399 NYD_X
; /* Signal handler */
403 pid
= waitpid(-1, &status
, WNOHANG
);
405 if (pid
== -1 && errno
== EINTR
)
410 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
412 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
421 static struct child
*
422 _findchild(int pid
, bool_t create
)
427 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
431 if (*cpp
== NULL
&& create
) {
432 *cpp
= smalloc(sizeof **cpp
);
434 (*cpp
)->done
= (*cpp
)->free
= 0;
442 _delchild(struct child
*cp
)
454 if (*(cpp
= &(*cpp
)->link
) == NULL
) {
455 DBG( n_err("! popen.c:_delchild(): implementation error\n"); )
463 command_manager_start(void)
465 struct sigaction nact
, oact
;
468 nact
.sa_handler
= &_sigchld
;
469 sigemptyset(&nact
.sa_mask
);
470 nact
.sa_flags
= SA_RESTART
475 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
476 n_panic(_("Cannot install signal handler for child process management"));
481 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
485 NYD2_ENTER
; /* (only for Fopen() and once in lex.c) */
487 if (scan_mode(oflags
, &osflags
) < 0)
489 osflags
|= _O_CLOEXEC
;
493 if ((fd
= open(file
, osflags
, 0666)) == -1)
497 fp
= fdopen(fd
, oflags
);
504 Fopen(char const *file
, char const *oflags
)
510 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
511 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
);
517 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
523 scan_mode(oflags
, &osflags
);
525 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
527 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
528 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
);
539 if (unregister_file(fp
, NULL
) == OKAY
)
544 return (i
== 3 ? 0 : EOF
);
548 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
551 char const *cload
= NULL
, *csave
= NULL
;
552 int flags
, osflags
, mode
, infd
;
558 if (scan_mode(oflags
, &osflags
) < 0)
562 rof
= OF_RDWR
| OF_UNLINK
;
563 if (osflags
& O_APPEND
)
565 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
567 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_MAILDIR
)) {
569 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
574 if ((ext
= strrchr(file
, '.')) != NULL
) {
575 if (!asccasecmp(ext
, ".gz"))
577 else if (!asccasecmp(ext
, ".xz")) {
579 osflags
&= ~O_APPEND
;
581 } else if (!asccasecmp(ext
, ".bz2")) {
583 osflags
&= ~O_APPEND
;
587 #define _X1 "file-hook-load-"
589 #define _X2 "file-hook-save-"
590 size_t l
= strlen(++ext
);
591 char *vbuf
= ac_alloc(l
+ MAX(sizeof(_X1
), sizeof(_X2
)));
593 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
594 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
595 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
596 cload
= vok_vlook(vbuf
);
597 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
598 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
599 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
600 csave
= vok_vlook(vbuf
);
605 if ((csave
!= NULL
) && (cload
!= NULL
)) {
607 osflags
&= ~O_APPEND
;
609 } else if ((csave
!= NULL
) | (cload
!= NULL
)) {
610 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
611 "Treating as plain text!"), ext
);
619 rv
= Fopen(file
, oflags
);
623 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
624 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
628 /* Note rv is not yet register_file()d, fclose() it in error path! */
629 if ((rv
= Ftmp(NULL
, "zopen", rof
)) == NULL
) {
630 n_perr(_("tmpfile"), 0);
634 if (flags
& FP_MAILDIR
)
636 else if (infd
>= 0) {
637 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
647 if ((infd
= creat(file
, 0666)) == -1) {
658 if (!(osflags
& O_APPEND
))
660 if ((offset
= ftell(rv
)) == -1) {
666 register_file(rv
, osflags
, 0, flags
, file
, offset
, csave
, NULL
);
673 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
675 /* The 8 is arbitrary but leaves room for a six character suffix (the
676 * POSIX minimum path length is 14, though we don't check that XXX).
677 * 8 should be more than sufficient given that we use base64url encoding
678 * for our random string */
679 enum {_RANDCHARS
= 8};
681 size_t maxname
, xlen
, i
;
688 assert(namehint
!= NULL
);
689 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
690 assert(!(oflags
& OF_RDONLY
));
691 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
700 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
701 maxname
= (size_t)pc
;
705 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
706 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
707 errno
= ENAMETOOLONG
;
713 /* Prepare the template string once, then iterate over the random range */
715 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
716 cp
= sstpcpy(cp
, tempdir
);
719 char *x
= sstpcpy(cp
, UAGENT
);
721 if (!(oflags
& OF_SUFFIX
))
722 x
= sstpcpy(x
, namehint
);
724 i
= PTR2SIZE(x
- cp
);
725 if (i
> maxname
- xlen
- _RANDCHARS
) {
726 size_t j
= maxname
- xlen
- _RANDCHARS
;
731 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
732 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
734 x
[xlen
+ _RANDCHARS
] = '\0';
738 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
739 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
740 if (oflags
& OF_APPEND
)
741 osoflags
|= O_APPEND
;
744 memcpy(cp
, getrandstring(_RANDCHARS
), _RANDCHARS
);
749 if ((fd
= open(cp_base
, osoflags
, 0600)) != -1) {
753 if (i
>= FTMP_OPEN_TRIES
) {
761 if (oflags
& OF_REGISTER
) {
762 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
765 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
766 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
767 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0,
768 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
769 cp_base
, 0L, NULL
, NULL
);
771 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
773 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
784 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
791 if ((cp
= cp_base
) != NULL
)
797 Ftmp_release(char **fn
)
813 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
826 pipe_cloexec(int fd
[2])
832 if (pipe2(fd
, O_CLOEXEC
) == -1)
837 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
838 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
847 Popen(char const *cmd
, char const *mode
, char const *sh
,
848 char const **env_addon
, int newfd1
)
850 struct termios
*tiosp
;
851 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
852 char mod
[2] = {'0', '\0'};
857 /* First clean up child structures */
859 struct child
**cpp
, *cp
;
862 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
864 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
865 if ((*cpp
)->pid
== -1) {
873 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
876 if (!pipe_cloexec(p
))
881 fd0
= COMMAND_FD_PASS
;
882 hisside
= fd1
= p
[WRITE
];
884 } else if (*mode
== 'W') {
886 hisside
= fd0
= p
[READ
];
891 hisside
= fd0
= p
[READ
];
892 fd1
= COMMAND_FD_PASS
;
896 /* In interactive mode both STDIN and STDOUT point to the terminal. If we
897 * pass through the TTY restore terminal attributes after pipe returns.
898 * XXX It shouldn't matter which FD we actually use in this case */
899 if ((options
& OPT_INTERACTIVE
) && (fd0
== COMMAND_FD_PASS
||
900 fd1
== COMMAND_FD_PASS
)) {
901 tiosp
= smalloc(sizeof *tiosp
);
902 tcgetattr(STDIN_FILENO
, tiosp
);
903 n_TERMCAP_SUSPEND(TRU1
);
909 if (cmd
== (char*)-1) {
910 if ((pid
= fork_child()) == -1)
911 n_perr(_("fork"), 0);
913 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
914 prepare_child(&nset
, fd0
, fd1
);
921 } else if (sh
== NULL
) {
922 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
924 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
932 if ((rv
= fdopen(myside
, mod
)) != NULL
)
933 register_file(rv
, 0, pid
,
934 (tiosp
== NULL
? FP_PIPE
: FP_PIPE
| FP_TERMIOS
),
935 NULL
, 0L, NULL
, tiosp
);
944 Pclose(FILE *ptr
, bool_t dowait
)
946 struct termios
*tiosp
;
956 unregister_file(ptr
, &tiosp
);
961 sigaddset(&nset
, SIGINT
);
962 sigaddset(&nset
, SIGHUP
);
963 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
964 rv
= wait_child(pid
, NULL
);
966 n_TERMCAP_RESUME(TRU1
);
967 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
969 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
984 char const *env_add
[2], *pager
;
988 assert(options
& OPT_INTERACTIVE
);
990 pager
= n_pager_get(env_add
+ 0);
993 if ((rv
= Popen(pager
, "w", NULL
, env_add
, COMMAND_FD_PASS
)) == NULL
)
1000 n_pager_close(FILE *fp
)
1006 sh
= safe_signal(SIGPIPE
, SIG_IGN
);
1007 rv
= Pclose(fp
, TRU1
);
1008 safe_signal(SIGPIPE
, sh
);
1014 close_all_files(void)
1017 while (fp_head
!= NULL
)
1018 if ((fp_head
->flags
& FP_MASK
) == FP_PIPE
)
1019 Pclose(fp_head
->fp
, TRU1
);
1021 Fclose(fp_head
->fp
);
1032 cp
= _findchild(0, TRU1
);
1034 if ((cp
->pid
= pid
= fork()) == -1) {
1036 n_perr(_("fork"), 0);
1043 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1044 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
1046 sigset_t nset
, oset
;
1051 /* TODO Of course this is a joke given that during a "p*" the PAGER may
1052 * TODO be up and running while we play around like this... but i guess
1053 * TODO this can't be helped at all unless we perform complete and true
1054 * TODO process group separation and ensure we don't deadlock us out
1055 * TODO via TTY jobcontrol signal storms (could this really happen?).
1056 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
1057 * TODO any action, and shall we find we need to run programs dump it
1058 * TODO all into a temporary file which is then passed through to the
1059 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
1060 if ((tio_set
= ((options
& OPT_INTERACTIVE
) &&
1061 (infd
== COMMAND_FD_PASS
|| outfd
== COMMAND_FD_PASS
)))) {
1062 tcgetattr((options
& OPT_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
), &a_popen_tios
);
1063 n_TERMCAP_SUSPEND(FAL0
);
1065 sigdelset(&nset
, SIGCHLD
);
1066 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
1067 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1069 a_popen_jobsigs_up();
1072 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1075 if (wait_child(rv
, NULL
))
1078 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1079 n_err(_("Fatal error in process\n"));
1085 a_popen_jobsigs_down();
1086 tio_set
= ((options
& OPT_TTYIN
) != 0);
1087 n_TERMCAP_RESUME(a_popen_hadsig
? TRU1
: FAL0
);
1088 tcsetattr((tio_set
? STDIN_FILENO
: STDOUT_FILENO
),
1089 (tio_set
? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1090 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1097 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1098 char const *a0
, char const *a1
, char const *a2
,
1099 char const **env_addon
)
1104 if ((rv
= fork_child()) == -1) {
1105 n_perr(_("fork"), 0);
1107 } else if (rv
== 0) {
1111 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1112 extern char **environ
;
1113 size_t ei
, ei_orig
, ai
, ai_orig
;
1116 /* TODO note we don't check the POSIX limit:
1117 * the total space used to store the environment and the arguments to
1118 * the process is limited to {ARG_MAX} bytes */
1119 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1122 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1125 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1126 memcpy(env
, environ
, sizeof(*env
) * ei
);
1128 /* Replace all those keys that yet exist */
1130 char const *ee
, *kvs
;
1134 kvs
= strchr(ee
, '=');
1135 assert(kvs
!= NULL
);
1136 kl
= PTR2SIZE(kvs
- ee
);
1138 for (ei
= ei_orig
; ei
-- > 0;) {
1139 char const *ekvs
= strchr(env
[ei
], '=');
1140 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1141 !memcmp(ee
, env
[ei
], kl
)) {
1142 env
[ei
] = UNCONST(ee
);
1143 env_addon
[ai
] = NULL
;
1149 /* And append the rest */
1150 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1151 if (env_addon
[ai
] != NULL
)
1152 env
[ei
++] = UNCONST(env_addon
[ai
]);
1158 i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
1160 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
1161 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
1162 (argv
[i
++] = UNCONST(a2
)) != NULL
)
1164 prepare_child(mask
, infd
, outfd
);
1165 execvp(argv
[0], argv
);
1174 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
1180 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1181 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1182 if ((i
= (infd
== COMMAND_FD_NULL
)))
1183 infd
= open("/dev/null", O_RDONLY
);
1185 dup2(infd
, STDIN_FILENO
);
1190 if ((i
= (outfd
== COMMAND_FD_NULL
)))
1191 outfd
= open("/dev/null", O_WRONLY
);
1193 dup2(outfd
, STDOUT_FILENO
);
1199 for (i
= 1; i
< NSIG
; ++i
)
1200 if (sigismember(nset
, i
))
1201 safe_signal(i
, SIG_IGN
);
1202 if (!sigismember(nset
, SIGINT
))
1203 safe_signal(SIGINT
, SIG_DFL
);
1207 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1214 sigset_t nset
, oset
;
1219 sigaddset(&nset
, SIGCHLD
);
1220 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1222 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1229 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1234 wait_child(int pid
, int *wait_status
)
1236 sigset_t nset
, oset
;
1243 sigaddset(&nset
, SIGCHLD
);
1244 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1246 cp
= _findchild(pid
, FAL0
);
1255 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1257 if (wait_status
!= NULL
)
1259 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);