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 - 2014 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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
54 # define _SET_CLOEXEC(FD) fcntl((FD), F_SETFD, FD_CLOEXEC)
56 # define _SET_CLOEXEC(FD)
86 static struct fp
*fp_head
;
87 static struct child
*_popen_child
;
89 static int scan_mode(char const *mode
, int *omode
);
90 static void register_file(FILE *fp
, int omode
, int ispipe
, int pid
,
91 int compressed
, char const *realfile
, long offset
);
92 static enum okay
_compress(struct fp
*fpp
);
93 static int _decompress(int compression
, int infd
, int outfd
);
94 static enum okay
unregister_file(FILE *fp
);
95 static int file_pid(FILE *fp
);
96 static int wait_command(int pid
);
97 static struct child
*findchild(int pid
);
98 static void delchild(struct child
*cp
);
101 scan_mode(char const *mode
, int *omode
)
108 {"w", O_WRONLY
| O_CREAT
| O_TRUNC
},
109 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
110 {"a", O_WRONLY
| O_APPEND
| O_CREAT
},
111 {"a+", O_RDWR
| O_APPEND
},
113 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
119 for (i
= 0; UICMP(z
, i
, <, NELEM(maps
)); ++i
)
120 if (!strcmp(maps
[i
].mode
, mode
)) {
121 *omode
= maps
[i
].omode
;
126 fprintf(stderr
, tr(152, "Internal error: bad stdio open mode %s\n"), mode
);
128 *omode
= 0; /* (silence CC) */
136 register_file(FILE *fp
, int omode
, int ispipe
, int pid
, int compressed
,
137 char const *realfile
, long offset
)
142 fpp
= smalloc(sizeof *fpp
);
148 fpp
->compressed
= compressed
;
149 fpp
->realfile
= realfile
? sstrdup(realfile
) : NULL
;
150 fpp
->offset
= offset
;
156 _compress(struct fp
*fpp
)
163 if (fpp
->omode
== O_RDONLY
) {
171 if (fseek(fpp
->fp
, fpp
->offset
, SEEK_SET
) < 0)
175 if ((fpp
->compressed
& FP_MASK
) == FP_IMAP
) {
176 rv
= imap_append(fpp
->realfile
, fpp
->fp
);
180 if ((fpp
->compressed
& FP_MASK
) == FP_MAILDIR
) {
181 rv
= maildir_append(fpp
->realfile
, fpp
->fp
);
185 outfd
= open(fpp
->realfile
, (fpp
->omode
| O_CREAT
) & ~O_EXCL
, 0666);
187 fprintf(stderr
, "Fatal: cannot create ");
188 perror(fpp
->realfile
);
191 if ((fpp
->omode
& O_APPEND
) == 0)
193 switch (fpp
->compressed
& FP_MASK
) {
195 cmd
[0] = "gzip"; cmd
[1] = "-c"; break;
197 cmd
[0] = "bzip2"; cmd
[1] = "-c"; break;
199 cmd
[0] = "cat"; cmd
[1] = NULL
; break;
201 if (run_command(cmd
[0], 0, fileno(fpp
->fp
), outfd
, cmd
[1], NULL
, NULL
) >= 0)
210 _decompress(int compression
, int infd
, int outfd
)
216 switch (compression
& FP_MASK
) {
217 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
218 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
219 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
225 rv
= run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], NULL
, NULL
);
232 unregister_file(FILE *fp
)
238 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
240 if ((p
->compressed
& FP_MASK
) != FP_RAW
) /* TODO ;} */
247 panic(tr(153, "Invalid file pointer"));
261 for (p
= fp_head
; p
; p
= p
->link
)
271 wait_command(int pid
)
276 if (!wait_child(pid
, NULL
)) {
277 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
278 fprintf(stderr
, tr(154, "Fatal error in process.\n"));
285 static struct child
*
291 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
295 *cpp
= smalloc(sizeof **cpp
);
297 (*cpp
)->done
= (*cpp
)->free
= 0;
305 delchild(struct child
*cp
)
310 for (cpp
= &_popen_child
; *cpp
!= cp
; cpp
= &(*cpp
)->link
)
318 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
324 if (scan_mode(oflags
, &osflags
) < 0)
326 osflags
|= O_CLOEXEC
;
330 if ((fd
= open(file
, osflags
, 0666)) < 0)
334 fp
= fdopen(fd
, oflags
);
341 Fopen(char const *file
, char const *oflags
)
347 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
348 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L);
354 Fdopen(int fd
, char const *oflags
)
360 scan_mode(oflags
, &osflags
);
361 osflags
|= O_CLOEXEC
;
363 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
364 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L);
375 if (unregister_file(fp
) == OKAY
)
380 return i
== 3 ? 0 : EOF
;
384 Zopen(char const *file
, char const *oflags
, int *compression
) /* FIXME MESS! */
387 int _compression
, osflags
, mode
, infd
;
393 if (compression
== NULL
)
394 compression
= &_compression
;
396 if (scan_mode(oflags
, &osflags
) < 0)
398 rof
= OF_RDWR
| OF_UNLINK
;
399 if (osflags
& O_APPEND
)
401 if (osflags
== O_RDONLY
) {
403 *compression
= FP_READONLY
;
410 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_IMAP
||
411 p
== PROTO_MAILDIR
)) {
412 *compression
|= (p
== PROTO_IMAP
) ? FP_IMAP
: FP_MAILDIR
;
413 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
418 if ((ext
= strrchr(file
, '.')) != NULL
) {
419 if (!strcmp(ext
, ".gz"))
420 *compression
|= FP_GZIP
;
421 else if (!strcmp(ext
, ".bz2"))
422 *compression
|= FP_BZIP2
;
427 *compression
|= FP_RAW
;
428 rv
= Fopen(file
, oflags
);
431 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
432 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
436 if ((rv
= Ftmp(NULL
, "zopen", rof
, 0600)) == NULL
) {
437 perror(tr(167, "tmpfile"));
440 if (infd
>= 0 || (*compression
& FP_MASK
) == FP_IMAP
||
441 (*compression
& FP_MASK
) == FP_MAILDIR
) {
442 if (_decompress(*compression
, infd
, fileno(rv
)) < 0) {
452 if ((infd
= creat(file
, 0666)) == -1) {
462 if (!(osflags
& O_APPEND
))
464 if ((offset
= ftell(rv
)) == -1) {
469 register_file(rv
, osflags
, 0, 0, *compression
, file
, offset
);
476 Ftmp(char **fn
, char const *prefix
, enum oflags oflags
, int mode
)
484 cp
= smalloc(strlen(tempdir
) + 1 + sizeof("mail") + strlen(prefix
) + 7 +1);
485 cp
= sstpcpy(cp
, tempdir
);
487 cp
= sstpcpy(cp
, "mail");
490 cp
= sstpcpy(cp
, prefix
);
492 /* TODO Ftmp(): unroll our own creation loop with atoi(random()) */
493 sstpcpy(cp
, ".XXXXXX");
497 if ((fd
= mkstemp(cp_base
)) == -1)
499 if (mode
!= (S_IRUSR
| S_IWUSR
) && fchmod(fd
, mode
) == -1)
501 if (oflags
& OF_APPEND
) {
504 if ((f
= fcntl(fd
, F_GETFL
)) == -1 ||
505 fcntl(fd
, F_SETFL
, f
| O_APPEND
) == -1) {
511 if (!(oflags
& OF_REGISTER
))
512 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
514 if (mktemp(cp_base
) == NULL
)
516 if ((fd
= open(cp_base
, O_CREAT
| O_EXCL
| O_RDWR
| O_CLOEXEC
|
517 (oflags
& OF_APPEND
? O_APPEND
: 0), mode
)) < 0)
521 fp
= (*((oflags
& OF_REGISTER
) ? &Fdopen
: &fdopen
))(fd
,
522 (oflags
& OF_RDWR
? "w+" : "w"));
523 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
532 if (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
))
537 if ((cp
= cp_base
) != NULL
)
543 Ftmp_release(char **fn
)
572 pipe_cloexec(int fd
[2])
579 fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
580 fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
588 Popen(char const *cmd
, char const *mode
, char const *sh
, int newfd1
)
590 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
591 char mod
[2] = { '0', '\0' };
596 if (!pipe_cloexec(p
))
602 hisside
= fd1
= p
[WRITE
];
604 } else if (*mode
== 'W') {
606 hisside
= fd0
= p
[READ
];
611 hisside
= fd0
= p
[READ
];
617 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
);
619 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
);
627 if ((rv
= fdopen(myside
, mod
)) != NULL
)
628 register_file(rv
, 0, 1, pid
, FP_RAW
, NULL
, 0L);
635 Pclose(FILE *ptr
, bool_t dowait
)
645 unregister_file(ptr
);
649 sigaddset(&nset
, SIGINT
);
650 sigaddset(&nset
, SIGHUP
);
651 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
652 rv
= wait_child(pid
, NULL
);
653 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
664 close_all_files(void)
667 while (fp_head
!= NULL
)
669 Pclose(fp_head
->fp
, TRU1
);
676 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
677 char const *a0
, char const *a1
, char const *a2
)
682 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
)) < 0)
685 rv
= wait_command(rv
);
691 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
692 char const *a0
, char const *a1
, char const *a2
)
697 if ((rv
= fork()) < 0) {
700 } else if (rv
== 0) {
702 int i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
704 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
705 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
706 (argv
[i
++] = UNCONST(a2
)) != NULL
)
708 prepare_child(mask
, infd
, outfd
);
709 execvp(argv
[0], argv
);
718 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
724 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
731 for (i
= 1; i
< NSIG
; ++i
)
732 if (sigismember(nset
, i
))
733 safe_signal(i
, SIG_IGN
);
734 if (!sigismember(nset
, SIGINT
))
735 safe_signal(SIGINT
, SIG_DFL
);
739 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
748 NYD_X
; /* Signal handler */
752 while ((pid
= waitpid(-1, &status
, WNOHANG
)) > 0) {
761 if (pid
== -1 && errno
== EINTR
)
773 sigaddset(&nset
, SIGCHLD
);
774 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
782 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
787 wait_child(int pid
, int *wait_status
)
796 sigaddset(&nset
, SIGCHLD
);
797 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
805 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
807 if (wait_status
!= NULL
)
809 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);
820 /* vim:set fenc=utf-8:s-it-mode */