xref: /freebsd/lib/libc/gen/popen.c (revision 77e2381a3ebd5f2128b8fda6100490aaaf42d889)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1988, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * This code is derived from software written by Ken Arnold and
658f0484fSRodney W. Grimes  * published in UNIX Review, Vol. 6, No. 8.
758f0484fSRodney W. Grimes  *
858f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
958f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1058f0484fSRodney W. Grimes  * are met:
1158f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1258f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1358f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1558f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1658f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1758f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1858f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1958f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2058f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2158f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2258f0484fSRodney W. Grimes  *    without specific prior written permission.
2358f0484fSRodney W. Grimes  *
2458f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2558f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2658f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2758f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2858f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2958f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3058f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3158f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3258f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3358f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3458f0484fSRodney W. Grimes  * SUCH DAMAGE.
3558f0484fSRodney W. Grimes  */
3658f0484fSRodney W. Grimes 
3758f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
38adf6ad9eSPeter Wemm static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
3958f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
4022626efaSDavid E. O'Brien #include <sys/cdefs.h>
4122626efaSDavid E. O'Brien __FBSDID("$FreeBSD$");
4258f0484fSRodney W. Grimes 
43d201fe46SDaniel Eischen #include "namespace.h"
4458f0484fSRodney W. Grimes #include <sys/param.h>
4558f0484fSRodney W. Grimes #include <sys/wait.h>
4658f0484fSRodney W. Grimes 
4758f0484fSRodney W. Grimes #include <signal.h>
4858f0484fSRodney W. Grimes #include <errno.h>
4958f0484fSRodney W. Grimes #include <unistd.h>
5058f0484fSRodney W. Grimes #include <stdio.h>
5158f0484fSRodney W. Grimes #include <stdlib.h>
5258f0484fSRodney W. Grimes #include <string.h>
5358f0484fSRodney W. Grimes #include <paths.h>
5477e2381aSTim J. Robbins #include <pthread.h>
55d201fe46SDaniel Eischen #include "un-namespace.h"
5677e2381aSTim J. Robbins #include "libc_private.h"
5758f0484fSRodney W. Grimes 
580c372549SPeter Wemm extern char **environ;
590c372549SPeter Wemm 
6058f0484fSRodney W. Grimes static struct pid {
6158f0484fSRodney W. Grimes 	struct pid *next;
6258f0484fSRodney W. Grimes 	FILE *fp;
6358f0484fSRodney W. Grimes 	pid_t pid;
6458f0484fSRodney W. Grimes } *pidlist;
6577e2381aSTim J. Robbins static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
6677e2381aSTim J. Robbins 
6777e2381aSTim J. Robbins #define	THREAD_LOCK()	if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
6877e2381aSTim J. Robbins #define	THREAD_UNLOCK()	if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
6958f0484fSRodney W. Grimes 
7058f0484fSRodney W. Grimes FILE *
71adf6ad9eSPeter Wemm popen(command, type)
72adf6ad9eSPeter Wemm 	const char *command, *type;
7358f0484fSRodney W. Grimes {
7458f0484fSRodney W. Grimes 	struct pid *cur;
7558f0484fSRodney W. Grimes 	FILE *iop;
76adf6ad9eSPeter Wemm 	int pdes[2], pid, twoway;
770c372549SPeter Wemm 	char *argv[4];
7826c51fb4SMike Smith 	struct pid *p;
7958f0484fSRodney W. Grimes 
80adf6ad9eSPeter Wemm 	/*
81d201fe46SDaniel Eischen 	 * Lite2 introduced two-way popen() pipes using _socketpair().
82adf6ad9eSPeter Wemm 	 * FreeBSD's pipe() is bidirectional, so we use that.
83adf6ad9eSPeter Wemm 	 */
84adf6ad9eSPeter Wemm 	if (strchr(type, '+')) {
85adf6ad9eSPeter Wemm 		twoway = 1;
86adf6ad9eSPeter Wemm 		type = "r+";
87adf6ad9eSPeter Wemm 	} else  {
88adf6ad9eSPeter Wemm 		twoway = 0;
89035e5608SBruce Evans 		if ((*type != 'r' && *type != 'w') || type[1])
90adf6ad9eSPeter Wemm 			return (NULL);
91adf6ad9eSPeter Wemm 	}
92adf6ad9eSPeter Wemm 	if (pipe(pdes) < 0)
9358f0484fSRodney W. Grimes 		return (NULL);
9458f0484fSRodney W. Grimes 
95035e5608SBruce Evans 	if ((cur = malloc(sizeof(struct pid))) == NULL) {
969233c4d9SJason Evans 		(void)_close(pdes[0]);
979233c4d9SJason Evans 		(void)_close(pdes[1]);
9858f0484fSRodney W. Grimes 		return (NULL);
99035e5608SBruce Evans 	}
10058f0484fSRodney W. Grimes 
1010c372549SPeter Wemm 	argv[0] = "sh";
1020c372549SPeter Wemm 	argv[1] = "-c";
1030c372549SPeter Wemm 	argv[2] = (char *)command;
1040c372549SPeter Wemm 	argv[3] = NULL;
1050c372549SPeter Wemm 
10677e2381aSTim J. Robbins 	THREAD_LOCK();
1070c372549SPeter Wemm 	switch (pid = vfork()) {
10858f0484fSRodney W. Grimes 	case -1:			/* Error. */
10977e2381aSTim J. Robbins 		THREAD_UNLOCK();
1109233c4d9SJason Evans 		(void)_close(pdes[0]);
1119233c4d9SJason Evans 		(void)_close(pdes[1]);
112e78bad23SJeffrey Hsu 		free(cur);
11358f0484fSRodney W. Grimes 		return (NULL);
11458f0484fSRodney W. Grimes 		/* NOTREACHED */
11558f0484fSRodney W. Grimes 	case 0:				/* Child. */
11658f0484fSRodney W. Grimes 		if (*type == 'r') {
1171174d9f9SJohn Dyson 			/*
118d201fe46SDaniel Eischen 			 * The _dup2() to STDIN_FILENO is repeated to avoid
1195ae9116aSJohn Dyson 			 * writing to pdes[1], which might corrupt the
1205ae9116aSJohn Dyson 			 * parent's copy.  This isn't good enough in
1215ae9116aSJohn Dyson 			 * general, since the _exit() is no return, so
1225ae9116aSJohn Dyson 			 * the compiler is free to corrupt all the local
1235ae9116aSJohn Dyson 			 * variables.
1241174d9f9SJohn Dyson 			 */
1259233c4d9SJason Evans 			(void)_close(pdes[0]);
1265ae9116aSJohn Dyson 			if (pdes[1] != STDOUT_FILENO) {
127d201fe46SDaniel Eischen 				(void)_dup2(pdes[1], STDOUT_FILENO);
1289233c4d9SJason Evans 				(void)_close(pdes[1]);
1292b9ac168SBruce Evans 				if (twoway)
130d201fe46SDaniel Eischen 					(void)_dup2(STDOUT_FILENO, STDIN_FILENO);
1315ae9116aSJohn Dyson 			} else if (twoway && (pdes[1] != STDIN_FILENO))
132d201fe46SDaniel Eischen 				(void)_dup2(pdes[1], STDIN_FILENO);
13358f0484fSRodney W. Grimes 		} else {
13458f0484fSRodney W. Grimes 			if (pdes[0] != STDIN_FILENO) {
135d201fe46SDaniel Eischen 				(void)_dup2(pdes[0], STDIN_FILENO);
1369233c4d9SJason Evans 				(void)_close(pdes[0]);
13758f0484fSRodney W. Grimes 			}
1389233c4d9SJason Evans 			(void)_close(pdes[1]);
13958f0484fSRodney W. Grimes 		}
14026c51fb4SMike Smith 		for (p = pidlist; p; p = p->next) {
1419233c4d9SJason Evans 			(void)_close(fileno(p->fp));
14226c51fb4SMike Smith 		}
143d201fe46SDaniel Eischen 		_execve(_PATH_BSHELL, argv, environ);
14458f0484fSRodney W. Grimes 		_exit(127);
14558f0484fSRodney W. Grimes 		/* NOTREACHED */
14658f0484fSRodney W. Grimes 	}
14777e2381aSTim J. Robbins 	THREAD_UNLOCK();
14858f0484fSRodney W. Grimes 
14958f0484fSRodney W. Grimes 	/* Parent; assume fdopen can't fail. */
15058f0484fSRodney W. Grimes 	if (*type == 'r') {
15158f0484fSRodney W. Grimes 		iop = fdopen(pdes[0], type);
1529233c4d9SJason Evans 		(void)_close(pdes[1]);
15358f0484fSRodney W. Grimes 	} else {
15458f0484fSRodney W. Grimes 		iop = fdopen(pdes[1], type);
1559233c4d9SJason Evans 		(void)_close(pdes[0]);
15658f0484fSRodney W. Grimes 	}
15758f0484fSRodney W. Grimes 
15858f0484fSRodney W. Grimes 	/* Link into list of file descriptors. */
15958f0484fSRodney W. Grimes 	cur->fp = iop;
16058f0484fSRodney W. Grimes 	cur->pid = pid;
16177e2381aSTim J. Robbins 	THREAD_LOCK();
16258f0484fSRodney W. Grimes 	cur->next = pidlist;
16358f0484fSRodney W. Grimes 	pidlist = cur;
16477e2381aSTim J. Robbins 	THREAD_UNLOCK();
16558f0484fSRodney W. Grimes 
16658f0484fSRodney W. Grimes 	return (iop);
16758f0484fSRodney W. Grimes }
16858f0484fSRodney W. Grimes 
16958f0484fSRodney W. Grimes /*
17058f0484fSRodney W. Grimes  * pclose --
17158f0484fSRodney W. Grimes  *	Pclose returns -1 if stream is not associated with a `popened' command,
17258f0484fSRodney W. Grimes  *	if already `pclosed', or waitpid returns an error.
17358f0484fSRodney W. Grimes  */
17458f0484fSRodney W. Grimes int
17558f0484fSRodney W. Grimes pclose(iop)
17658f0484fSRodney W. Grimes 	FILE *iop;
17758f0484fSRodney W. Grimes {
17822626efaSDavid E. O'Brien 	struct pid *cur, *last;
17958f0484fSRodney W. Grimes 	int omask;
180adf6ad9eSPeter Wemm 	int pstat;
18158f0484fSRodney W. Grimes 	pid_t pid;
18258f0484fSRodney W. Grimes 
18377e2381aSTim J. Robbins 	/*
18477e2381aSTim J. Robbins 	 * Find the appropriate file pointer and remove it from the list.
18577e2381aSTim J. Robbins 	 */
18677e2381aSTim J. Robbins 	THREAD_LOCK();
18758f0484fSRodney W. Grimes 	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
18858f0484fSRodney W. Grimes 		if (cur->fp == iop)
18958f0484fSRodney W. Grimes 			break;
19077e2381aSTim J. Robbins 	if (cur == NULL) {
19177e2381aSTim J. Robbins 		THREAD_UNLOCK();
19258f0484fSRodney W. Grimes 		return (-1);
19377e2381aSTim J. Robbins 	}
19477e2381aSTim J. Robbins 	if (last == NULL)
19577e2381aSTim J. Robbins 		pidlist = cur->next;
19677e2381aSTim J. Robbins 	else
19777e2381aSTim J. Robbins 		last->next = cur->next;
19877e2381aSTim J. Robbins 	THREAD_UNLOCK();
19958f0484fSRodney W. Grimes 
200adf6ad9eSPeter Wemm 	(void)fclose(iop);
201adf6ad9eSPeter Wemm 
20258f0484fSRodney W. Grimes 	do {
2039233c4d9SJason Evans 		pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
20458f0484fSRodney W. Grimes 	} while (pid == -1 && errno == EINTR);
20558f0484fSRodney W. Grimes 
20658f0484fSRodney W. Grimes 	free(cur);
20758f0484fSRodney W. Grimes 
208adf6ad9eSPeter Wemm 	return (pid == -1 ? -1 : pstat);
20958f0484fSRodney W. Grimes }
210