1 /* $NetBSD: popen.c,v 1.27 2003/08/07 16:42:55 agc 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.27 2003/08/07 16:42:55 agc 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 pid_t pid; 72 } *pidlist; 73 74 FILE * 75 popen(command, type) 76 const char *command, *type; 77 { 78 struct pid *cur, *old; 79 FILE *iop; 80 int pdes[2], pid, twoway, serrno; 81 82 _DIAGASSERT(command != NULL); 83 _DIAGASSERT(type != NULL); 84 85 #ifdef __GNUC__ 86 /* This outrageous construct just to shut up a GCC warning. */ 87 (void) &cur; (void) &twoway; (void) &type; 88 #endif 89 90 if (strchr(type, '+')) { 91 twoway = 1; 92 type = "r+"; 93 if (socketpair(AF_LOCAL, SOCK_STREAM, 0, pdes) < 0) 94 return (NULL); 95 } else { 96 twoway = 0; 97 if ((*type != 'r' && *type != 'w') || type[1] || 98 (pipe(pdes) < 0)) { 99 errno = EINVAL; 100 return (NULL); 101 } 102 } 103 104 if ((cur = malloc(sizeof(struct pid))) == NULL) { 105 (void)close(pdes[0]); 106 (void)close(pdes[1]); 107 errno = ENOMEM; 108 return (NULL); 109 } 110 111 rwlock_rdlock(&__environ_lock); 112 switch (pid = vfork()) { 113 case -1: /* Error. */ 114 serrno = errno; 115 rwlock_unlock(&__environ_lock); 116 free(cur); 117 (void)close(pdes[0]); 118 (void)close(pdes[1]); 119 errno = serrno; 120 return (NULL); 121 /* NOTREACHED */ 122 case 0: /* Child. */ 123 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams 124 from previous popen() calls that remain open in the 125 parent process are closed in the new child process. */ 126 for (old = pidlist; old; old = old->next) 127 close(fileno(old->fp)); /* don't allow a flush */ 128 129 if (*type == 'r') { 130 (void)close(pdes[0]); 131 if (pdes[1] != STDOUT_FILENO) { 132 (void)dup2(pdes[1], STDOUT_FILENO); 133 (void)close(pdes[1]); 134 } 135 if (twoway) 136 (void)dup2(STDOUT_FILENO, STDIN_FILENO); 137 } else { 138 (void)close(pdes[1]); 139 if (pdes[0] != STDIN_FILENO) { 140 (void)dup2(pdes[0], STDIN_FILENO); 141 (void)close(pdes[0]); 142 } 143 } 144 145 execl(_PATH_BSHELL, "sh", "-c", command, NULL); 146 _exit(127); 147 /* NOTREACHED */ 148 } 149 rwlock_unlock(&__environ_lock); 150 151 /* Parent; assume fdopen can't fail. */ 152 if (*type == 'r') { 153 iop = fdopen(pdes[0], type); 154 (void)close(pdes[1]); 155 } else { 156 iop = fdopen(pdes[1], type); 157 (void)close(pdes[0]); 158 } 159 160 /* Link into list of file descriptors. */ 161 cur->fp = iop; 162 cur->pid = pid; 163 cur->next = pidlist; 164 pidlist = cur; 165 166 return (iop); 167 } 168 169 /* 170 * pclose -- 171 * Pclose returns -1 if stream is not associated with a `popened' command, 172 * if already `pclosed', or waitpid returns an error. 173 */ 174 int 175 pclose(iop) 176 FILE *iop; 177 { 178 struct pid *cur, *last; 179 int pstat; 180 pid_t pid; 181 182 _DIAGASSERT(iop != NULL); 183 184 /* Find the appropriate file pointer. */ 185 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 186 if (cur->fp == iop) 187 break; 188 if (cur == NULL) 189 return (-1); 190 191 (void)fclose(iop); 192 193 do { 194 pid = waitpid(cur->pid, &pstat, 0); 195 } while (pid == -1 && errno == EINTR); 196 197 /* Remove the entry from the linked list. */ 198 if (last == NULL) 199 pidlist = cur->next; 200 else 201 last->next = cur->next; 202 free(cur); 203 204 return (pid == -1 ? -1 : pstat); 205 } 206