xref: /dragonfly/lib/libc/gen/popen.c (revision 1de703da)
1 /*
2  * Copyright (c) 1988, 1993
3  *	The Regents of the University of California.  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  * $FreeBSD: src/lib/libc/gen/popen.c,v 1.14 2000/01/27 23:06:19 jasone Exp $
37  * $DragonFly: src/lib/libc/gen/popen.c,v 1.2 2003/06/17 04:26:42 dillon Exp $
38  *
39  * @(#)popen.c	8.3 (Berkeley) 5/3/95
40  */
41 
42 #include <sys/param.h>
43 #include <sys/wait.h>
44 
45 #include <signal.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <string.h>
51 #include <paths.h>
52 
53 extern char **environ;
54 
55 static struct pid {
56 	struct pid *next;
57 	FILE *fp;
58 	pid_t pid;
59 } *pidlist;
60 
61 FILE *
62 popen(command, type)
63 	const char *command, *type;
64 {
65 	struct pid *cur;
66 	FILE *iop;
67 	int pdes[2], pid, twoway;
68 	char *argv[4];
69 	struct pid *p;
70 
71 	/*
72 	 * Lite2 introduced two-way popen() pipes using socketpair().
73 	 * FreeBSD's pipe() is bidirectional, so we use that.
74 	 */
75 	if (strchr(type, '+')) {
76 		twoway = 1;
77 		type = "r+";
78 	} else  {
79 		twoway = 0;
80 		if ((*type != 'r' && *type != 'w') || type[1])
81 			return (NULL);
82 	}
83 	if (pipe(pdes) < 0)
84 		return (NULL);
85 
86 	if ((cur = malloc(sizeof(struct pid))) == NULL) {
87 		(void)_close(pdes[0]);
88 		(void)_close(pdes[1]);
89 		return (NULL);
90 	}
91 
92 	argv[0] = "sh";
93 	argv[1] = "-c";
94 	argv[2] = (char *)command;
95 	argv[3] = NULL;
96 
97 	switch (pid = vfork()) {
98 	case -1:			/* Error. */
99 		(void)_close(pdes[0]);
100 		(void)_close(pdes[1]);
101 		free(cur);
102 		return (NULL);
103 		/* NOTREACHED */
104 	case 0:				/* Child. */
105 		if (*type == 'r') {
106 			/*
107 			 * The dup2() to STDIN_FILENO is repeated to avoid
108 			 * writing to pdes[1], which might corrupt the
109 			 * parent's copy.  This isn't good enough in
110 			 * general, since the _exit() is no return, so
111 			 * the compiler is free to corrupt all the local
112 			 * variables.
113 			 */
114 			(void)_close(pdes[0]);
115 			if (pdes[1] != STDOUT_FILENO) {
116 				(void)dup2(pdes[1], STDOUT_FILENO);
117 				(void)_close(pdes[1]);
118 				if (twoway)
119 					(void)dup2(STDOUT_FILENO, STDIN_FILENO);
120 			} else if (twoway && (pdes[1] != STDIN_FILENO))
121 				(void)dup2(pdes[1], STDIN_FILENO);
122 		} else {
123 			if (pdes[0] != STDIN_FILENO) {
124 				(void)dup2(pdes[0], STDIN_FILENO);
125 				(void)_close(pdes[0]);
126 			}
127 			(void)_close(pdes[1]);
128 		}
129 		for (p = pidlist; p; p = p->next) {
130 			(void)_close(fileno(p->fp));
131 		}
132 		execve(_PATH_BSHELL, argv, environ);
133 		_exit(127);
134 		/* NOTREACHED */
135 	}
136 
137 	/* Parent; assume fdopen can't fail. */
138 	if (*type == 'r') {
139 		iop = fdopen(pdes[0], type);
140 		(void)_close(pdes[1]);
141 	} else {
142 		iop = fdopen(pdes[1], type);
143 		(void)_close(pdes[0]);
144 	}
145 
146 	/* Link into list of file descriptors. */
147 	cur->fp = iop;
148 	cur->pid =  pid;
149 	cur->next = pidlist;
150 	pidlist = cur;
151 
152 	return (iop);
153 }
154 
155 /*
156  * pclose --
157  *	Pclose returns -1 if stream is not associated with a `popened' command,
158  *	if already `pclosed', or waitpid returns an error.
159  */
160 int
161 pclose(iop)
162 	FILE *iop;
163 {
164 	register struct pid *cur, *last;
165 	int omask;
166 	int pstat;
167 	pid_t pid;
168 
169 	/* Find the appropriate file pointer. */
170 	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
171 		if (cur->fp == iop)
172 			break;
173 	if (cur == NULL)
174 		return (-1);
175 
176 	(void)fclose(iop);
177 
178 	do {
179 		pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
180 	} while (pid == -1 && errno == EINTR);
181 
182 	/* Remove the entry from the linked list. */
183 	if (last == NULL)
184 		pidlist = cur->next;
185 	else
186 		last->next = cur->next;
187 	free(cur);
188 
189 	return (pid == -1 ? -1 : pstat);
190 }
191