1 /* $NetBSD: popen.c,v 1.32 2012/06/25 22:32:43 abs 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.32 2012/06/25 22:32:43 abs 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 <fcntl.h> 58 59 #include "env.h" 60 #include "reentrant.h" 61 62 #ifdef __weak_alias 63 __weak_alias(popen,_popen) 64 __weak_alias(pclose,_pclose) 65 #endif 66 67 static struct pid { 68 struct pid *next; 69 FILE *fp; 70 #ifdef _REENTRANT 71 int fd; 72 #endif 73 pid_t pid; 74 } *pidlist; 75 76 #ifdef _REENTRANT 77 static rwlock_t pidlist_lock = RWLOCK_INITIALIZER; 78 #endif 79 80 FILE * 81 popen(const char *command, const char *type) 82 { 83 struct pid *cur, *old; 84 FILE *iop; 85 const char * volatile xtype = type; 86 int pdes[2], pid, serrno; 87 volatile int twoway; 88 int flags; 89 90 _DIAGASSERT(command != NULL); 91 _DIAGASSERT(xtype != NULL); 92 93 flags = strchr(xtype, 'e') ? O_CLOEXEC : 0; 94 if (strchr(xtype, '+')) { 95 int stype = flags ? (SOCK_STREAM | SOCK_CLOEXEC) : SOCK_STREAM; 96 twoway = 1; 97 xtype = "r+"; 98 if (socketpair(AF_LOCAL, stype, 0, pdes) < 0) 99 return NULL; 100 } else { 101 twoway = 0; 102 xtype = strrchr(xtype, 'r') ? "r" : "w"; 103 if (pipe2(pdes, flags) == -1) 104 return NULL; 105 } 106 107 if ((cur = malloc(sizeof(struct pid))) == NULL) { 108 (void)close(pdes[0]); 109 (void)close(pdes[1]); 110 errno = ENOMEM; 111 return (NULL); 112 } 113 114 #if defined(__minix) 115 rwlock_rdlock(&pidlist_lock); 116 #else 117 (void)rwlock_rdlock(&pidlist_lock); 118 #endif /* defined(__minix) */ 119 (void)__readlockenv(); 120 switch (pid = vfork()) { 121 case -1: /* Error. */ 122 serrno = errno; 123 (void)__unlockenv(); 124 #if defined(__minix) 125 rwlock_unlock(&pidlist_lock); 126 #else 127 (void)rwlock_unlock(&pidlist_lock); 128 #endif /* defined(__minix) */ 129 free(cur); 130 (void)close(pdes[0]); 131 (void)close(pdes[1]); 132 errno = serrno; 133 return (NULL); 134 /* NOTREACHED */ 135 case 0: /* Child. */ 136 /* POSIX.2 B.3.2.2 "popen() shall ensure that any streams 137 from previous popen() calls that remain open in the 138 parent process are closed in the new child process. */ 139 for (old = pidlist; old; old = old->next) 140 #ifdef _REENTRANT 141 close(old->fd); /* don't allow a flush */ 142 #else 143 close(fileno(old->fp)); /* don't allow a flush */ 144 #endif 145 146 if (*xtype == 'r') { 147 (void)close(pdes[0]); 148 if (pdes[1] != STDOUT_FILENO) { 149 (void)dup2(pdes[1], STDOUT_FILENO); 150 (void)close(pdes[1]); 151 } 152 if (twoway) 153 (void)dup2(STDOUT_FILENO, STDIN_FILENO); 154 } else { 155 (void)close(pdes[1]); 156 if (pdes[0] != STDIN_FILENO) { 157 (void)dup2(pdes[0], STDIN_FILENO); 158 (void)close(pdes[0]); 159 } 160 } 161 162 execl(_PATH_BSHELL, "sh", "-c", command, NULL); 163 _exit(127); 164 /* NOTREACHED */ 165 } 166 (void)__unlockenv(); 167 168 /* Parent; assume fdopen can't fail. */ 169 if (*xtype == 'r') { 170 iop = fdopen(pdes[0], xtype); 171 #ifdef _REENTRANT 172 cur->fd = pdes[0]; 173 #endif 174 (void)close(pdes[1]); 175 } else { 176 iop = fdopen(pdes[1], xtype); 177 #ifdef _REENTRANT 178 cur->fd = pdes[1]; 179 #endif 180 (void)close(pdes[0]); 181 } 182 183 /* Link into list of file descriptors. */ 184 cur->fp = iop; 185 cur->pid = pid; 186 cur->next = pidlist; 187 pidlist = cur; 188 #if defined(__minix) 189 rwlock_unlock(&pidlist_lock); 190 #else 191 (void)rwlock_unlock(&pidlist_lock); 192 #endif /* defined(__minix) */ 193 194 return (iop); 195 } 196 197 /* 198 * pclose -- 199 * Pclose returns -1 if stream is not associated with a `popened' command, 200 * if already `pclosed', or waitpid returns an error. 201 */ 202 int 203 pclose(FILE *iop) 204 { 205 struct pid *cur, *last; 206 int pstat; 207 pid_t pid; 208 209 _DIAGASSERT(iop != NULL); 210 211 rwlock_wrlock(&pidlist_lock); 212 213 /* Find the appropriate file pointer. */ 214 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 215 if (cur->fp == iop) 216 break; 217 if (cur == NULL) { 218 #if defined(__minix) 219 rwlock_unlock(&pidlist_lock); 220 #else 221 (void)rwlock_unlock(&pidlist_lock); 222 #endif /* defined(__minix) */ 223 return (-1); 224 } 225 226 (void)fclose(iop); 227 228 /* Remove the entry from the linked list. */ 229 if (last == NULL) 230 pidlist = cur->next; 231 else 232 last->next = cur->next; 233 234 #if defined(__minix) 235 rwlock_unlock(&pidlist_lock); 236 #else 237 (void)rwlock_unlock(&pidlist_lock); 238 #endif /* defined(__minix) */ 239 240 do { 241 pid = waitpid(cur->pid, &pstat, 0); 242 } while (pid == -1 && errno == EINTR); 243 244 free(cur); 245 246 return (pid == -1 ? -1 : pstat); 247 } 248