mime_child.c revision 1.5 1 /* $NetBSD: mime_child.c,v 1.5 2006/11/29 01:29:46 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Anon Ymous.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39
40 #ifdef MIME_SUPPORT
41
42 #include <sys/cdefs.h>
43 #ifndef __lint__
44 __RCSID("$NetBSD: mime_child.c,v 1.5 2006/11/29 01:29:46 christos Exp $");
45 #endif /* not __lint__ */
46
47 #include <assert.h>
48 #include <fcntl.h>
49 #include <signal.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53
54 #include "def.h"
55 #include "extern.h"
56
57 #ifdef MIME_SUPPORT
58 #include "mime.h"
59 #include "mime_child.h"
60 #endif
61
62 /*
63 * This module contains the core routines used by mime modules that
64 * need to fork children. Nothing else in the mime modules should be
65 * doing a pipe(), fork(), or a call to any popen functions that do.
66 * These routines are tied to the file registry in popen.c and hence
67 * share much of popen code.
68 */
69
70 #define READ 0
71 #define WRITE 1
72
73 static int
74 get_cmd_flags(const char *cmd, const char **cmd_start)
75 {
76 const char *cp;
77 int flags;
78
79 flags = 0;
80 for (cp = cmd; cp && *cp; cp++)
81 switch (*cp) {
82 case '+':
83 flags |= CMD_FLAG_ALTERNATIVE;
84 break;
85 case '-':
86 flags |= CMD_FLAG_NO_DECODE;
87 break;
88 case '!':
89 flags |= CMD_FLAG_SHELLCMD;
90 break;
91 default:
92 goto done;
93 }
94 done:
95 if (cmd_start)
96 *cmd_start = cp;
97
98 return flags;
99 }
100
101
102 static int
103 prepare_pipe(sigset_t *nset, int p[2])
104 {
105 if (pipe(p) == -1)
106 return -1;
107
108 (void)fcntl(p[READ], F_SETFD, FD_CLOEXEC);
109 (void)fcntl(p[WRITE], F_SETFD, FD_CLOEXEC);
110 /*
111 * We _must_ ignore SIGINT and SIGPIPE or the child
112 * will end up in our earlier handlers.
113 */
114 (void)sigemptyset(nset);
115 (void)sigaddset(nset, SIGINT);
116 (void)sigaddset(nset, SIGPIPE);
117 (void)sigaddset(nset, SIGHUP);
118 (void)sigaddset(nset, SIGTSTP);
119 (void)sigaddset(nset, SIGTTOU);
120 (void)sigaddset(nset, SIGTTIN);
121
122 return 0;
123 }
124
125
126 PUBLIC int
127 mime_run_command(const char *cmd, FILE *fo)
128 {
129 sigset_t nset;
130 FILE *nfo;
131 pid_t pid;
132 int p[2];
133 int flags;
134
135 if (cmd == NULL)
136 return 0;
137
138 flags = get_cmd_flags(cmd, &cmd);
139 if (fo == NULL) /* no output file, just return the flags! */
140 return flags;
141
142 if ((flags & CMD_FLAG_SHELLCMD) != 0) { /* run command under the shell */
143 char *cp;
144 char *shellcmd;
145 if ((shellcmd = value(ENAME_SHELL)) == NULL)
146 shellcmd = __UNCONST(_PATH_CSHELL);
147 (void)sasprintf(&cp, "%s -c '%s'", shellcmd, cmd);
148 cmd = cp;
149 }
150 if (prepare_pipe(&nset, p) != 0) {
151 warn("mime_run_command: prepare_pipe");
152 return flags; /* XXX - this or -1? */
153 }
154 flush_files(fo, 0); /* flush fo, all registered files, and stdout */
155
156 switch (pid = start_command(cmd, &nset, p[READ], fileno(fo), NULL)) {
157 case -1: /* error */
158 /* start_command already did a warn(). */
159 warnx("mime_run_command: %s", cmd); /* tell a bit more */
160 (void)close(p[READ]);
161 (void)close(p[WRITE]);
162 return flags; /* XXX - this or -1? */
163
164 case 0: /* child */
165 assert(/*CONSTCOND*/ 0); /* a real coding error! */
166 /* NOTREACHED */
167
168 default: /* parent */
169 (void)close(p[READ]);
170
171 nfo = fdopen(p[WRITE], "w");
172 if (nfo == NULL) {
173 warn("mime_run_command: fdopen");
174 (void)close(p[WRITE]);
175 warn("fdopen");
176 return flags;
177 }
178 register_file(nfo, 1, pid);
179 return flags;
180 }
181 }
182
183
184 PUBLIC void
185 mime_run_function(void (*fn)(FILE *, FILE *, void *), FILE *fo, void *cookie)
186 {
187 sigset_t nset;
188 FILE *nfo;
189 pid_t pid;
190 int p[2];
191
192 if (prepare_pipe(&nset, p) != 0) {
193 warn("mime_run_function: pipe");
194 return;
195 }
196 flush_files(fo, 0); /* flush fo, all registered files, and stdout */
197
198 switch (pid = fork()) {
199 case -1: /* error */
200 warn("mime_run_function: fork");
201 (void)close(p[READ]);
202 (void)close(p[WRITE]);
203 return;
204
205 case 0: /* child */
206 (void)close(p[WRITE]);
207 prepare_child(&nset, p[READ], fileno(fo));
208 fn(stdin, stdout, cookie);
209 (void)fflush(stdout);
210 _exit(0);
211 /* NOTREACHED */
212
213 default: /* parent */
214 (void)close(p[READ]);
215 nfo = fdopen(p[WRITE], "w");
216 if (nfo == NULL) {
217 warn("run_function: fdopen");
218 (void)close(p[WRITE]);
219 return;
220 }
221 register_file(nfo, 1, pid);
222 return;
223 }
224 }
225
226 #endif /* MIME_SUPPORT */
227