2 * S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 Steffen "Daode" Nurpmeso.
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
73 static struct fp
*fp_head
;
82 static struct child
*child
;
84 static int scan_mode(const char *mode
, int *omode
);
85 static void register_file(FILE *fp
, int omode
, int pipe
, int pid
,
86 int compressed
, const char *realfile
, long offset
);
87 static enum okay
compress(struct fp
*fpp
);
88 static int decompress(int compression
, int input
, int output
);
89 static enum okay
unregister_file(FILE *fp
);
90 static int file_pid(FILE *fp
);
91 static int wait_command(int pid
);
92 static struct child
*findchild(int pid
);
93 static void delchild(struct child
*cp
);
96 * Provide BSD-like signal() on all systems.
99 safe_signal(int signum
, sighandler_type handler
)
101 struct sigaction nact
, oact
;
103 nact
.sa_handler
= handler
;
104 sigemptyset(&nact
.sa_mask
);
107 nact
.sa_flags
|= SA_RESTART
;
109 if (sigaction(signum
, &nact
, &oact
) != 0)
111 return oact
.sa_handler
;
115 scan_mode(const char *mode
, int *omode
)
118 if (!strcmp(mode
, "r")) {
120 } else if (!strcmp(mode
, "w")) {
121 *omode
= O_WRONLY
| O_CREAT
| O_TRUNC
;
122 } else if (!strcmp(mode
, "wx")) {
123 *omode
= O_WRONLY
| O_CREAT
| O_EXCL
;
124 } else if (!strcmp(mode
, "a")) {
125 *omode
= O_WRONLY
| O_APPEND
| O_CREAT
;
126 } else if (!strcmp(mode
, "a+")) {
127 *omode
= O_RDWR
| O_APPEND
;
128 } else if (!strcmp(mode
, "r+")) {
130 } else if (!strcmp(mode
, "w+")) {
131 *omode
= O_RDWR
| O_CREAT
| O_EXCL
;
133 fprintf(stderr
, catgets(catd
, CATSET
, 152,
134 "Internal error: bad stdio open mode %s\n"), mode
);
142 safe_fopen(const char *file
, const char *mode
, int *omode
)
146 if (scan_mode(mode
, omode
) < 0)
148 if ((fd
= open(file
, *omode
, 0666)) < 0)
150 return fdopen(fd
, mode
);
154 Fopen(const char *file
, const char *mode
)
159 if ((fp
= safe_fopen(file
, mode
, &omode
)) != NULL
) {
160 register_file(fp
, omode
, 0, 0, FP_UNCOMPRESSED
, NULL
, 0L);
161 fcntl(fileno(fp
), F_SETFD
, FD_CLOEXEC
);
167 Fdopen(int fd
, const char *mode
)
172 scan_mode(mode
, &omode
);
173 if ((fp
= fdopen(fd
, mode
)) != NULL
) {
174 register_file(fp
, omode
, 0, 0, FP_UNCOMPRESSED
, NULL
, 0L);
175 fcntl(fileno(fp
), F_SETFD
, FD_CLOEXEC
);
184 if (unregister_file(fp
) == OKAY
)
188 return i
== 3 ? 0 : EOF
;
192 Zopen(const char *file
, const char *mode
, int *compression
)
205 if (scan_mode(mode
, &omode
) < 0)
207 if (compression
== NULL
)
208 compression
= &_compression
;
209 bits
= R_OK
| (omode
== O_RDONLY
? 0 : W_OK
);
210 if (omode
& O_APPEND
&& ((p
= which_protocol(file
)) == PROTO_IMAP
||
211 p
== PROTO_MAILDIR
)) {
212 *compression
= p
== PROTO_IMAP
? FP_IMAP
: FP_MAILDIR
;
213 omode
= O_RDWR
| O_APPEND
| O_CREAT
;
218 if ((extension
= strrchr(file
, '.')) != NULL
) {
220 if (strcmp(extension
, ".gz") == 0)
222 if (strcmp(extension
, ".bz2") == 0)
225 if (access(file
, F_OK
) == 0) {
226 *compression
= FP_UNCOMPRESSED
;
227 return Fopen(file
, mode
);
228 } else if (access(rp
=savecat(file
, ".gz"), bits
) == 0) {
229 gzip
: *compression
= FP_GZIPPED
;
230 } else if (access(rp
=savecat(file
, ".bz2"), bits
) == 0) {
231 bz2
: *compression
= FP_BZIP2ED
;
233 *compression
= FP_UNCOMPRESSED
;
234 return Fopen(file
, mode
);
236 if (access(rp
, W_OK
) < 0)
237 *compression
|= FP_READONLY
;
238 if ((input
= open(rp
, bits
& W_OK
? O_RDWR
: O_RDONLY
)) < 0
239 && ((omode
&O_CREAT
) == 0 || errno
!= ENOENT
))
241 open
: if ((output
= Ftemp(&tempfn
, "Rz", "w+", 0600, 0)) == NULL
) {
242 perror(catgets(catd
, CATSET
, 167, "tmpfile"));
247 if (input
>= 0 || (*compression
&FP_MASK
) == FP_IMAP
||
248 (*compression
&FP_MASK
) == FP_MAILDIR
) {
249 if (decompress(*compression
, input
, fileno(output
)) < 0) {
255 if ((input
= creat(rp
, 0666)) < 0) {
262 if (omode
& O_APPEND
) {
265 if ((flags
= fcntl(fileno(output
), F_GETFL
)) != -1)
266 fcntl(fileno(output
), F_SETFL
, flags
| O_APPEND
);
267 offset
= ftell(output
);
272 register_file(output
, omode
, 0, 0, *compression
, rp
, offset
);
277 Popen(const char *cmd
, const char *mode
, const char *shell
, int newfd1
)
280 int myside
, hisside
, fd0
, fd1
;
282 char mod
[2] = { '0', '\0' };
288 fcntl(p
[READ
], F_SETFD
, FD_CLOEXEC
);
289 fcntl(p
[WRITE
], F_SETFD
, FD_CLOEXEC
);
293 hisside
= fd1
= p
[WRITE
];
295 } else if (*mode
== 'W') {
297 hisside
= fd0
= p
[READ
];
302 hisside
= fd0
= p
[READ
];
308 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
);
310 pid
= start_command(shell
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
);
318 if ((fp
= fdopen(myside
, mod
)) != NULL
)
319 register_file(fp
, 0, 1, pid
, FP_UNCOMPRESSED
, NULL
, 0L);
332 unregister_file(ptr
);
335 sigaddset(&nset
, SIGINT
);
336 sigaddset(&nset
, SIGHUP
);
337 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
339 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
344 close_all_files(void)
355 register_file(FILE *fp
, int omode
, int pipe
, int pid
, int compressed
,
356 const char *realfile
, long offset
)
360 fpp
= (struct fp
*)smalloc(sizeof *fpp
);
366 fpp
->compressed
= compressed
;
367 fpp
->realfile
= realfile
? sstrdup(realfile
) : NULL
;
368 fpp
->offset
= offset
;
373 compress(struct fp
*fpp
)
379 if (fpp
->omode
== O_RDONLY
)
383 fseek(fpp
->fp
, fpp
->offset
, SEEK_SET
);
384 if ((fpp
->compressed
&FP_MASK
) == FP_IMAP
) {
385 return imap_append(fpp
->realfile
, fpp
->fp
);
387 if ((fpp
->compressed
&FP_MASK
) == FP_MAILDIR
) {
388 return maildir_append(fpp
->realfile
, fpp
->fp
);
390 if ((output
= open(fpp
->realfile
,
391 (fpp
->omode
|O_CREAT
)&~O_EXCL
,
393 fprintf(stderr
, "Fatal: cannot create ");
394 perror(fpp
->realfile
);
397 if ((fpp
->omode
& O_APPEND
) == 0)
398 ftruncate(output
, 0);
399 switch (fpp
->compressed
& FP_MASK
) {
401 command
[0] = "gzip"; command
[1] = "-c"; break;
403 command
[0] = "bzip2"; command
[1] = "-c"; break;
405 command
[0] = "cat"; command
[1] = NULL
; break;
407 if (run_command(command
[0], 0, fileno(fpp
->fp
), output
,
408 command
[1], NULL
, NULL
) < 0)
417 decompress(int compression
, int input
, int output
)
422 * Note that it is not possible to handle 'pack' or 'compress'
423 * formats because appending data does not work with them.
425 switch (compression
& FP_MASK
) {
426 case FP_GZIPPED
: command
[0] = "gzip"; command
[1] = "-cd"; break;
427 case FP_BZIP2ED
: command
[0] = "bzip2"; command
[1] = "-cd"; break;
428 case FP_IMAP
: return 0;
429 case FP_MAILDIR
: return 0;
430 default: command
[0] = "cat"; command
[1] = NULL
;
432 return run_command(command
[0], 0, input
, output
,
433 command
[1], NULL
, NULL
);
437 unregister_file(FILE *fp
)
442 for (pp
= &fp_head
; (p
= *pp
) != (struct fp
*)NULL
; pp
= &p
->link
)
444 if ((p
->compressed
&FP_MASK
) != FP_UNCOMPRESSED
)
450 panic(catgets(catd
, CATSET
, 153, "Invalid file pointer"));
460 for (p
= fp_head
; p
; p
= p
->link
)
467 * Run a command without a shell, with optional arguments and splicing
468 * of stdin and stdout. The command name can be a sequence of words.
469 * Signals must be handled by the caller.
470 * "Mask" contains the signals to ignore in the new process.
471 * SIGINT is enabled unless it's in the mask.
475 run_command(char *cmd
, sigset_t
*mask
, int infd
, int outfd
,
476 char *a0
, char *a1
, char *a2
)
480 if ((pid
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
)) < 0)
482 return wait_command(pid
);
487 start_command(const char *cmd
, sigset_t
*mask
, int infd
, int outfd
,
488 const char *a0
, const char *a1
, const char *a2
)
492 if ((pid
= fork()) < 0) {
498 int i
= getrawlist(cmd
, strlen(cmd
),
499 argv
, sizeof argv
/ sizeof *argv
, 0);
501 if ((argv
[i
++] = (char *)a0
) != NULL
&&
502 (argv
[i
++] = (char *)a1
) != NULL
&&
503 (argv
[i
++] = (char *)a2
) != NULL
)
505 prepare_child(mask
, infd
, outfd
);
506 execvp(argv
[0], argv
);
514 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
520 * All file descriptors other than 0, 1, and 2 are supposed to be
528 for (i
= 1; i
< NSIG
; i
++)
529 if (sigismember(nset
, i
))
530 safe_signal(i
, SIG_IGN
);
531 if (!sigismember(nset
, SIGINT
))
532 safe_signal(SIGINT
, SIG_DFL
);
535 sigprocmask(SIG_UNBLOCK
, &fset
, (sigset_t
*)NULL
);
539 wait_command(int pid
)
542 if (wait_child(pid
) < 0 && (value("bsdcompat") || value("bsdmsgs"))) {
543 printf(catgets(catd
, CATSET
, 154, "Fatal error in process.\n"));
549 static struct child
*
554 for (cpp
= &child
; *cpp
!= (struct child
*)NULL
&& (*cpp
)->pid
!= pid
;
557 if (*cpp
== (struct child
*)NULL
) {
558 *cpp
= (struct child
*) smalloc(sizeof (struct child
));
560 (*cpp
)->done
= (*cpp
)->free
= 0;
561 (*cpp
)->link
= (struct child
*)NULL
;
567 delchild(struct child
*cp
)
571 for (cpp
= &child
; *cpp
!= cp
; cpp
= &(*cpp
)->link
)
587 while ((pid
= waitpid(-1, (int*)&status
, WNOHANG
)) > 0) {
596 if (pid
== -1 && errno
== EINTR
)
603 * Mark a child as don't care.
609 struct child
*cp
= findchild(pid
);
611 sigaddset(&nset
, SIGCHLD
);
612 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
618 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
622 * Wait for a specific child to die.
626 * This version is correct code, but causes harm on some loosing
627 * systems. So we use the second one instead.
633 struct child
*cp
= findchild(pid
);
635 sigaddset(&nset
, SIGCHLD
);
636 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
640 wait_status
= cp
->status
;
642 sigprocmask(SIG_SETMASK
, &oset
, (sigset_t
*)NULL
);
644 if (WIFEXITED(wait_status
) && (WEXITSTATUS(wait_status
) == 0))
654 struct sigaction nact
, oact
;
656 nact
.sa_handler
= SIG_DFL
;
657 sigemptyset(&nact
.sa_mask
);
658 nact
.sa_flags
= SA_NOCLDSTOP
;
659 sigaction(SIGCHLD
, &nact
, &oact
);
664 term
= wait(&wait_status
);
665 if (term
== -1 && errno
== EINTR
)
667 if (term
== 0 || term
== -1)
669 cp
= findchild(term
);
670 if (cp
->free
|| term
== pid
) {
674 cp
->status
= wait_status
;
676 } while (term
!= pid
);
678 wait_status
= cp
->status
;
682 sigaction(SIGCHLD
, &oact
, NULL
);
684 * Make sure no zombies are left.
688 if (WIFEXITED(wait_status
) && (WEXITSTATUS(wait_status
) == 0))