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