158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1988, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * This code is derived from software written by Ken Arnold and 658f0484fSRodney W. Grimes * published in UNIX Review, Vol. 6, No. 8. 758f0484fSRodney W. Grimes * 858f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 958f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 1058f0484fSRodney W. Grimes * are met: 1158f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 1258f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1358f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1458f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1558f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1658f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1758f0484fSRodney W. Grimes * must display the following acknowledgement: 1858f0484fSRodney W. Grimes * This product includes software developed by the University of 1958f0484fSRodney W. Grimes * California, Berkeley and its contributors. 2058f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 2158f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 2258f0484fSRodney W. Grimes * without specific prior written permission. 2358f0484fSRodney W. Grimes * 2458f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2558f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2658f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2758f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2858f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2958f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 3058f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 3158f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 3258f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3358f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3458f0484fSRodney W. Grimes * SUCH DAMAGE. 3592927338SJason Evans * 3692927338SJason Evans * $FreeBSD$ 3758f0484fSRodney W. Grimes */ 3858f0484fSRodney W. Grimes 3958f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 40adf6ad9eSPeter Wemm static char sccsid[] = "@(#)popen.c 8.3 (Berkeley) 5/3/95"; 4158f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 4258f0484fSRodney W. Grimes 4358f0484fSRodney W. Grimes #include <sys/param.h> 4458f0484fSRodney W. Grimes #include <sys/wait.h> 4558f0484fSRodney W. Grimes 4658f0484fSRodney W. Grimes #include <signal.h> 4758f0484fSRodney W. Grimes #include <errno.h> 4858f0484fSRodney W. Grimes #include <unistd.h> 4958f0484fSRodney W. Grimes #include <stdio.h> 5058f0484fSRodney W. Grimes #include <stdlib.h> 5158f0484fSRodney W. Grimes #include <string.h> 5258f0484fSRodney W. Grimes #include <paths.h> 5358f0484fSRodney W. Grimes 540c372549SPeter Wemm extern char **environ; 550c372549SPeter Wemm 5658f0484fSRodney W. Grimes static struct pid { 5758f0484fSRodney W. Grimes struct pid *next; 5858f0484fSRodney W. Grimes FILE *fp; 5958f0484fSRodney W. Grimes pid_t pid; 6058f0484fSRodney W. Grimes } *pidlist; 6158f0484fSRodney W. Grimes 6258f0484fSRodney W. Grimes FILE * 63adf6ad9eSPeter Wemm popen(command, type) 64adf6ad9eSPeter Wemm const char *command, *type; 6558f0484fSRodney W. Grimes { 6658f0484fSRodney W. Grimes struct pid *cur; 6758f0484fSRodney W. Grimes FILE *iop; 68adf6ad9eSPeter Wemm int pdes[2], pid, twoway; 690c372549SPeter Wemm char *argv[4]; 7026c51fb4SMike Smith struct pid *p; 7158f0484fSRodney W. Grimes 72adf6ad9eSPeter Wemm /* 73adf6ad9eSPeter Wemm * Lite2 introduced two-way popen() pipes using socketpair(). 74adf6ad9eSPeter Wemm * FreeBSD's pipe() is bidirectional, so we use that. 75adf6ad9eSPeter Wemm */ 76adf6ad9eSPeter Wemm if (strchr(type, '+')) { 77adf6ad9eSPeter Wemm twoway = 1; 78adf6ad9eSPeter Wemm type = "r+"; 79adf6ad9eSPeter Wemm } else { 80adf6ad9eSPeter Wemm twoway = 0; 81035e5608SBruce Evans if ((*type != 'r' && *type != 'w') || type[1]) 82adf6ad9eSPeter Wemm return (NULL); 83adf6ad9eSPeter Wemm } 84adf6ad9eSPeter Wemm if (pipe(pdes) < 0) 8558f0484fSRodney W. Grimes return (NULL); 8658f0484fSRodney W. Grimes 87035e5608SBruce Evans if ((cur = malloc(sizeof(struct pid))) == NULL) { 889233c4d9SJason Evans (void)_close(pdes[0]); 899233c4d9SJason Evans (void)_close(pdes[1]); 9058f0484fSRodney W. Grimes return (NULL); 91035e5608SBruce Evans } 9258f0484fSRodney W. Grimes 930c372549SPeter Wemm argv[0] = "sh"; 940c372549SPeter Wemm argv[1] = "-c"; 950c372549SPeter Wemm argv[2] = (char *)command; 960c372549SPeter Wemm argv[3] = NULL; 970c372549SPeter Wemm 980c372549SPeter Wemm switch (pid = vfork()) { 9958f0484fSRodney W. Grimes case -1: /* Error. */ 1009233c4d9SJason Evans (void)_close(pdes[0]); 1019233c4d9SJason Evans (void)_close(pdes[1]); 102e78bad23SJeffrey Hsu free(cur); 10358f0484fSRodney W. Grimes return (NULL); 10458f0484fSRodney W. Grimes /* NOTREACHED */ 10558f0484fSRodney W. Grimes case 0: /* Child. */ 10658f0484fSRodney W. Grimes if (*type == 'r') { 1071174d9f9SJohn Dyson /* 1085ae9116aSJohn Dyson * The dup2() to STDIN_FILENO is repeated to avoid 1095ae9116aSJohn Dyson * writing to pdes[1], which might corrupt the 1105ae9116aSJohn Dyson * parent's copy. This isn't good enough in 1115ae9116aSJohn Dyson * general, since the _exit() is no return, so 1125ae9116aSJohn Dyson * the compiler is free to corrupt all the local 1135ae9116aSJohn Dyson * variables. 1141174d9f9SJohn Dyson */ 1159233c4d9SJason Evans (void)_close(pdes[0]); 1165ae9116aSJohn Dyson if (pdes[1] != STDOUT_FILENO) { 1175ae9116aSJohn Dyson (void)dup2(pdes[1], STDOUT_FILENO); 1189233c4d9SJason Evans (void)_close(pdes[1]); 1192b9ac168SBruce Evans if (twoway) 1205ae9116aSJohn Dyson (void)dup2(STDOUT_FILENO, STDIN_FILENO); 1215ae9116aSJohn Dyson } else if (twoway && (pdes[1] != STDIN_FILENO)) 1225ae9116aSJohn Dyson (void)dup2(pdes[1], STDIN_FILENO); 12358f0484fSRodney W. Grimes } else { 12458f0484fSRodney W. Grimes if (pdes[0] != STDIN_FILENO) { 12558f0484fSRodney W. Grimes (void)dup2(pdes[0], STDIN_FILENO); 1269233c4d9SJason Evans (void)_close(pdes[0]); 12758f0484fSRodney W. Grimes } 1289233c4d9SJason Evans (void)_close(pdes[1]); 12958f0484fSRodney W. Grimes } 13026c51fb4SMike Smith for (p = pidlist; p; p = p->next) { 1319233c4d9SJason Evans (void)_close(fileno(p->fp)); 13226c51fb4SMike Smith } 1330c372549SPeter Wemm execve(_PATH_BSHELL, argv, environ); 13458f0484fSRodney W. Grimes _exit(127); 13558f0484fSRodney W. Grimes /* NOTREACHED */ 13658f0484fSRodney W. Grimes } 13758f0484fSRodney W. Grimes 13858f0484fSRodney W. Grimes /* Parent; assume fdopen can't fail. */ 13958f0484fSRodney W. Grimes if (*type == 'r') { 14058f0484fSRodney W. Grimes iop = fdopen(pdes[0], type); 1419233c4d9SJason Evans (void)_close(pdes[1]); 14258f0484fSRodney W. Grimes } else { 14358f0484fSRodney W. Grimes iop = fdopen(pdes[1], type); 1449233c4d9SJason Evans (void)_close(pdes[0]); 14558f0484fSRodney W. Grimes } 14658f0484fSRodney W. Grimes 14758f0484fSRodney W. Grimes /* Link into list of file descriptors. */ 14858f0484fSRodney W. Grimes cur->fp = iop; 14958f0484fSRodney W. Grimes cur->pid = pid; 15058f0484fSRodney W. Grimes cur->next = pidlist; 15158f0484fSRodney W. Grimes pidlist = cur; 15258f0484fSRodney W. Grimes 15358f0484fSRodney W. Grimes return (iop); 15458f0484fSRodney W. Grimes } 15558f0484fSRodney W. Grimes 15658f0484fSRodney W. Grimes /* 15758f0484fSRodney W. Grimes * pclose -- 15858f0484fSRodney W. Grimes * Pclose returns -1 if stream is not associated with a `popened' command, 15958f0484fSRodney W. Grimes * if already `pclosed', or waitpid returns an error. 16058f0484fSRodney W. Grimes */ 16158f0484fSRodney W. Grimes int 16258f0484fSRodney W. Grimes pclose(iop) 16358f0484fSRodney W. Grimes FILE *iop; 16458f0484fSRodney W. Grimes { 16558f0484fSRodney W. Grimes register struct pid *cur, *last; 16658f0484fSRodney W. Grimes int omask; 167adf6ad9eSPeter Wemm int pstat; 16858f0484fSRodney W. Grimes pid_t pid; 16958f0484fSRodney W. Grimes 17058f0484fSRodney W. Grimes /* Find the appropriate file pointer. */ 17158f0484fSRodney W. Grimes for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 17258f0484fSRodney W. Grimes if (cur->fp == iop) 17358f0484fSRodney W. Grimes break; 17458f0484fSRodney W. Grimes if (cur == NULL) 17558f0484fSRodney W. Grimes return (-1); 17658f0484fSRodney W. Grimes 177adf6ad9eSPeter Wemm (void)fclose(iop); 178adf6ad9eSPeter Wemm 17958f0484fSRodney W. Grimes do { 1809233c4d9SJason Evans pid = _wait4(cur->pid, &pstat, 0, (struct rusage *)0); 18158f0484fSRodney W. Grimes } while (pid == -1 && errno == EINTR); 18258f0484fSRodney W. Grimes 18358f0484fSRodney W. Grimes /* Remove the entry from the linked list. */ 18458f0484fSRodney W. Grimes if (last == NULL) 18558f0484fSRodney W. Grimes pidlist = cur->next; 18658f0484fSRodney W. Grimes else 18758f0484fSRodney W. Grimes last->next = cur->next; 18858f0484fSRodney W. Grimes free(cur); 18958f0484fSRodney W. Grimes 190adf6ad9eSPeter Wemm return (pid == -1 ? -1 : pstat); 19158f0484fSRodney W. Grimes } 192