1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * Copyright (c) 2004 The DragonFly Project. 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. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * $FreeBSD: src/lib/libc/gen/popen.c,v 1.14 2000/01/27 23:06:19 jasone Exp $ 38 * $Id: popen.c,v 1.6 2005/02/06 06:57:30 cpressey Exp $ 39 * 40 * @(#)popen.c 8.3 (Berkeley) 5/3/95 41 * 42 * A modified version of the standard popen()/pclose() functions 43 * which adds a third function, pgetpid(), which allows the program 44 * which used popen() to obtain the pid of the process on the other 45 * end of the pipe. 46 */ 47 48 #include <sys/param.h> 49 #include <sys/wait.h> 50 #include <sys/types.h> 51 #include <sys/time.h> 52 53 #include <errno.h> 54 #include <signal.h> 55 #include <stdarg.h> 56 #include <paths.h> 57 #include <stdio.h> 58 #include <stdlib.h> 59 #include <string.h> 60 #include <unistd.h> 61 62 #include "popen.h" 63 64 extern char **environ; 65 66 static struct pid { 67 struct pid *next; 68 FILE *fp; 69 pid_t pid; 70 } *pidlist; 71 72 FILE * 73 aura_popen(const char *fmt, const char *type, ...) 74 { 75 FILE *iop; 76 struct pid *cur; 77 struct pid *p; 78 va_list args; 79 char *command; 80 const char *argv[4]; 81 int pdes[2], pid; 82 volatile enum { READ, WRITE, TWO_WAY } pipedir; 83 84 /* 85 * Lite2 introduced two-way popen() pipes using socketpair(). 86 * FreeBSD's pipe() is bidirectional, so we use that. 87 */ 88 if (strchr(type, '+')) { 89 pipedir = TWO_WAY; 90 } else { 91 if (type[1] != '\0') 92 return(NULL); 93 if (*type == 'r') 94 pipedir = READ; 95 else if (*type == 'w') 96 pipedir = WRITE; 97 else 98 return(NULL); 99 } 100 101 if (pipe(pdes) < 0) 102 return(NULL); 103 104 if ((cur = malloc(sizeof(struct pid))) == NULL) { 105 close(pdes[0]); 106 close(pdes[1]); 107 return(NULL); 108 } 109 110 va_start(args, type); 111 vasprintf(&command, fmt, args); 112 va_end(args); 113 114 argv[0] = "sh"; 115 argv[1] = "-c"; 116 argv[2] = command; 117 argv[3] = NULL; 118 119 switch (pid = vfork()) { 120 case -1: /* Error. */ 121 close(pdes[0]); 122 close(pdes[1]); 123 free(cur); 124 free(command); 125 return(NULL); 126 /* NOTREACHED */ 127 case 0: /* Child. */ 128 if (pipedir == READ || pipedir == TWO_WAY) { 129 /* 130 * The dup2() to STDIN_FILENO is repeated to avoid 131 * writing to pdes[1], which might corrupt the 132 * parent's copy. This isn't good enough in 133 * general, since the _exit() is no return, so 134 * the compiler is free to corrupt all the local 135 * variables. 136 */ 137 close(pdes[0]); 138 if (pdes[1] != STDOUT_FILENO) { 139 dup2(pdes[1], STDOUT_FILENO); 140 close(pdes[1]); 141 if (pipedir == TWO_WAY) 142 dup2(STDOUT_FILENO, STDIN_FILENO); 143 } else if (pipedir == TWO_WAY && 144 (pdes[1] != STDIN_FILENO)) { 145 dup2(pdes[1], STDIN_FILENO); 146 } 147 } else { 148 if (pdes[0] != STDIN_FILENO) { 149 dup2(pdes[0], STDIN_FILENO); 150 close(pdes[0]); 151 } 152 close(pdes[1]); 153 } 154 for (p = pidlist; p; p = p->next) { 155 close(fileno(p->fp)); 156 } 157 execve(_PATH_BSHELL, __DECONST(char **, argv), environ); 158 _exit(127); 159 /* NOTREACHED */ 160 } 161 162 /* Parent; assume fdopen can't fail. */ 163 if (pipedir == READ || pipedir == TWO_WAY) { 164 iop = fdopen(pdes[0], type); 165 close(pdes[1]); 166 } else { 167 iop = fdopen(pdes[1], type); 168 close(pdes[0]); 169 } 170 171 /* Link into list of file descriptors. */ 172 cur->fp = iop; 173 cur->pid = pid; 174 cur->next = pidlist; 175 pidlist = cur; 176 177 free(command); 178 return(iop); 179 } 180 181 /* 182 * pclose -- 183 * Pclose returns -1 if stream is not associated with a `popened' command, 184 * if already `pclosed', or waitpid returns an error. 185 */ 186 int 187 aura_pclose(FILE *iop) 188 { 189 struct pid *cur, *last; 190 int pstat; 191 pid_t pid; 192 193 /* Find the appropriate file pointer. */ 194 for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) { 195 if (cur->fp == iop) 196 break; 197 } 198 199 if (cur == NULL) 200 return (-1); 201 202 fclose(iop); 203 204 do { 205 pid = wait4(cur->pid, &pstat, 0, (struct rusage *)0); 206 } while (pid == -1 && errno == EINTR); 207 208 /* Remove the entry from the linked list. */ 209 if (last == NULL) 210 pidlist = cur->next; 211 else 212 last->next = cur->next; 213 free(cur); 214 215 return (pid == -1 ? -1 : pstat); 216 } 217 218 pid_t 219 aura_pgetpid(FILE *iop) 220 { 221 struct pid *cur; 222 223 /* Find the appropriate file pointer. */ 224 for (cur = pidlist; cur; cur = cur->next) 225 if (cur->fp == iop) 226 break; 227 if (cur == NULL) 228 return (-1); 229 return(cur->pid); 230 } 231 232 /* 233 * Returns: 234 * 1 if all went well. 235 * 0 if an error occurred, in which case err is set to: 236 * AURA_PGETS_TIMEOUT: select() timed out. 237 * AURA_PGETS_SELECT_ERR: a select() error occurred. 238 * AURA_PGETS_EOF: end of file condition on pipe. 239 * AURA_PGETS_FGETS_ERR: a fgets() error occurred. 240 */ 241 int 242 aura_pgets(FILE *p, char *buf, size_t len, long msec, int *err) 243 { 244 struct timeval tv; 245 struct timeval *tvp; 246 fd_set r; 247 int n; 248 249 tv.tv_sec = msec / 1000; 250 tv.tv_usec = (msec % 1000) * 1000; 251 tvp = (msec < 0 ? NULL : &tv); 252 253 FD_ZERO(&r); 254 FD_SET(fileno(p), &r); 255 256 *err = 0; 257 buf[0] = '\0'; 258 259 if (feof(p)) { 260 *err = AURA_PGETS_EOF; 261 return(0); 262 } 263 if (ferror(p)) { 264 *err = AURA_PGETS_FGETS_ERR; 265 return(0); 266 } 267 268 n = select(fileno(p) + 1, &r, NULL, NULL, tvp); 269 270 if (n == 0) { 271 *err = AURA_PGETS_TIMEOUT; 272 return(0); 273 } else if (n < 0) { 274 *err = AURA_PGETS_SELECT_ERR; 275 return(0); 276 } else { 277 /* Data came in; read it. */ 278 if (fgets(buf, len, p) == NULL) { 279 if (feof(p)) { 280 *err = AURA_PGETS_EOF; 281 } else { 282 *err = AURA_PGETS_FGETS_ERR; 283 } 284 return(0); 285 } else { 286 return(1); 287 } 288 } 289 } 290