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