1 /* $OpenBSD: popen.c,v 1.18 2007/11/26 19:26:46 kurt Exp $ */ 2 /* 3 * Copyright (c) 1988, 1993 4 * The Regents of the University of California. All rights reserved. 5 * 6 * This code is derived from software written by Ken Arnold and 7 * published in UNIX Review, Vol. 6, No. 8. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 3. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #include <sys/param.h> 35 #include <sys/wait.h> 36 37 #include <signal.h> 38 #include <errno.h> 39 #include <unistd.h> 40 #include <stdio.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <paths.h> 44 #include "thread_private.h" 45 46 static struct pid { 47 struct pid *next; 48 FILE *fp; 49 pid_t pid; 50 } *pidlist; 51 52 static void *pidlist_lock = NULL; 53 54 FILE * 55 popen(const char *program, const char *type) 56 { 57 struct pid * volatile cur; 58 FILE *iop; 59 int pdes[2]; 60 pid_t pid; 61 62 if ((*type != 'r' && *type != 'w') || type[1] != '\0') { 63 errno = EINVAL; 64 return (NULL); 65 } 66 67 if ((cur = malloc(sizeof(struct pid))) == NULL) 68 return (NULL); 69 70 if (pipe(pdes) < 0) { 71 free(cur); 72 return (NULL); 73 } 74 75 _MUTEX_LOCK(&pidlist_lock); 76 switch (pid = vfork()) { 77 case -1: /* Error. */ 78 _MUTEX_UNLOCK(&pidlist_lock); 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 _MUTEX_UNLOCK(&pidlist_lock); 120 121 /* Parent; assume fdopen can't fail. */ 122 if (*type == 'r') { 123 iop = fdopen(pdes[0], type); 124 (void)close(pdes[1]); 125 } else { 126 iop = fdopen(pdes[1], type); 127 (void)close(pdes[0]); 128 } 129 130 /* Link into list of file descriptors. */ 131 cur->fp = iop; 132 cur->pid = pid; 133 _MUTEX_LOCK(&pidlist_lock); 134 cur->next = pidlist; 135 pidlist = cur; 136 _MUTEX_UNLOCK(&pidlist_lock); 137 138 return (iop); 139 } 140 141 /* 142 * pclose -- 143 * Pclose returns -1 if stream is not associated with a `popened' command, 144 * if already `pclosed', or waitpid returns an error. 145 */ 146 int 147 pclose(FILE *iop) 148 { 149 struct pid *cur, *last; 150 int pstat; 151 pid_t pid; 152 153 /* Find the appropriate file pointer. */ 154 _MUTEX_LOCK(&pidlist_lock); 155 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 156 if (cur->fp == iop) 157 break; 158 159 if (cur == NULL) { 160 _MUTEX_UNLOCK(&pidlist_lock); 161 return (-1); 162 } 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 _MUTEX_UNLOCK(&pidlist_lock); 170 171 (void)fclose(iop); 172 173 do { 174 pid = waitpid(cur->pid, &pstat, 0); 175 } while (pid == -1 && errno == EINTR); 176 177 free(cur); 178 179 return (pid == -1 ? -1 : pstat); 180 } 181