1 /* $OpenBSD: popen.c,v 1.17 2005/08/08 08:05:34 espie 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 45 static struct pid { 46 struct pid *next; 47 FILE *fp; 48 pid_t pid; 49 } *pidlist; 50 51 FILE * 52 popen(const char *program, const char *type) 53 { 54 struct pid * volatile cur; 55 FILE *iop; 56 int pdes[2]; 57 pid_t pid; 58 59 if ((*type != 'r' && *type != 'w') || type[1] != '\0') { 60 errno = EINVAL; 61 return (NULL); 62 } 63 64 if ((cur = malloc(sizeof(struct pid))) == NULL) 65 return (NULL); 66 67 if (pipe(pdes) < 0) { 68 free(cur); 69 return (NULL); 70 } 71 72 switch (pid = vfork()) { 73 case -1: /* Error. */ 74 (void)close(pdes[0]); 75 (void)close(pdes[1]); 76 free(cur); 77 return (NULL); 78 /* NOTREACHED */ 79 case 0: /* Child. */ 80 { 81 struct pid *pcur; 82 /* 83 * because vfork() instead of fork(), must leak FILE *, 84 * but luckily we are terminally headed for an execl() 85 */ 86 for (pcur = pidlist; pcur; pcur = pcur->next) 87 close(fileno(pcur->fp)); 88 89 if (*type == 'r') { 90 int tpdes1 = pdes[1]; 91 92 (void) close(pdes[0]); 93 /* 94 * We must NOT modify pdes, due to the 95 * semantics of vfork. 96 */ 97 if (tpdes1 != STDOUT_FILENO) { 98 (void)dup2(tpdes1, STDOUT_FILENO); 99 (void)close(tpdes1); 100 tpdes1 = STDOUT_FILENO; 101 } 102 } else { 103 (void)close(pdes[1]); 104 if (pdes[0] != STDIN_FILENO) { 105 (void)dup2(pdes[0], STDIN_FILENO); 106 (void)close(pdes[0]); 107 } 108 } 109 execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL); 110 _exit(127); 111 /* NOTREACHED */ 112 } 113 } 114 115 /* Parent; assume fdopen can't fail. */ 116 if (*type == 'r') { 117 iop = fdopen(pdes[0], type); 118 (void)close(pdes[1]); 119 } else { 120 iop = fdopen(pdes[1], type); 121 (void)close(pdes[0]); 122 } 123 124 /* Link into list of file descriptors. */ 125 cur->fp = iop; 126 cur->pid = pid; 127 cur->next = pidlist; 128 pidlist = cur; 129 130 return (iop); 131 } 132 133 /* 134 * pclose -- 135 * Pclose returns -1 if stream is not associated with a `popened' command, 136 * if already `pclosed', or waitpid returns an error. 137 */ 138 int 139 pclose(FILE *iop) 140 { 141 struct pid *cur, *last; 142 int pstat; 143 pid_t pid; 144 145 /* Find the appropriate file pointer. */ 146 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) 147 if (cur->fp == iop) 148 break; 149 150 if (cur == NULL) 151 return (-1); 152 153 (void)fclose(iop); 154 155 do { 156 pid = waitpid(cur->pid, &pstat, 0); 157 } while (pid == -1 && errno == EINTR); 158 159 /* Remove the entry from the linked list. */ 160 if (last == NULL) 161 pidlist = cur->next; 162 else 163 last->next = cur->next; 164 free(cur); 165 166 return (pid == -1 ? -1 : pstat); 167 } 168