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 - 2018 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 FP_MASK
= (1u<<7) - 1,
57 /* TODO FP_UNLINK: should be in a separated process so that unlinking
58 * TODO the temporary "garbage" is "safe"(r than it is like that) */
66 struct termios
*fp_tios
;
67 n_sighdl_t fp_osigint
; /* Only if FP_TERMIOS */
78 static struct fp
*fp_head
;
79 static struct child
*_popen_child
;
81 /* TODO Rather temporary: deal with job control with FD_PASS */
82 static struct termios a_popen_tios
;
83 static sighandler_type a_popen_otstp
, a_popen_ottin
, a_popen_ottou
;
84 static volatile int a_popen_hadsig
;
86 static int a_popen_scan_mode(char const *mode
, int *omode
);
87 static void register_file(FILE *fp
, int omode
, int pid
,
88 int flags
, char const *realfile
, long offset
,
89 char const *save_cmd
, struct termios
*tiosp
,
91 static enum okay
_file_save(struct fp
*fpp
);
92 static int a_popen_file_load(int flags
, int infd
, int outfd
,
93 char const *load_cmd
);
94 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 a_popen_sigchld(int signo
);
106 static struct child
*a_popen_child_find(int pid
, bool_t create
);
107 static void a_popen_child_del(struct child
*cp
);
110 a_popen_scan_mode(char const *mode
, int *omode
){
116 {"w", O_WRONLY
| O_CREAT
| n_O_NOXY_BITS
| O_TRUNC
},
117 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
118 {"a", O_WRONLY
| O_APPEND
| O_CREAT
| n_O_NOXY_BITS
},
119 {"a+", O_RDWR
| O_APPEND
| O_CREAT
| n_O_NOXY_BITS
},
121 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
126 for(i
= 0; UICMP(z
, i
, <, n_NELEM(maps
)); ++i
)
127 if(!strcmp(maps
[i
].mode
, mode
)){
128 *omode
= maps
[i
].omode
;
133 DBG( n_alert(_("Internal error: bad stdio open mode %s"), mode
); )
134 n_err_no
= n_ERR_INVAL
;
135 *omode
= 0; /* (silence CC) */
143 register_file(FILE *fp
, int omode
, int pid
, int flags
,
144 char const *realfile
, long offset
, char const *save_cmd
,
145 struct termios
*tiosp
, n_sighdl_t osigint
)
150 assert(!(flags
& FP_UNLINK
) || realfile
!= NULL
);
151 assert(!(flags
& FP_TERMIOS
) || tiosp
!= NULL
);
153 fpp
= smalloc(sizeof *fpp
);
159 fpp
->realfile
= (realfile
!= NULL
) ? sstrdup(realfile
) : NULL
;
160 fpp
->save_cmd
= (save_cmd
!= NULL
) ? sstrdup(save_cmd
) : NULL
;
161 fpp
->fp_tios
= tiosp
;
162 fpp
->fp_osigint
= osigint
;
163 fpp
->offset
= offset
;
169 _file_save(struct fp
*fpp
)
176 if (fpp
->omode
== O_RDONLY
) {
185 /* Ensure the I/O library doesn't optimize the fseek(3) away! */
186 if(!n_real_seek(fpp
->fp
, fpp
->offset
, SEEK_SET
)){
188 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
189 n_shexp_quote_cp(fpp
->realfile
, FAL0
), n_err_to_doc(outfd
));
194 if ((fpp
->flags
& FP_MASK
) == FP_IMAP
) {
195 rv
= imap_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
200 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
201 rv
= maildir_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
205 outfd
= open(fpp
->realfile
,
206 ((fpp
->omode
| O_CREAT
| (fpp
->omode
& O_APPEND
? 0 : O_TRUNC
) |
207 n_O_NOXY_BITS
) & ~O_EXCL
), 0666);
210 n_err(_("Fatal: cannot create %s: %s\n"),
211 n_shexp_quote_cp(fpp
->realfile
, FAL0
), n_err_to_doc(outfd
));
216 switch(fpp
->flags
& FP_MASK
){
218 cmd
[0] = ok_vlook(SHELL
);
220 cmd
[2] = fpp
->save_cmd
;
227 if (n_child_run(cmd
[0], 0, fileno(fpp
->fp
), outfd
,
228 cmd
[1], cmd
[2], NULL
, NULL
, NULL
) >= 0)
238 a_popen_file_load(int flags
, int infd
, int outfd
, char const *load_cmd
){
244 switch(flags
& FP_MASK
){
250 cmd
[0] = ok_vlook(SHELL
);
260 rv
= n_child_run(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
, NULL
);
267 unregister_file(FILE *fp
, struct termios
**tiosp
, n_sighdl_t
*osigint
)
276 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
278 switch (p
->flags
& FP_MASK
) {
286 if ((p
->flags
& FP_UNLINK
) && unlink(p
->realfile
))
290 if (p
->save_cmd
!= NULL
)
292 if (p
->realfile
!= NULL
)
294 if (p
->flags
& FP_TERMIOS
) {
297 *osigint
= p
->fp_osigint
;
304 DBGOR(n_panic
, n_alert
)(_("Invalid file pointer"));
319 for (p
= fp_head
; p
; p
= p
->link
)
329 a_popen_jobsigs_up(void){
335 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
336 a_popen_otstp
= safe_signal(SIGTSTP
, &a_popen_jobsig
);
337 a_popen_ottin
= safe_signal(SIGTTIN
, &a_popen_jobsig
);
338 a_popen_ottou
= safe_signal(SIGTTOU
, &a_popen_jobsig
);
340 /* This assumes oset contains nothing but SIGCHLD, so to say */
341 sigdelset(&oset
, SIGTSTP
);
342 sigdelset(&oset
, SIGTTIN
);
343 sigdelset(&oset
, SIGTTOU
);
344 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
349 a_popen_jobsigs_down(void){
355 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
356 safe_signal(SIGTSTP
, a_popen_otstp
);
357 safe_signal(SIGTTIN
, a_popen_ottin
);
358 safe_signal(SIGTTOU
, a_popen_ottou
);
360 sigaddset(&oset
, SIGTSTP
);
361 sigaddset(&oset
, SIGTTIN
);
362 sigaddset(&oset
, SIGTTOU
);
363 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
368 a_popen_jobsig(int sig
){
369 sighandler_type oldact
;
372 NYD_X
; /* Signal handler */
374 hadsig
= (a_popen_hadsig
!= 0);
377 n_TERMCAP_SUSPEND(TRU1
);
379 oldact
= safe_signal(sig
, SIG_DFL
);
382 sigaddset(&nset
, sig
);
383 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
385 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
387 safe_signal(sig
, oldact
);
391 a_popen_sigchld(int signo
){
395 NYD_X
; /* Signal handler */
399 pid
= waitpid(-1, &status
, WNOHANG
);
401 if (pid
== -1 && n_err_no
== n_ERR_INTR
)
406 if ((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
) {
409 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
417 static struct child
*
418 a_popen_child_find(int pid
, bool_t create
){
419 struct child
**cpp
, *cp
;
422 for(cpp
= &_popen_child
; (cp
= *cpp
) != NULL
&& cp
->pid
!= pid
;
426 if(cp
== NULL
&& create
)
427 (*cpp
= cp
= scalloc(1, sizeof *cp
))->pid
= pid
;
433 a_popen_child_del(struct child
*cp
){
445 if(*(cpp
= &(*cpp
)->link
) == NULL
){
446 DBG( n_err("! a_popen_child_del(): implementation error\n"); )
454 n_child_manager_start(void)
456 struct sigaction nact
, oact
;
459 nact
.sa_handler
= &a_popen_sigchld
;
460 sigemptyset(&nact
.sa_mask
);
461 nact
.sa_flags
= SA_RESTART
466 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
467 n_panic(_("Cannot install signal handler for child process management"));
472 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
476 NYD2_ENTER
; /* (only for Fopen() and once in go.c) */
478 if (a_popen_scan_mode(oflags
, &osflags
) < 0)
480 osflags
|= _O_CLOEXEC
;
484 if ((fd
= open(file
, osflags
, 0666)) == -1)
488 fp
= fdopen(fd
, oflags
);
495 Fopen(char const *file
, char const *oflags
)
501 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
502 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
,NULL
);
508 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
514 a_popen_scan_mode(oflags
, &osflags
);
516 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
518 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
519 register_file(fp
, osflags
, 0, FP_RAW
, NULL
, 0L, NULL
, NULL
,NULL
);
530 if (unregister_file(fp
, NULL
, NULL
) == OKAY
)
535 return (i
== 3 ? 0 : EOF
);
539 n_fopen_any(char const *file
, char const *oflags
, /* TODO should take flags */
540 enum n_fopen_state
*fs_or_null
){ /* TODO as bits, return state */
541 /* TODO Support file locking upon open time */
545 int osflags
, flags
, omode
, infd
;
546 char const *cload
, *csave
;
547 enum n_fopen_state fs
;
552 fs
= n_FOPEN_STATE_NONE
;
553 cload
= csave
= NULL
;
555 if(a_popen_scan_mode(oflags
, &osflags
) < 0)
559 rof
= OF_RDWR
| OF_UNLINK
;
560 if(osflags
& O_APPEND
)
562 omode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
564 /* We don't want to find mbox.bz2 when doing "copy * mbox", but only for
565 * "file mbox", so don't try hooks when writing */
566 p
= which_protocol(csave
= file
, TRU1
, ((omode
& W_OK
) == 0), &file
);
567 fs
= (enum n_fopen_state
)p
;
575 osflags
= O_RDWR
| O_APPEND
| O_CREAT
| n_O_NOXY_BITS
;
579 n_err_no
= n_ERR_OPNOTSUPP
;
582 case n_PROTO_MAILDIR
:
583 if(fs_or_null
!= NULL
&& !access(file
, F_OK
))
584 fs
|= n_FOPEN_STATE_EXISTS
;
586 osflags
= O_RDWR
| O_APPEND
| O_CREAT
| n_O_NOXY_BITS
;
590 struct n_file_type ft
;
592 if(!(osflags
& O_EXCL
) && fs_or_null
!= NULL
&& !access(file
, F_OK
))
593 fs
|= n_FOPEN_STATE_EXISTS
;
595 if(n_filetype_exists(&ft
, file
)){
597 cload
= ft
.ft_load_dat
;
598 csave
= ft
.ft_save_dat
;
599 /* Cause truncation for compressor/hook output files */
600 osflags
&= ~O_APPEND
;
602 if((infd
= open(file
, (omode
& W_OK
? O_RDWR
: O_RDONLY
))) != -1)
603 fs
|= n_FOPEN_STATE_EXISTS
;
604 else if(!(osflags
& O_CREAT
) || n_err_no
!= n_ERR_NOENT
)
608 rv
= Fopen(file
, oflags
);
609 if((osflags
& O_EXCL
) && rv
== NULL
)
610 fs
|= n_FOPEN_STATE_EXISTS
;
616 /* Note rv is not yet register_file()d, fclose() it in error path! */
617 if((rv
= Ftmp(NULL
, "fopenany", rof
)) == NULL
){
618 n_perr(_("tmpfile"), 0);
622 if(flags
& (FP_IMAP
| FP_MAILDIR
))
625 if(a_popen_file_load(flags
, infd
, fileno(rv
), cload
) < 0){
635 if((infd
= creat(file
, 0666)) == -1){
646 if(!(osflags
& O_APPEND
))
648 if((offset
= ftell(rv
)) == -1){
654 register_file(rv
, osflags
, 0, flags
, file
, offset
, csave
, NULL
,NULL
);
656 if(fs_or_null
!= NULL
)
663 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
665 /* The 8 is arbitrary but leaves room for a six character suffix (the
666 * POSIX minimum path length is 14, though we don't check that XXX).
667 * 8 should be more than sufficient given that we use base64url encoding
668 * for our random string */
669 enum {_RANDCHARS
= 8u};
672 size_t maxname
, xlen
, i
;
679 assert(namehint
!= NULL
);
680 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
681 assert(!(oflags
& OF_RDONLY
));
682 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
687 tmpdir
= ok_vlook(TMPDIR
);
692 if ((pc
= pathconf(tmpdir
, _PC_NAME_MAX
)) != -1)
693 maxname
= (size_t)pc
;
697 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
698 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
699 n_err_no
= n_ERR_NAMETOOLONG
;
705 /* Prepare the template string once, then iterate over the random range */
707 cp
= smalloc(strlen(tmpdir
) + 1 + maxname
+1);
708 cp
= sstpcpy(cp
, tmpdir
);
711 char *x
= sstpcpy(cp
, VAL_UAGENT
);
713 if (!(oflags
& OF_SUFFIX
))
714 x
= sstpcpy(x
, namehint
);
716 i
= PTR2SIZE(x
- cp
);
717 if (i
> maxname
- xlen
- _RANDCHARS
) {
718 size_t j
= maxname
- xlen
- _RANDCHARS
;
722 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
723 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
725 x
[xlen
+ _RANDCHARS
] = '\0';
729 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
730 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
731 if (oflags
& OF_APPEND
)
732 osoflags
|= O_APPEND
;
734 for(relesigs
= TRU1
, i
= 0;; ++i
){
735 memcpy(cp
, n_random_create_cp(_RANDCHARS
, NULL
), _RANDCHARS
);
739 if((fd
= open(cp_base
, osoflags
, 0600)) != -1){
743 if(i
>= FTMP_OPEN_TRIES
){
750 if (oflags
& OF_REGISTER
) {
751 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
754 a_popen_scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
755 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
756 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0,
757 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
758 cp_base
, 0L, NULL
, NULL
,NULL
);
760 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
762 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
773 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
780 if ((cp
= cp_base
) != NULL
)
786 Ftmp_release(char **fn
)
802 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
815 pipe_cloexec(int fd
[2]){
822 if(pipe2(fd
, O_CLOEXEC
) != -1)
826 n_fd_cloexec_set(fd
[0]);
827 n_fd_cloexec_set(fd
[1]);
836 Popen(char const *cmd
, char const *mode
, char const *sh
,
837 char const **env_addon
, int newfd1
)
839 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
843 struct termios
*tiosp
;
847 /* First clean up child structures */
849 struct child
**cpp
, *cp
;
852 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
853 if ((*cpp
)->pid
== -1) {
865 n_UNINIT(osigint
, SIG_ERR
);
866 mod
[0] = '0', mod
[1] = '\0';
868 if (!pipe_cloexec(p
))
873 fd0
= n_CHILD_FD_PASS
;
874 hisside
= fd1
= p
[WRITE
];
876 } else if (*mode
== 'W') {
878 hisside
= fd0
= p
[READ
];
883 hisside
= fd0
= p
[READ
];
884 fd1
= n_CHILD_FD_PASS
;
888 /* In interactive mode both STDIN and STDOUT point to the terminal. If we
889 * pass through the TTY restore terminal attributes after pipe returns.
890 * XXX It shouldn't matter which FD we actually use in this case */
891 if ((n_psonce
& n_PSO_INTERACTIVE
) && (fd0
== n_CHILD_FD_PASS
||
892 fd1
== n_CHILD_FD_PASS
)) {
893 osigint
= n_signal(SIGINT
, SIG_IGN
);
894 tiosp
= smalloc(sizeof *tiosp
);
895 tcgetattr(STDIN_FILENO
, tiosp
);
896 n_TERMCAP_SUSPEND(TRU1
);
901 if (cmd
== (char*)-1) {
902 if ((pid
= n_child_fork()) == -1)
903 n_perr(_("fork"), 0);
905 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
906 n_child_prepare(&nset
, fd0
, fd1
);
909 /* TODO should close all other open FDs except stds and reset memory */
910 /* Standard I/O drives me insane! All we need is a sync operation
911 * that causes n_stdin to forget about any read buffer it may have.
912 * We cannot use fflush(3), this works with Musl and Solaris, but not
913 * with GlibC. (For at least pipes.) We cannot use fdreopen(),
914 * because this function does not exist! Luckily (!!!) we only use
915 * n_stdin not stdin in our child, otherwise all bets were off!
916 * TODO (Unless we would fiddle around with FILE* directly:
917 * TODO #ifdef __GLIBC__
918 * TODO n_stdin->_IO_read_ptr = n_stdin->_IO_read_end;
920 * TODO n_stdin->_r = 0;
921 * TODO #elif n_OS_SOLARIS || n_OS_SUNOS
922 * TODO n_stdin->_cnt = 0;
924 * TODO ) which should have additional config test for sure! */
925 n_stdin
= fdopen(STDIN_FILENO
, "r");
926 /*n_stdout = fdopen(STDOUT_FILENO, "w");*/
927 /*n_stderr = fdopen(STDERR_FILENO, "w");*/
933 } else if (sh
== NULL
) {
934 pid
= n_child_start(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
936 pid
= n_child_start(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
944 if ((rv
= fdopen(myside
, mod
)) != NULL
)
945 register_file(rv
, 0, pid
,
946 (tiosp
== NULL
? FP_PIPE
: FP_PIPE
| FP_TERMIOS
),
947 NULL
, 0L, NULL
, tiosp
, osigint
);
951 if(rv
== NULL
&& tiosp
!= NULL
){
952 n_TERMCAP_RESUME(TRU1
);
953 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
955 n_signal(SIGINT
, osigint
);
962 Pclose(FILE *ptr
, bool_t dowait
)
965 struct termios
*tiosp
;
974 unregister_file(ptr
, &tiosp
, &osigint
);
979 rv
= n_child_wait(pid
, NULL
);
981 n_TERMCAP_RESUME(TRU1
);
982 tcsetattr(STDIN_FILENO
, TCSAFLUSH
, tiosp
);
983 n_signal(SIGINT
, osigint
);
999 n_psignal(FILE *fp
, int sig
){
1003 if((rv
= file_pid(fp
)) >= 0){
1006 if((cp
= a_popen_child_find(rv
, FAL0
)) != NULL
){
1007 if((rv
= kill(rv
, sig
)) != 0)
1019 char const *env_add
[2], *pager
;
1023 assert(n_psonce
& n_PSO_INTERACTIVE
);
1025 pager
= n_pager_get(env_add
+ 0);
1028 if ((rv
= Popen(pager
, "w", NULL
, env_add
, n_CHILD_FD_PASS
)) == NULL
)
1035 n_pager_close(FILE *fp
)
1041 sh
= safe_signal(SIGPIPE
, SIG_IGN
);
1042 rv
= Pclose(fp
, TRU1
);
1043 safe_signal(SIGPIPE
, sh
);
1049 close_all_files(void)
1052 while (fp_head
!= NULL
)
1053 if ((fp_head
->flags
& FP_MASK
) == FP_PIPE
)
1054 Pclose(fp_head
->fp
, TRU1
);
1056 Fclose(fp_head
->fp
);
1060 /* TODO The entire n_child_ series should be replaced with an object, but
1061 * TODO at least have carrier arguments. We anyway need a command manager
1062 * TODO that keeps track and knows how to handle job control ++++! */
1065 n_child_run(char const *cmd
, sigset_t
*mask_or_null
, int infd
, int outfd
,
1066 char const *a0_or_null
, char const *a1_or_null
, char const *a2_or_null
,
1067 char const **env_addon_or_null
, int *wait_status_or_null
)
1069 sigset_t nset
, oset
;
1070 sighandler_type soldint
;
1072 enum {a_NONE
= 0, a_INTIGN
= 1<<0, a_TTY
= 1<<1} f
;
1076 n_UNINIT(soldint
, SIG_ERR
);
1078 /* TODO Of course this is a joke given that during a "p*" the PAGER may
1079 * TODO be up and running while we play around like this... but i guess
1080 * TODO this can't be helped at all unless we perform complete and true
1081 * TODO process group separation and ensure we don't deadlock us out
1082 * TODO via TTY jobcontrol signal storms (could this really happen?).
1083 * TODO Or have a built-in pager. Or query any necessity BEFORE we start
1084 * TODO any action, and shall we find we need to run programs dump it
1085 * TODO all into a temporary file which is then passed through to the
1086 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
1087 if(infd
== n_CHILD_FD_PASS
|| outfd
== n_CHILD_FD_PASS
){
1088 soldint
= safe_signal(SIGINT
, SIG_IGN
);
1091 if(n_psonce
& n_PSO_INTERACTIVE
){
1093 tcgetattr((n_psonce
& n_PSO_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
),
1095 n_TERMCAP_SUSPEND(FAL0
);
1097 sigdelset(&nset
, SIGCHLD
);
1098 sigdelset(&nset
, SIGINT
);
1099 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
1100 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1102 a_popen_jobsigs_up();
1106 if((rv
= n_child_start(cmd
, mask_or_null
, infd
, outfd
, a0_or_null
,
1107 a1_or_null
, a2_or_null
, env_addon_or_null
)) < 0){
1114 if(n_child_wait(rv
, &ws
))
1116 else if(wait_status_or_null
== NULL
|| !WIFEXITED(ws
)){
1117 if(ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1118 n_err(_("Fatal error in process\n"));
1122 if(wait_status_or_null
!= NULL
)
1123 *wait_status_or_null
= ws
;
1127 a_popen_jobsigs_down();
1128 n_TERMCAP_RESUME(a_popen_hadsig
? TRU1
: FAL0
);
1129 tcsetattr(((n_psonce
& n_PSO_TTYIN
) ? STDIN_FILENO
: STDOUT_FILENO
),
1130 ((n_psonce
& n_PSO_TTYIN
) ? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1131 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1134 if(soldint
!= SIG_IGN
)
1135 safe_signal(SIGINT
, soldint
);
1145 n_child_start(char const *cmd
, sigset_t
*mask_or_null
, int infd
, int outfd
,
1146 char const *a0_or_null
, char const *a1_or_null
, char const *a2_or_null
,
1147 char const **env_addon_or_null
)
1152 if ((rv
= n_child_fork()) == -1) {
1154 n_perr(_("fork"), 0);
1157 } else if (rv
== 0) {
1161 if (env_addon_or_null
!= NULL
) {
1162 extern char **environ
;
1163 size_t ei
, ei_orig
, ai
, ai_orig
;
1166 /* TODO note we don't check the POSIX limit:
1167 * the total space used to store the environment and the arguments to
1168 * the process is limited to {ARG_MAX} bytes */
1169 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1172 for (ai
= 0; env_addon_or_null
[ai
] != NULL
; ++ai
)
1175 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1176 memcpy(env
, environ
, sizeof(*env
) * ei
);
1178 /* Replace all those keys that yet exist */
1180 char const *ee
, *kvs
;
1183 ee
= env_addon_or_null
[ai
];
1184 kvs
= strchr(ee
, '=');
1185 assert(kvs
!= NULL
);
1186 kl
= PTR2SIZE(kvs
- ee
);
1188 for (ei
= ei_orig
; ei
-- > 0;) {
1189 char const *ekvs
= strchr(env
[ei
], '=');
1190 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1191 !memcmp(ee
, env
[ei
], kl
)) {
1192 env
[ei
] = n_UNCONST(ee
);
1193 env_addon_or_null
[ai
] = NULL
;
1199 /* And append the rest */
1200 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1201 if (env_addon_or_null
[ai
] != NULL
)
1202 env
[ei
++] = n_UNCONST(env_addon_or_null
[ai
]);
1208 i
= (int)getrawlist(TRU1
, argv
, n_NELEM(argv
), cmd
, strlen(cmd
));
1210 if ((argv
[i
++] = n_UNCONST(a0_or_null
)) != NULL
&&
1211 (argv
[i
++] = n_UNCONST(a1_or_null
)) != NULL
&&
1212 (argv
[i
++] = n_UNCONST(a2_or_null
)) != NULL
)
1214 n_child_prepare(mask_or_null
, infd
, outfd
);
1215 execvp(argv
[0], argv
);
1225 /* Strictly speaking we should do so in the waitpid(2) case too, but since
1226 * we explicitly waitpid(2) on the pid if just the structure exists, which
1227 * n_child_wait() does in the parent, all is fine */
1228 #if n_SIGSUSPEND_NOT_WAITPID
1229 sigset_t nset
, oset
;
1235 #if n_SIGSUSPEND_NOT_WAITPID
1237 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1240 cp
= a_popen_child_find(0, TRU1
);
1242 if((cp
->pid
= pid
= fork()) == -1){
1243 a_popen_child_del(cp
);
1244 n_perr(_("fork"), 0);
1247 #if n_SIGSUSPEND_NOT_WAITPID
1248 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1255 n_child_prepare(sigset_t
*nset_or_null
, int infd
, int outfd
)
1261 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1262 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1263 if ((i
= (infd
== n_CHILD_FD_NULL
)))
1264 infd
= open(n_path_devnull
, O_RDONLY
);
1266 dup2(infd
, STDIN_FILENO
);
1271 if ((i
= (outfd
== n_CHILD_FD_NULL
)))
1272 outfd
= open(n_path_devnull
, O_WRONLY
);
1274 dup2(outfd
, STDOUT_FILENO
);
1279 if (nset_or_null
!= NULL
) {
1280 for (i
= 1; i
< NSIG
; ++i
)
1281 if (sigismember(nset_or_null
, i
))
1282 safe_signal(i
, SIG_IGN
);
1283 if (!sigismember(nset_or_null
, SIGINT
))
1284 safe_signal(SIGINT
, SIG_DFL
);
1288 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1293 n_child_free(int pid
){
1294 sigset_t nset
, oset
;
1299 sigaddset(&nset
, SIGCHLD
);
1300 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1302 if((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
){
1304 a_popen_child_del(cp
);
1309 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1314 n_child_wait(int pid
, int *wait_status_or_null
){
1315 #if !n_SIGSUSPEND_NOT_WAITPID
1324 #if !n_SIGSUSPEND_NOT_WAITPID
1326 sigaddset(&nset
, SIGCHLD
);
1327 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1330 if((cp
= a_popen_child_find(pid
, FAL0
)) != NULL
){
1331 #if n_SIGSUSPEND_NOT_WAITPID
1333 sigdelset(&nset
, SIGCHLD
);
1335 sigsuspend(&nset
); /* TODO we should allow more than SIGCHLD!! */
1338 waitpid(pid
, &ws
, 0);
1340 a_popen_child_del(cp
);
1344 #if !n_SIGSUSPEND_NOT_WAITPID
1345 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1348 if(wait_status_or_null
!= NULL
)
1349 *wait_status_or_null
= ws
;
1350 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);