xref: /freebsd/contrib/blocklist/port/popenve.c (revision 5f4c09dd)
15f4c09ddSEd Maste /*	$NetBSD: popenve.c,v 1.2 2015/01/22 03:10:50 christos Exp $	*/
25f4c09ddSEd Maste 
35f4c09ddSEd Maste /*
45f4c09ddSEd Maste  * Copyright (c) 1988, 1993
55f4c09ddSEd Maste  *	The Regents of the University of California.  All rights reserved.
65f4c09ddSEd Maste  *
75f4c09ddSEd Maste  * This code is derived from software written by Ken Arnold and
85f4c09ddSEd Maste  * published in UNIX Review, Vol. 6, No. 8.
95f4c09ddSEd Maste  *
105f4c09ddSEd Maste  * Redistribution and use in source and binary forms, with or without
115f4c09ddSEd Maste  * modification, are permitted provided that the following conditions
125f4c09ddSEd Maste  * are met:
135f4c09ddSEd Maste  * 1. Redistributions of source code must retain the above copyright
145f4c09ddSEd Maste  *    notice, this list of conditions and the following disclaimer.
155f4c09ddSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
165f4c09ddSEd Maste  *    notice, this list of conditions and the following disclaimer in the
175f4c09ddSEd Maste  *    documentation and/or other materials provided with the distribution.
185f4c09ddSEd Maste  * 3. Neither the name of the University nor the names of its contributors
195f4c09ddSEd Maste  *    may be used to endorse or promote products derived from this software
205f4c09ddSEd Maste  *    without specific prior written permission.
215f4c09ddSEd Maste  *
225f4c09ddSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
235f4c09ddSEd Maste  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
245f4c09ddSEd Maste  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
255f4c09ddSEd Maste  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
265f4c09ddSEd Maste  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
275f4c09ddSEd Maste  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
285f4c09ddSEd Maste  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
295f4c09ddSEd Maste  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
305f4c09ddSEd Maste  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
315f4c09ddSEd Maste  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
325f4c09ddSEd Maste  * SUCH DAMAGE.
335f4c09ddSEd Maste  */
345f4c09ddSEd Maste 
355f4c09ddSEd Maste #ifdef HAVE_CONFIG_H
365f4c09ddSEd Maste #include "config.h"
375f4c09ddSEd Maste #endif
385f4c09ddSEd Maste 
395f4c09ddSEd Maste #include <sys/cdefs.h>
405f4c09ddSEd Maste #if defined(LIBC_SCCS) && !defined(lint)
415f4c09ddSEd Maste #if 0
425f4c09ddSEd Maste static char sccsid[] = "@(#)popen.c	8.3 (Berkeley) 5/3/95";
435f4c09ddSEd Maste #else
445f4c09ddSEd Maste __RCSID("$NetBSD: popenve.c,v 1.2 2015/01/22 03:10:50 christos Exp $");
455f4c09ddSEd Maste #endif
465f4c09ddSEd Maste #endif /* LIBC_SCCS and not lint */
475f4c09ddSEd Maste 
485f4c09ddSEd Maste #include <sys/param.h>
495f4c09ddSEd Maste #include <sys/wait.h>
505f4c09ddSEd Maste #include <sys/socket.h>
515f4c09ddSEd Maste 
525f4c09ddSEd Maste #include <assert.h>
535f4c09ddSEd Maste #include <errno.h>
545f4c09ddSEd Maste #include <paths.h>
555f4c09ddSEd Maste #include <signal.h>
565f4c09ddSEd Maste #include <stdio.h>
575f4c09ddSEd Maste #include <stdlib.h>
585f4c09ddSEd Maste #include <string.h>
595f4c09ddSEd Maste #include <unistd.h>
605f4c09ddSEd Maste #include <fcntl.h>
615f4c09ddSEd Maste 
625f4c09ddSEd Maste #ifdef __weak_alias
635f4c09ddSEd Maste __weak_alias(popen,_popen)
645f4c09ddSEd Maste __weak_alias(pclose,_pclose)
655f4c09ddSEd Maste #endif
665f4c09ddSEd Maste 
675f4c09ddSEd Maste static struct pid {
685f4c09ddSEd Maste 	struct pid *next;
695f4c09ddSEd Maste 	FILE *fp;
705f4c09ddSEd Maste #ifdef _REENTRANT
715f4c09ddSEd Maste 	int fd;
725f4c09ddSEd Maste #endif
735f4c09ddSEd Maste 	pid_t pid;
745f4c09ddSEd Maste } *pidlist;
755f4c09ddSEd Maste 
765f4c09ddSEd Maste #ifdef _REENTRANT
775f4c09ddSEd Maste static rwlock_t pidlist_lock = RWLOCK_INITIALIZER;
785f4c09ddSEd Maste #endif
795f4c09ddSEd Maste 
805f4c09ddSEd Maste static struct pid *
pdes_get(int * pdes,const char ** type)815f4c09ddSEd Maste pdes_get(int *pdes, const char **type)
825f4c09ddSEd Maste {
835f4c09ddSEd Maste 	struct pid *cur;
845f4c09ddSEd Maste 	int flags = strchr(*type, 'e') ? O_CLOEXEC : 0;
855f4c09ddSEd Maste 	int serrno;
865f4c09ddSEd Maste 
875f4c09ddSEd Maste 	if (strchr(*type, '+')) {
885f4c09ddSEd Maste #ifndef SOCK_CLOEXEC
895f4c09ddSEd Maste #define SOCK_CLOEXEC 0
905f4c09ddSEd Maste #endif
915f4c09ddSEd Maste 		int stype = flags ? (SOCK_STREAM | SOCK_CLOEXEC) : SOCK_STREAM;
925f4c09ddSEd Maste 		*type = "r+";
935f4c09ddSEd Maste 		if (socketpair(AF_LOCAL, stype, 0, pdes) < 0)
945f4c09ddSEd Maste 			return NULL;
955f4c09ddSEd Maste #if SOCK_CLOEXEC == 0
965f4c09ddSEd Maste 		fcntl(pdes[0], F_SETFD, FD_CLOEXEC);
975f4c09ddSEd Maste 		fcntl(pdes[1], F_SETFD, FD_CLOEXEC);
985f4c09ddSEd Maste #endif
995f4c09ddSEd Maste 	} else  {
1005f4c09ddSEd Maste 		*type = strrchr(*type, 'r') ? "r" : "w";
1015f4c09ddSEd Maste #if SOCK_CLOEXEC != 0
1025f4c09ddSEd Maste 		if (pipe2(pdes, flags) == -1)
1035f4c09ddSEd Maste 			return NULL;
1045f4c09ddSEd Maste #else
1055f4c09ddSEd Maste 		if (pipe(pdes) == -1)
1065f4c09ddSEd Maste 			return NULL;
1075f4c09ddSEd Maste 		fcntl(pdes[0], F_SETFL, fcntl(pdes[0], F_GETFL) | flags);
1085f4c09ddSEd Maste 		fcntl(pdes[1], F_SETFL, fcntl(pdes[1], F_GETFL) | flags);
1095f4c09ddSEd Maste #endif
1105f4c09ddSEd Maste 	}
1115f4c09ddSEd Maste 
1125f4c09ddSEd Maste 	if ((cur = malloc(sizeof(*cur))) != NULL)
1135f4c09ddSEd Maste 		return cur;
1145f4c09ddSEd Maste 	serrno = errno;
1155f4c09ddSEd Maste 	(void)close(pdes[0]);
1165f4c09ddSEd Maste 	(void)close(pdes[1]);
1175f4c09ddSEd Maste 	errno = serrno;
1185f4c09ddSEd Maste 	return NULL;
1195f4c09ddSEd Maste }
1205f4c09ddSEd Maste 
1215f4c09ddSEd Maste static void
pdes_child(int * pdes,const char * type)1225f4c09ddSEd Maste pdes_child(int *pdes, const char *type)
1235f4c09ddSEd Maste {
1245f4c09ddSEd Maste 	struct pid *old;
1255f4c09ddSEd Maste 
1265f4c09ddSEd Maste 	/* POSIX.2 B.3.2.2 "popen() shall ensure that any streams
1275f4c09ddSEd Maste 	   from previous popen() calls that remain open in the
1285f4c09ddSEd Maste 	   parent process are closed in the new child process. */
1295f4c09ddSEd Maste 	for (old = pidlist; old; old = old->next)
1305f4c09ddSEd Maste #ifdef _REENTRANT
1315f4c09ddSEd Maste 		(void)close(old->fd); /* don't allow a flush */
1325f4c09ddSEd Maste #else
1335f4c09ddSEd Maste 		(void)close(fileno(old->fp)); /* don't allow a flush */
1345f4c09ddSEd Maste #endif
1355f4c09ddSEd Maste 
1365f4c09ddSEd Maste 	if (type[0] == 'r') {
1375f4c09ddSEd Maste 		(void)close(pdes[0]);
1385f4c09ddSEd Maste 		if (pdes[1] != STDOUT_FILENO) {
1395f4c09ddSEd Maste 			(void)dup2(pdes[1], STDOUT_FILENO);
1405f4c09ddSEd Maste 			(void)close(pdes[1]);
1415f4c09ddSEd Maste 		}
1425f4c09ddSEd Maste 		if (type[1] == '+')
1435f4c09ddSEd Maste 			(void)dup2(STDOUT_FILENO, STDIN_FILENO);
1445f4c09ddSEd Maste 	} else {
1455f4c09ddSEd Maste 		(void)close(pdes[1]);
1465f4c09ddSEd Maste 		if (pdes[0] != STDIN_FILENO) {
1475f4c09ddSEd Maste 			(void)dup2(pdes[0], STDIN_FILENO);
1485f4c09ddSEd Maste 			(void)close(pdes[0]);
1495f4c09ddSEd Maste 		}
1505f4c09ddSEd Maste 	}
1515f4c09ddSEd Maste }
1525f4c09ddSEd Maste 
1535f4c09ddSEd Maste static void
pdes_parent(int * pdes,struct pid * cur,pid_t pid,const char * type)1545f4c09ddSEd Maste pdes_parent(int *pdes, struct pid *cur, pid_t pid, const char *type)
1555f4c09ddSEd Maste {
1565f4c09ddSEd Maste 	FILE *iop;
1575f4c09ddSEd Maste 
1585f4c09ddSEd Maste 	/* Parent; assume fdopen can't fail. */
1595f4c09ddSEd Maste 	if (*type == 'r') {
1605f4c09ddSEd Maste 		iop = fdopen(pdes[0], type);
1615f4c09ddSEd Maste #ifdef _REENTRANT
1625f4c09ddSEd Maste 		cur->fd = pdes[0];
1635f4c09ddSEd Maste #endif
1645f4c09ddSEd Maste 		(void)close(pdes[1]);
1655f4c09ddSEd Maste 	} else {
1665f4c09ddSEd Maste 		iop = fdopen(pdes[1], type);
1675f4c09ddSEd Maste #ifdef _REENTRANT
1685f4c09ddSEd Maste 		cur->fd = pdes[1];
1695f4c09ddSEd Maste #endif
1705f4c09ddSEd Maste 		(void)close(pdes[0]);
1715f4c09ddSEd Maste 	}
1725f4c09ddSEd Maste 
1735f4c09ddSEd Maste 	/* Link into list of file descriptors. */
1745f4c09ddSEd Maste 	cur->fp = iop;
1755f4c09ddSEd Maste 	cur->pid =  pid;
1765f4c09ddSEd Maste 	cur->next = pidlist;
1775f4c09ddSEd Maste 	pidlist = cur;
1785f4c09ddSEd Maste }
1795f4c09ddSEd Maste 
1805f4c09ddSEd Maste static void
pdes_error(int * pdes,struct pid * cur)1815f4c09ddSEd Maste pdes_error(int *pdes, struct pid *cur)
1825f4c09ddSEd Maste {
1835f4c09ddSEd Maste 	free(cur);
1845f4c09ddSEd Maste 	(void)close(pdes[0]);
1855f4c09ddSEd Maste 	(void)close(pdes[1]);
1865f4c09ddSEd Maste }
1875f4c09ddSEd Maste 
1885f4c09ddSEd Maste FILE *
popenve(const char * cmd,char * const * argv,char * const * envp,const char * type)1895f4c09ddSEd Maste popenve(const char *cmd, char *const *argv, char *const *envp, const char *type)
1905f4c09ddSEd Maste {
1915f4c09ddSEd Maste 	struct pid *cur;
1925f4c09ddSEd Maste 	int pdes[2], serrno;
1935f4c09ddSEd Maste 	pid_t pid;
1945f4c09ddSEd Maste 
1955f4c09ddSEd Maste 	if ((cur = pdes_get(pdes, &type)) == NULL)
1965f4c09ddSEd Maste 		return NULL;
1975f4c09ddSEd Maste 
1985f4c09ddSEd Maste #ifdef _REENTRANT
1995f4c09ddSEd Maste 	(void)rwlock_rdlock(&pidlist_lock);
2005f4c09ddSEd Maste #endif
2015f4c09ddSEd Maste 	switch (pid = vfork()) {
2025f4c09ddSEd Maste 	case -1:			/* Error. */
2035f4c09ddSEd Maste 		serrno = errno;
2045f4c09ddSEd Maste #ifdef _REENTRANT
2055f4c09ddSEd Maste 		(void)rwlock_unlock(&pidlist_lock);
2065f4c09ddSEd Maste #endif
2075f4c09ddSEd Maste 		pdes_error(pdes, cur);
2085f4c09ddSEd Maste 		errno = serrno;
2095f4c09ddSEd Maste 		return NULL;
2105f4c09ddSEd Maste 		/* NOTREACHED */
2115f4c09ddSEd Maste 	case 0:				/* Child. */
2125f4c09ddSEd Maste 		pdes_child(pdes, type);
2135f4c09ddSEd Maste 		execve(cmd, argv, envp);
2145f4c09ddSEd Maste 		_exit(127);
2155f4c09ddSEd Maste 		/* NOTREACHED */
2165f4c09ddSEd Maste 	}
2175f4c09ddSEd Maste 
2185f4c09ddSEd Maste 	pdes_parent(pdes, cur, pid, type);
2195f4c09ddSEd Maste 
2205f4c09ddSEd Maste #ifdef _REENTRANT
2215f4c09ddSEd Maste 	(void)rwlock_unlock(&pidlist_lock);
2225f4c09ddSEd Maste #endif
2235f4c09ddSEd Maste 
2245f4c09ddSEd Maste 	return cur->fp;
2255f4c09ddSEd Maste }
2265f4c09ddSEd Maste 
2275f4c09ddSEd Maste /*
2285f4c09ddSEd Maste  * pclose --
2295f4c09ddSEd Maste  *	Pclose returns -1 if stream is not associated with a `popened' command,
2305f4c09ddSEd Maste  *	if already `pclosed', or waitpid returns an error.
2315f4c09ddSEd Maste  */
2325f4c09ddSEd Maste int
pcloseve(FILE * iop)2335f4c09ddSEd Maste pcloseve(FILE *iop)
2345f4c09ddSEd Maste {
2355f4c09ddSEd Maste 	struct pid *cur, *last;
2365f4c09ddSEd Maste 	int pstat;
2375f4c09ddSEd Maste 	pid_t pid;
2385f4c09ddSEd Maste 
2395f4c09ddSEd Maste #ifdef _REENTRANT
2405f4c09ddSEd Maste 	rwlock_wrlock(&pidlist_lock);
2415f4c09ddSEd Maste #endif
2425f4c09ddSEd Maste 
2435f4c09ddSEd Maste 	/* Find the appropriate file pointer. */
2445f4c09ddSEd Maste 	for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next)
2455f4c09ddSEd Maste 		if (cur->fp == iop)
2465f4c09ddSEd Maste 			break;
2475f4c09ddSEd Maste 	if (cur == NULL) {
2485f4c09ddSEd Maste #ifdef _REENTRANT
2495f4c09ddSEd Maste 		(void)rwlock_unlock(&pidlist_lock);
2505f4c09ddSEd Maste #endif
2515f4c09ddSEd Maste 		errno = ESRCH;
2525f4c09ddSEd Maste 		return -1;
2535f4c09ddSEd Maste 	}
2545f4c09ddSEd Maste 
2555f4c09ddSEd Maste 	(void)fclose(iop);
2565f4c09ddSEd Maste 
2575f4c09ddSEd Maste 	/* Remove the entry from the linked list. */
2585f4c09ddSEd Maste 	if (last == NULL)
2595f4c09ddSEd Maste 		pidlist = cur->next;
2605f4c09ddSEd Maste 	else
2615f4c09ddSEd Maste 		last->next = cur->next;
2625f4c09ddSEd Maste 
2635f4c09ddSEd Maste #ifdef _REENTRANT
2645f4c09ddSEd Maste 	(void)rwlock_unlock(&pidlist_lock);
2655f4c09ddSEd Maste #endif
2665f4c09ddSEd Maste 
2675f4c09ddSEd Maste 	do {
2685f4c09ddSEd Maste 		pid = waitpid(cur->pid, &pstat, 0);
2695f4c09ddSEd Maste 	} while (pid == -1 && errno == EINTR);
2705f4c09ddSEd Maste 
2715f4c09ddSEd Maste 	free(cur);
2725f4c09ddSEd Maste 
2735f4c09ddSEd Maste 	return pid == -1 ? -1 : pstat;
2745f4c09ddSEd Maste }
275