Home | History | Annotate | Line # | Download | only in ftpd
popen.c revision 1.8
      1 /*	$NetBSD: popen.c,v 1.8 1997/06/18 19:05:52 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1988, 1993, 1994
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software written by Ken Arnold and
      8  * published in UNIX Review, Vol. 6, No. 8.
      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 University of
     21  *	California, Berkeley and its contributors.
     22  * 4. Neither the name of the University nor the names of its contributors
     23  *    may be used to endorse or promote products derived from this software
     24  *    without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  * SUCH DAMAGE.
     37  *
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 #if 0
     43 static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
     44 #else
     45 __RCSID("$NetBSD: popen.c,v 1.8 1997/06/18 19:05:52 christos Exp $");
     46 #endif
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/wait.h>
     51 
     52 #include <errno.h>
     53 #include <glob.h>
     54 #include <signal.h>
     55 #include <stdio.h>
     56 #include <stdlib.h>
     57 #include <string.h>
     58 #include <unistd.h>
     59 
     60 #include "extern.h"
     61 
     62 #define INCR	100
     63 /*
     64  * Special version of popen which avoids call to shell.  This ensures noone
     65  * may create a pipe to a hidden program as a side effect of a list or dir
     66  * command.
     67  */
     68 static int *pids;
     69 static int fds;
     70 
     71 FILE *
     72 ftpd_popen(program, type, nostderr)
     73 	char *program, *type;
     74 	int nostderr;
     75 {
     76 	char *cp;
     77 	FILE *iop = NULL;
     78 	int argc, gargc, pdes[2], pid;
     79 	char **pop, **np;
     80 	char **argv = NULL, **gargv = NULL;
     81 	size_t nargc = 100, ngargc = 100;
     82 #ifdef __GNUC__
     83 	(void) &iop;
     84 	(void) &gargc;
     85 	(void) &gargv;
     86 	(void) &argv;
     87 #endif
     88 
     89 	if ((*type != 'r' && *type != 'w') || type[1])
     90 		return (NULL);
     91 
     92 	if (!pids) {
     93 		if ((fds = getdtablesize()) <= 0)
     94 			return (NULL);
     95 		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
     96 			return (NULL);
     97 		memset(pids, 0, fds * sizeof(int));
     98 	}
     99 	if (pipe(pdes) < 0)
    100 		return (NULL);
    101 
    102 	if ((argv = malloc(nargc * sizeof(char *))) == NULL)
    103 		return NULL;
    104 
    105 #define CHECKMORE(c, v, n) \
    106 	if (c >= n + 2) { \
    107 		n += INCR; \
    108 		if ((np = realloc(v, n * sizeof(char *))) == NULL) \
    109 			goto pfree; \
    110 		else \
    111 			v = np; \
    112 	}
    113 
    114 	/* break up string into pieces */
    115 	for (argc = 0, cp = program;; cp = NULL) {
    116 		CHECKMORE(argc, argv, nargc)
    117 		if (!(argv[argc++] = strtok(cp, " \t\n")))
    118 			break;
    119 	}
    120 
    121 	if ((gargv = malloc(ngargc * sizeof(char *))) == NULL)
    122 		goto pfree;
    123 
    124 	/* glob each piece */
    125 	gargv[0] = argv[0];
    126 	for (gargc = argc = 1; argv[argc]; argc++) {
    127 		glob_t gl;
    128 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
    129 
    130 		memset(&gl, 0, sizeof(gl));
    131 		if (glob(argv[argc], flags, NULL, &gl)) {
    132 			CHECKMORE(gargc, gargv, ngargc)
    133 			if ((gargv[gargc++] = strdup(argv[argc])) == NULL)
    134 				goto pfree;
    135 		}
    136 		else
    137 			for (pop = gl.gl_pathv; *pop; pop++) {
    138 				CHECKMORE(gargc, gargv, ngargc)
    139 				if ((gargv[gargc++] = strdup(*pop)) == NULL)
    140 					goto pfree;
    141 			}
    142 		globfree(&gl);
    143 	}
    144 	gargv[gargc] = NULL;
    145 
    146 	switch(pid = vfork()) {
    147 	case -1:			/* error */
    148 		(void)close(pdes[0]);
    149 		(void)close(pdes[1]);
    150 		goto pfree;
    151 		/* NOTREACHED */
    152 	case 0:				/* child */
    153 		if (*type == 'r') {
    154 			if (pdes[1] != STDOUT_FILENO) {
    155 				dup2(pdes[1], STDOUT_FILENO);
    156 				(void)close(pdes[1]);
    157 			}
    158 			if (nostderr)
    159 				(void)close(STDERR_FILENO);
    160 			else		/* stderr too! */
    161 				dup2(STDOUT_FILENO, STDERR_FILENO);
    162 			(void)close(pdes[0]);
    163 		} else {
    164 			if (pdes[0] != STDIN_FILENO) {
    165 				dup2(pdes[0], STDIN_FILENO);
    166 				(void)close(pdes[0]);
    167 			}
    168 			(void)close(pdes[1]);
    169 		}
    170 		execv(gargv[0], gargv);
    171 		_exit(1);
    172 	}
    173 	/* parent; assume fdopen can't fail...  */
    174 	if (*type == 'r') {
    175 		iop = fdopen(pdes[0], type);
    176 		(void)close(pdes[1]);
    177 	} else {
    178 		iop = fdopen(pdes[1], type);
    179 		(void)close(pdes[0]);
    180 	}
    181 	pids[fileno(iop)] = pid;
    182 
    183 pfree:	if (gargv) {
    184 		for (argc = 1; argc < gargc; argc++)
    185 			free(gargv[argc]);
    186 		free(gargv);
    187 	}
    188 	if (argv)
    189 		free(argv);
    190 
    191 	return (iop);
    192 }
    193 
    194 int
    195 ftpd_pclose(iop)
    196 	FILE *iop;
    197 {
    198 	int fdes, status;
    199 	pid_t pid;
    200 	sigset_t sigset, osigset;
    201 
    202 	/*
    203 	 * pclose returns -1 if stream is not associated with a
    204 	 * `popened' command, or, if already `pclosed'.
    205 	 */
    206 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
    207 		return (-1);
    208 	(void)fclose(iop);
    209 	sigemptyset(&sigset);
    210 	sigaddset(&sigset, SIGINT);
    211 	sigaddset(&sigset, SIGQUIT);
    212 	sigaddset(&sigset, SIGHUP);
    213 	sigprocmask(SIG_BLOCK, &sigset, &osigset);
    214 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
    215 		continue;
    216 	sigprocmask(SIG_SETMASK, &osigset, NULL);
    217 	pids[fdes] = 0;
    218 	if (pid < 0)
    219 		return (pid);
    220 	if (WIFEXITED(status))
    221 		return (WEXITSTATUS(status));
    222 	return (1);
    223 }
    224