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
65 /* TODO FP_UNLINK: should be in a separated process so that unlinking
66 * TODO the temporary "garbage" is "safe"(r than it is like that) */
79 static struct fp
*fp_head
;
80 static struct child
*_popen_child
;
82 /* TODO Rather temporary: deal with job control with FD_PASS */
83 static struct termios a_popen_tios
;
84 static sighandler_type a_popen_otstp
, a_popen_ottin
, a_popen_ottou
;
85 static volatile int a_popen_hadsig
;
87 static int scan_mode(char const *mode
, int *omode
);
88 static void register_file(FILE *fp
, int omode
, int ispipe
, int pid
,
89 int flags
, char const *realfile
, long offset
,
90 char const *save_cmd
);
91 static enum okay
_file_save(struct fp
*fpp
);
92 static int _file_load(int flags
, int infd
, int outfd
,
93 char const *load_cmd
);
94 static enum okay
unregister_file(FILE *fp
);
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 _sigchld(int signo
);
105 static struct child
*_findchild(int pid
, bool_t create
);
106 static void _delchild(struct child
*cp
);
109 scan_mode(char const *mode
, int *omode
)
116 {"w", O_WRONLY
| O_CREAT
| O_TRUNC
},
117 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
118 {"a", O_WRONLY
| O_APPEND
| O_CREAT
},
119 {"a+", O_RDWR
| O_APPEND
},
121 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
127 for (i
= 0; UICMP(z
, i
, <, NELEM(maps
)); ++i
)
128 if (!strcmp(maps
[i
].mode
, mode
)) {
129 *omode
= maps
[i
].omode
;
134 n_alert(_("Internal error: bad stdio open mode %s"), mode
);
136 *omode
= 0; /* (silence CC) */
144 register_file(FILE *fp
, int omode
, int ispipe
, int pid
, int flags
,
145 char const *realfile
, long offset
, char const *save_cmd
)
150 assert(!(flags
& FP_UNLINK
) || realfile
!= NULL
);
152 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
->offset
= offset
;
167 _file_save(struct fp
*fpp
)
174 if (fpp
->omode
== O_RDONLY
) {
184 if ((fpp
->flags
& FP_MASK
) == FP_IMAP
) {
185 rv
= imap_append(fpp
->realfile
, fpp
->fp
);
189 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
190 if (fseek(fpp
->fp
, fpp
->offset
, SEEK_SET
) == -1) {
192 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
193 fpp
->realfile
, strerror(outfd
));
196 rv
= maildir_append(fpp
->realfile
, fpp
->fp
, fpp
->offset
);
200 /* Ensure the I/O library doesn't optimize the fseek(3) away! */
201 if(lseek(infd
= fileno(fpp
->fp
), fpp
->offset
, SEEK_SET
) == -1){
203 n_err(_("Fatal: cannot restore file position and save %s: %s\n"),
204 fpp
->realfile
, strerror(outfd
));
208 outfd
= open(fpp
->realfile
,
209 ((fpp
->omode
| O_CREAT
| (fpp
->omode
& O_APPEND
? 0 : O_TRUNC
))
213 n_err(_("Fatal: cannot create %s: %s\n"),
214 fpp
->realfile
, strerror(outfd
));
219 switch (fpp
->flags
& FP_MASK
) {
221 cmd
[0] = "gzip"; cmd
[1] = "-c"; break;
223 cmd
[0] = "bzip2"; cmd
[1] = "-c"; break;
225 cmd
[0] = "xz"; cmd
[1] = "-c"; break;
227 cmd
[0] = "cat"; cmd
[1] = NULL
; break;
229 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
232 cmd
[2] = fpp
->save_cmd
;
234 if (run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
) >= 0)
244 _file_load(int flags
, int infd
, int outfd
, char const *load_cmd
)
251 switch (flags
& FP_MASK
) {
252 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
253 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
254 case FP_XZ
: cmd
[0] = "xz"; cmd
[1] = "-cd"; break;
255 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
257 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
268 rv
= run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
, NULL
);
275 unregister_file(FILE *fp
)
281 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
283 if ((p
->flags
& FP_MASK
) != FP_RAW
) /* TODO ;} */
285 if (p
->flags
& FP_UNLINK
&& unlink(p
->realfile
))
288 if (p
->save_cmd
!= NULL
)
290 if (p
->realfile
!= NULL
)
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 oldact
= safe_signal(sig
, SIG_DFL
);
371 sigaddset(&nset
, sig
);
372 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
374 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
376 safe_signal(sig
, oldact
);
385 NYD_X
; /* Signal handler */
389 pid
= waitpid(-1, &status
, WNOHANG
);
391 if (pid
== -1 && errno
== EINTR
)
396 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
398 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
407 static struct child
*
408 _findchild(int pid
, bool_t create
)
413 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
417 if (*cpp
== NULL
&& create
) {
418 *cpp
= smalloc(sizeof **cpp
);
420 (*cpp
)->done
= (*cpp
)->free
= 0;
428 _delchild(struct child
*cp
)
440 if (*(cpp
= &(*cpp
)->link
) == NULL
) {
441 DBG( n_err("! popen.c:_delchild(): implementation error\n"); )
449 command_manager_start(void)
451 struct sigaction nact
, oact
;
454 nact
.sa_handler
= &_sigchld
;
455 sigemptyset(&nact
.sa_mask
);
464 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
465 n_panic(_("Cannot install signal handler for child process management"));
470 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
474 NYD2_ENTER
; /* (only for Fopen() and once in lex.c) */
476 if (scan_mode(oflags
, &osflags
) < 0)
478 osflags
|= _O_CLOEXEC
;
482 if ((fd
= open(file
, osflags
, 0666)) == -1)
486 fp
= fdopen(fd
, oflags
);
493 Fopen(char const *file
, char const *oflags
)
499 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
500 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
506 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
512 scan_mode(oflags
, &osflags
);
514 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
516 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
517 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
528 if (unregister_file(fp
) == OKAY
)
533 return (i
== 3 ? 0 : EOF
);
537 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
540 char const *cload
= NULL
, *csave
= NULL
;
541 int flags
, osflags
, mode
, infd
;
547 if (scan_mode(oflags
, &osflags
) < 0)
551 rof
= OF_RDWR
| OF_UNLINK
;
552 if (osflags
& O_APPEND
)
554 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
556 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_IMAP
||
557 p
== PROTO_MAILDIR
)) {
558 flags
|= (p
== PROTO_IMAP
) ? FP_IMAP
: FP_MAILDIR
;
559 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
564 if ((ext
= strrchr(file
, '.')) != NULL
) {
565 if (!asccasecmp(ext
, ".gz"))
567 else if (!asccasecmp(ext
, ".xz")) {
569 osflags
&= ~O_APPEND
;
571 } else if (!asccasecmp(ext
, ".bz2")) {
573 osflags
&= ~O_APPEND
;
577 #define _X1 "file-hook-load-"
579 #define _X2 "file-hook-save-"
580 size_t l
= strlen(++ext
);
581 char *vbuf
= ac_alloc(l
+ MAX(sizeof(_X1
), sizeof(_X2
)));
583 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
584 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
585 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
586 cload
= vok_vlook(vbuf
);
587 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
588 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
589 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
590 csave
= vok_vlook(vbuf
);
595 if ((csave
!= NULL
) && (cload
!= NULL
)) {
597 osflags
&= ~O_APPEND
;
599 } else if ((csave
!= NULL
) | (cload
!= NULL
)) {
600 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
601 "Treating as plain text!"), ext
);
609 rv
= Fopen(file
, oflags
);
613 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
614 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
618 /* Note rv is not yet register_file()d, fclose() it in error path! */
619 if ((rv
= Ftmp(NULL
, "zopen", rof
, 0600)) == NULL
) {
620 n_perr(_("tmpfile"), 0);
624 if (flags
& (FP_IMAP
| FP_MAILDIR
))
626 else if (infd
>= 0) {
627 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
637 if ((infd
= creat(file
, 0666)) == -1) {
648 if (!(osflags
& O_APPEND
))
650 if ((offset
= ftell(rv
)) == -1) {
656 register_file(rv
, osflags
, 0, 0, flags
, file
, offset
, csave
);
663 Ftmp(char **fn
, char const *namehint
, enum oflags oflags
, int mode
)
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
= 8};
671 size_t maxname
, xlen
, i
;
678 assert(namehint
!= NULL
);
679 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
680 assert(!(oflags
& OF_RDONLY
));
681 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
690 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
691 maxname
= (size_t)pc
;
695 if ((oflags
& OF_SUFFIX
) && *namehint
!= '\0') {
696 if ((xlen
= strlen(namehint
)) > maxname
- _RANDCHARS
) {
697 errno
= ENAMETOOLONG
;
703 /* Prepare the template string once, then iterate over the random range */
705 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
706 cp
= sstpcpy(cp
, tempdir
);
709 char *x
= sstpcpy(cp
, UAGENT
);
711 if (!(oflags
& OF_SUFFIX
))
712 x
= sstpcpy(x
, namehint
);
714 i
= PTR2SIZE(x
- cp
);
715 if (i
> maxname
- xlen
- _RANDCHARS
) {
716 size_t j
= maxname
- xlen
- _RANDCHARS
;
721 if ((oflags
& OF_SUFFIX
) && xlen
> 0)
722 memcpy(x
+ _RANDCHARS
, namehint
, xlen
);
724 x
[xlen
+ _RANDCHARS
] = '\0';
728 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
729 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
730 if (oflags
& OF_APPEND
)
731 osoflags
|= O_APPEND
;
734 memcpy(cp
, getrandstring(_RANDCHARS
), _RANDCHARS
);
739 if ((fd
= open(cp_base
, osoflags
, mode
)) != -1) {
743 if (i
>= FTMP_OPEN_TRIES
) {
751 if (oflags
& OF_REGISTER
) {
752 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
755 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
756 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
757 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0, 0,
758 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
761 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
763 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
774 if (relesigs
&& (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
)))
781 if ((cp
= cp_base
) != NULL
)
787 Ftmp_release(char **fn
)
816 pipe_cloexec(int fd
[2])
822 if (pipe2(fd
, O_CLOEXEC
) == -1)
827 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
828 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
837 Popen(char const *cmd
, char const *mode
, char const *sh
,
838 char const **env_addon
, int newfd1
)
840 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
841 char mod
[2] = {'0', '\0'};
846 /* First clean up child structures */
848 struct child
**cpp
, *cp
;
851 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
853 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
854 if ((*cpp
)->pid
== -1) {
862 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
865 if (!pipe_cloexec(p
))
871 hisside
= fd1
= p
[WRITE
];
873 } else if (*mode
== 'W') {
875 hisside
= fd0
= p
[READ
];
880 hisside
= fd0
= p
[READ
];
887 if (cmd
== (char*)-1) {
888 if ((pid
= fork_child()) == -1)
889 n_perr(_("fork"), 0);
891 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
892 prepare_child(&nset
, fd0
, fd1
);
899 } else if (sh
== NULL
) {
900 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
902 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
910 if ((rv
= fdopen(myside
, mod
)) != NULL
)
911 register_file(rv
, 0, 1, pid
, FP_RAW
, NULL
, 0L, NULL
);
920 Pclose(FILE *ptr
, bool_t dowait
)
930 unregister_file(ptr
);
934 sigaddset(&nset
, SIGINT
);
935 sigaddset(&nset
, SIGHUP
);
936 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
937 rv
= wait_child(pid
, NULL
);
938 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
949 close_all_files(void)
952 while (fp_head
!= NULL
)
954 Pclose(fp_head
->fp
, TRU1
);
967 cp
= _findchild(0, TRU1
);
969 if ((cp
->pid
= pid
= fork()) == -1) {
971 n_perr(_("fork"), 0);
978 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
979 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
986 /* TODO Of course this is a joke given that during a "p*" the PAGER may
987 * TODO be up and running while we play around like this... but i guess
988 * TODO this can't be helped at all unless we perform complete and true
989 * TODO process group separation and ensure we don't deadlock us out
990 * TODO via TTY jobcontrol signal storms (could this really happen?).
991 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
992 * TODO any action, and shall we find we need to run programs dump it
993 * TODO all into a temporary file which is then passed through to the
994 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
995 if ((tio_set
= ((options
& OPT_INTERACTIVE
) &&
996 (infd
== COMMAND_FD_PASS
|| outfd
== COMMAND_FD_PASS
)))) {
997 tcgetattr((options
& OPT_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
), &a_popen_tios
);
999 sigdelset(&nset
, SIGCHLD
);
1000 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
1001 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1003 a_popen_jobsigs_up();
1006 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
1009 if (wait_child(rv
, NULL
))
1012 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
1013 n_err(_("Fatal error in process\n"));
1019 a_popen_jobsigs_down();
1020 tio_set
= ((options
& OPT_TTYIN
) != 0);
1021 tcsetattr((tio_set
? STDIN_FILENO
: STDOUT_FILENO
),
1022 (tio_set
? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1023 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1030 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1031 char const *a0
, char const *a1
, char const *a2
,
1032 char const **env_addon
)
1037 if ((rv
= fork_child()) == -1) {
1038 n_perr(_("fork"), 0);
1040 } else if (rv
== 0) {
1044 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1045 extern char **environ
;
1046 size_t ei
, ei_orig
, ai
, ai_orig
;
1049 /* TODO note we don't check the POSIX limit:
1050 * the total space used to store the environment and the arguments to
1051 * the process is limited to {ARG_MAX} bytes */
1052 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1055 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1058 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1059 memcpy(env
, environ
, sizeof(*env
) * ei
);
1061 /* Replace all those keys that yet exist */
1063 char const *ee
, *kvs
;
1067 kvs
= strchr(ee
, '=');
1068 assert(kvs
!= NULL
);
1069 kl
= PTR2SIZE(kvs
- ee
);
1071 for (ei
= ei_orig
; ei
-- > 0;) {
1072 char const *ekvs
= strchr(env
[ei
], '=');
1073 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1074 !memcmp(ee
, env
[ei
], kl
)) {
1075 env
[ei
] = UNCONST(ee
);
1076 env_addon
[ai
] = NULL
;
1082 /* And append the rest */
1083 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1084 if (env_addon
[ai
] != NULL
)
1085 env
[ei
++] = UNCONST(env_addon
[ai
]);
1091 i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
1093 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
1094 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
1095 (argv
[i
++] = UNCONST(a2
)) != NULL
)
1097 prepare_child(mask
, infd
, outfd
);
1098 execvp(argv
[0], argv
);
1107 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
1113 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1114 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1115 if ((i
= (infd
== COMMAND_FD_NULL
)))
1116 infd
= open("/dev/null", O_RDONLY
);
1118 dup2(infd
, STDIN_FILENO
);
1123 if ((i
= (outfd
== COMMAND_FD_NULL
)))
1124 outfd
= open("/dev/null", O_WRONLY
);
1126 dup2(outfd
, STDOUT_FILENO
);
1132 for (i
= 1; i
< NSIG
; ++i
)
1133 if (sigismember(nset
, i
))
1134 safe_signal(i
, SIG_IGN
);
1135 if (!sigismember(nset
, SIGINT
))
1136 safe_signal(SIGINT
, SIG_DFL
);
1140 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1147 sigset_t nset
, oset
;
1152 sigaddset(&nset
, SIGCHLD
);
1153 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1155 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1162 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1167 wait_child(int pid
, int *wait_status
)
1169 sigset_t nset
, oset
;
1176 sigaddset(&nset
, SIGCHLD
);
1177 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1179 cp
= _findchild(pid
, FAL0
);
1188 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1190 if (wait_status
!= NULL
)
1192 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);