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 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
56 /* TODO FP_UNLINK: should be in a separated process so that unlinking
57 * TODO the temporary "garbage" is "safe"(r than it is like that) */
65 struct termios
*fp_tios
;
66 n_sighdl_t fp_osigint
; /* Only if FP_TERMIOS */
77 static struct fp
*fp_head
;
78 static struct child
*_popen_child
;
80 /* TODO Rather temporary: deal with job control with FD_PASS */
81 static struct termios a_popen_tios
;
82 static sighandler_type a_popen_otstp
, a_popen_ottin
, a_popen_ottou
;
83 static volatile int a_popen_hadsig
;
85 static int a_popen_scan_mode(char const *mode
, int *omode
);
86 static void register_file(FILE *fp
, int omode
, int pid
,
87 int flags
, char const *realfile
, long offset
,
88 char const *save_cmd
, struct termios
*tiosp
,
90 static enum okay
_file_save(struct fp
*fpp
);
91 static int a_popen_file_load(int flags
, int infd
, int outfd
,
92 char const *load_cmd
);
93 static enum okay
unregister_file(FILE *fp
, struct termios
**tiosp
,
95 static int file_pid(FILE *fp
);
97 /* TODO Rather temporary: deal with job control with FD_PASS */
98 static void a_popen_jobsigs_up(void);
99 static void a_popen_jobsigs_down(void);
100 static void a_popen_jobsig(int sig
);
103 static void a_popen_sigchld(int signo
);
105 static struct child
*a_popen_child_find(int pid
, bool_t create
);
106 static void a_popen_child_del(struct child
*cp
);
109 a_popen_scan_mode(char const *mode
, int *omode
){
115 {"w", O_WRONLY
| O_CREAT
| n_O_NOFOLLOW
| O_TRUNC
},
116 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
117 {"a", O_WRONLY
| O_APPEND
| O_CREAT
| n_O_NOFOLLOW
},
118 {"a+", O_RDWR
| O_APPEND
| O_CREAT
| n_O_NOFOLLOW
},
120 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
125 for(i
= 0; UICMP(z
, i
, <, n_NELEM(maps
)); ++i
)
126 if(!strcmp(maps
[i
].mode
, mode
)){
127 *omode
= maps
[i
].omode
;
132 DBG( n_alert(_("Internal error: bad stdio open mode %s"), mode
); )
133 n_err_no
= n_ERR_INVAL
;
134 *omode
= 0; /* (silence CC) */
142 register_file(FILE *fp
, int omode
, int pid
, int flags
,
143 char const *realfile
, long offset
, char const *save_cmd
,
144 struct termios
*tiosp
, n_sighdl_t osigint
)
149 assert(!(flags
& FP_UNLINK
) || realfile
!= NULL
);
150 assert(!(flags
& FP_TERMIOS
) || tiosp
!= NULL
);
152 fpp
= smalloc(sizeof *fpp
);
158 fpp
->realfile
= (realfile
!= NULL
) ? sstrdup(realfile
) : NULL
;
159 fpp
->save_cmd
= (save_cmd
!= NULL
) ? sstrdup(save_cmd
) : NULL
;
160 fpp
->fp_tios
= tiosp
;
161 fpp
->fp_osigint
= osigint
;
162 fpp
->offset
= offset
;
168 _file_save(struct fp
*fpp
)
175 if (fpp
->omode
== O_RDONLY
) {
184 /* Ensure the I/O library doesn't optimize the fseek(3) away! */
185 if(!n_real_seek(fpp
->fp
, fpp
->offset
, SEEK_SET
)){
187 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
188 n_shexp_quote_cp(fpp
->realfile
, FAL0
), n_err_to_doc(outfd
));
192 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
193 rv
= maildir_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
197 outfd
= open(fpp
->realfile
,
198 ((fpp
->omode
| O_CREAT
| (fpp
->omode
& O_APPEND
? 0 : O_TRUNC
) |
199 n_O_NOFOLLOW
) & ~O_EXCL
), 0666);
202 n_err(_("Fatal: cannot create %s: %s\n"),
203 n_shexp_quote_cp(fpp
->realfile
, FAL0
), n_err_to_doc(outfd
));
208 switch(fpp
->flags
& FP_MASK
){
210 cmd
[0] = ok_vlook(SHELL
);
212 cmd
[2] = fpp
->save_cmd
;
219 if (n_child_run(cmd
[0], 0, fileno(fpp
->fp
), outfd
,
220 cmd
[1], cmd
[2], NULL
, NULL
) >= 0)
230 a_popen_file_load(int flags
, int infd
, int outfd
, char const *load_cmd
){
236 switch(flags
& FP_MASK
){
241 cmd
[0] = ok_vlook(SHELL
);
251 rv
= n_child_run(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
);
258 unregister_file(FILE *fp
, struct termios
**tiosp
, n_sighdl_t
*osigint
)
267 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
269 switch (p
->flags
& FP_MASK
) {
277 if ((p
->flags
& FP_UNLINK
) && unlink(p
->realfile
))
281 if (p
->save_cmd
!= NULL
)
283 if (p
->realfile
!= NULL
)
285 if (p
->flags
& FP_TERMIOS
) {
288 *osigint
= p
->fp_osigint
;
295 DBGOR(n_panic
, n_alert
)(_("Invalid file pointer"));
310 for (p
= fp_head
; p
; p
= p
->link
)
320 a_popen_jobsigs_up(void){
326 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
327 a_popen_otstp
= safe_signal(SIGTSTP
, &a_popen_jobsig
);
328 a_popen_ottin
= safe_signal(SIGTTIN
, &a_popen_jobsig
);
329 a_popen_ottou
= safe_signal(SIGTTOU
, &a_popen_jobsig
);
331 /* This assumes oset contains nothing but SIGCHLD, so to say */
332 sigdelset(&oset
, SIGTSTP
);
333 sigdelset(&oset
, SIGTTIN
);
334 sigdelset(&oset
, SIGTTOU
);
335 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
340 a_popen_jobsigs_down(void){
346 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
347 safe_signal(SIGTSTP
, a_popen_otstp
);
348 safe_signal(SIGTTIN
, a_popen_ottin
);
349 safe_signal(SIGTTOU
, a_popen_ottou
);
351 sigaddset(&oset
, SIGTSTP
);
352 sigaddset(&oset
, SIGTTIN
);
353 sigaddset(&oset
, SIGTTOU
);
354 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
359 a_popen_jobsig(int sig
){
360 sighandler_type oldact
;
363 NYD_X
; /* Signal handler */
365 hadsig
= (a_popen_hadsig
!= 0);
368 n_TERMCAP_SUSPEND(TRU1
);
370 oldact
= safe_signal(sig
, SIG_DFL
);
373 sigaddset(&nset
, sig
);
374 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
376 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
378 safe_signal(sig
, oldact
);
382 a_popen_sigchld(int signo
){
386 NYD_X
; /* Signal handler */
390 pid
= waitpid(-1, &status
, WNOHANG
);
392 if (pid
== -1 && n_err_no
== n_ERR_INTR
)
397 if ((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
) {
399 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
408 static struct child
*
409 a_popen_child_find(int pid
, bool_t create
){
410 struct child
**cpp
, *cp
;
413 for(cpp
= &_popen_child
; (cp
= *cpp
) != NULL
&& cp
->pid
!= pid
;
417 if(cp
== NULL
&& create
)
418 (*cpp
= cp
= scalloc(1, sizeof *cp
))->pid
= pid
;
424 a_popen_child_del(struct child
*cp
){
436 if(*(cpp
= &(*cpp
)->link
) == NULL
){
437 DBG( n_err("! a_popen_child_del(): implementation error\n"); )
445 n_child_manager_start(void)
447 struct sigaction nact
, oact
;
450 nact
.sa_handler
= &a_popen_sigchld
;
451 sigemptyset(&nact
.sa_mask
);
452 nact
.sa_flags
= SA_RESTART
457 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
458 n_panic(_("Cannot install signal handler for child process management"));
463 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
467 NYD2_ENTER
; /* (only for Fopen() and once in go.c) */
469 if (a_popen_scan_mode(oflags
, &osflags
) < 0)
471 osflags
|= _O_CLOEXEC
;
475 if ((fd
= open(file
, osflags
, 0666)) == -1)
479 fp
= fdopen(fd
, oflags
);
486 Fopen(char const *file
, char const *oflags
)
492 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
493 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
,NULL
);
499 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
505 a_popen_scan_mode(oflags
, &osflags
);
507 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
509 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
510 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
,NULL
);
521 if (unregister_file(fp
, NULL
, NULL
) == OKAY
)
526 return (i
== 3 ? 0 : EOF
);
530 n_fopen_any(char const *file
, char const *oflags
, /* TODO should take flags */
531 enum n_fopen_state
*fs_or_null
){ /* TODO as bits, return state */
532 /* TODO Support file locking upon open time */
536 int osflags
, flags
, omode
, infd
;
537 char const *cload
, *csave
;
538 enum n_fopen_state fs
;
543 fs
= n_FOPEN_STATE_NONE
;
544 cload
= csave
= NULL
;
546 if(a_popen_scan_mode(oflags
, &osflags
) < 0)
550 rof
= OF_RDWR
| OF_UNLINK
;
551 if(osflags
& O_APPEND
)
553 omode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
555 /* We don't want to find mbox.bz2 when doing "copy * mbox", but only for
556 * "file mbox", so don't try hooks when writing */
557 p
= which_protocol(file
, TRU1
, ((omode
& W_OK
) == 0), &file
);
558 fs
= (enum n_fopen_state
)p
;
562 case n_PROTO_MAILDIR
:
563 if(fs_or_null
!= NULL
&& !access(file
, F_OK
))
564 fs
|= n_FOPEN_STATE_EXISTS
;
566 osflags
= O_RDWR
| O_APPEND
| O_CREAT
| n_O_NOFOLLOW
;
570 struct n_file_type ft
;
572 if(!(osflags
& O_EXCL
) && fs_or_null
!= NULL
&& !access(file
, F_OK
))
573 fs
|= n_FOPEN_STATE_EXISTS
;
575 if(n_filetype_exists(&ft
, file
)){
577 cload
= ft
.ft_load_dat
;
578 csave
= ft
.ft_save_dat
;
579 /* Cause truncation for compressor/hook output files */
580 osflags
&= ~O_APPEND
;
582 if((infd
= open(file
, (omode
& W_OK
? O_RDWR
: O_RDONLY
))) == -1){
583 if(!(osflags
& O_CREAT
) || n_err_no
!= n_ERR_NOENT
)
586 fs
|= n_FOPEN_STATE_EXISTS
;
589 rv
= Fopen(file
, oflags
);
590 if((osflags
& O_EXCL
) && rv
== NULL
)
591 fs
|= n_FOPEN_STATE_EXISTS
;
597 /* Note rv is not yet register_file()d, fclose() it in error path! */
598 if((rv
= Ftmp(NULL
, "fopenany", rof
)) == NULL
){
599 n_perr(_("tmpfile"), 0);
603 if(flags
& FP_MAILDIR
)
606 if(a_popen_file_load(flags
, infd
, fileno(rv
), cload
) < 0){
616 if((infd
= creat(file
, 0666)) == -1){
627 if(!(osflags
& O_APPEND
))
629 if((offset
= ftell(rv
)) == -1){
635 register_file(rv
, osflags
, 0, flags
, file
, offset
, csave
, NULL
,NULL
);
637 if(fs_or_null
!= NULL
)
644 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
646 /* The 8 is arbitrary but leaves room for a six character suffix (the
647 * POSIX minimum path length is 14, though we don't check that XXX).
648 * 8 should be more than sufficient given that we use base64url encoding
649 * for our random string */
650 enum {_RANDCHARS
= 8u};
653 size_t maxname
, xlen
, i
;
660 assert(namehint
!= NULL
);
661 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
662 assert(!(oflags
& OF_RDONLY
));
663 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
668 tmpdir
= ok_vlook(TMPDIR
);
673 if ((pc
= pathconf(tmpdir
, _PC_NAME_MAX
)) != -1)
674 maxname
= (size_t)pc
;
678 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
679 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
680 n_err_no
= n_ERR_NAMETOOLONG
;
686 /* Prepare the template string once, then iterate over the random range */
688 cp
= smalloc(strlen(tmpdir
) + 1 + maxname
+1);
689 cp
= sstpcpy(cp
, tmpdir
);
692 char *x
= sstpcpy(cp
, VAL_UAGENT
);
694 if (!(oflags
& OF_SUFFIX
))
695 x
= sstpcpy(x
, namehint
);
697 i
= PTR2SIZE(x
- cp
);
698 if (i
> maxname
- xlen
- _RANDCHARS
) {
699 size_t j
= maxname
- xlen
- _RANDCHARS
;
704 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
705 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
707 x
[xlen
+ _RANDCHARS
] = '\0';
711 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
712 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
713 if (oflags
& OF_APPEND
)
714 osoflags
|= O_APPEND
;
717 memcpy(cp
, n_random_create_cp(_RANDCHARS
, NULL
), _RANDCHARS
);
722 if ((fd
= open(cp_base
, osoflags
, 0600)) != -1) {
726 if (i
>= FTMP_OPEN_TRIES
) {
734 if (oflags
& OF_REGISTER
) {
735 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
738 a_popen_scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
739 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
740 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0,
741 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
742 cp_base
, 0L, NULL
, NULL
,NULL
);
744 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
746 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
757 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
764 if ((cp
= cp_base
) != NULL
)
770 Ftmp_release(char **fn
)
786 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
799 pipe_cloexec(int fd
[2])
805 if (pipe2(fd
, O_CLOEXEC
) == -1)
820 Popen(char const *cmd
, char const *mode
, char const *sh
,
821 char const **env_addon
, int newfd1
)
823 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
827 struct termios
*tiosp
;
831 /* First clean up child structures */
833 struct child
**cpp
, *cp
;
836 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
837 if ((*cpp
)->pid
== -1) {
849 n_UNINIT(osigint
, SIG_ERR
);
850 mod
[0] = '0', mod
[1] = '\0';
852 if (!pipe_cloexec(p
))
857 fd0
= n_CHILD_FD_PASS
;
858 hisside
= fd1
= p
[WRITE
];
860 } else if (*mode
== 'W') {
862 hisside
= fd0
= p
[READ
];
867 hisside
= fd0
= p
[READ
];
868 fd1
= n_CHILD_FD_PASS
;
872 /* In interactive mode both STDIN and STDOUT point to the terminal. If we
873 * pass through the TTY restore terminal attributes after pipe returns.
874 * XXX It shouldn't matter which FD we actually use in this case */
875 if ((n_psonce
& n_PSO_INTERACTIVE
) && (fd0
== n_CHILD_FD_PASS
||
876 fd1
== n_CHILD_FD_PASS
)) {
877 osigint
= n_signal(SIGINT
, SIG_IGN
);
878 tiosp
= smalloc(sizeof *tiosp
);
879 tcgetattr(STDIN_FILENO
, tiosp
);
880 n_TERMCAP_SUSPEND(TRU1
);
885 if (cmd
== (char*)-1) {
886 if ((pid
= n_child_fork()) == -1)
887 n_perr(_("fork"), 0);
889 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
890 n_child_prepare(&nset
, fd0
, fd1
);
893 /* TODO should close all other open FDs except stds and reset memory */
894 /* Standard I/O drives me insane! All we need is a sync operation
895 * that causes n_stdin to forget about any read buffer it may have.
896 * We cannot use fflush(3), this works with Musl and Solaris, but not
897 * with GlibC. (For at least pipes.) We cannot use fdreopen(),
898 * because this function does not exist! Luckily (!!!) we only use
899 * n_stdin not stdin in our child, otherwise all bets were off!
900 * TODO (Unless we would fiddle around with FILE* directly:
901 * TODO #ifdef __GLIBC__
902 * TODO n_stdin->_IO_read_ptr = n_stdin->_IO_read_end;
904 * TODO n_stdin->_r = 0;
905 * TODO #elif n_OS_SOLARIS || n_OS_SUNOS
906 * TODO n_stdin->_cnt = 0;
908 * TODO ) which should have additional config test for sure! */
909 n_stdin
= fdopen(STDIN_FILENO
, "r");
910 /*n_stdout = fdopen(STDOUT_FILENO, "w");*/
911 /*n_stderr = fdopen(STDERR_FILENO, "w");*/
917 } else if (sh
== NULL
) {
918 pid
= n_child_start(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
920 pid
= n_child_start(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
928 if ((rv
= fdopen(myside
, mod
)) != NULL
)
929 register_file(rv
, 0, pid
,
930 (tiosp
== NULL
? FP_PIPE
: FP_PIPE
| FP_TERMIOS
),
931 NULL
, 0L, NULL
, tiosp
, osigint
);
935 if(rv
== NULL
&& tiosp
!= NULL
){
936 n_TERMCAP_RESUME(TRU1
);
937 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
939 n_signal(SIGINT
, osigint
);
946 Pclose(FILE *ptr
, bool_t dowait
)
949 struct termios
*tiosp
;
958 unregister_file(ptr
, &tiosp
, &osigint
);
963 rv
= n_child_wait(pid
, NULL
);
965 n_TERMCAP_RESUME(TRU1
);
966 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
967 n_signal(SIGINT
, osigint
);
985 char const *env_add
[2], *pager
;
989 assert(n_psonce
& n_PSO_INTERACTIVE
);
991 pager
= n_pager_get(env_add
+ 0);
994 if ((rv
= Popen(pager
, "w", NULL
, env_add
, n_CHILD_FD_PASS
)) == NULL
)
1001 n_pager_close(FILE *fp
)
1007 sh
= safe_signal(SIGPIPE
, SIG_IGN
);
1008 rv
= Pclose(fp
, TRU1
);
1009 safe_signal(SIGPIPE
, sh
);
1015 close_all_files(void)
1018 while (fp_head
!= NULL
)
1019 if ((fp_head
->flags
& FP_MASK
) == FP_PIPE
)
1020 Pclose(fp_head
->fp
, TRU1
);
1022 Fclose(fp_head
->fp
);
1027 n_child_run(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1028 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
1030 sigset_t nset
, oset
;
1031 sighandler_type soldint
;
1033 enum {a_NONE
= 0, a_INTIGN
= 1<<0, a_TTY
= 1<<1} f
;
1037 n_UNINIT(soldint
, SIG_ERR
);
1039 /* TODO Of course this is a joke given that during a "p*" the PAGER may
1040 * TODO be up and running while we play around like this... but i guess
1041 * TODO this can't be helped at all unless we perform complete and true
1042 * TODO process group separation and ensure we don't deadlock us out
1043 * TODO via TTY jobcontrol signal storms (could this really happen?).
1044 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
1045 * TODO any action, and shall we find we need to run programs dump it
1046 * TODO all into a temporary file which is then passed through to the
1047 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
1048 if(infd
== n_CHILD_FD_PASS
|| outfd
== n_CHILD_FD_PASS
){
1049 soldint
= safe_signal(SIGINT
, SIG_IGN
);
1052 if(n_psonce
& n_PSO_INTERACTIVE
){
1054 tcgetattr((n_psonce
& n_PSO_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
),
1056 n_TERMCAP_SUSPEND(FAL0
);
1058 sigdelset(&nset
, SIGCHLD
);
1059 sigdelset(&nset
, SIGINT
);
1060 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
1061 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1063 a_popen_jobsigs_up();
1067 if((rv
= n_child_start(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1070 if(n_child_wait(rv
, NULL
))
1073 if(ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1074 n_err(_("Fatal error in process\n"));
1080 a_popen_jobsigs_down();
1081 n_TERMCAP_RESUME(a_popen_hadsig
? TRU1
: FAL0
);
1082 tcsetattr(((n_psonce
& n_PSO_TTYIN
) ? STDIN_FILENO
: STDOUT_FILENO
),
1083 ((n_psonce
& n_PSO_TTYIN
) ? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1084 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1087 if(soldint
!= SIG_IGN
)
1088 safe_signal(SIGINT
, soldint
);
1095 n_child_start(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
= n_child_fork()) == -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
] = n_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
++] = n_UNCONST(env_addon
[ai
]);
1156 i
= (int)getrawlist(TRU1
, argv
, n_NELEM(argv
), cmd
, strlen(cmd
));
1158 if ((argv
[i
++] = n_UNCONST(a0
)) != NULL
&&
1159 (argv
[i
++] = n_UNCONST(a1
)) != NULL
&&
1160 (argv
[i
++] = n_UNCONST(a2
)) != NULL
)
1162 n_child_prepare(mask
, infd
, outfd
);
1163 execvp(argv
[0], argv
);
1177 cp
= a_popen_child_find(0, TRU1
);
1179 if((cp
->pid
= pid
= fork()) == -1){
1180 a_popen_child_del(cp
);
1181 n_perr(_("fork"), 0);
1188 n_child_prepare(sigset_t
*nset
, int infd
, int outfd
)
1194 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1195 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1196 if ((i
= (infd
== n_CHILD_FD_NULL
)))
1197 infd
= open("/dev/null", O_RDONLY
);
1199 dup2(infd
, STDIN_FILENO
);
1204 if ((i
= (outfd
== n_CHILD_FD_NULL
)))
1205 outfd
= open("/dev/null", O_WRONLY
);
1207 dup2(outfd
, STDOUT_FILENO
);
1213 for (i
= 1; i
< NSIG
; ++i
)
1214 if (sigismember(nset
, i
))
1215 safe_signal(i
, SIG_IGN
);
1216 if (!sigismember(nset
, SIGINT
))
1217 safe_signal(SIGINT
, SIG_DFL
);
1221 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1226 n_child_free(int pid
){
1227 sigset_t nset
, oset
;
1232 sigaddset(&nset
, SIGCHLD
);
1233 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1235 if((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
){
1237 a_popen_child_del(cp
);
1242 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1247 n_child_wait(int pid
, int *wait_status
){
1248 sigset_t nset
, oset
;
1255 sigaddset(&nset
, SIGCHLD
);
1256 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1258 if((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
){
1262 a_popen_child_del(cp
);
1266 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1268 if(wait_status
!= NULL
)
1270 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);