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
76 static struct fp
*fp_head
;
77 static struct child
*_popen_child
;
79 static int scan_mode(char const *mode
, int *omode
);
80 static void register_file(FILE *fp
, int omode
, int ispipe
, int pid
,
81 int flags
, char const *realfile
, long offset
,
82 char const *save_cmd
);
83 static enum okay
_file_save(struct fp
*fpp
);
84 static int _file_load(int flags
, int infd
, int outfd
,
85 char const *load_cmd
);
86 static enum okay
unregister_file(FILE *fp
);
87 static int file_pid(FILE *fp
);
90 static void _sigchld(int signo
);
92 static int wait_command(int pid
);
93 static struct child
*_findchild(int pid
, bool_t create
);
94 static void _delchild(struct child
*cp
);
97 scan_mode(char const *mode
, int *omode
)
104 {"w", O_WRONLY
| O_CREAT
| O_TRUNC
},
105 {"wx", O_WRONLY
| O_CREAT
| O_EXCL
},
106 {"a", O_WRONLY
| O_APPEND
| O_CREAT
},
107 {"a+", O_RDWR
| O_APPEND
},
109 {"w+", O_RDWR
| O_CREAT
| O_EXCL
}
115 for (i
= 0; UICMP(z
, i
, <, NELEM(maps
)); ++i
)
116 if (!strcmp(maps
[i
].mode
, mode
)) {
117 *omode
= maps
[i
].omode
;
122 n_alert(_("Internal error: bad stdio open mode %s"), mode
);
124 *omode
= 0; /* (silence CC) */
132 register_file(FILE *fp
, int omode
, int ispipe
, int pid
, int flags
,
133 char const *realfile
, long offset
, char const *save_cmd
)
138 fpp
= smalloc(sizeof *fpp
);
145 fpp
->realfile
= (realfile
!= NULL
) ? sstrdup(realfile
) : NULL
;
146 fpp
->save_cmd
= (save_cmd
!= NULL
) ? sstrdup(save_cmd
) : NULL
;
147 fpp
->offset
= offset
;
153 _file_save(struct fp
*fpp
)
160 if (fpp
->omode
== O_RDONLY
) {
168 if (fseek(fpp
->fp
, fpp
->offset
, SEEK_SET
) == -1)
172 if ((fpp
->flags
& FP_MASK
) == FP_IMAP
) {
173 rv
= imap_append(fpp
->realfile
, fpp
->fp
);
177 if ((fpp
->flags
& FP_MASK
) == FP_MAILDIR
) {
178 rv
= maildir_append(fpp
->realfile
, fpp
->fp
);
182 outfd
= open(fpp
->realfile
, (fpp
->omode
| O_CREAT
) & ~O_EXCL
, 0666);
184 n_err(_("Fatal: cannot create \"%s\": %s\n"),
185 fpp
->realfile
, strerror(errno
));
188 if (!(fpp
->omode
& O_APPEND
))
192 switch (fpp
->flags
& FP_MASK
) {
194 cmd
[0] = "gzip"; cmd
[1] = "-c"; break;
196 cmd
[0] = "bzip2"; cmd
[1] = "-c"; break;
198 cmd
[0] = "xz"; cmd
[1] = "-c"; break;
200 cmd
[0] = "cat"; cmd
[1] = NULL
; break;
202 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
205 cmd
[2] = fpp
->save_cmd
;
207 if (run_command(cmd
[0], 0, fileno(fpp
->fp
), outfd
, cmd
[1], cmd
[2], NULL
)
217 _file_load(int flags
, int infd
, int outfd
, char const *load_cmd
)
224 switch (flags
& FP_MASK
) {
225 case FP_GZIP
: cmd
[0] = "gzip"; cmd
[1] = "-cd"; break;
226 case FP_BZIP2
: cmd
[0] = "bzip2"; cmd
[1] = "-cd"; break;
227 case FP_XZ
: cmd
[0] = "xz"; cmd
[1] = "-cd"; break;
228 default: cmd
[0] = "cat"; cmd
[1] = NULL
; break;
230 if ((cmd
[0] = ok_vlook(SHELL
)) == NULL
)
241 rv
= run_command(cmd
[0], 0, infd
, outfd
, cmd
[1], cmd
[2], NULL
);
248 unregister_file(FILE *fp
)
254 for (pp
= &fp_head
; (p
= *pp
) != NULL
; pp
= &p
->link
)
256 if ((p
->flags
& FP_MASK
) != FP_RAW
) /* TODO ;} */
259 if (p
->save_cmd
!= NULL
)
261 if (p
->realfile
!= NULL
)
266 DBGOR(n_panic
, n_alert
)(_("Invalid file pointer"));
281 for (p
= fp_head
; p
; p
= p
->link
)
296 NYD_X
; /* Signal handler */
300 pid
= waitpid(-1, &status
, WNOHANG
);
302 if (pid
== -1 && errno
== EINTR
)
307 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
309 cp
->pid
= -1; /* XXX Was _delchild(cp);# */
319 wait_command(int pid
)
324 if (!wait_child(pid
, NULL
)) {
325 if (ok_blook(bsdcompat
) || ok_blook(bsdmsgs
))
326 n_err(_("Fatal error in process\n"));
333 static struct child
*
334 _findchild(int pid
, bool_t create
)
339 for (cpp
= &_popen_child
; *cpp
!= NULL
&& (*cpp
)->pid
!= pid
;
343 if (*cpp
== NULL
&& create
) {
344 *cpp
= smalloc(sizeof **cpp
);
346 (*cpp
)->done
= (*cpp
)->free
= 0;
354 _delchild(struct child
*cp
)
366 if (*(cpp
= &(*cpp
)->link
) == NULL
) {
367 DBG( n_err("! popen.c:_delchild(): implementation error\n"); )
375 command_manager_start(void)
377 struct sigaction nact
, oact
;
380 nact
.sa_handler
= &_sigchld
;
381 sigemptyset(&nact
.sa_mask
);
390 if (sigaction(SIGCHLD
, &nact
, &oact
) != 0)
391 n_panic(_("Cannot install signal handler for child process management"));
396 safe_fopen(char const *file
, char const *oflags
, int *xflags
)
400 NYD2_ENTER
; /* (only for Fopen() and once in lex.c) */
402 if (scan_mode(oflags
, &osflags
) < 0)
404 osflags
|= _O_CLOEXEC
;
408 if ((fd
= open(file
, osflags
, 0666)) == -1)
412 fp
= fdopen(fd
, oflags
);
419 Fopen(char const *file
, char const *oflags
)
425 if ((fp
= safe_fopen(file
, oflags
, &osflags
)) != NULL
)
426 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
432 Fdopen(int fd
, char const *oflags
, bool_t nocloexec
)
438 scan_mode(oflags
, &osflags
);
440 osflags
|= _O_CLOEXEC
; /* Ensured to be set by caller as documented! */
442 if ((fp
= fdopen(fd
, oflags
)) != NULL
)
443 register_file(fp
, osflags
, 0, 0, FP_RAW
, NULL
, 0L, NULL
);
454 if (unregister_file(fp
) == OKAY
)
459 return (i
== 3 ? 0 : EOF
);
463 Zopen(char const *file
, char const *oflags
) /* FIXME MESS! */
466 char const *cload
= NULL
, *csave
= NULL
;
467 int flags
, osflags
, mode
, infd
;
473 if (scan_mode(oflags
, &osflags
) < 0)
477 rof
= OF_RDWR
| OF_UNLINK
;
478 if (osflags
& O_APPEND
)
480 mode
= (osflags
== O_RDONLY
) ? R_OK
: R_OK
| W_OK
;
482 if ((osflags
& O_APPEND
) && ((p
= which_protocol(file
)) == PROTO_IMAP
||
483 p
== PROTO_MAILDIR
)) {
484 flags
|= (p
== PROTO_IMAP
) ? FP_IMAP
: FP_MAILDIR
;
485 osflags
= O_RDWR
| O_APPEND
| O_CREAT
;
490 if ((ext
= strrchr(file
, '.')) != NULL
) {
491 if (!strcmp(ext
, ".gz"))
493 else if (!strcmp(ext
, ".xz"))
495 else if (!strcmp(ext
, ".bz2"))
499 #define _X1 "file-hook-load-"
501 #define _X2 "file-hook-save-"
502 size_t l
= strlen(++ext
);
503 char *vbuf
= ac_alloc(l
+ MAX(sizeof(_X1
), sizeof(_X2
)));
505 memcpy(vbuf
, _X1
, sizeof(_X1
) -1);
506 memcpy(vbuf
+ sizeof(_X1
) -1, ext
, l
);
507 vbuf
[sizeof(_X1
) -1 + l
] = '\0';
508 cload
= vok_vlook(vbuf
);
509 memcpy(vbuf
, _X2
, sizeof(_X2
) -1);
510 memcpy(vbuf
+ sizeof(_X2
) -1, ext
, l
);
511 vbuf
[sizeof(_X2
) -1 + l
] = '\0';
512 csave
= vok_vlook(vbuf
);
517 if ((csave
!= NULL
) && (cload
!= NULL
))
519 else if ((csave
!= NULL
) | (cload
!= NULL
)) {
520 n_alert(_("Only one of *mailbox-(load|save)-%s* is set! "
521 "Treating as plain text!"), ext
);
529 rv
= Fopen(file
, oflags
);
533 if ((infd
= open(file
, (mode
& W_OK
) ? O_RDWR
: O_RDONLY
)) == -1 &&
534 (!(osflags
& O_CREAT
) || errno
!= ENOENT
))
538 /* Note rv is not yet register_file()d, fclose() it in error path! */
539 if ((rv
= Ftmp(NULL
, "zopen", rof
, 0600)) == NULL
) {
540 n_perr(_("tmpfile"), 0);
544 if (flags
& (FP_IMAP
| FP_MAILDIR
))
546 else if (infd
>= 0) {
547 if (_file_load(flags
, infd
, fileno(rv
), cload
) < 0) {
557 if ((infd
= creat(file
, 0666)) == -1) {
568 if (!(osflags
& O_APPEND
))
570 if ((offset
= ftell(rv
)) == -1) {
576 register_file(rv
, osflags
, 0, 0, flags
, file
, offset
, csave
);
583 Ftmp(char **fn
, char const *prefix
, enum oflags oflags
, int mode
)
586 size_t maxname
, tries
;
591 assert((oflags
& OF_WRONLY
) || (oflags
& OF_RDWR
));
592 assert(!(oflags
& OF_RDONLY
));
599 if ((pc
= pathconf(tempdir
, _PC_NAME_MAX
)) != -1)
600 maxname
= (size_t)pc
;
605 cp
= smalloc(strlen(tempdir
) + 1 + maxname
+1);
606 cp
= sstpcpy(cp
, tempdir
);
609 osoflags
= O_CREAT
| O_EXCL
| _O_CLOEXEC
;
610 osoflags
|= (oflags
& OF_WRONLY
) ? O_WRONLY
: O_RDWR
;
611 if (oflags
& OF_APPEND
)
612 osoflags
|= O_APPEND
;
614 for (tries
= 0;; ++tries
) {
618 x
= sstpcpy(cp
, UAGENT
);
620 if (*prefix
!= '\0') {
621 x
= sstpcpy(x
, prefix
);
625 /* Calculate length of a random string addon */
626 i
= PTR2SIZE(x
- cp
);
627 if (i
>= maxname
>> 1) {
632 /* But don't be too fatalistic */
633 if (i
> 8 && tries
< FTMP_OPEN_TRIES
/ 2)
635 memcpy(x
, getrandstring(i
), i
+1);
638 if ((fd
= open(cp_base
, osoflags
, mode
)) != -1) {
642 if (tries
>= FTMP_OPEN_TRIES
) {
649 if (oflags
& OF_REGISTER
)
650 fp
= Fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"), FAL0
);
652 fp
= fdopen(fd
, (oflags
& OF_RDWR
? "w+" : "w"));
654 if (fp
== NULL
|| (oflags
& OF_UNLINK
)) {
665 if (fp
== NULL
|| !(oflags
& OF_HOLDSIGS
))
672 if ((cp
= cp_base
) != NULL
)
678 Ftmp_release(char **fn
)
707 pipe_cloexec(int fd
[2])
713 if (pipe2(fd
, O_CLOEXEC
) == -1)
718 (void)fcntl(fd
[0], F_SETFD
, FD_CLOEXEC
);
719 (void)fcntl(fd
[1], F_SETFD
, FD_CLOEXEC
);
728 Popen(char const *cmd
, char const *mode
, char const *sh
,
729 char const **env_addon
, int newfd1
)
731 int p
[2], myside
, hisside
, fd0
, fd1
, pid
;
732 char mod
[2] = {'0', '\0'};
737 /* First clean up child structures */
739 struct child
**cpp
, *cp
;
742 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
744 for (cpp
= &_popen_child
; *cpp
!= NULL
;) {
745 if ((*cpp
)->pid
== -1) {
753 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
756 if (!pipe_cloexec(p
))
762 hisside
= fd1
= p
[WRITE
];
764 } else if (*mode
== 'W') {
766 hisside
= fd0
= p
[READ
];
771 hisside
= fd0
= p
[READ
];
778 if (cmd
== (char*)-1) {
779 if ((pid
= fork_child()) == -1)
780 n_perr(_("fork"), 0);
782 union {char const *ccp
; int (*ptf
)(void); int es
;} u
;
783 prepare_child(&nset
, fd0
, fd1
);
790 } else if (sh
== NULL
) {
791 pid
= start_command(cmd
, &nset
, fd0
, fd1
, NULL
, NULL
, NULL
, env_addon
);
793 pid
= start_command(sh
, &nset
, fd0
, fd1
, "-c", cmd
, NULL
, env_addon
);
801 if ((rv
= fdopen(myside
, mod
)) != NULL
)
802 register_file(rv
, 0, 1, pid
, FP_RAW
, NULL
, 0L, NULL
);
811 Pclose(FILE *ptr
, bool_t dowait
)
821 unregister_file(ptr
);
825 sigaddset(&nset
, SIGINT
);
826 sigaddset(&nset
, SIGHUP
);
827 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
828 rv
= wait_child(pid
, NULL
);
829 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
840 close_all_files(void)
843 while (fp_head
!= NULL
)
845 Pclose(fp_head
->fp
, TRU1
);
858 cp
= _findchild(0, TRU1
);
860 if ((cp
->pid
= pid
= fork()) == -1) {
862 n_perr(_("fork"), 0);
869 run_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
870 char const *a0
, char const *a1
, char const *a2
)
875 if ((rv
= start_command(cmd
, mask
, infd
, outfd
, a0
, a1
, a2
, NULL
)) < 0)
878 rv
= wait_command(rv
);
884 start_command(char const *cmd
, sigset_t
*mask
, int infd
, int outfd
,
885 char const *a0
, char const *a1
, char const *a2
,
886 char const **env_addon
)
891 if ((rv
= fork_child()) == -1) {
892 n_perr(_("fork"), 0);
894 } else if (rv
== 0) {
898 if (env_addon
!= NULL
) { /* TODO env_addon; should have struct child */
899 extern char **environ
;
900 size_t ei
, ei_orig
, ai
, ai_orig
;
903 /* TODO note we don't check the POSIX limit:
904 * the total space used to store the environment and the arguments to
905 * the process is limited to {ARG_MAX} bytes */
906 for (ei
= 0; environ
[ei
] != NULL
; ++ei
)
909 for (ai
= 0; env_addon
[ai
] != NULL
; ++ai
)
912 env
= ac_alloc(sizeof(*env
) * (ei
+ ai
+1));
913 memcpy(env
, environ
, sizeof(*env
) * ei
);
915 /* Replace all those keys that yet exist */
917 char const *ee
, *kvs
;
921 kvs
= strchr(ee
, '=');
923 kl
= PTR2SIZE(kvs
- ee
);
925 for (ei
= ei_orig
; ei
-- > 0;) {
926 char const *ekvs
= strchr(env
[ei
], '=');
927 if (ekvs
!= NULL
&& kl
== PTR2SIZE(ekvs
- env
[ei
]) &&
928 !memcmp(ee
, env
[ei
], kl
)) {
929 env
[ei
] = UNCONST(ee
);
930 env_addon
[ai
] = NULL
;
936 /* And append the rest */
937 for (ei
= ei_orig
, ai
= ai_orig
; ai
-- > 0;)
938 if (env_addon
[ai
] != NULL
)
939 env
[ei
++] = UNCONST(env_addon
[ai
]);
945 i
= getrawlist(cmd
, strlen(cmd
), argv
, NELEM(argv
), 0);
947 if ((argv
[i
++] = UNCONST(a0
)) != NULL
&&
948 (argv
[i
++] = UNCONST(a1
)) != NULL
&&
949 (argv
[i
++] = UNCONST(a2
)) != NULL
)
951 prepare_child(mask
, infd
, outfd
);
952 execvp(argv
[0], argv
);
961 prepare_child(sigset_t
*nset
, int infd
, int outfd
)
967 /* All file descriptors other than 0, 1, and 2 are supposed to be cloexec */
969 dup2(infd
, STDIN_FILENO
);
971 dup2(outfd
, STDOUT_FILENO
);
974 for (i
= 1; i
< NSIG
; ++i
)
975 if (sigismember(nset
, i
))
976 safe_signal(i
, SIG_IGN
);
977 if (!sigismember(nset
, SIGINT
))
978 safe_signal(SIGINT
, SIG_DFL
);
982 sigprocmask(SIG_SETMASK
, &fset
, NULL
);
994 sigaddset(&nset
, SIGCHLD
);
995 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
997 if ((cp
= _findchild(pid
, FAL0
)) != NULL
) {
1004 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1009 wait_child(int pid
, int *wait_status
)
1011 sigset_t nset
, oset
;
1018 sigaddset(&nset
, SIGCHLD
);
1019 sigprocmask(SIG_BLOCK
, &nset
, &oset
);
1021 cp
= _findchild(pid
, FAL0
);
1030 sigprocmask(SIG_SETMASK
, &oset
, NULL
);
1032 if (wait_status
!= NULL
)
1034 rv
= (WIFEXITED(ws
) && WEXITSTATUS(ws
) == 0);