[444] | 1 | /* $NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $ */ |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (c) 1988, 1993, 2006 |
---|
| 5 | * The Regents of the University of California. All rights reserved. |
---|
| 6 | * |
---|
| 7 | * This code is derived from software written by Ken Arnold and |
---|
| 8 | * published in UNIX Review, Vol. 6, No. 8. |
---|
| 9 | * |
---|
| 10 | * Redistribution and use in source and binary forms, with or without |
---|
| 11 | * modification, are permitted provided that the following conditions |
---|
| 12 | * are met: |
---|
| 13 | * 1. Redistributions of source code must retain the above copyright |
---|
| 14 | * notice, this list of conditions and the following disclaimer. |
---|
| 15 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 16 | * notice, this list of conditions and the following disclaimer in the |
---|
| 17 | * documentation and/or other materials provided with the distribution. |
---|
| 18 | * 4. Neither the name of the University nor the names of its contributors |
---|
| 19 | * may be used to endorse or promote products derived from this software |
---|
| 20 | * without specific prior written permission. |
---|
| 21 | * |
---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
---|
| 23 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
---|
| 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
---|
| 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
---|
| 28 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
| 29 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
---|
| 30 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
---|
| 31 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
---|
| 32 | * SUCH DAMAGE. |
---|
| 33 | */ |
---|
| 34 | |
---|
| 35 | /* |
---|
| 36 | FUNCTION |
---|
| 37 | <<popen>>, <<pclose>>---tie a stream to a command string |
---|
| 38 | |
---|
| 39 | INDEX |
---|
| 40 | popen |
---|
| 41 | INDEX |
---|
| 42 | pclose |
---|
| 43 | |
---|
| 44 | SYNOPSIS |
---|
| 45 | #include <stdio.h> |
---|
| 46 | FILE *popen(const char *<[s]>, const char *<[mode]>); |
---|
| 47 | |
---|
| 48 | int pclose(FILE *<[f]>); |
---|
| 49 | |
---|
| 50 | DESCRIPTION |
---|
| 51 | Use <<popen>> to create a stream to a child process executing a |
---|
| 52 | command string <<*<[s]>>> as processed by <</bin/sh>> on your system. |
---|
| 53 | The argument <[mode]> must start with either `<<r>>', where the stream |
---|
| 54 | reads from the child's <<stdout>>, or `<<w>>', where the stream writes |
---|
| 55 | to the child's <<stdin>>. As an extension, <[mode]> may also contain |
---|
| 56 | `<<e>>' to set the close-on-exec bit of the parent's file descriptor. |
---|
| 57 | The stream created by <<popen>> must be closed by <<pclose>> to avoid |
---|
| 58 | resource leaks. |
---|
| 59 | |
---|
| 60 | Streams created by prior calls to <<popen>> are not visible in |
---|
| 61 | subsequent <<popen>> children, regardless of the close-on-exec bit. |
---|
| 62 | |
---|
| 63 | Use ``<<system(NULL)>>'' to test whether your system has <</bin/sh>> |
---|
| 64 | available. |
---|
| 65 | |
---|
| 66 | RETURNS |
---|
| 67 | <<popen>> returns a file stream opened with the specified <[mode]>, |
---|
| 68 | or <<NULL>> if a child process could not be created. <<pclose>> |
---|
| 69 | returns -1 if the stream was not created by <<popen>> or if the |
---|
| 70 | application used <<wait>> or similar to steal the status; otherwise |
---|
| 71 | it returns the exit status of the child which can be interpreted |
---|
| 72 | in the same manner as a status obtained by <<waitpid>>. |
---|
| 73 | |
---|
| 74 | PORTABILITY |
---|
| 75 | POSIX.2 requires <<popen>> and <<pclose>>, but only specifies a mode |
---|
| 76 | of just <<r>> or <<w>>. Where <<sh>> is found is left unspecified. |
---|
| 77 | |
---|
| 78 | Supporting OS subroutines required: <<_exit>>, <<_execve>>, <<_fork_r>>, |
---|
| 79 | <<_wait_r>>, <<pipe>>, <<fcntl>>, <<sbrk>>. |
---|
| 80 | */ |
---|
| 81 | |
---|
| 82 | #ifndef _NO_POPEN |
---|
| 83 | |
---|
| 84 | #if defined(LIBC_SCCS) && !defined(lint) |
---|
| 85 | #if 0 |
---|
| 86 | static char sccsid[] = "@(#)popen.c 8.1 (Berkeley) 6/4/93"; |
---|
| 87 | #else |
---|
| 88 | static char rcsid[] = "$NetBSD: popen.c,v 1.11 1995/06/16 07:05:33 jtc Exp $"; |
---|
| 89 | #endif |
---|
| 90 | #endif /* LIBC_SCCS and not lint */ |
---|
| 91 | |
---|
| 92 | #include <sys/param.h> |
---|
| 93 | #include <sys/types.h> |
---|
| 94 | #include <sys/wait.h> |
---|
| 95 | |
---|
| 96 | #include <signal.h> |
---|
| 97 | #include <errno.h> |
---|
| 98 | #include <unistd.h> |
---|
| 99 | #include <stdio.h> |
---|
| 100 | #include <stdlib.h> |
---|
| 101 | #include <string.h> |
---|
| 102 | #include <paths.h> |
---|
| 103 | #include <fcntl.h> |
---|
| 104 | |
---|
| 105 | static struct pid { |
---|
| 106 | struct pid *next; |
---|
| 107 | FILE *fp; |
---|
| 108 | pid_t pid; |
---|
| 109 | } *pidlist; |
---|
| 110 | |
---|
| 111 | FILE * |
---|
| 112 | popen (const char *program, |
---|
| 113 | const char *type) |
---|
| 114 | { |
---|
| 115 | struct pid *cur; |
---|
| 116 | FILE *iop; |
---|
| 117 | int pdes[2], pid; |
---|
| 118 | |
---|
| 119 | if ((*type != 'r' && *type != 'w') |
---|
| 120 | || (type[1] |
---|
| 121 | #ifdef HAVE_FCNTL |
---|
| 122 | && (type[2] || (type[1] != 'e')) |
---|
| 123 | #endif |
---|
| 124 | )) { |
---|
| 125 | errno = EINVAL; |
---|
| 126 | return (NULL); |
---|
| 127 | } |
---|
| 128 | |
---|
| 129 | if ((cur = malloc(sizeof(struct pid))) == NULL) |
---|
| 130 | return (NULL); |
---|
| 131 | |
---|
| 132 | if (pipe(pdes) < 0) { |
---|
| 133 | free(cur); |
---|
| 134 | return (NULL); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | switch (pid = vfork()) { |
---|
| 138 | case -1: /* Error. */ |
---|
| 139 | (void)close(pdes[0]); |
---|
| 140 | (void)close(pdes[1]); |
---|
| 141 | free(cur); |
---|
| 142 | return (NULL); |
---|
| 143 | /* NOTREACHED */ |
---|
| 144 | case 0: /* Child. */ |
---|
| 145 | if (*type == 'r') { |
---|
| 146 | if (pdes[1] != STDOUT_FILENO) { |
---|
| 147 | (void)dup2(pdes[1], STDOUT_FILENO); |
---|
| 148 | (void)close(pdes[1]); |
---|
| 149 | } |
---|
| 150 | if (pdes[0] != STDOUT_FILENO) { |
---|
| 151 | (void) close(pdes[0]); |
---|
| 152 | } |
---|
| 153 | } else { |
---|
| 154 | if (pdes[0] != STDIN_FILENO) { |
---|
| 155 | (void)dup2(pdes[0], STDIN_FILENO); |
---|
| 156 | (void)close(pdes[0]); |
---|
| 157 | } |
---|
| 158 | (void)close(pdes[1]); |
---|
| 159 | } |
---|
| 160 | /* Close all fd's created by prior popen. */ |
---|
| 161 | for (cur = pidlist; cur; cur = cur->next) |
---|
| 162 | (void)close (fileno (cur->fp)); |
---|
| 163 | execl(_PATH_BSHELL, "sh", "-c", program, NULL); |
---|
| 164 | _exit(127); |
---|
| 165 | /* NOTREACHED */ |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /* Parent; assume fdopen can't fail. */ |
---|
| 169 | if (*type == 'r') { |
---|
| 170 | iop = fdopen(pdes[0], type); |
---|
| 171 | (void)close(pdes[1]); |
---|
| 172 | } else { |
---|
| 173 | iop = fdopen(pdes[1], type); |
---|
| 174 | (void)close(pdes[0]); |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | #ifdef HAVE_FCNTL |
---|
| 178 | /* Mark pipe cloexec if requested. */ |
---|
| 179 | if (type[1] == 'e') |
---|
| 180 | fcntl (fileno (iop), F_SETFD, |
---|
| 181 | fcntl (fileno (iop), F_GETFD, 0) | FD_CLOEXEC); |
---|
| 182 | #endif /* HAVE_FCNTL */ |
---|
| 183 | |
---|
| 184 | /* Link into list of file descriptors. */ |
---|
| 185 | cur->fp = iop; |
---|
| 186 | cur->pid = pid; |
---|
| 187 | cur->next = pidlist; |
---|
| 188 | pidlist = cur; |
---|
| 189 | |
---|
| 190 | return (iop); |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | /* |
---|
| 194 | * pclose -- |
---|
| 195 | * Pclose returns -1 if stream is not associated with a `popened' command, |
---|
| 196 | * if already `pclosed', or waitpid returns an error. |
---|
| 197 | */ |
---|
| 198 | int |
---|
| 199 | pclose (FILE *iop) |
---|
| 200 | { |
---|
| 201 | register struct pid *cur, *last; |
---|
| 202 | int pstat; |
---|
| 203 | pid_t pid; |
---|
| 204 | |
---|
| 205 | (void)fclose(iop); |
---|
| 206 | |
---|
| 207 | /* Find the appropriate file pointer. */ |
---|
| 208 | for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) |
---|
| 209 | if (cur->fp == iop) |
---|
| 210 | break; |
---|
| 211 | if (cur == NULL) |
---|
| 212 | return (-1); |
---|
| 213 | |
---|
| 214 | do { |
---|
| 215 | pid = waitpid(cur->pid, &pstat, 0); |
---|
| 216 | } while (pid == -1 && errno == EINTR); |
---|
| 217 | |
---|
| 218 | /* Remove the entry from the linked list. */ |
---|
| 219 | if (last == NULL) |
---|
| 220 | pidlist = cur->next; |
---|
| 221 | else |
---|
| 222 | last->next = cur->next; |
---|
| 223 | free(cur); |
---|
| 224 | |
---|
| 225 | return (pid == -1 ? -1 : pstat); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | #endif /* !_NO_POPEN */ |
---|