1*0a6a1f1dSLionel Sambuc /* $NetBSD: popenve.c,v 1.2 2015/01/22 03:10:50 christos Exp $ */
2*0a6a1f1dSLionel Sambuc
3*0a6a1f1dSLionel Sambuc /*
4*0a6a1f1dSLionel Sambuc * Copyright (c) 1988, 1993
5*0a6a1f1dSLionel Sambuc * The Regents of the University of California. All rights reserved.
6*0a6a1f1dSLionel Sambuc *
7*0a6a1f1dSLionel Sambuc * This code is derived from software written by Ken Arnold and
8*0a6a1f1dSLionel Sambuc * published in UNIX Review, Vol. 6, No. 8.
9*0a6a1f1dSLionel Sambuc *
10*0a6a1f1dSLionel Sambuc * Redistribution and use in source and binary forms, with or without
11*0a6a1f1dSLionel Sambuc * modification, are permitted provided that the following conditions
12*0a6a1f1dSLionel Sambuc * are met:
13*0a6a1f1dSLionel Sambuc * 1. Redistributions of source code must retain the above copyright
14*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer.
15*0a6a1f1dSLionel Sambuc * 2. Redistributions in binary form must reproduce the above copyright
16*0a6a1f1dSLionel Sambuc * notice, this list of conditions and the following disclaimer in the
17*0a6a1f1dSLionel Sambuc * documentation and/or other materials provided with the distribution.
18*0a6a1f1dSLionel Sambuc * 3. Neither the name of the University nor the names of its contributors
19*0a6a1f1dSLionel Sambuc * may be used to endorse or promote products derived from this software
20*0a6a1f1dSLionel Sambuc * without specific prior written permission.
21*0a6a1f1dSLionel Sambuc *
22*0a6a1f1dSLionel Sambuc * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23*0a6a1f1dSLionel Sambuc * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24*0a6a1f1dSLionel Sambuc * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25*0a6a1f1dSLionel Sambuc * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26*0a6a1f1dSLionel Sambuc * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27*0a6a1f1dSLionel Sambuc * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28*0a6a1f1dSLionel Sambuc * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29*0a6a1f1dSLionel Sambuc * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30*0a6a1f1dSLionel Sambuc * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31*0a6a1f1dSLionel Sambuc * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32*0a6a1f1dSLionel Sambuc * SUCH DAMAGE.
33*0a6a1f1dSLionel Sambuc */
34*0a6a1f1dSLionel Sambuc
35*0a6a1f1dSLionel Sambuc #ifdef HAVE_CONFIG_H
36*0a6a1f1dSLionel Sambuc #include "config.h"
37*0a6a1f1dSLionel Sambuc #endif
38*0a6a1f1dSLionel Sambuc
39*0a6a1f1dSLionel Sambuc #include <sys/cdefs.h>
40*0a6a1f1dSLionel Sambuc #if defined(LIBC_SCCS) && !defined(lint)
41*0a6a1f1dSLionel Sambuc #if 0
42*0a6a1f1dSLionel Sambuc static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95";
43*0a6a1f1dSLionel Sambuc #else
44*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: popenve.c,v 1.2 2015/01/22 03:10:50 christos Exp $");
45*0a6a1f1dSLionel Sambuc #endif
46*0a6a1f1dSLionel Sambuc #endif /* LIBC_SCCS and not lint */
47*0a6a1f1dSLionel Sambuc
48*0a6a1f1dSLionel Sambuc #include <sys/param.h>
49*0a6a1f1dSLionel Sambuc #include <sys/wait.h>
50*0a6a1f1dSLionel Sambuc #include <sys/socket.h>
51*0a6a1f1dSLionel Sambuc
52*0a6a1f1dSLionel Sambuc #include <assert.h>
53*0a6a1f1dSLionel Sambuc #include <errno.h>
54*0a6a1f1dSLionel Sambuc #include <paths.h>
55*0a6a1f1dSLionel Sambuc #include <signal.h>
56*0a6a1f1dSLionel Sambuc #include <stdio.h>
57*0a6a1f1dSLionel Sambuc #include <stdlib.h>
58*0a6a1f1dSLionel Sambuc #include <string.h>
59*0a6a1f1dSLionel Sambuc #include <unistd.h>
60*0a6a1f1dSLionel Sambuc #include <fcntl.h>
61*0a6a1f1dSLionel Sambuc
62*0a6a1f1dSLionel Sambuc #ifdef __weak_alias
63*0a6a1f1dSLionel Sambuc __weak_alias(popen,_popen)
64*0a6a1f1dSLionel Sambuc __weak_alias(pclose,_pclose)
65*0a6a1f1dSLionel Sambuc #endif
66*0a6a1f1dSLionel Sambuc
67*0a6a1f1dSLionel Sambuc static struct pid {
68*0a6a1f1dSLionel Sambuc struct pid *next;
69*0a6a1f1dSLionel Sambuc FILE *fp;
70*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
71*0a6a1f1dSLionel Sambuc int fd;
72*0a6a1f1dSLionel Sambuc #endif
73*0a6a1f1dSLionel Sambuc pid_t pid;
74*0a6a1f1dSLionel Sambuc } *pidlist;
75*0a6a1f1dSLionel Sambuc
76*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
77*0a6a1f1dSLionel Sambuc static rwlock_t pidlist_lock = RWLOCK_INITIALIZER;
78*0a6a1f1dSLionel Sambuc #endif
79*0a6a1f1dSLionel Sambuc
80*0a6a1f1dSLionel Sambuc static struct pid *
pdes_get(int * pdes,const char ** type)81*0a6a1f1dSLionel Sambuc pdes_get(int *pdes, const char **type)
82*0a6a1f1dSLionel Sambuc {
83*0a6a1f1dSLionel Sambuc struct pid *cur;
84*0a6a1f1dSLionel Sambuc int flags = strchr(*type, 'e') ? O_CLOEXEC : 0;
85*0a6a1f1dSLionel Sambuc int serrno;
86*0a6a1f1dSLionel Sambuc
87*0a6a1f1dSLionel Sambuc if (strchr(*type, '+')) {
88*0a6a1f1dSLionel Sambuc #ifndef SOCK_CLOEXEC
89*0a6a1f1dSLionel Sambuc #define SOCK_CLOEXEC 0
90*0a6a1f1dSLionel Sambuc #endif
91*0a6a1f1dSLionel Sambuc int stype = flags ? (SOCK_STREAM | SOCK_CLOEXEC) : SOCK_STREAM;
92*0a6a1f1dSLionel Sambuc *type = "r+";
93*0a6a1f1dSLionel Sambuc if (socketpair(AF_LOCAL, stype, 0, pdes) < 0)
94*0a6a1f1dSLionel Sambuc return NULL;
95*0a6a1f1dSLionel Sambuc #if SOCK_CLOEXEC == 0
96*0a6a1f1dSLionel Sambuc fcntl(pdes[0], F_SETFD, FD_CLOEXEC);
97*0a6a1f1dSLionel Sambuc fcntl(pdes[1], F_SETFD, FD_CLOEXEC);
98*0a6a1f1dSLionel Sambuc #endif
99*0a6a1f1dSLionel Sambuc } else {
100*0a6a1f1dSLionel Sambuc *type = strrchr(*type, 'r') ? "r" : "w";
101*0a6a1f1dSLionel Sambuc #if SOCK_CLOEXEC != 0
102*0a6a1f1dSLionel Sambuc if (pipe2(pdes, flags) == -1)
103*0a6a1f1dSLionel Sambuc return NULL;
104*0a6a1f1dSLionel Sambuc #else
105*0a6a1f1dSLionel Sambuc if (pipe(pdes) == -1)
106*0a6a1f1dSLionel Sambuc return NULL;
107*0a6a1f1dSLionel Sambuc fcntl(pdes[0], F_SETFL, fcntl(pdes[0], F_GETFL) | flags);
108*0a6a1f1dSLionel Sambuc fcntl(pdes[1], F_SETFL, fcntl(pdes[1], F_GETFL) | flags);
109*0a6a1f1dSLionel Sambuc #endif
110*0a6a1f1dSLionel Sambuc }
111*0a6a1f1dSLionel Sambuc
112*0a6a1f1dSLionel Sambuc if ((cur = malloc(sizeof(*cur))) != NULL)
113*0a6a1f1dSLionel Sambuc return cur;
114*0a6a1f1dSLionel Sambuc serrno = errno;
115*0a6a1f1dSLionel Sambuc (void)close(pdes[0]);
116*0a6a1f1dSLionel Sambuc (void)close(pdes[1]);
117*0a6a1f1dSLionel Sambuc errno = serrno;
118*0a6a1f1dSLionel Sambuc return NULL;
119*0a6a1f1dSLionel Sambuc }
120*0a6a1f1dSLionel Sambuc
121*0a6a1f1dSLionel Sambuc static void
pdes_child(int * pdes,const char * type)122*0a6a1f1dSLionel Sambuc pdes_child(int *pdes, const char *type)
123*0a6a1f1dSLionel Sambuc {
124*0a6a1f1dSLionel Sambuc struct pid *old;
125*0a6a1f1dSLionel Sambuc
126*0a6a1f1dSLionel Sambuc /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
127*0a6a1f1dSLionel Sambuc from previous popen() calls that remain open in the
128*0a6a1f1dSLionel Sambuc parent process are closed in the new child process. */
129*0a6a1f1dSLionel Sambuc for (old = pidlist; old; old = old->next)
130*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
131*0a6a1f1dSLionel Sambuc (void)close(old->fd); /* don't allow a flush */
132*0a6a1f1dSLionel Sambuc #else
133*0a6a1f1dSLionel Sambuc (void)close(fileno(old->fp)); /* don't allow a flush */
134*0a6a1f1dSLionel Sambuc #endif
135*0a6a1f1dSLionel Sambuc
136*0a6a1f1dSLionel Sambuc if (type[0] == 'r') {
137*0a6a1f1dSLionel Sambuc (void)close(pdes[0]);
138*0a6a1f1dSLionel Sambuc if (pdes[1] != STDOUT_FILENO) {
139*0a6a1f1dSLionel Sambuc (void)dup2(pdes[1], STDOUT_FILENO);
140*0a6a1f1dSLionel Sambuc (void)close(pdes[1]);
141*0a6a1f1dSLionel Sambuc }
142*0a6a1f1dSLionel Sambuc if (type[1] == '+')
143*0a6a1f1dSLionel Sambuc (void)dup2(STDOUT_FILENO, STDIN_FILENO);
144*0a6a1f1dSLionel Sambuc } else {
145*0a6a1f1dSLionel Sambuc (void)close(pdes[1]);
146*0a6a1f1dSLionel Sambuc if (pdes[0] != STDIN_FILENO) {
147*0a6a1f1dSLionel Sambuc (void)dup2(pdes[0], STDIN_FILENO);
148*0a6a1f1dSLionel Sambuc (void)close(pdes[0]);
149*0a6a1f1dSLionel Sambuc }
150*0a6a1f1dSLionel Sambuc }
151*0a6a1f1dSLionel Sambuc }
152*0a6a1f1dSLionel Sambuc
153*0a6a1f1dSLionel Sambuc static void
pdes_parent(int * pdes,struct pid * cur,pid_t pid,const char * type)154*0a6a1f1dSLionel Sambuc pdes_parent(int *pdes, struct pid *cur, pid_t pid, const char *type)
155*0a6a1f1dSLionel Sambuc {
156*0a6a1f1dSLionel Sambuc FILE *iop;
157*0a6a1f1dSLionel Sambuc
158*0a6a1f1dSLionel Sambuc /* Parent; assume fdopen can't fail. */
159*0a6a1f1dSLionel Sambuc if (*type == 'r') {
160*0a6a1f1dSLionel Sambuc iop = fdopen(pdes[0], type);
161*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
162*0a6a1f1dSLionel Sambuc cur->fd = pdes[0];
163*0a6a1f1dSLionel Sambuc #endif
164*0a6a1f1dSLionel Sambuc (void)close(pdes[1]);
165*0a6a1f1dSLionel Sambuc } else {
166*0a6a1f1dSLionel Sambuc iop = fdopen(pdes[1], type);
167*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
168*0a6a1f1dSLionel Sambuc cur->fd = pdes[1];
169*0a6a1f1dSLionel Sambuc #endif
170*0a6a1f1dSLionel Sambuc (void)close(pdes[0]);
171*0a6a1f1dSLionel Sambuc }
172*0a6a1f1dSLionel Sambuc
173*0a6a1f1dSLionel Sambuc /* Link into list of file descriptors. */
174*0a6a1f1dSLionel Sambuc cur->fp = iop;
175*0a6a1f1dSLionel Sambuc cur->pid = pid;
176*0a6a1f1dSLionel Sambuc cur->next = pidlist;
177*0a6a1f1dSLionel Sambuc pidlist = cur;
178*0a6a1f1dSLionel Sambuc }
179*0a6a1f1dSLionel Sambuc
180*0a6a1f1dSLionel Sambuc static void
pdes_error(int * pdes,struct pid * cur)181*0a6a1f1dSLionel Sambuc pdes_error(int *pdes, struct pid *cur)
182*0a6a1f1dSLionel Sambuc {
183*0a6a1f1dSLionel Sambuc free(cur);
184*0a6a1f1dSLionel Sambuc (void)close(pdes[0]);
185*0a6a1f1dSLionel Sambuc (void)close(pdes[1]);
186*0a6a1f1dSLionel Sambuc }
187*0a6a1f1dSLionel Sambuc
188*0a6a1f1dSLionel Sambuc FILE *
popenve(const char * cmd,char * const * argv,char * const * envp,const char * type)189*0a6a1f1dSLionel Sambuc popenve(const char *cmd, char *const *argv, char *const *envp, const char *type)
190*0a6a1f1dSLionel Sambuc {
191*0a6a1f1dSLionel Sambuc struct pid *cur;
192*0a6a1f1dSLionel Sambuc int pdes[2], serrno;
193*0a6a1f1dSLionel Sambuc pid_t pid;
194*0a6a1f1dSLionel Sambuc
195*0a6a1f1dSLionel Sambuc if ((cur = pdes_get(pdes, &type)) == NULL)
196*0a6a1f1dSLionel Sambuc return NULL;
197*0a6a1f1dSLionel Sambuc
198*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
199*0a6a1f1dSLionel Sambuc (void)rwlock_rdlock(&pidlist_lock);
200*0a6a1f1dSLionel Sambuc #endif
201*0a6a1f1dSLionel Sambuc switch (pid = vfork()) {
202*0a6a1f1dSLionel Sambuc case -1: /* Error. */
203*0a6a1f1dSLionel Sambuc serrno = errno;
204*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
205*0a6a1f1dSLionel Sambuc (void)rwlock_unlock(&pidlist_lock);
206*0a6a1f1dSLionel Sambuc #endif
207*0a6a1f1dSLionel Sambuc pdes_error(pdes, cur);
208*0a6a1f1dSLionel Sambuc errno = serrno;
209*0a6a1f1dSLionel Sambuc return NULL;
210*0a6a1f1dSLionel Sambuc /* NOTREACHED */
211*0a6a1f1dSLionel Sambuc case 0: /* Child. */
212*0a6a1f1dSLionel Sambuc pdes_child(pdes, type);
213*0a6a1f1dSLionel Sambuc execve(cmd, argv, envp);
214*0a6a1f1dSLionel Sambuc _exit(127);
215*0a6a1f1dSLionel Sambuc /* NOTREACHED */
216*0a6a1f1dSLionel Sambuc }
217*0a6a1f1dSLionel Sambuc
218*0a6a1f1dSLionel Sambuc pdes_parent(pdes, cur, pid, type);
219*0a6a1f1dSLionel Sambuc
220*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
221*0a6a1f1dSLionel Sambuc (void)rwlock_unlock(&pidlist_lock);
222*0a6a1f1dSLionel Sambuc #endif
223*0a6a1f1dSLionel Sambuc
224*0a6a1f1dSLionel Sambuc return cur->fp;
225*0a6a1f1dSLionel Sambuc }
226*0a6a1f1dSLionel Sambuc
227*0a6a1f1dSLionel Sambuc /*
228*0a6a1f1dSLionel Sambuc * pclose --
229*0a6a1f1dSLionel Sambuc * Pclose returns -1 if stream is not associated with a `popened' command,
230*0a6a1f1dSLionel Sambuc * if already `pclosed', or waitpid returns an error.
231*0a6a1f1dSLionel Sambuc */
232*0a6a1f1dSLionel Sambuc int
pcloseve(FILE * iop)233*0a6a1f1dSLionel Sambuc pcloseve(FILE *iop)
234*0a6a1f1dSLionel Sambuc {
235*0a6a1f1dSLionel Sambuc struct pid *cur, *last;
236*0a6a1f1dSLionel Sambuc int pstat;
237*0a6a1f1dSLionel Sambuc pid_t pid;
238*0a6a1f1dSLionel Sambuc
239*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
240*0a6a1f1dSLionel Sambuc rwlock_wrlock(&pidlist_lock);
241*0a6a1f1dSLionel Sambuc #endif
242*0a6a1f1dSLionel Sambuc
243*0a6a1f1dSLionel Sambuc /* Find the appropriate file pointer. */
244*0a6a1f1dSLionel Sambuc for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
245*0a6a1f1dSLionel Sambuc if (cur->fp == iop)
246*0a6a1f1dSLionel Sambuc break;
247*0a6a1f1dSLionel Sambuc if (cur == NULL) {
248*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
249*0a6a1f1dSLionel Sambuc (void)rwlock_unlock(&pidlist_lock);
250*0a6a1f1dSLionel Sambuc #endif
251*0a6a1f1dSLionel Sambuc errno = ESRCH;
252*0a6a1f1dSLionel Sambuc return -1;
253*0a6a1f1dSLionel Sambuc }
254*0a6a1f1dSLionel Sambuc
255*0a6a1f1dSLionel Sambuc (void)fclose(iop);
256*0a6a1f1dSLionel Sambuc
257*0a6a1f1dSLionel Sambuc /* Remove the entry from the linked list. */
258*0a6a1f1dSLionel Sambuc if (last == NULL)
259*0a6a1f1dSLionel Sambuc pidlist = cur->next;
260*0a6a1f1dSLionel Sambuc else
261*0a6a1f1dSLionel Sambuc last->next = cur->next;
262*0a6a1f1dSLionel Sambuc
263*0a6a1f1dSLionel Sambuc #ifdef _REENTRANT
264*0a6a1f1dSLionel Sambuc (void)rwlock_unlock(&pidlist_lock);
265*0a6a1f1dSLionel Sambuc #endif
266*0a6a1f1dSLionel Sambuc
267*0a6a1f1dSLionel Sambuc do {
268*0a6a1f1dSLionel Sambuc pid = waitpid(cur->pid, &pstat, 0);
269*0a6a1f1dSLionel Sambuc } while (pid == -1 && errno == EINTR);
270*0a6a1f1dSLionel Sambuc
271*0a6a1f1dSLionel Sambuc free(cur);
272*0a6a1f1dSLionel Sambuc
273*0a6a1f1dSLionel Sambuc return pid == -1 ? -1 : pstat;
274*0a6a1f1dSLionel Sambuc }
275