xref: /freebsd/lib/libc/gen/popen.c (revision e9dec7758d3372e48746d32ba8c485cf89d83802)
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  * 4. Neither the name of the University nor the names of its contributors
1758f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1858f0484fSRodney W. Grimes  *    without specific prior written permission.
1958f0484fSRodney W. Grimes  *
2058f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2158f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2258f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2358f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2458f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2558f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2658f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2758f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2858f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2958f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3058f0484fSRodney W. Grimes  * SUCH DAMAGE.
3158f0484fSRodney W. Grimes  */
3258f0484fSRodney W. Grimes 
3358f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
34adf6ad9eSPeter Wemm static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
3558f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3622626efaSDavid E. O'Brien #include <sys/cdefs.h>
3722626efaSDavid E. O'Brien __FBSDID("$FreeBSD$");
3858f0484fSRodney W. Grimes 
39d201fe46SDaniel Eischen #include "namespace.h"
4058f0484fSRodney W. Grimes #include <sys/param.h>
417e7f7ca3SEd Schouten #include <sys/queue.h>
4258f0484fSRodney W. Grimes #include <sys/wait.h>
4358f0484fSRodney W. Grimes 
4458f0484fSRodney W. Grimes #include <signal.h>
4558f0484fSRodney W. Grimes #include <errno.h>
46*e9dec775SJilles Tjoelker #include <fcntl.h>
4758f0484fSRodney W. Grimes #include <unistd.h>
4858f0484fSRodney W. Grimes #include <stdio.h>
4958f0484fSRodney W. Grimes #include <stdlib.h>
5058f0484fSRodney W. Grimes #include <string.h>
5158f0484fSRodney W. Grimes #include <paths.h>
5277e2381aSTim J. Robbins #include <pthread.h>
53d201fe46SDaniel Eischen #include "un-namespace.h"
5477e2381aSTim J. Robbins #include "libc_private.h"
5558f0484fSRodney W. Grimes 
560c372549SPeter Wemm extern char **environ;
570c372549SPeter Wemm 
587e7f7ca3SEd Schouten struct pid {
597e7f7ca3SEd Schouten 	SLIST_ENTRY(pid) next;
6058f0484fSRodney W. Grimes 	FILE *fp;
6158f0484fSRodney W. Grimes 	pid_t pid;
627e7f7ca3SEd Schouten };
637e7f7ca3SEd Schouten static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
6477e2381aSTim J. Robbins static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
6577e2381aSTim J. Robbins 
6677e2381aSTim J. Robbins #define	THREAD_LOCK()	if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
6777e2381aSTim J. Robbins #define	THREAD_UNLOCK()	if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
6858f0484fSRodney W. Grimes 
6958f0484fSRodney W. Grimes FILE *
70adf6ad9eSPeter Wemm popen(command, type)
71adf6ad9eSPeter Wemm 	const char *command, *type;
7258f0484fSRodney W. Grimes {
7358f0484fSRodney W. Grimes 	struct pid *cur;
7458f0484fSRodney W. Grimes 	FILE *iop;
75*e9dec775SJilles Tjoelker 	int pdes[2], pid, twoway, cloexec;
760c372549SPeter Wemm 	char *argv[4];
7726c51fb4SMike Smith 	struct pid *p;
7858f0484fSRodney W. Grimes 
79*e9dec775SJilles Tjoelker 	cloexec = strchr(type, 'e') != NULL;
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;
89*e9dec775SJilles Tjoelker 		if ((*type != 'r' && *type != 'w') ||
90*e9dec775SJilles Tjoelker 		    (type[1] && (type[1] != 'e' || type[2])))
91adf6ad9eSPeter Wemm 			return (NULL);
92adf6ad9eSPeter Wemm 	}
93*e9dec775SJilles Tjoelker 	if ((cloexec ? pipe2(pdes, O_CLOEXEC) : pipe(pdes)) < 0)
9458f0484fSRodney W. Grimes 		return (NULL);
9558f0484fSRodney W. Grimes 
96035e5608SBruce Evans 	if ((cur = malloc(sizeof(struct pid))) == NULL) {
979233c4d9SJason Evans 		(void)_close(pdes[0]);
989233c4d9SJason Evans 		(void)_close(pdes[1]);
9958f0484fSRodney W. Grimes 		return (NULL);
100035e5608SBruce Evans 	}
10158f0484fSRodney W. Grimes 
1020c372549SPeter Wemm 	argv[0] = "sh";
1030c372549SPeter Wemm 	argv[1] = "-c";
1040c372549SPeter Wemm 	argv[2] = (char *)command;
1050c372549SPeter Wemm 	argv[3] = NULL;
1060c372549SPeter Wemm 
10777e2381aSTim J. Robbins 	THREAD_LOCK();
1080c372549SPeter Wemm 	switch (pid = vfork()) {
10958f0484fSRodney W. Grimes 	case -1:			/* Error. */
11077e2381aSTim J. Robbins 		THREAD_UNLOCK();
1119233c4d9SJason Evans 		(void)_close(pdes[0]);
1129233c4d9SJason Evans 		(void)_close(pdes[1]);
113e78bad23SJeffrey Hsu 		free(cur);
11458f0484fSRodney W. Grimes 		return (NULL);
11558f0484fSRodney W. Grimes 		/* NOTREACHED */
11658f0484fSRodney W. Grimes 	case 0:				/* Child. */
11758f0484fSRodney W. Grimes 		if (*type == 'r') {
1181174d9f9SJohn Dyson 			/*
119d201fe46SDaniel Eischen 			 * The _dup2() to STDIN_FILENO is repeated to avoid
1205ae9116aSJohn Dyson 			 * writing to pdes[1], which might corrupt the
1215ae9116aSJohn Dyson 			 * parent's copy.  This isn't good enough in
1225ae9116aSJohn Dyson 			 * general, since the _exit() is no return, so
1235ae9116aSJohn Dyson 			 * the compiler is free to corrupt all the local
1245ae9116aSJohn Dyson 			 * variables.
1251174d9f9SJohn Dyson 			 */
126*e9dec775SJilles Tjoelker 			if (!cloexec)
1279233c4d9SJason Evans 				(void)_close(pdes[0]);
1285ae9116aSJohn Dyson 			if (pdes[1] != STDOUT_FILENO) {
129d201fe46SDaniel Eischen 				(void)_dup2(pdes[1], STDOUT_FILENO);
130*e9dec775SJilles Tjoelker 				if (!cloexec)
1319233c4d9SJason Evans 					(void)_close(pdes[1]);
1322b9ac168SBruce Evans 				if (twoway)
133d201fe46SDaniel Eischen 					(void)_dup2(STDOUT_FILENO, STDIN_FILENO);
134*e9dec775SJilles Tjoelker 			} else if (twoway && (pdes[1] != STDIN_FILENO)) {
135d201fe46SDaniel Eischen 				(void)_dup2(pdes[1], STDIN_FILENO);
136*e9dec775SJilles Tjoelker 				if (cloexec)
137*e9dec775SJilles Tjoelker 					(void)_fcntl(pdes[1], F_SETFD, 0);
138*e9dec775SJilles Tjoelker 			} else if (cloexec)
139*e9dec775SJilles Tjoelker 				(void)_fcntl(pdes[1], F_SETFD, 0);
14058f0484fSRodney W. Grimes 		} else {
14158f0484fSRodney W. Grimes 			if (pdes[0] != STDIN_FILENO) {
142d201fe46SDaniel Eischen 				(void)_dup2(pdes[0], STDIN_FILENO);
143*e9dec775SJilles Tjoelker 				if (!cloexec)
1449233c4d9SJason Evans 					(void)_close(pdes[0]);
145*e9dec775SJilles Tjoelker 			} else if (cloexec)
146*e9dec775SJilles Tjoelker 				(void)_fcntl(pdes[0], F_SETFD, 0);
147*e9dec775SJilles Tjoelker 			if (!cloexec)
1489233c4d9SJason Evans 				(void)_close(pdes[1]);
14958f0484fSRodney W. Grimes 		}
1507e7f7ca3SEd Schouten 		SLIST_FOREACH(p, &pidlist, next)
1519233c4d9SJason Evans 			(void)_close(fileno(p->fp));
152d201fe46SDaniel Eischen 		_execve(_PATH_BSHELL, argv, environ);
15358f0484fSRodney W. Grimes 		_exit(127);
15458f0484fSRodney W. Grimes 		/* NOTREACHED */
15558f0484fSRodney W. Grimes 	}
15677e2381aSTim J. Robbins 	THREAD_UNLOCK();
15758f0484fSRodney W. Grimes 
15858f0484fSRodney W. Grimes 	/* Parent; assume fdopen can't fail. */
15958f0484fSRodney W. Grimes 	if (*type == 'r') {
16058f0484fSRodney W. Grimes 		iop = fdopen(pdes[0], type);
1619233c4d9SJason Evans 		(void)_close(pdes[1]);
16258f0484fSRodney W. Grimes 	} else {
16358f0484fSRodney W. Grimes 		iop = fdopen(pdes[1], type);
1649233c4d9SJason Evans 		(void)_close(pdes[0]);
16558f0484fSRodney W. Grimes 	}
16658f0484fSRodney W. Grimes 
16758f0484fSRodney W. Grimes 	/* Link into list of file descriptors. */
16858f0484fSRodney W. Grimes 	cur->fp = iop;
16958f0484fSRodney W. Grimes 	cur->pid = pid;
17077e2381aSTim J. Robbins 	THREAD_LOCK();
1717e7f7ca3SEd Schouten 	SLIST_INSERT_HEAD(&pidlist, cur, next);
17277e2381aSTim J. Robbins 	THREAD_UNLOCK();
17358f0484fSRodney W. Grimes 
17458f0484fSRodney W. Grimes 	return (iop);
17558f0484fSRodney W. Grimes }
17658f0484fSRodney W. Grimes 
17758f0484fSRodney W. Grimes /*
17858f0484fSRodney W. Grimes  * pclose --
17958f0484fSRodney W. Grimes  *	Pclose returns -1 if stream is not associated with a `popened' command,
18058f0484fSRodney W. Grimes  *	if already `pclosed', or waitpid returns an error.
18158f0484fSRodney W. Grimes  */
18258f0484fSRodney W. Grimes int
18358f0484fSRodney W. Grimes pclose(iop)
18458f0484fSRodney W. Grimes 	FILE *iop;
18558f0484fSRodney W. Grimes {
1867e7f7ca3SEd Schouten 	struct pid *cur, *last = NULL;
187adf6ad9eSPeter Wemm 	int pstat;
18858f0484fSRodney W. Grimes 	pid_t pid;
18958f0484fSRodney W. Grimes 
19077e2381aSTim J. Robbins 	/*
19177e2381aSTim J. Robbins 	 * Find the appropriate file pointer and remove it from the list.
19277e2381aSTim J. Robbins 	 */
19377e2381aSTim J. Robbins 	THREAD_LOCK();
1947e7f7ca3SEd Schouten 	SLIST_FOREACH(cur, &pidlist, next) {
19558f0484fSRodney W. Grimes 		if (cur->fp == iop)
19658f0484fSRodney W. Grimes 			break;
1977e7f7ca3SEd Schouten 		last = cur;
1987e7f7ca3SEd Schouten 	}
19977e2381aSTim J. Robbins 	if (cur == NULL) {
20077e2381aSTim J. Robbins 		THREAD_UNLOCK();
20158f0484fSRodney W. Grimes 		return (-1);
20277e2381aSTim J. Robbins 	}
20377e2381aSTim J. Robbins 	if (last == NULL)
2047e7f7ca3SEd Schouten 		SLIST_REMOVE_HEAD(&pidlist, next);
20577e2381aSTim J. Robbins 	else
2063d98b75bSEd Schouten 		SLIST_REMOVE_AFTER(last, next);
20777e2381aSTim J. Robbins 	THREAD_UNLOCK();
20858f0484fSRodney W. Grimes 
209adf6ad9eSPeter Wemm 	(void)fclose(iop);
210adf6ad9eSPeter Wemm 
21158f0484fSRodney W. Grimes 	do {
2129233c4d9SJason Evans 		pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
21358f0484fSRodney W. Grimes 	} while (pid == -1 && errno == EINTR);
21458f0484fSRodney W. Grimes 
21558f0484fSRodney W. Grimes 	free(cur);
21658f0484fSRodney W. Grimes 
217adf6ad9eSPeter Wemm 	return (pid == -1 ? -1 : pstat);
21858f0484fSRodney W. Grimes }
219