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
60 /* TODO FP_UNLINK: should be in a separated process so that unlinking
61 * TODO the temporary "garbage" is "safe"(r than it is like that) */
69 struct termios
*fp_tios
;
70 n_sighdl_t fp_osigint
; /* Only if FP_TERMIOS */
81 static struct fp
*fp_head
;
82 static struct child
*_popen_child
;
84 /* TODO Rather temporary: deal with job control with FD_PASS */
85 static struct termios a_popen_tios
;
86 static sighandler_type a_popen_otstp
, a_popen_ottin
, a_popen_ottou
;
87 static volatile int a_popen_hadsig
;
89 static int scan_mode(char const *mode
, int *omode
);
90 static void register_file(FILE *fp
, int omode
, int pid
,
91 int flags
, char const *realfile
, long offset
,
92 char const *save_cmd
, struct termios
*tiosp
,
94 static enum okay
_file_save(struct fp
*fpp
);
95 static int _file_load(int flags
, int infd
, int outfd
,
96 char const *load_cmd
);
97 static enum okay
unregister_file(FILE *fp
, struct termios
**tiosp
,
99 static int file_pid(FILE *fp
);
101 /* TODO Rather temporary: deal with job control with FD_PASS */
102 static void a_popen_jobsigs_up(void);
103 static void a_popen_jobsigs_down(void);
104 static void a_popen_jobsig(int sig
);
107 static void a_popen_sigchld(int signo
);
109 static struct child
*a_popen_child_find(int pid
, bool_t create
);
110 static void a_popen_child_del(struct child
*cp
);
113 scan_mode(char const *mode
, int *omode
)
120 {"w", O_WRONLY
| O_CREAT
| n_O_NOFOLLOW
| O_TRUNC
},
121 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
122 {"a", O_WRONLY
| O_APPEND
| O_CREAT
| n_O_NOFOLLOW
},
123 {"a+", O_RDWR
| O_APPEND
},
125 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
131 for (i
= 0; UICMP(z
, i
, <, n_NELEM(maps
)); ++i
)
132 if (!strcmp(maps
[i
].mode
, mode
)) {
133 *omode
= maps
[i
].omode
;
138 n_alert(_("Internal error: bad stdio open mode %s"), mode
);
139 n_err_no
= n_ERR_INVAL
;
140 *omode
= 0; /* (silence CC) */
148 register_file(FILE *fp
, int omode
, int pid
, int flags
,
149 char const *realfile
, long offset
, char const *save_cmd
,
150 struct termios
*tiosp
, n_sighdl_t osigint
)
155 assert(!(flags
& FP_UNLINK
) || realfile
!= NULL
);
156 assert(!(flags
& FP_TERMIOS
) || tiosp
!= NULL
);
158 fpp
= smalloc(sizeof *fpp
);
164 fpp
->realfile
= (realfile
!= NULL
) ? sstrdup(realfile
) : NULL
;
165 fpp
->save_cmd
= (save_cmd
!= NULL
) ? sstrdup(save_cmd
) : NULL
;
166 fpp
->fp_tios
= tiosp
;
167 fpp
->fp_osigint
= osigint
;
168 fpp
->offset
= offset
;
174 _file_save(struct fp
*fpp
)
181 if (fpp
->omode
== O_RDONLY
) {
190 /* Ensure the I/O library doesn't optimize the fseek(3) away! */
191 if(!n_real_seek(fpp
->fp
, fpp
->offset
, SEEK_SET
)){
193 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
194 n_shexp_quote_cp(fpp
->realfile
, FAL0
), n_err_to_doc(outfd
));
198 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
199 rv
= maildir_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
203 outfd
= open(fpp
->realfile
,
204 ((fpp
->omode
| O_CREAT
| (fpp
->omode
& O_APPEND
? 0 : O_TRUNC
) |
205 n_O_NOFOLLOW
) & ~O_EXCL
), 0666);
208 n_err(_("Fatal: cannot create %s: %s\n"),
209 n_shexp_quote_cp(fpp
->realfile
, FAL0
), n_err_to_doc(outfd
));
214 switch (fpp
->flags
& FP_MASK
) {
216 cmd
[0] = "gzip"; cmd
[1] = "-c"; break;
218 cmd
[0] = "bzip2"; cmd
[1] = "-c"; break;
220 cmd
[0] = "xz"; cmd
[1] = "-c"; break;
222 cmd
[0] = "cat"; cmd
[1] = NULL
; break;
224 cmd
[0] = ok_vlook(SHELL
);
226 cmd
[2] = fpp
->save_cmd
;
228 if (n_child_run(cmd
[0], 0, fileno(fpp
->fp
), outfd
,
229 cmd
[1], cmd
[2], NULL
, NULL
) >= 0)
239 _file_load(int flags
, int infd
, int outfd
, char const *load_cmd
)
246 switch (flags
& FP_MASK
) {
247 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
248 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
249 case FP_XZ
: cmd
[0] = "xz"; cmd
[1] = "-cd"; break;
250 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
252 cmd
[0] = ok_vlook(SHELL
);
261 rv
= n_child_run(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
);
268 unregister_file(FILE *fp
, struct termios
**tiosp
, n_sighdl_t
*osigint
)
277 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
279 switch (p
->flags
& FP_MASK
) {
287 if ((p
->flags
& FP_UNLINK
) && unlink(p
->realfile
))
291 if (p
->save_cmd
!= NULL
)
293 if (p
->realfile
!= NULL
)
295 if (p
->flags
& FP_TERMIOS
) {
298 *osigint
= p
->fp_osigint
;
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
);
392 a_popen_sigchld(int signo
){
396 NYD_X
; /* Signal handler */
400 pid
= waitpid(-1, &status
, WNOHANG
);
402 if (pid
== -1 && n_err_no
== n_ERR_INTR
)
407 if ((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
) {
409 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
418 static struct child
*
419 a_popen_child_find(int pid
, bool_t create
){
420 struct child
**cpp
, *cp
;
423 for(cpp
= &_popen_child
; (cp
= *cpp
) != NULL
&& cp
->pid
!= pid
;
427 if(cp
== NULL
&& create
)
428 (*cpp
= cp
= scalloc(1, sizeof *cp
))->pid
= pid
;
434 a_popen_child_del(struct child
*cp
){
446 if(*(cpp
= &(*cpp
)->link
) == NULL
){
447 DBG( n_err("! a_popen_child_del(): implementation error\n"); )
455 n_child_manager_start(void)
457 struct sigaction nact
, oact
;
460 nact
.sa_handler
= &a_popen_sigchld
;
461 sigemptyset(&nact
.sa_mask
);
462 nact
.sa_flags
= SA_RESTART
467 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
468 n_panic(_("Cannot install signal handler for child process management"));
473 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
477 NYD2_ENTER
; /* (only for Fopen() and once in go.c) */
479 if (scan_mode(oflags
, &osflags
) < 0)
481 osflags
|= _O_CLOEXEC
;
485 if ((fd
= open(file
, osflags
, 0666)) == -1)
489 fp
= fdopen(fd
, oflags
);
496 Fopen(char const *file
, char const *oflags
)
502 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
503 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
,NULL
);
509 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
515 scan_mode(oflags
, &osflags
);
517 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
519 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
520 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
,NULL
);
531 if (unregister_file(fp
, NULL
, NULL
) == OKAY
)
536 return (i
== 3 ? 0 : EOF
);
540 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
543 char const *cload
= NULL
, *csave
= NULL
;
544 int flags
, osflags
, mode
, infd
;
550 if (scan_mode(oflags
, &osflags
) < 0)
554 rof
= OF_RDWR
| OF_UNLINK
;
555 if (osflags
& O_APPEND
)
557 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
559 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_MAILDIR
)) {
561 osflags
= O_RDWR
| O_APPEND
| O_CREAT
| n_O_NOFOLLOW
;
566 if ((ext
= strrchr(file
, '.')) != NULL
) {
567 if (!asccasecmp(ext
, ".gz"))
569 else if (!asccasecmp(ext
, ".xz")) {
571 osflags
&= ~O_APPEND
;
573 } else if (!asccasecmp(ext
, ".bz2")) {
575 osflags
&= ~O_APPEND
;
579 #define _X1 "file-hook-load-"
581 #define _X2 "file-hook-save-"
582 size_t l
= strlen(++ext
);
583 char *vbuf
= ac_alloc(l
+ n_MAX(sizeof(_X1
), sizeof(_X2
)));
585 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
586 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
587 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
588 cload
= n_var_vlook(vbuf
, FAL0
);
589 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
590 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
591 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
592 csave
= n_var_vlook(vbuf
, FAL0
);
597 if ((csave
!= NULL
) && (cload
!= NULL
)) {
599 osflags
&= ~O_APPEND
;
601 } else if ((csave
!= NULL
) | (cload
!= NULL
)) {
602 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
603 "Treating as plain text!"), ext
);
611 rv
= Fopen(file
, oflags
);
615 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
616 (!(osflags
& O_CREAT
) || n_err_no
!= n_ERR_NOENT
))
620 /* Note rv is not yet register_file()d, fclose() it in error path! */
621 if ((rv
= Ftmp(NULL
, "zopen", rof
)) == NULL
) {
622 n_perr(_("tmpfile"), 0);
626 if (flags
& FP_MAILDIR
)
628 else if (infd
>= 0) {
629 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
639 if ((infd
= creat(file
, 0666)) == -1) {
650 if (!(osflags
& O_APPEND
))
652 if ((offset
= ftell(rv
)) == -1) {
658 register_file(rv
, osflags
, 0, flags
, file
, offset
, csave
, NULL
,NULL
);
665 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
667 /* The 8 is arbitrary but leaves room for a six character suffix (the
668 * POSIX minimum path length is 14, though we don't check that XXX).
669 * 8 should be more than sufficient given that we use base64url encoding
670 * for our random string */
671 enum {_RANDCHARS
= 8u};
674 size_t maxname
, xlen
, i
;
681 assert(namehint
!= NULL
);
682 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
683 assert(!(oflags
& OF_RDONLY
));
684 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
689 tmpdir
= ok_vlook(TMPDIR
);
694 if ((pc
= pathconf(tmpdir
, _PC_NAME_MAX
)) != -1)
695 maxname
= (size_t)pc
;
699 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
700 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
701 n_err_no
= n_ERR_NAMETOOLONG
;
707 /* Prepare the template string once, then iterate over the random range */
709 cp
= smalloc(strlen(tmpdir
) + 1 + maxname
+1);
710 cp
= sstpcpy(cp
, tmpdir
);
713 char *x
= sstpcpy(cp
, VAL_UAGENT
);
715 if (!(oflags
& OF_SUFFIX
))
716 x
= sstpcpy(x
, namehint
);
718 i
= PTR2SIZE(x
- cp
);
719 if (i
> maxname
- xlen
- _RANDCHARS
) {
720 size_t j
= maxname
- xlen
- _RANDCHARS
;
725 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
726 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
728 x
[xlen
+ _RANDCHARS
] = '\0';
732 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
733 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
734 if (oflags
& OF_APPEND
)
735 osoflags
|= O_APPEND
;
738 memcpy(cp
, n_random_create_cp(_RANDCHARS
), _RANDCHARS
);
743 if ((fd
= open(cp_base
, osoflags
, 0600)) != -1) {
747 if (i
>= FTMP_OPEN_TRIES
) {
755 if (oflags
& OF_REGISTER
) {
756 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
759 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
760 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
761 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0,
762 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
763 cp_base
, 0L, NULL
, NULL
,NULL
);
765 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
767 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
778 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
785 if ((cp
= cp_base
) != NULL
)
791 Ftmp_release(char **fn
)
807 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
820 pipe_cloexec(int fd
[2])
826 if (pipe2(fd
, O_CLOEXEC
) == -1)
831 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
832 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
841 Popen(char const *cmd
, char const *mode
, char const *sh
,
842 char const **env_addon
, int newfd1
)
844 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
848 struct termios
*tiosp
;
852 /* First clean up child structures */
854 struct child
**cpp
, *cp
;
857 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
858 if ((*cpp
)->pid
== -1) {
870 n_UNINIT(osigint
, SIG_ERR
);
871 mod
[0] = '0', mod
[1] = '\0';
873 if (!pipe_cloexec(p
))
878 fd0
= n_CHILD_FD_PASS
;
879 hisside
= fd1
= p
[WRITE
];
881 } else if (*mode
== 'W') {
883 hisside
= fd0
= p
[READ
];
888 hisside
= fd0
= p
[READ
];
889 fd1
= n_CHILD_FD_PASS
;
893 /* In interactive mode both STDIN and STDOUT point to the terminal. If we
894 * pass through the TTY restore terminal attributes after pipe returns.
895 * XXX It shouldn't matter which FD we actually use in this case */
896 if ((n_psonce
& n_PSO_INTERACTIVE
) && (fd0
== n_CHILD_FD_PASS
||
897 fd1
== n_CHILD_FD_PASS
)) {
898 osigint
= n_signal(SIGINT
, SIG_IGN
);
899 tiosp
= smalloc(sizeof *tiosp
);
900 tcgetattr(STDIN_FILENO
, tiosp
);
901 n_TERMCAP_SUSPEND(TRU1
);
906 if (cmd
== (char*)-1) {
907 if ((pid
= n_child_fork()) == -1)
908 n_perr(_("fork"), 0);
910 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
911 n_child_prepare(&nset
, fd0
, fd1
);
918 } else if (sh
== NULL
) {
919 pid
= n_child_start(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
921 pid
= n_child_start(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
929 if ((rv
= fdopen(myside
, mod
)) != NULL
)
930 register_file(rv
, 0, pid
,
931 (tiosp
== NULL
? FP_PIPE
: FP_PIPE
| FP_TERMIOS
),
932 NULL
, 0L, NULL
, tiosp
, osigint
);
936 if(rv
== NULL
&& tiosp
!= NULL
){
937 n_TERMCAP_RESUME(TRU1
);
938 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
940 n_signal(SIGINT
, osigint
);
947 Pclose(FILE *ptr
, bool_t dowait
)
950 struct termios
*tiosp
;
959 unregister_file(ptr
, &tiosp
, &osigint
);
964 rv
= n_child_wait(pid
, NULL
);
966 n_TERMCAP_RESUME(TRU1
);
967 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
968 n_signal(SIGINT
, osigint
);
986 char const *env_add
[2], *pager
;
990 assert(n_psonce
& n_PSO_INTERACTIVE
);
992 pager
= n_pager_get(env_add
+ 0);
995 if ((rv
= Popen(pager
, "w", NULL
, env_add
, n_CHILD_FD_PASS
)) == NULL
)
1002 n_pager_close(FILE *fp
)
1008 sh
= safe_signal(SIGPIPE
, SIG_IGN
);
1009 rv
= Pclose(fp
, TRU1
);
1010 safe_signal(SIGPIPE
, sh
);
1016 close_all_files(void)
1019 while (fp_head
!= NULL
)
1020 if ((fp_head
->flags
& FP_MASK
) == FP_PIPE
)
1021 Pclose(fp_head
->fp
, TRU1
);
1023 Fclose(fp_head
->fp
);
1028 n_child_run(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1029 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
1031 sigset_t nset
, oset
;
1032 sighandler_type soldint
;
1034 enum {a_NONE
= 0, a_INTIGN
= 1<<0, a_TTY
= 1<<1} f
;
1038 n_UNINIT(soldint
, SIG_ERR
);
1040 /* TODO Of course this is a joke given that during a "p*" the PAGER may
1041 * TODO be up and running while we play around like this... but i guess
1042 * TODO this can't be helped at all unless we perform complete and true
1043 * TODO process group separation and ensure we don't deadlock us out
1044 * TODO via TTY jobcontrol signal storms (could this really happen?).
1045 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
1046 * TODO any action, and shall we find we need to run programs dump it
1047 * TODO all into a temporary file which is then passed through to the
1048 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
1049 if(infd
== n_CHILD_FD_PASS
|| outfd
== n_CHILD_FD_PASS
){
1050 soldint
= safe_signal(SIGINT
, SIG_IGN
);
1053 if(n_psonce
& n_PSO_INTERACTIVE
){
1055 tcgetattr((n_psonce
& n_PSO_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
),
1057 n_TERMCAP_SUSPEND(FAL0
);
1059 sigdelset(&nset
, SIGCHLD
);
1060 sigdelset(&nset
, SIGINT
);
1061 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
1062 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1064 a_popen_jobsigs_up();
1068 if((rv
= n_child_start(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1071 if(n_child_wait(rv
, NULL
))
1074 if(ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1075 n_err(_("Fatal error in process\n"));
1081 a_popen_jobsigs_down();
1082 n_TERMCAP_RESUME(a_popen_hadsig
? TRU1
: FAL0
);
1083 tcsetattr(((n_psonce
& n_PSO_TTYIN
) ? STDIN_FILENO
: STDOUT_FILENO
),
1084 ((n_psonce
& n_PSO_TTYIN
) ? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1085 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1088 if(soldint
!= SIG_IGN
)
1089 safe_signal(SIGINT
, soldint
);
1096 n_child_start(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1097 char const *a0
, char const *a1
, char const *a2
,
1098 char const **env_addon
)
1103 if ((rv
= n_child_fork()) == -1) {
1104 n_perr(_("fork"), 0);
1106 } else if (rv
== 0) {
1110 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1111 extern char **environ
;
1112 size_t ei
, ei_orig
, ai
, ai_orig
;
1115 /* TODO note we don't check the POSIX limit:
1116 * the total space used to store the environment and the arguments to
1117 * the process is limited to {ARG_MAX} bytes */
1118 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1121 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1124 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1125 memcpy(env
, environ
, sizeof(*env
) * ei
);
1127 /* Replace all those keys that yet exist */
1129 char const *ee
, *kvs
;
1133 kvs
= strchr(ee
, '=');
1134 assert(kvs
!= NULL
);
1135 kl
= PTR2SIZE(kvs
- ee
);
1137 for (ei
= ei_orig
; ei
-- > 0;) {
1138 char const *ekvs
= strchr(env
[ei
], '=');
1139 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1140 !memcmp(ee
, env
[ei
], kl
)) {
1141 env
[ei
] = n_UNCONST(ee
);
1142 env_addon
[ai
] = NULL
;
1148 /* And append the rest */
1149 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1150 if (env_addon
[ai
] != NULL
)
1151 env
[ei
++] = n_UNCONST(env_addon
[ai
]);
1157 i
= (int)getrawlist(TRU1
, argv
, n_NELEM(argv
), cmd
, strlen(cmd
));
1159 if ((argv
[i
++] = n_UNCONST(a0
)) != NULL
&&
1160 (argv
[i
++] = n_UNCONST(a1
)) != NULL
&&
1161 (argv
[i
++] = n_UNCONST(a2
)) != NULL
)
1163 n_child_prepare(mask
, infd
, outfd
);
1164 execvp(argv
[0], argv
);
1178 cp
= a_popen_child_find(0, TRU1
);
1180 if((cp
->pid
= pid
= fork()) == -1){
1181 a_popen_child_del(cp
);
1182 n_perr(_("fork"), 0);
1189 n_child_prepare(sigset_t
*nset
, int infd
, int outfd
)
1195 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1196 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1197 if ((i
= (infd
== n_CHILD_FD_NULL
)))
1198 infd
= open("/dev/null", O_RDONLY
);
1200 dup2(infd
, STDIN_FILENO
);
1205 if ((i
= (outfd
== n_CHILD_FD_NULL
)))
1206 outfd
= open("/dev/null", O_WRONLY
);
1208 dup2(outfd
, STDOUT_FILENO
);
1214 for (i
= 1; i
< NSIG
; ++i
)
1215 if (sigismember(nset
, i
))
1216 safe_signal(i
, SIG_IGN
);
1217 if (!sigismember(nset
, SIGINT
))
1218 safe_signal(SIGINT
, SIG_DFL
);
1222 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1227 n_child_free(int pid
){
1228 sigset_t nset
, oset
;
1233 sigaddset(&nset
, SIGCHLD
);
1234 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1236 if((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
){
1238 a_popen_child_del(cp
);
1243 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1248 n_child_wait(int pid
, int *wait_status
){
1249 sigset_t nset
, oset
;
1256 sigaddset(&nset
, SIGCHLD
);
1257 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1259 if((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
){
1263 a_popen_child_del(cp
);
1267 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1269 if(wait_status
!= NULL
)
1271 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);