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