Home | History | Annotate | Line # | Download | only in mail
mime_child.c revision 1.4
      1 /*	$NetBSD: mime_child.c,v 1.4 2006/11/29 01:25:28 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.4 2006/11/29 01:25:28 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 	return 0;
    118 }
    119 
    120 
    121 PUBLIC int
    122 mime_run_command(const char *cmd, FILE *fo)
    123 {
    124 	sigset_t nset;
    125 	FILE *nfo;
    126 	pid_t pid;
    127 	int p[2];
    128 	int flags;
    129 
    130 	if (cmd == NULL)
    131 		return 0;
    132 
    133 	flags = get_cmd_flags(cmd, &cmd);
    134 	if (fo == NULL)		/* no output file, just return the flags! */
    135 		return flags;
    136 
    137 	if ((flags & CMD_FLAG_SHELLCMD) != 0) {	/* run command under the shell */
    138 		char *cp;
    139 		char *shellcmd;
    140 		if ((shellcmd = value(ENAME_SHELL)) == NULL)
    141 			shellcmd = __UNCONST(_PATH_CSHELL);
    142 		(void)sasprintf(&cp, "%s -c '%s'", shellcmd, cmd);
    143 		cmd = cp;
    144 	}
    145 	if (prepare_pipe(&nset, p) != 0) {
    146 		warn("mime_run_command: prepare_pipe");
    147 		return flags;	/* XXX - this or -1? */
    148 	}
    149 	flush_files(fo, 0); /* flush fo, all registered files, and stdout */
    150 
    151 	switch (pid = start_command(cmd, &nset, p[READ], fileno(fo), NULL)) {
    152 	case -1:	/* error */
    153 		/* start_command already did a warn(). */
    154 		warnx("mime_run_command: %s", cmd); /* tell a bit more */
    155 		(void)close(p[READ]);
    156 		(void)close(p[WRITE]);
    157 		return flags;			/* XXX - this or -1? */
    158 
    159 	case 0:		/* child */
    160 		assert(/*CONSTCOND*/ 0);	/* a real coding error! */
    161 		/* NOTREACHED */
    162 
    163 	default:	/* parent */
    164 		(void)close(p[READ]);
    165 
    166 		nfo = fdopen(p[WRITE], "w");
    167 		if (nfo == NULL) {
    168 			warn("mime_run_command: fdopen");
    169 			(void)close(p[WRITE]);
    170 			warn("fdopen");
    171 			return flags;
    172 		}
    173 		register_file(nfo, 1, pid);
    174 		return flags;
    175 	}
    176 }
    177 
    178 
    179 PUBLIC void
    180 mime_run_function(void (*fn)(FILE *, FILE *, void *), FILE *fo, void *cookie)
    181 {
    182 	sigset_t nset;
    183 	FILE *nfo;
    184 	pid_t pid;
    185 	int p[2];
    186 
    187 	if (prepare_pipe(&nset, p) != 0) {
    188 		warn("mime_run_function: pipe");
    189 		return;
    190 	}
    191 	flush_files(fo, 0); /* flush fo, all registered files, and stdout */
    192 
    193 	switch (pid = fork()) {
    194 	case -1:	/* error */
    195 		warn("mime_run_function: fork");
    196 		(void)close(p[READ]);
    197 		(void)close(p[WRITE]);
    198 		return;
    199 
    200 	case 0:		/* child */
    201 		(void)close(p[WRITE]);
    202 		prepare_child(&nset, p[READ], fileno(fo));
    203 		fn(stdin, stdout, cookie);
    204 		(void)fflush(stdout);
    205 		_exit(0);
    206 		/* NOTREACHED */
    207 
    208 	default:	/* parent */
    209 		(void)close(p[READ]);
    210 		nfo = fdopen(p[WRITE], "w");
    211 		if (nfo == NULL) {
    212 			warn("run_function: fdopen");
    213 			(void)close(p[WRITE]);
    214 			return;
    215 		}
    216 		register_file(nfo, 1, pid);
    217 		return;
    218 	}
    219 }
    220 
    221 #endif /* MIME_SUPPORT */
    222