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
64 /* TODO FP_UNLINK: should be in a separated process so that unlinking
65 * TODO the temporary "garbage" is "safe"(r than it is like that) */
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 scan_mode(char const *mode
, int *omode
);
87 static void register_file(FILE *fp
, int omode
, int ispipe
, int pid
,
88 int flags
, char const *realfile
, long offset
,
89 char const *save_cmd
);
90 static enum okay
_file_save(struct fp
*fpp
);
91 static int _file_load(int flags
, int infd
, int outfd
,
92 char const *load_cmd
);
93 static enum okay
unregister_file(FILE *fp
);
94 static int file_pid(FILE *fp
);
96 /* TODO Rather temporary: deal with job control with FD_PASS */
97 static void a_popen_jobsigs_up(void);
98 static void a_popen_jobsigs_down(void);
99 static void a_popen_jobsig(int sig
);
102 static void _sigchld(int signo
);
104 static struct child
*_findchild(int pid
, bool_t create
);
105 static void _delchild(struct child
*cp
);
108 scan_mode(char const *mode
, int *omode
)
115 {"w", O_WRONLY
| O_CREAT
| O_TRUNC
},
116 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
117 {"a", O_WRONLY
| O_APPEND
| O_CREAT
},
118 {"a+", O_RDWR
| O_APPEND
},
120 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
126 for (i
= 0; UICMP(z
, i
, <, NELEM(maps
)); ++i
)
127 if (!strcmp(maps
[i
].mode
, mode
)) {
128 *omode
= maps
[i
].omode
;
133 n_alert(_("Internal error: bad stdio open mode %s"), mode
);
135 *omode
= 0; /* (silence CC) */
143 register_file(FILE *fp
, int omode
, int ispipe
, int pid
, int flags
,
144 char const *realfile
, long offset
, char const *save_cmd
)
149 assert(!(flags
& FP_UNLINK
) || realfile
!= NULL
);
151 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
->offset
= offset
;
166 _file_save(struct fp
*fpp
)
173 if (fpp
->omode
== O_RDONLY
) {
182 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
183 if (fseek(fpp
->fp
, fpp
->offset
, SEEK_SET
) == -1) {
185 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
186 fpp
->realfile
, strerror(outfd
));
189 rv
= maildir_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
193 /* Ensure the I/O library doesn't optimize the fseek(3) away! */
194 if(lseek(infd
= fileno(fpp
->fp
), fpp
->offset
, SEEK_SET
) == -1){
196 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
197 fpp
->realfile
, strerror(outfd
));
201 outfd
= open(fpp
->realfile
,
202 ((fpp
->omode
| O_CREAT
| (fpp
->omode
& O_APPEND
? 0 : O_TRUNC
))
206 n_err(_("Fatal: cannot create %s: %s\n"),
207 fpp
->realfile
, strerror(outfd
));
212 switch (fpp
->flags
& FP_MASK
) {
214 cmd
[0] = "gzip"; cmd
[1] = "-c"; break;
216 cmd
[0] = "bzip2"; cmd
[1] = "-c"; break;
218 cmd
[0] = "xz"; cmd
[1] = "-c"; break;
220 cmd
[0] = "cat"; cmd
[1] = NULL
; break;
222 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
225 cmd
[2] = fpp
->save_cmd
;
227 if (run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
) >= 0)
237 _file_load(int flags
, int infd
, int outfd
, char const *load_cmd
)
244 switch (flags
& FP_MASK
) {
245 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
246 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
247 case FP_XZ
: cmd
[0] = "xz"; cmd
[1] = "-cd"; break;
248 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
250 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
260 rv
= run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
);
267 unregister_file(FILE *fp
)
273 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
275 if ((p
->flags
& FP_MASK
) != FP_RAW
) /* TODO ;} */
277 if (p
->flags
& FP_UNLINK
&& unlink(p
->realfile
))
280 if (p
->save_cmd
!= NULL
)
282 if (p
->realfile
!= NULL
)
287 DBGOR(n_panic
, n_alert
)(_("Invalid file pointer"));
302 for (p
= fp_head
; p
; p
= p
->link
)
312 a_popen_jobsigs_up(void){
318 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
319 a_popen_otstp
= safe_signal(SIGTSTP
, &a_popen_jobsig
);
320 a_popen_ottin
= safe_signal(SIGTTIN
, &a_popen_jobsig
);
321 a_popen_ottou
= safe_signal(SIGTTOU
, &a_popen_jobsig
);
323 /* This assumes oset contains nothing but SIGCHLD, so to say */
324 sigdelset(&oset
, SIGTSTP
);
325 sigdelset(&oset
, SIGTTIN
);
326 sigdelset(&oset
, SIGTTOU
);
327 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
332 a_popen_jobsigs_down(void){
338 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
339 safe_signal(SIGTSTP
, a_popen_otstp
);
340 safe_signal(SIGTTIN
, a_popen_ottin
);
341 safe_signal(SIGTTOU
, a_popen_ottou
);
343 sigaddset(&oset
, SIGTSTP
);
344 sigaddset(&oset
, SIGTTIN
);
345 sigaddset(&oset
, SIGTTOU
);
346 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
351 a_popen_jobsig(int sig
){
352 sighandler_type oldact
;
355 NYD_X
; /* Signal handler */
357 hadsig
= (a_popen_hadsig
!= 0);
360 oldact
= safe_signal(sig
, SIG_DFL
);
363 sigaddset(&nset
, sig
);
364 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
366 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
368 safe_signal(sig
, oldact
);
377 NYD_X
; /* Signal handler */
381 pid
= waitpid(-1, &status
, WNOHANG
);
383 if (pid
== -1 && errno
== EINTR
)
388 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
390 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
399 static struct child
*
400 _findchild(int pid
, bool_t create
)
405 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
409 if (*cpp
== NULL
&& create
) {
410 *cpp
= smalloc(sizeof **cpp
);
412 (*cpp
)->done
= (*cpp
)->free
= 0;
420 _delchild(struct child
*cp
)
432 if (*(cpp
= &(*cpp
)->link
) == NULL
) {
433 DBG( n_err("! popen.c:_delchild(): implementation error\n"); )
441 command_manager_start(void)
443 struct sigaction nact
, oact
;
446 nact
.sa_handler
= &_sigchld
;
447 sigemptyset(&nact
.sa_mask
);
456 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
457 n_panic(_("Cannot install signal handler for child process management"));
462 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
466 NYD2_ENTER
; /* (only for Fopen() and once in lex.c) */
468 if (scan_mode(oflags
, &osflags
) < 0)
470 osflags
|= _O_CLOEXEC
;
474 if ((fd
= open(file
, osflags
, 0666)) == -1)
478 fp
= fdopen(fd
, oflags
);
485 Fopen(char const *file
, char const *oflags
)
491 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
492 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
498 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
504 scan_mode(oflags
, &osflags
);
506 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
508 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
509 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
520 if (unregister_file(fp
) == OKAY
)
525 return (i
== 3 ? 0 : EOF
);
529 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
532 char const *cload
= NULL
, *csave
= NULL
;
533 int flags
, osflags
, mode
, infd
;
539 if (scan_mode(oflags
, &osflags
) < 0)
543 rof
= OF_RDWR
| OF_UNLINK
;
544 if (osflags
& O_APPEND
)
546 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
548 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_MAILDIR
)) {
550 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
555 if ((ext
= strrchr(file
, '.')) != NULL
) {
556 if (!asccasecmp(ext
, ".gz"))
558 else if (!asccasecmp(ext
, ".xz")) {
560 osflags
&= ~O_APPEND
;
562 } else if (!asccasecmp(ext
, ".bz2")) {
564 osflags
&= ~O_APPEND
;
568 #define _X1 "file-hook-load-"
570 #define _X2 "file-hook-save-"
571 size_t l
= strlen(++ext
);
572 char *vbuf
= ac_alloc(l
+ MAX(sizeof(_X1
), sizeof(_X2
)));
574 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
575 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
576 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
577 cload
= vok_vlook(vbuf
);
578 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
579 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
580 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
581 csave
= vok_vlook(vbuf
);
586 if ((csave
!= NULL
) && (cload
!= NULL
)) {
588 osflags
&= ~O_APPEND
;
590 } else if ((csave
!= NULL
) | (cload
!= NULL
)) {
591 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
592 "Treating as plain text!"), ext
);
600 rv
= Fopen(file
, oflags
);
604 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
605 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
609 /* Note rv is not yet register_file()d, fclose() it in error path! */
610 if ((rv
= Ftmp(NULL
, "zopen", rof
)) == NULL
) {
611 n_perr(_("tmpfile"), 0);
615 if (flags
& FP_MAILDIR
)
617 else if (infd
>= 0) {
618 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
628 if ((infd
= creat(file
, 0666)) == -1) {
639 if (!(osflags
& O_APPEND
))
641 if ((offset
= ftell(rv
)) == -1) {
647 register_file(rv
, osflags
, 0, 0, flags
, file
, offset
, csave
);
654 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
656 /* The 8 is arbitrary but leaves room for a six character suffix (the
657 * POSIX minimum path length is 14, though we don't check that XXX).
658 * 8 should be more than sufficient given that we use base64url encoding
659 * for our random string */
660 enum {_RANDCHARS
= 8};
662 size_t maxname
, xlen
, i
;
669 assert(namehint
!= NULL
);
670 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
671 assert(!(oflags
& OF_RDONLY
));
672 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
681 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
682 maxname
= (size_t)pc
;
686 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
687 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
688 errno
= ENAMETOOLONG
;
694 /* Prepare the template string once, then iterate over the random range */
696 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
697 cp
= sstpcpy(cp
, tempdir
);
700 char *x
= sstpcpy(cp
, UAGENT
);
702 if (!(oflags
& OF_SUFFIX
))
703 x
= sstpcpy(x
, namehint
);
705 i
= PTR2SIZE(x
- cp
);
706 if (i
> maxname
- xlen
- _RANDCHARS
) {
707 size_t j
= maxname
- xlen
- _RANDCHARS
;
712 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
713 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
715 x
[xlen
+ _RANDCHARS
] = '\0';
719 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
720 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
721 if (oflags
& OF_APPEND
)
722 osoflags
|= O_APPEND
;
725 memcpy(cp
, getrandstring(_RANDCHARS
), _RANDCHARS
);
730 if ((fd
= open(cp_base
, osoflags
, 0600)) != -1) {
734 if (i
>= FTMP_OPEN_TRIES
) {
742 if (oflags
& OF_REGISTER
) {
743 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
746 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
747 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
748 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0, 0,
749 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
752 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
754 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
765 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
772 if ((cp
= cp_base
) != NULL
)
778 Ftmp_release(char **fn
)
794 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
807 pipe_cloexec(int fd
[2])
813 if (pipe2(fd
, O_CLOEXEC
) == -1)
818 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
819 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
828 Popen(char const *cmd
, char const *mode
, char const *sh
,
829 char const **env_addon
, int newfd1
)
831 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
832 char mod
[2] = {'0', '\0'};
837 /* First clean up child structures */
839 struct child
**cpp
, *cp
;
842 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
844 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
845 if ((*cpp
)->pid
== -1) {
853 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
856 if (!pipe_cloexec(p
))
862 hisside
= fd1
= p
[WRITE
];
864 } else if (*mode
== 'W') {
866 hisside
= fd0
= p
[READ
];
871 hisside
= fd0
= p
[READ
];
878 if (cmd
== (char*)-1) {
879 if ((pid
= fork_child()) == -1)
880 n_perr(_("fork"), 0);
882 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
883 prepare_child(&nset
, fd0
, fd1
);
890 } else if (sh
== NULL
) {
891 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
893 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
901 if ((rv
= fdopen(myside
, mod
)) != NULL
)
902 register_file(rv
, 0, 1, pid
, FP_RAW
, NULL
, 0L, NULL
);
911 Pclose(FILE *ptr
, bool_t dowait
)
921 unregister_file(ptr
);
925 sigaddset(&nset
, SIGINT
);
926 sigaddset(&nset
, SIGHUP
);
927 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
928 rv
= wait_child(pid
, NULL
);
929 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
940 close_all_files(void)
943 while (fp_head
!= NULL
)
945 Pclose(fp_head
->fp
, TRU1
);
958 cp
= _findchild(0, TRU1
);
960 if ((cp
->pid
= pid
= fork()) == -1) {
962 n_perr(_("fork"), 0);
969 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
970 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
977 /* TODO Of course this is a joke given that during a "p*" the PAGER may
978 * TODO be up and running while we play around like this... but i guess
979 * TODO this can't be helped at all unless we perform complete and true
980 * TODO process group separation and ensure we don't deadlock us out
981 * TODO via TTY jobcontrol signal storms (could this really happen?).
982 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
983 * TODO any action, and shall we find we need to run programs dump it
984 * TODO all into a temporary file which is then passed through to the
985 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
986 if ((tio_set
= ((options
& OPT_INTERACTIVE
) &&
987 (infd
== COMMAND_FD_PASS
|| outfd
== COMMAND_FD_PASS
)))) {
988 tcgetattr((options
& OPT_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
), &a_popen_tios
);
990 sigdelset(&nset
, SIGCHLD
);
991 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
992 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
994 a_popen_jobsigs_up();
997 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1000 if (wait_child(rv
, NULL
))
1003 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1004 n_err(_("Fatal error in process\n"));
1010 a_popen_jobsigs_down();
1011 tio_set
= ((options
& OPT_TTYIN
) != 0);
1012 tcsetattr((tio_set
? STDIN_FILENO
: STDOUT_FILENO
),
1013 (tio_set
? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1014 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1021 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1022 char const *a0
, char const *a1
, char const *a2
,
1023 char const **env_addon
)
1028 if ((rv
= fork_child()) == -1) {
1029 n_perr(_("fork"), 0);
1031 } else if (rv
== 0) {
1035 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1036 extern char **environ
;
1037 size_t ei
, ei_orig
, ai
, ai_orig
;
1040 /* TODO note we don't check the POSIX limit:
1041 * the total space used to store the environment and the arguments to
1042 * the process is limited to {ARG_MAX} bytes */
1043 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1046 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1049 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1050 memcpy(env
, environ
, sizeof(*env
) * ei
);
1052 /* Replace all those keys that yet exist */
1054 char const *ee
, *kvs
;
1058 kvs
= strchr(ee
, '=');
1059 assert(kvs
!= NULL
);
1060 kl
= PTR2SIZE(kvs
- ee
);
1062 for (ei
= ei_orig
; ei
-- > 0;) {
1063 char const *ekvs
= strchr(env
[ei
], '=');
1064 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1065 !memcmp(ee
, env
[ei
], kl
)) {
1066 env
[ei
] = UNCONST(ee
);
1067 env_addon
[ai
] = NULL
;
1073 /* And append the rest */
1074 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1075 if (env_addon
[ai
] != NULL
)
1076 env
[ei
++] = UNCONST(env_addon
[ai
]);
1082 i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
1084 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
1085 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
1086 (argv
[i
++] = UNCONST(a2
)) != NULL
)
1088 prepare_child(mask
, infd
, outfd
);
1089 execvp(argv
[0], argv
);
1098 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
1104 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1105 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1106 if ((i
= (infd
== COMMAND_FD_NULL
)))
1107 infd
= open("/dev/null", O_RDONLY
);
1109 dup2(infd
, STDIN_FILENO
);
1114 if ((i
= (outfd
== COMMAND_FD_NULL
)))
1115 outfd
= open("/dev/null", O_WRONLY
);
1117 dup2(outfd
, STDOUT_FILENO
);
1123 for (i
= 1; i
< NSIG
; ++i
)
1124 if (sigismember(nset
, i
))
1125 safe_signal(i
, SIG_IGN
);
1126 if (!sigismember(nset
, SIGINT
))
1127 safe_signal(SIGINT
, SIG_DFL
);
1131 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1138 sigset_t nset
, oset
;
1143 sigaddset(&nset
, SIGCHLD
);
1144 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1146 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1153 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1158 wait_child(int pid
, int *wait_status
)
1160 sigset_t nset
, oset
;
1167 sigaddset(&nset
, SIGCHLD
);
1168 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1170 cp
= _findchild(pid
, FAL0
);
1179 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1181 if (wait_status
!= NULL
)
1183 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);