xref: /original-bsd/libexec/ftpd/popen.c (revision a5a0fb88)
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 are permitted
9  * provided that the above copyright notice and this paragraph are
10  * duplicated in all such forms and that any documentation,
11  * advertising materials, and other materials related to such
12  * distribution and use acknowledge that the software was developed
13  * by the University of California, Berkeley.  The name of the
14  * University may not be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
18  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19  *
20  * static char sccsid[] = "@(#)popen.c	5.7 (Berkeley) 9/1/88";
21  */
22 
23 #ifndef lint
24 static char sccsid[] = "@(#)popen.c	5.6 (Berkeley) 12/19/88";
25 #endif /* not lint */
26 
27 #include <sys/types.h>
28 #include <sys/signal.h>
29 #include <sys/wait.h>
30 #include <stdio.h>
31 
32 /*
33  * Special version of popen which avoids call to shell.  This insures noone
34  * may create a pipe to a hidden program as a side effect of a list or dir
35  * command.
36  */
37 static int *pids;
38 static int fds;
39 
40 FILE *
41 ftpd_popen(program, type)
42 	char *program, *type;
43 {
44 	register char *cp;
45 	FILE *iop;
46 	int argc, gargc, pdes[2], pid;
47 	char **pop, *argv[100], *gargv[1000], *vv[2];
48 	extern char **glob(), **copyblk(), *strtok(), *malloc();
49 
50 	if (*type != 'r' && *type != 'w' || type[1])
51 		return(NULL);
52 
53 	if (!pids) {
54 		if ((fds = getdtablesize()) <= 0)
55 			return(NULL);
56 		if ((pids = (int *)malloc((u_int)(fds * sizeof(int)))) == NULL)
57 			return(NULL);
58 		bzero((char *)pids, fds * sizeof(int));
59 	}
60 	if (pipe(pdes) < 0)
61 		return(NULL);
62 
63 	/* break up string into pieces */
64 	for (argc = 0, cp = program;; cp = NULL)
65 		if (!(argv[argc++] = strtok(cp, " \t\n")))
66 			break;
67 
68 	/* glob each piece */
69 	gargv[0] = argv[0];
70 	for (gargc = argc = 1; argv[argc]; argc++) {
71 		if (!(pop = glob(argv[argc]))) {	/* globbing failed */
72 			vv[0] = argv[argc];
73 			vv[1] = NULL;
74 			pop = copyblk(vv);
75 		}
76 		argv[argc] = (char *)pop;		/* save to free later */
77 		while (*pop && gargc < 1000)
78 			gargv[gargc++] = *pop++;
79 	}
80 	gargv[gargc] = NULL;
81 
82 	iop = NULL;
83 	switch(pid = vfork()) {
84 	case -1:			/* error */
85 		(void)close(pdes[0]);
86 		(void)close(pdes[1]);
87 		goto pfree;
88 		/* NOTREACHED */
89 	case 0:				/* child */
90 		if (*type == 'r') {
91 			if (pdes[1] != 1) {
92 				dup2(pdes[1], 1);
93 				dup2(pdes[1], 2);	/* stderr, too! */
94 				(void)close(pdes[1]);
95 			}
96 			(void)close(pdes[0]);
97 		} else {
98 			if (pdes[0] != 0) {
99 				dup2(pdes[0], 0);
100 				(void)close(pdes[0]);
101 			}
102 			(void)close(pdes[1]);
103 		}
104 		execv(gargv[0], gargv);
105 		_exit(1);
106 	}
107 	/* parent; assume fdopen can't fail...  */
108 	if (*type == 'r') {
109 		iop = fdopen(pdes[0], type);
110 		(void)close(pdes[1]);
111 	} else {
112 		iop = fdopen(pdes[1], type);
113 		(void)close(pdes[0]);
114 	}
115 	pids[fileno(iop)] = pid;
116 
117 pfree:	for (argc = 1; argv[argc] != NULL; argc++) {
118 		blkfree((char **)argv[argc]);
119 		free((char *)argv[argc]);
120 	}
121 	return(iop);
122 }
123 
124 ftpd_pclose(iop)
125 	FILE *iop;
126 {
127 	register int fdes;
128 	int omask;
129 	union wait stat_loc;
130 	int pid;
131 
132 	/*
133 	 * pclose returns -1 if stream is not associated with a
134 	 * `popened' command, or, if already `pclosed'.
135 	 */
136 	if (pids == 0 || pids[fdes = fileno(iop)] == 0)
137 		return(-1);
138 	(void)fclose(iop);
139 	omask = sigblock(sigmask(SIGINT)|sigmask(SIGQUIT)|sigmask(SIGHUP));
140 	while ((pid = wait(&stat_loc)) != pids[fdes] && pid != -1);
141 	(void)sigsetmask(omask);
142 	pids[fdes] = 0;
143 	return(pid == -1 ? -1 : stat_loc.w_status);
144 }
145