xref: /freebsd/lib/libc/gen/popen.c (revision 8a16b7a18f5d0b031f09832fd7752fba717e2a97)
1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni  *
458f0484fSRodney W. Grimes  * Copyright (c) 1988, 1993
558f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
658f0484fSRodney W. Grimes  *
758f0484fSRodney W. Grimes  * This code is derived from software written by Ken Arnold and
858f0484fSRodney W. Grimes  * published in UNIX Review, Vol. 6, No. 8.
958f0484fSRodney W. Grimes  *
1058f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1158f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1258f0484fSRodney W. Grimes  * are met:
1358f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1458f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1558f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1658f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1758f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
1958f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2058f0484fSRodney W. Grimes  *    without specific prior written permission.
2158f0484fSRodney W. Grimes  *
2258f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2358f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2458f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2558f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2658f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2758f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2858f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2958f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3058f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3158f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3258f0484fSRodney W. Grimes  * SUCH DAMAGE.
3358f0484fSRodney W. Grimes  */
3458f0484fSRodney W. Grimes 
3558f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
36adf6ad9eSPeter Wemm static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
3758f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
3822626efaSDavid E. O'Brien #include <sys/cdefs.h>
3922626efaSDavid E. O'Brien __FBSDID("$FreeBSD$");
4058f0484fSRodney W. Grimes 
41d201fe46SDaniel Eischen #include "namespace.h"
4258f0484fSRodney W. Grimes #include <sys/param.h>
437e7f7ca3SEd Schouten #include <sys/queue.h>
4458f0484fSRodney W. Grimes #include <sys/wait.h>
4558f0484fSRodney W. Grimes 
4658f0484fSRodney W. Grimes #include <signal.h>
4758f0484fSRodney W. Grimes #include <errno.h>
48e9dec775SJilles Tjoelker #include <fcntl.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 
607e7f7ca3SEd Schouten struct pid {
617e7f7ca3SEd Schouten 	SLIST_ENTRY(pid) next;
6258f0484fSRodney W. Grimes 	FILE *fp;
6358f0484fSRodney W. Grimes 	pid_t pid;
647e7f7ca3SEd Schouten };
657e7f7ca3SEd Schouten static SLIST_HEAD(, pid) pidlist = SLIST_HEAD_INITIALIZER(pidlist);
6677e2381aSTim J. Robbins static pthread_mutex_t pidlist_mutex = PTHREAD_MUTEX_INITIALIZER;
6777e2381aSTim J. Robbins 
6877e2381aSTim J. Robbins #define	THREAD_LOCK()	if (__isthreaded) _pthread_mutex_lock(&pidlist_mutex)
6977e2381aSTim J. Robbins #define	THREAD_UNLOCK()	if (__isthreaded) _pthread_mutex_unlock(&pidlist_mutex)
7058f0484fSRodney W. Grimes 
7158f0484fSRodney W. Grimes FILE *
7255b6b759SCraig Rodrigues popen(const char *command, const char *type)
7358f0484fSRodney W. Grimes {
7458f0484fSRodney W. Grimes 	struct pid *cur;
7558f0484fSRodney W. Grimes 	FILE *iop;
76e9dec775SJilles Tjoelker 	int pdes[2], pid, twoway, cloexec;
773945141fSRavi Pokala 	int pdes_unused_in_parent;
780c372549SPeter Wemm 	char *argv[4];
7926c51fb4SMike Smith 	struct pid *p;
8058f0484fSRodney W. Grimes 
81e9dec775SJilles Tjoelker 	cloexec = strchr(type, 'e') != NULL;
82adf6ad9eSPeter Wemm 	/*
83d201fe46SDaniel Eischen 	 * Lite2 introduced two-way popen() pipes using _socketpair().
84adf6ad9eSPeter Wemm 	 * FreeBSD's pipe() is bidirectional, so we use that.
85adf6ad9eSPeter Wemm 	 */
86adf6ad9eSPeter Wemm 	if (strchr(type, '+')) {
87adf6ad9eSPeter Wemm 		twoway = 1;
88adf6ad9eSPeter Wemm 		type = "r+";
89adf6ad9eSPeter Wemm 	} else  {
90adf6ad9eSPeter Wemm 		twoway = 0;
91e9dec775SJilles Tjoelker 		if ((*type != 'r' && *type != 'w') ||
92e9dec775SJilles Tjoelker 		    (type[1] && (type[1] != 'e' || type[2])))
93adf6ad9eSPeter Wemm 			return (NULL);
94adf6ad9eSPeter Wemm 	}
9502804449SJilles Tjoelker 	if (pipe2(pdes, O_CLOEXEC) < 0)
9658f0484fSRodney W. Grimes 		return (NULL);
9758f0484fSRodney W. Grimes 
98035e5608SBruce Evans 	if ((cur = malloc(sizeof(struct pid))) == NULL) {
999233c4d9SJason Evans 		(void)_close(pdes[0]);
1009233c4d9SJason Evans 		(void)_close(pdes[1]);
10158f0484fSRodney W. Grimes 		return (NULL);
102035e5608SBruce Evans 	}
10358f0484fSRodney W. Grimes 
1043945141fSRavi Pokala 	if (*type == 'r') {
1053945141fSRavi Pokala 		iop = fdopen(pdes[0], type);
1063945141fSRavi Pokala 		pdes_unused_in_parent = pdes[1];
1073945141fSRavi Pokala 	} else {
1083945141fSRavi Pokala 		iop = fdopen(pdes[1], type);
1093945141fSRavi Pokala 		pdes_unused_in_parent = pdes[0];
1103945141fSRavi Pokala 	}
1113945141fSRavi Pokala 	if (iop == NULL) {
1123945141fSRavi Pokala 		(void)_close(pdes[0]);
1133945141fSRavi Pokala 		(void)_close(pdes[1]);
1143945141fSRavi Pokala 		free(cur);
1153945141fSRavi Pokala 		return (NULL);
1163945141fSRavi Pokala 	}
1173945141fSRavi Pokala 
1180c372549SPeter Wemm 	argv[0] = "sh";
1190c372549SPeter Wemm 	argv[1] = "-c";
1200c372549SPeter Wemm 	argv[2] = (char *)command;
1210c372549SPeter Wemm 	argv[3] = NULL;
1220c372549SPeter Wemm 
12377e2381aSTim J. Robbins 	THREAD_LOCK();
1240c372549SPeter Wemm 	switch (pid = vfork()) {
12558f0484fSRodney W. Grimes 	case -1:			/* Error. */
12677e2381aSTim J. Robbins 		THREAD_UNLOCK();
1273945141fSRavi Pokala 		/*
1283945141fSRavi Pokala 		 * The _close() closes the unused end of pdes[], while
1293945141fSRavi Pokala 		 * the fclose() closes the used end of pdes[], *and* cleans
1303945141fSRavi Pokala 		 * up iop.
1313945141fSRavi Pokala 		 */
1323945141fSRavi Pokala 		(void)_close(pdes_unused_in_parent);
133e78bad23SJeffrey Hsu 		free(cur);
1343945141fSRavi Pokala 		(void)fclose(iop);
13558f0484fSRodney W. Grimes 		return (NULL);
13658f0484fSRodney W. Grimes 		/* NOTREACHED */
13758f0484fSRodney W. Grimes 	case 0:				/* Child. */
13858f0484fSRodney W. Grimes 		if (*type == 'r') {
1391174d9f9SJohn Dyson 			/*
140d201fe46SDaniel Eischen 			 * The _dup2() to STDIN_FILENO is repeated to avoid
1415ae9116aSJohn Dyson 			 * writing to pdes[1], which might corrupt the
1425ae9116aSJohn Dyson 			 * parent's copy.  This isn't good enough in
1435ae9116aSJohn Dyson 			 * general, since the _exit() is no return, so
1445ae9116aSJohn Dyson 			 * the compiler is free to corrupt all the local
1455ae9116aSJohn Dyson 			 * variables.
1461174d9f9SJohn Dyson 			 */
1475ae9116aSJohn Dyson 			if (pdes[1] != STDOUT_FILENO) {
148d201fe46SDaniel Eischen 				(void)_dup2(pdes[1], STDOUT_FILENO);
1492b9ac168SBruce Evans 				if (twoway)
150d201fe46SDaniel Eischen 					(void)_dup2(STDOUT_FILENO, STDIN_FILENO);
151e9dec775SJilles Tjoelker 			} else if (twoway && (pdes[1] != STDIN_FILENO)) {
152d201fe46SDaniel Eischen 				(void)_dup2(pdes[1], STDIN_FILENO);
153e9dec775SJilles Tjoelker 				(void)_fcntl(pdes[1], F_SETFD, 0);
15402804449SJilles Tjoelker 			} else
155e9dec775SJilles Tjoelker 				(void)_fcntl(pdes[1], F_SETFD, 0);
15658f0484fSRodney W. Grimes 		} else {
15758f0484fSRodney W. Grimes 			if (pdes[0] != STDIN_FILENO) {
158d201fe46SDaniel Eischen 				(void)_dup2(pdes[0], STDIN_FILENO);
15902804449SJilles Tjoelker 			} else
160e9dec775SJilles Tjoelker 				(void)_fcntl(pdes[0], F_SETFD, 0);
16158f0484fSRodney W. Grimes 		}
1627e7f7ca3SEd Schouten 		SLIST_FOREACH(p, &pidlist, next)
1639233c4d9SJason Evans 			(void)_close(fileno(p->fp));
164d201fe46SDaniel Eischen 		_execve(_PATH_BSHELL, argv, environ);
16558f0484fSRodney W. Grimes 		_exit(127);
16658f0484fSRodney W. Grimes 		/* NOTREACHED */
16758f0484fSRodney W. Grimes 	}
16877e2381aSTim J. Robbins 	THREAD_UNLOCK();
16958f0484fSRodney W. Grimes 
1703945141fSRavi Pokala 	/* Parent. */
1713945141fSRavi Pokala 	(void)_close(pdes_unused_in_parent);
17258f0484fSRodney W. Grimes 
17358f0484fSRodney W. Grimes 	/* Link into list of file descriptors. */
17458f0484fSRodney W. Grimes 	cur->fp = iop;
17558f0484fSRodney W. Grimes 	cur->pid = pid;
17677e2381aSTim J. Robbins 	THREAD_LOCK();
1777e7f7ca3SEd Schouten 	SLIST_INSERT_HEAD(&pidlist, cur, next);
17877e2381aSTim J. Robbins 	THREAD_UNLOCK();
17958f0484fSRodney W. Grimes 
18002804449SJilles Tjoelker 	/*
18102804449SJilles Tjoelker 	 * To guard against undesired fd passing with concurrent calls,
18202804449SJilles Tjoelker 	 * only clear the close-on-exec flag after linking the file into
18302804449SJilles Tjoelker 	 * the list which will cause an explicit close.
18402804449SJilles Tjoelker 	 */
18502804449SJilles Tjoelker 	if (!cloexec)
18602804449SJilles Tjoelker 		(void)_fcntl(*type == 'r' ? pdes[0] : pdes[1], F_SETFD, 0);
18702804449SJilles Tjoelker 
18858f0484fSRodney W. Grimes 	return (iop);
18958f0484fSRodney W. Grimes }
19058f0484fSRodney W. Grimes 
19158f0484fSRodney W. Grimes /*
19258f0484fSRodney W. Grimes  * pclose --
19358f0484fSRodney W. Grimes  *	Pclose returns -1 if stream is not associated with a `popened' command,
19458f0484fSRodney W. Grimes  *	if already `pclosed', or waitpid returns an error.
19558f0484fSRodney W. Grimes  */
19658f0484fSRodney W. Grimes int
19755b6b759SCraig Rodrigues pclose(FILE *iop)
19858f0484fSRodney W. Grimes {
1997e7f7ca3SEd Schouten 	struct pid *cur, *last = NULL;
200adf6ad9eSPeter Wemm 	int pstat;
20158f0484fSRodney W. Grimes 	pid_t pid;
20258f0484fSRodney W. Grimes 
20377e2381aSTim J. Robbins 	/*
20477e2381aSTim J. Robbins 	 * Find the appropriate file pointer and remove it from the list.
20577e2381aSTim J. Robbins 	 */
20677e2381aSTim J. Robbins 	THREAD_LOCK();
2077e7f7ca3SEd Schouten 	SLIST_FOREACH(cur, &pidlist, next) {
20858f0484fSRodney W. Grimes 		if (cur->fp == iop)
20958f0484fSRodney W. Grimes 			break;
2107e7f7ca3SEd Schouten 		last = cur;
2117e7f7ca3SEd Schouten 	}
21277e2381aSTim J. Robbins 	if (cur == NULL) {
21377e2381aSTim J. Robbins 		THREAD_UNLOCK();
21458f0484fSRodney W. Grimes 		return (-1);
21577e2381aSTim J. Robbins 	}
21677e2381aSTim J. Robbins 	if (last == NULL)
2177e7f7ca3SEd Schouten 		SLIST_REMOVE_HEAD(&pidlist, next);
21877e2381aSTim J. Robbins 	else
2193d98b75bSEd Schouten 		SLIST_REMOVE_AFTER(last, next);
22077e2381aSTim J. Robbins 	THREAD_UNLOCK();
22158f0484fSRodney W. Grimes 
222adf6ad9eSPeter Wemm 	(void)fclose(iop);
223adf6ad9eSPeter Wemm 
22458f0484fSRodney W. Grimes 	do {
2259233c4d9SJason Evans 		pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0);
22658f0484fSRodney W. Grimes 	} while (pid == -1 && errno == EINTR);
22758f0484fSRodney W. Grimes 
22858f0484fSRodney W. Grimes 	free(cur);
22958f0484fSRodney W. Grimes 
230adf6ad9eSPeter Wemm 	return (pid == -1 ? -1 : pstat);
23158f0484fSRodney W. Grimes }
232