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