1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * This code is derived from software written by Ken Arnold and 6 * published in UNIX Review, Vol. 6, No. 8. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #if defined(LIBC_SCCS) && !defined(lint) 34 static char rcsid[] = "$OpenBSD: popen.c,v 1.13 2003/06/02 20:18:34 millert Exp $"; 35 #endif /* LIBC_SCCS and not lint */ 36 37 #include <sys/param.h> 38 #include <sys/wait.h> 39 40 #include <signal.h> 41 #include <errno.h> 42 #include <unistd.h> 43 #include <stdio.h> 44 #include <stdlib.h> 45 #include <string.h> 46 #include <paths.h> 47 48 static struct pid { 49 struct pid *next; 50 FILE *fp; 51 pid_t pid; 52 } *pidlist; 53 54 FILE * 55 popen(program, type) 56 const char *program; 57 const char *type; 58 { 59 struct pid * volatile cur; 60 FILE *iop; 61 int pdes[2]; 62 pid_t pid; 63 64 if ((*type != 'r' && *type != 'w') || type[1] != '\0') { 65 errno = EINVAL; 66 return (NULL); 67 } 68 69 if ((cur = malloc(sizeof(struct pid))) == NULL) 70 return (NULL); 71 72 if (pipe(pdes) < 0) { 73 free(cur); 74 return (NULL); 75 } 76 77 switch (pid = vfork()) { 78 case -1: /* Error. */ 79 (void)close(pdes[0]); 80 (void)close(pdes[1]); 81 free(cur); 82 return (NULL); 83 /* NOTREACHED */ 84 case 0: /* Child. */ 85 { 86 struct pid *pcur; 87 /* 88 * because vfork() instead of fork(), must leak FILE *, 89 * but luckily we are terminally headed for an execl() 90 */ 91 for (pcur = pidlist; pcur; pcur = pcur->next) 92 close(fileno(pcur->fp)); 93 94 if (*type == 'r') { 95 int tpdes1 = pdes[1]; 96 97 (void) close(pdes[0]); 98 /* 99 * We must NOT modify pdes, due to the 100 * semantics of vfork. 101 */ 102 if (tpdes1 != STDOUT_FILENO) { 103 (void)dup2(tpdes1, STDOUT_FILENO); 104 (void)close(tpdes1); 105 tpdes1 = STDOUT_FILENO; 106 } 107 } else { 108 (void)close(pdes[1]); 109 if (pdes[0] != STDIN_FILENO) { 110 (void)dup2(pdes[0], STDIN_FILENO); 111 (void)close(pdes[0]); 112 } 113 } 114 execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL); 115 _exit(127); 116 /* NOTREACHED */ 117 } 118 } 119 120 /* Parent; assume fdopen can't fail. */ 121 if (*type == 'r') { 122 iop = fdopen(pdes[0], type); 123 (void)close(pdes[1]); 124 } else { 125 iop = fdopen(pdes[1], type); 126 (void)close(pdes[0]); 127 } 128 129 /* Link into list of file descriptors. */ 130 cur->fp = iop; 131 cur->pid = pid; 132 cur->next = pidlist; 133 pidlist = cur; 134 135 return (iop); 136 } 137 138 /* 139 * pclose -- 140 * Pclose returns -1 if stream is not associated with a `popened' command, 141 * if already `pclosed', or waitpid returns an error. 142 */ 143 int 144 pclose(iop) 145 FILE *iop; 146 { 147 register struct pid *cur, *last; 148 int pstat; 149 pid_t pid; 150 151 (void)fclose(iop); 152 153 /* Find the appropriate file pointer. */ 154 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 155 if (cur->fp == iop) 156 break; 157 if (cur == NULL) 158 return (-1); 159 160 do { 161 pid = waitpid(cur->pid, &pstat, 0); 162 } while (pid == -1 && errno == EINTR); 163 164 /* Remove the entry from the linked list. */ 165 if (last == NULL) 166 pidlist = cur->next; 167 else 168 last->next = cur->next; 169 free(cur); 170 171 return (pid == -1 ? -1 : pstat); 172 } 173