Home | History | Annotate | Line # | Download | only in ftpd
popen.c revision 1.5
      1  1.5      cgd /*	$NetBSD: popen.c,v 1.5 1995/04/11 02:45:00 cgd Exp $	*/
      2  1.5      cgd 
      3  1.1      cgd /*
      4  1.3  deraadt  * Copyright (c) 1988, 1993, 1994
      5  1.3  deraadt  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * This code is derived from software written by Ken Arnold and
      8  1.1      cgd  * published in UNIX Review, Vol. 6, No. 8.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  *
     38  1.1      cgd  */
     39  1.1      cgd 
     40  1.1      cgd #ifndef lint
     41  1.5      cgd #if 0
     42  1.3  deraadt static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 4/6/94";
     43  1.5      cgd #else
     44  1.5      cgd static char rcsid[] = "$NetBSD: popen.c,v 1.5 1995/04/11 02:45:00 cgd Exp $";
     45  1.5      cgd #endif
     46  1.1      cgd #endif /* not lint */
     47  1.1      cgd 
     48  1.1      cgd #include <sys/types.h>
     49  1.1      cgd #include <sys/wait.h>
     50  1.3  deraadt 
     51  1.3  deraadt #include <errno.h>
     52  1.3  deraadt #include <glob.h>
     53  1.1      cgd #include <signal.h>
     54  1.1      cgd #include <stdio.h>
     55  1.1      cgd #include <stdlib.h>
     56  1.1      cgd #include <string.h>
     57  1.3  deraadt #include <unistd.h>
     58  1.3  deraadt 
     59  1.3  deraadt #include "extern.h"
     60  1.1      cgd 
     61  1.1      cgd /*
     62  1.3  deraadt  * Special version of popen which avoids call to shell.  This ensures noone
     63  1.1      cgd  * may create a pipe to a hidden program as a side effect of a list or dir
     64  1.1      cgd  * command.
     65  1.1      cgd  */
     66  1.1      cgd static int *pids;
     67  1.1      cgd static int fds;
     68  1.1      cgd 
     69  1.1      cgd FILE *
     70  1.1      cgd ftpd_popen(program, type)
     71  1.1      cgd 	char *program, *type;
     72  1.1      cgd {
     73  1.3  deraadt 	char *cp;
     74  1.1      cgd 	FILE *iop;
     75  1.1      cgd 	int argc, gargc, pdes[2], pid;
     76  1.3  deraadt 	char **pop, *argv[100], *gargv[1000];
     77  1.1      cgd 
     78  1.1      cgd 	if (*type != 'r' && *type != 'w' || type[1])
     79  1.3  deraadt 		return (NULL);
     80  1.1      cgd 
     81  1.1      cgd 	if (!pids) {
     82  1.1      cgd 		if ((fds = getdtablesize()) <= 0)
     83  1.3  deraadt 			return (NULL);
     84  1.1      cgd 		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
     85  1.3  deraadt 			return (NULL);
     86  1.3  deraadt 		memset(pids, 0, fds * sizeof(int));
     87  1.1      cgd 	}
     88  1.1      cgd 	if (pipe(pdes) < 0)
     89  1.3  deraadt 		return (NULL);
     90  1.1      cgd 
     91  1.1      cgd 	/* break up string into pieces */
     92  1.1      cgd 	for (argc = 0, cp = program;; cp = NULL)
     93  1.1      cgd 		if (!(argv[argc++] = strtok(cp, " \t\n")))
     94  1.1      cgd 			break;
     95  1.1      cgd 
     96  1.1      cgd 	/* glob each piece */
     97  1.1      cgd 	gargv[0] = argv[0];
     98  1.1      cgd 	for (gargc = argc = 1; argv[argc]; argc++) {
     99  1.3  deraadt 		glob_t gl;
    100  1.3  deraadt 		int flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
    101  1.3  deraadt 
    102  1.3  deraadt 		memset(&gl, 0, sizeof(gl));
    103  1.3  deraadt 		if (glob(argv[argc], flags, NULL, &gl))
    104  1.3  deraadt 			gargv[gargc++] = strdup(argv[argc]);
    105  1.3  deraadt 		else
    106  1.3  deraadt 			for (pop = gl.gl_pathv; *pop; pop++)
    107  1.3  deraadt 				gargv[gargc++] = strdup(*pop);
    108  1.3  deraadt 		globfree(&gl);
    109  1.1      cgd 	}
    110  1.1      cgd 	gargv[gargc] = NULL;
    111  1.1      cgd 
    112  1.1      cgd 	iop = NULL;
    113  1.1      cgd 	switch(pid = vfork()) {
    114  1.1      cgd 	case -1:			/* error */
    115  1.1      cgd 		(void)close(pdes[0]);
    116  1.1      cgd 		(void)close(pdes[1]);
    117  1.1      cgd 		goto pfree;
    118  1.1      cgd 		/* NOTREACHED */
    119  1.1      cgd 	case 0:				/* child */
    120  1.1      cgd 		if (*type == 'r') {
    121  1.3  deraadt 			if (pdes[1] != STDOUT_FILENO) {
    122  1.3  deraadt 				dup2(pdes[1], STDOUT_FILENO);
    123  1.1      cgd 				(void)close(pdes[1]);
    124  1.1      cgd 			}
    125  1.3  deraadt 			dup2(STDOUT_FILENO, STDERR_FILENO); /* stderr too! */
    126  1.1      cgd 			(void)close(pdes[0]);
    127  1.1      cgd 		} else {
    128  1.3  deraadt 			if (pdes[0] != STDIN_FILENO) {
    129  1.3  deraadt 				dup2(pdes[0], STDIN_FILENO);
    130  1.1      cgd 				(void)close(pdes[0]);
    131  1.1      cgd 			}
    132  1.1      cgd 			(void)close(pdes[1]);
    133  1.1      cgd 		}
    134  1.1      cgd 		execv(gargv[0], gargv);
    135  1.1      cgd 		_exit(1);
    136  1.1      cgd 	}
    137  1.1      cgd 	/* parent; assume fdopen can't fail...  */
    138  1.1      cgd 	if (*type == 'r') {
    139  1.1      cgd 		iop = fdopen(pdes[0], type);
    140  1.1      cgd 		(void)close(pdes[1]);
    141  1.1      cgd 	} else {
    142  1.1      cgd 		iop = fdopen(pdes[1], type);
    143  1.1      cgd 		(void)close(pdes[0]);
    144  1.1      cgd 	}
    145  1.1      cgd 	pids[fileno(iop)] = pid;
    146  1.1      cgd 
    147  1.3  deraadt pfree:	for (argc = 1; gargv[argc] != NULL; argc++)
    148  1.3  deraadt 		free(gargv[argc]);
    149  1.3  deraadt 
    150  1.3  deraadt 	return (iop);
    151  1.1      cgd }
    152  1.1      cgd 
    153  1.3  deraadt int
    154  1.1      cgd ftpd_pclose(iop)
    155  1.1      cgd 	FILE *iop;
    156  1.1      cgd {
    157  1.3  deraadt 	int fdes, omask, status;
    158  1.3  deraadt 	pid_t pid;
    159  1.4  mycroft 	sigset_t sigset, osigset;
    160  1.1      cgd 
    161  1.1      cgd 	/*
    162  1.1      cgd 	 * pclose returns -1 if stream is not associated with a
    163  1.1      cgd 	 * `popened' command, or, if already `pclosed'.
    164  1.1      cgd 	 */
    165  1.1      cgd 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
    166  1.3  deraadt 		return (-1);
    167  1.1      cgd 	(void)fclose(iop);
    168  1.4  mycroft 	sigemptyset(&sigset);
    169  1.4  mycroft 	sigaddset(&sigset, SIGINT);
    170  1.4  mycroft 	sigaddset(&sigset, SIGQUIT);
    171  1.4  mycroft 	sigaddset(&sigset, SIGHUP);
    172  1.4  mycroft 	sigprocmask(SIG_BLOCK, &sigset, &osigset);
    173  1.3  deraadt 	while ((pid = waitpid(pids[fdes], &status, 0)) < 0 && errno == EINTR)
    174  1.3  deraadt 		continue;
    175  1.4  mycroft 	sigprocmask(SIG_SETMASK, &osigset, NULL);
    176  1.1      cgd 	pids[fdes] = 0;
    177  1.3  deraadt 	if (pid < 0)
    178  1.3  deraadt 		return (pid);
    179  1.3  deraadt 	if (WIFEXITED(status))
    180  1.3  deraadt 		return (WEXITSTATUS(status));
    181  1.3  deraadt 	return (1);
    182  1.1      cgd }
    183