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 n_TERMCAP_SUSPEND(TRU1
);
362 oldact
= safe_signal(sig
, SIG_DFL
);
365 sigaddset(&nset
, sig
);
366 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
368 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
370 safe_signal(sig
, oldact
);
379 NYD_X
; /* Signal handler */
383 pid
= waitpid(-1, &status
, WNOHANG
);
385 if (pid
== -1 && errno
== EINTR
)
390 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
392 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
401 static struct child
*
402 _findchild(int pid
, bool_t create
)
407 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
411 if (*cpp
== NULL
&& create
) {
412 *cpp
= smalloc(sizeof **cpp
);
414 (*cpp
)->done
= (*cpp
)->free
= 0;
422 _delchild(struct child
*cp
)
434 if (*(cpp
= &(*cpp
)->link
) == NULL
) {
435 DBG( n_err("! popen.c:_delchild(): implementation error\n"); )
443 command_manager_start(void)
445 struct sigaction nact
, oact
;
448 nact
.sa_handler
= &_sigchld
;
449 sigemptyset(&nact
.sa_mask
);
458 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
459 n_panic(_("Cannot install signal handler for child process management"));
464 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
468 NYD2_ENTER
; /* (only for Fopen() and once in lex.c) */
470 if (scan_mode(oflags
, &osflags
) < 0)
472 osflags
|= _O_CLOEXEC
;
476 if ((fd
= open(file
, osflags
, 0666)) == -1)
480 fp
= fdopen(fd
, oflags
);
487 Fopen(char const *file
, char const *oflags
)
493 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
494 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
500 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
506 scan_mode(oflags
, &osflags
);
508 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
510 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
511 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
522 if (unregister_file(fp
) == OKAY
)
527 return (i
== 3 ? 0 : EOF
);
531 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
534 char const *cload
= NULL
, *csave
= NULL
;
535 int flags
, osflags
, mode
, infd
;
541 if (scan_mode(oflags
, &osflags
) < 0)
545 rof
= OF_RDWR
| OF_UNLINK
;
546 if (osflags
& O_APPEND
)
548 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
550 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_MAILDIR
)) {
552 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
557 if ((ext
= strrchr(file
, '.')) != NULL
) {
558 if (!asccasecmp(ext
, ".gz"))
560 else if (!asccasecmp(ext
, ".xz")) {
562 osflags
&= ~O_APPEND
;
564 } else if (!asccasecmp(ext
, ".bz2")) {
566 osflags
&= ~O_APPEND
;
570 #define _X1 "file-hook-load-"
572 #define _X2 "file-hook-save-"
573 size_t l
= strlen(++ext
);
574 char *vbuf
= ac_alloc(l
+ MAX(sizeof(_X1
), sizeof(_X2
)));
576 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
577 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
578 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
579 cload
= vok_vlook(vbuf
);
580 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
581 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
582 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
583 csave
= vok_vlook(vbuf
);
588 if ((csave
!= NULL
) && (cload
!= NULL
)) {
590 osflags
&= ~O_APPEND
;
592 } else if ((csave
!= NULL
) | (cload
!= NULL
)) {
593 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
594 "Treating as plain text!"), ext
);
602 rv
= Fopen(file
, oflags
);
606 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
607 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
611 /* Note rv is not yet register_file()d, fclose() it in error path! */
612 if ((rv
= Ftmp(NULL
, "zopen", rof
)) == NULL
) {
613 n_perr(_("tmpfile"), 0);
617 if (flags
& FP_MAILDIR
)
619 else if (infd
>= 0) {
620 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
630 if ((infd
= creat(file
, 0666)) == -1) {
641 if (!(osflags
& O_APPEND
))
643 if ((offset
= ftell(rv
)) == -1) {
649 register_file(rv
, osflags
, 0, 0, flags
, file
, offset
, csave
);
656 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
)
658 /* The 8 is arbitrary but leaves room for a six character suffix (the
659 * POSIX minimum path length is 14, though we don't check that XXX).
660 * 8 should be more than sufficient given that we use base64url encoding
661 * for our random string */
662 enum {_RANDCHARS
= 8};
664 size_t maxname
, xlen
, i
;
671 assert(namehint
!= NULL
);
672 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
673 assert(!(oflags
& OF_RDONLY
));
674 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
683 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
684 maxname
= (size_t)pc
;
688 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
689 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
690 errno
= ENAMETOOLONG
;
696 /* Prepare the template string once, then iterate over the random range */
698 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
699 cp
= sstpcpy(cp
, tempdir
);
702 char *x
= sstpcpy(cp
, UAGENT
);
704 if (!(oflags
& OF_SUFFIX
))
705 x
= sstpcpy(x
, namehint
);
707 i
= PTR2SIZE(x
- cp
);
708 if (i
> maxname
- xlen
- _RANDCHARS
) {
709 size_t j
= maxname
- xlen
- _RANDCHARS
;
714 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
715 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
717 x
[xlen
+ _RANDCHARS
] = '\0';
721 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
722 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
723 if (oflags
& OF_APPEND
)
724 osoflags
|= O_APPEND
;
727 memcpy(cp
, getrandstring(_RANDCHARS
), _RANDCHARS
);
732 if ((fd
= open(cp_base
, osoflags
, 0600)) != -1) {
736 if (i
>= FTMP_OPEN_TRIES
) {
744 if (oflags
& OF_REGISTER
) {
745 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
748 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
749 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
750 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0, 0,
751 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
754 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
756 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
767 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
774 if ((cp
= cp_base
) != NULL
)
780 Ftmp_release(char **fn
)
796 Ftmp_free(char **fn
) /* TODO DROP: OF_REGISTER_FREEPATH! */
809 pipe_cloexec(int fd
[2])
815 if (pipe2(fd
, O_CLOEXEC
) == -1)
820 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
821 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
830 Popen(char const *cmd
, char const *mode
, char const *sh
,
831 char const **env_addon
, int newfd1
)
833 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
834 char mod
[2] = {'0', '\0'};
839 /* First clean up child structures */
841 struct child
**cpp
, *cp
;
844 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
846 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
847 if ((*cpp
)->pid
== -1) {
855 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
858 if (!pipe_cloexec(p
))
864 hisside
= fd1
= p
[WRITE
];
866 } else if (*mode
== 'W') {
868 hisside
= fd0
= p
[READ
];
873 hisside
= fd0
= p
[READ
];
880 if (cmd
== (char*)-1) {
881 if ((pid
= fork_child()) == -1)
882 n_perr(_("fork"), 0);
884 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
885 prepare_child(&nset
, fd0
, fd1
);
892 } else if (sh
== NULL
) {
893 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
895 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
903 if ((rv
= fdopen(myside
, mod
)) != NULL
)
904 register_file(rv
, 0, 1, pid
, FP_RAW
, NULL
, 0L, NULL
);
913 Pclose(FILE *ptr
, bool_t dowait
)
923 unregister_file(ptr
);
927 sigaddset(&nset
, SIGINT
);
928 sigaddset(&nset
, SIGHUP
);
929 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
930 rv
= wait_child(pid
, NULL
);
931 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
942 close_all_files(void)
945 while (fp_head
!= NULL
)
947 Pclose(fp_head
->fp
, TRU1
);
960 cp
= _findchild(0, TRU1
);
962 if ((cp
->pid
= pid
= fork()) == -1) {
964 n_perr(_("fork"), 0);
971 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
972 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
979 /* TODO Of course this is a joke given that during a "p*" the PAGER may
980 * TODO be up and running while we play around like this... but i guess
981 * TODO this can't be helped at all unless we perform complete and true
982 * TODO process group separation and ensure we don't deadlock us out
983 * TODO via TTY jobcontrol signal storms (could this really happen?).
984 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
985 * TODO any action, and shall we find we need to run programs dump it
986 * TODO all into a temporary file which is then passed through to the
987 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
988 if ((tio_set
= ((options
& OPT_INTERACTIVE
) &&
989 (infd
== COMMAND_FD_PASS
|| outfd
== COMMAND_FD_PASS
)))) {
990 tcgetattr((options
& OPT_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
), &a_popen_tios
);
991 n_TERMCAP_SUSPEND(FAL0
);
993 sigdelset(&nset
, SIGCHLD
);
994 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
995 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
997 a_popen_jobsigs_up();
1000 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1003 if (wait_child(rv
, NULL
))
1006 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1007 n_err(_("Fatal error in process\n"));
1013 a_popen_jobsigs_down();
1014 tio_set
= ((options
& OPT_TTYIN
) != 0);
1015 n_TERMCAP_RESUME(a_popen_hadsig
? TRU1
: FAL0
);
1016 tcsetattr((tio_set
? STDIN_FILENO
: STDOUT_FILENO
),
1017 (tio_set
? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1018 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1025 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1026 char const *a0
, char const *a1
, char const *a2
,
1027 char const **env_addon
)
1032 if ((rv
= fork_child()) == -1) {
1033 n_perr(_("fork"), 0);
1035 } else if (rv
== 0) {
1039 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1040 extern char **environ
;
1041 size_t ei
, ei_orig
, ai
, ai_orig
;
1044 /* TODO note we don't check the POSIX limit:
1045 * the total space used to store the environment and the arguments to
1046 * the process is limited to {ARG_MAX} bytes */
1047 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1050 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1053 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1054 memcpy(env
, environ
, sizeof(*env
) * ei
);
1056 /* Replace all those keys that yet exist */
1058 char const *ee
, *kvs
;
1062 kvs
= strchr(ee
, '=');
1063 assert(kvs
!= NULL
);
1064 kl
= PTR2SIZE(kvs
- ee
);
1066 for (ei
= ei_orig
; ei
-- > 0;) {
1067 char const *ekvs
= strchr(env
[ei
], '=');
1068 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1069 !memcmp(ee
, env
[ei
], kl
)) {
1070 env
[ei
] = UNCONST(ee
);
1071 env_addon
[ai
] = NULL
;
1077 /* And append the rest */
1078 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1079 if (env_addon
[ai
] != NULL
)
1080 env
[ei
++] = UNCONST(env_addon
[ai
]);
1086 i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
1088 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
1089 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
1090 (argv
[i
++] = UNCONST(a2
)) != NULL
)
1092 prepare_child(mask
, infd
, outfd
);
1093 execvp(argv
[0], argv
);
1102 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
1108 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1109 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1110 if ((i
= (infd
== COMMAND_FD_NULL
)))
1111 infd
= open("/dev/null", O_RDONLY
);
1113 dup2(infd
, STDIN_FILENO
);
1118 if ((i
= (outfd
== COMMAND_FD_NULL
)))
1119 outfd
= open("/dev/null", O_WRONLY
);
1121 dup2(outfd
, STDOUT_FILENO
);
1127 for (i
= 1; i
< NSIG
; ++i
)
1128 if (sigismember(nset
, i
))
1129 safe_signal(i
, SIG_IGN
);
1130 if (!sigismember(nset
, SIGINT
))
1131 safe_signal(SIGINT
, SIG_DFL
);
1135 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1142 sigset_t nset
, oset
;
1147 sigaddset(&nset
, SIGCHLD
);
1148 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1150 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1157 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1162 wait_child(int pid
, int *wait_status
)
1164 sigset_t nset
, oset
;
1171 sigaddset(&nset
, SIGCHLD
);
1172 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1174 cp
= _findchild(pid
, FAL0
);
1183 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1185 if (wait_status
!= NULL
)
1187 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);