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 *prefix
, enum oflags oflags
, int mode
)
666 size_t maxname
, tries
;
671 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
672 assert(!(oflags
& OF_RDONLY
));
673 assert(!(oflags
& OF_REGISTER_UNLINK
) || (oflags
& OF_REGISTER
));
680 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
681 maxname
= (size_t)pc
;
686 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
687 cp
= sstpcpy(cp
, tempdir
);
690 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
691 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
692 if (oflags
& OF_APPEND
)
693 osoflags
|= O_APPEND
;
695 for (tries
= 0;; ++tries
) {
699 x
= sstpcpy(cp
, UAGENT
);
701 if (*prefix
!= '\0') {
702 x
= sstpcpy(x
, prefix
);
706 /* Calculate length of a random string addon */
707 i
= PTR2SIZE(x
- cp
);
708 if (i
>= maxname
>> 1) {
713 /* But don't be too fatalistic */
714 if (i
> 8 && tries
< FTMP_OPEN_TRIES
/ 2)
716 memcpy(x
, getrandstring(i
), i
+1);
719 if ((fd
= open(cp_base
, osoflags
, mode
)) != -1) {
723 if (tries
>= FTMP_OPEN_TRIES
) {
730 if (oflags
& OF_REGISTER
) {
731 char const *osflags
= (oflags
& OF_RDWR
? "w+" : "w");
734 scan_mode(osflags
, &osflagbits
); /* TODO osoflags&xy ?!!? */
735 if ((fp
= fdopen(fd
, osflags
)) != NULL
)
736 register_file(fp
, osflagbits
| _O_CLOEXEC
, 0, 0,
737 (FP_RAW
| (oflags
& OF_REGISTER_UNLINK
? FP_UNLINK
: 0)),
740 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
742 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
753 if (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
))
760 if ((cp
= cp_base
) != NULL
)
766 Ftmp_release(char **fn
)
795 pipe_cloexec(int fd
[2])
801 if (pipe2(fd
, O_CLOEXEC
) == -1)
806 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
807 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
816 Popen(char const *cmd
, char const *mode
, char const *sh
,
817 char const **env_addon
, int newfd1
)
819 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
820 char mod
[2] = {'0', '\0'};
825 /* First clean up child structures */
827 struct child
**cpp
, *cp
;
830 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
832 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
833 if ((*cpp
)->pid
== -1) {
841 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
844 if (!pipe_cloexec(p
))
850 hisside
= fd1
= p
[WRITE
];
852 } else if (*mode
== 'W') {
854 hisside
= fd0
= p
[READ
];
859 hisside
= fd0
= p
[READ
];
866 if (cmd
== (char*)-1) {
867 if ((pid
= fork_child()) == -1)
868 n_perr(_("fork"), 0);
870 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
871 prepare_child(&nset
, fd0
, fd1
);
878 } else if (sh
== NULL
) {
879 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
881 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
889 if ((rv
= fdopen(myside
, mod
)) != NULL
)
890 register_file(rv
, 0, 1, pid
, FP_RAW
, NULL
, 0L, NULL
);
899 Pclose(FILE *ptr
, bool_t dowait
)
909 unregister_file(ptr
);
913 sigaddset(&nset
, SIGINT
);
914 sigaddset(&nset
, SIGHUP
);
915 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
916 rv
= wait_child(pid
, NULL
);
917 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
928 close_all_files(void)
931 while (fp_head
!= NULL
)
933 Pclose(fp_head
->fp
, TRU1
);
946 cp
= _findchild(0, TRU1
);
948 if ((cp
->pid
= pid
= fork()) == -1) {
950 n_perr(_("fork"), 0);
957 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
958 char const *a0
, char const *a1
, char const *a2
, char const **env_addon
)
965 /* TODO Of course this is a joke given that during a "p*" the PAGER may
966 * TODO be up and running while we play around like this... but i guess
967 * TODO this can't be helped at all unless we perform complete and true
968 * TODO process group separation and ensure we don't deadlock us out
969 * TODO via TTY jobcontrol signal storms (could this really happen?).
970 * TODO Or have a builtin pager. Or query any necessity BEFORE we start
971 * TODO any action, and shall we find we need to run programs dump it
972 * TODO all into a temporary file which is then passed through to the
973 * TODO PAGER. Ugh. That still won't help for "needsterminal" anyway */
974 if ((tio_set
= ((options
& OPT_INTERACTIVE
) &&
975 (infd
== COMMAND_FD_PASS
|| outfd
== COMMAND_FD_PASS
)))) {
976 tcgetattr((options
& OPT_TTYIN
? STDIN_FILENO
: STDOUT_FILENO
), &a_popen_tios
);
978 sigdelset(&nset
, SIGCHLD
);
979 /* sigdelset(&nset, SIGPIPE); TODO would need a handler */
980 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
982 a_popen_jobsigs_up();
985 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, env_addon
)) < 0)
988 if (wait_child(rv
, NULL
))
991 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
992 n_err(_("Fatal error in process\n"));
998 a_popen_jobsigs_down();
999 tio_set
= ((options
& OPT_TTYIN
) != 0);
1000 tcsetattr((tio_set
? STDIN_FILENO
: STDOUT_FILENO
),
1001 (tio_set
? TCSAFLUSH
: TCSADRAIN
), &a_popen_tios
);
1002 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1009 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
1010 char const *a0
, char const *a1
, char const *a2
,
1011 char const **env_addon
)
1016 if ((rv
= fork_child()) == -1) {
1017 n_perr(_("fork"), 0);
1019 } else if (rv
== 0) {
1023 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
1024 extern char **environ
;
1025 size_t ei
, ei_orig
, ai
, ai_orig
;
1028 /* TODO note we don't check the POSIX limit:
1029 * the total space used to store the environment and the arguments to
1030 * the process is limited to {ARG_MAX} bytes */
1031 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
1034 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
1037 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
1038 memcpy(env
, environ
, sizeof(*env
) * ei
);
1040 /* Replace all those keys that yet exist */
1042 char const *ee
, *kvs
;
1046 kvs
= strchr(ee
, '=');
1047 assert(kvs
!= NULL
);
1048 kl
= PTR2SIZE(kvs
- ee
);
1050 for (ei
= ei_orig
; ei
-- > 0;) {
1051 char const *ekvs
= strchr(env
[ei
], '=');
1052 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
1053 !memcmp(ee
, env
[ei
], kl
)) {
1054 env
[ei
] = UNCONST(ee
);
1055 env_addon
[ai
] = NULL
;
1061 /* And append the rest */
1062 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
1063 if (env_addon
[ai
] != NULL
)
1064 env
[ei
++] = UNCONST(env_addon
[ai
]);
1070 i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
1072 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
1073 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
1074 (argv
[i
++] = UNCONST(a2
)) != NULL
)
1076 prepare_child(mask
, infd
, outfd
);
1077 execvp(argv
[0], argv
);
1086 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
1092 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
1093 /* TODO WHAT IS WITH STDERR_FILENO DAMN? */
1094 if ((i
= (infd
== COMMAND_FD_NULL
)))
1095 infd
= open("/dev/null", O_RDONLY
);
1097 dup2(infd
, STDIN_FILENO
);
1102 if ((i
= (outfd
== COMMAND_FD_NULL
)))
1103 outfd
= open("/dev/null", O_WRONLY
);
1105 dup2(outfd
, STDOUT_FILENO
);
1111 for (i
= 1; i
< NSIG
; ++i
)
1112 if (sigismember(nset
, i
))
1113 safe_signal(i
, SIG_IGN
);
1114 if (!sigismember(nset
, SIGINT
))
1115 safe_signal(SIGINT
, SIG_DFL
);
1119 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
1126 sigset_t nset
, oset
;
1131 sigaddset(&nset
, SIGCHLD
);
1132 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1134 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1141 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1146 wait_child(int pid
, int *wait_status
)
1148 sigset_t nset
, oset
;
1155 sigaddset(&nset
, SIGCHLD
);
1156 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1158 cp
= _findchild(pid
, FAL0
);
1167 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1169 if (wait_status
!= NULL
)
1171 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);