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