Home | History | Annotate | Line # | Download | only in mail
mime_child.c revision 1.1
      1 /*	$NetBSD: mime_child.c,v 1.1 2006/10/21 21:37:21 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.1 2006/10/21 21:37:21 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)sigfillset(nset);	/* XXX - ignore all of em? */
    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("SHELL")) == NULL)
    139 			shellcmd = __UNCONST(_PATH_CSHELL);
    140 		if (asprintf(&cp, "%s -c '%s'", shellcmd, cmd) == -1)
    141 			warn("mime_run_command: asprintf");
    142 		else {
    143 			cmd = savestr(cp);
    144 			free(cp);
    145 		}
    146 	}
    147 	if (prepare_pipe(&nset, p) != 0) {
    148 		warn("mime_run_command: prepare_pipe");
    149 		return flags;	/* XXX - this or -1? */
    150 	}
    151 	flush_files(fo, 0); /* flush fo, all registered files, and stdout */
    152 
    153 	switch (pid = start_command(cmd, &nset, p[READ], fileno(fo), NULL)) {
    154 	case -1:	/* error */
    155 		/* start_command already did a warn(). */
    156 		warnx("mime_run_command: %s", cmd); /* tell a bit more */
    157 		(void)close(p[READ]);
    158 		(void)close(p[WRITE]);
    159 		return flags;			/* XXX - this or -1? */
    160 
    161 	case 0:		/* child */
    162 		assert(/*CONSTCOND*/ 0);	/* a real coding error! */
    163 		/* NOTREACHED */
    164 
    165 	default:	/* parent */
    166 		(void)close(p[READ]);
    167 
    168 		nfo = fdopen(p[WRITE], "w");
    169 		if (nfo == NULL) {
    170 			warn("mime_run_command: fdopen");
    171 			(void)close(p[WRITE]);
    172 			warn("fdopen");
    173 			return flags;
    174 		}
    175 		register_file(nfo, 1, pid);
    176 		return flags;
    177 	}
    178 }
    179 
    180 
    181 PUBLIC void
    182 mime_run_function(void (*fn)(FILE *, FILE *, void *), FILE *fo, void *cookie)
    183 {
    184 	sigset_t nset;
    185 	FILE *nfo;
    186 	pid_t pid;
    187 	int p[2];
    188 
    189 	if (prepare_pipe(&nset, p) != 0) {
    190 		warn("mime_run_function: pipe");
    191 		return;
    192 	}
    193 	flush_files(fo, 0); /* flush fo, all registered files, and stdout */
    194 
    195 	switch (pid = fork()) {
    196 	case -1:	/* error */
    197 		warn("mime_run_function: fork");
    198 		(void)close(p[READ]);
    199 		(void)close(p[WRITE]);
    200 		return;
    201 
    202 	case 0:		/* child */
    203 		(void)close(p[WRITE]);
    204 		prepare_child(&nset, p[READ], fileno(fo));
    205 		fn(stdin, stdout, cookie);
    206 		(void)fflush(stdout);
    207 		_exit(0);
    208 		/* NOTREACHED */
    209 
    210 	default:	/* parent */
    211 		(void)close(p[READ]);
    212 		nfo = fdopen(p[WRITE], "w");
    213 		if (nfo == NULL) {
    214 			warn("run_function: fdopen");
    215 			(void)close(p[WRITE]);
    216 			return;
    217 		}
    218 		register_file(nfo, 1, pid);
    219 		return;
    220 	}
    221 }
    222 
    223 #endif /* MIME_SUPPORT */
    224