| 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 |
* Copyright (c) 2010-2015 Piotr Roszatycki <[email protected]> |
| 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 |
* 3. 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 |
#include <config.h> |
| 36 |
|
| 37 |
#ifdef __GNUC__ |
| 38 |
|
| 39 |
#define _BSD_SOURCE |
| 40 |
#define _POSIX_SOURCE |
| 41 |
#define _DEFAULT_SOURCE |
| 42 |
#include <sys/param.h> |
| 43 |
#include <sys/wait.h> |
| 44 |
|
| 45 |
#include <signal.h> |
| 46 |
#include <errno.h> |
| 47 |
#include <unistd.h> |
| 48 |
#include <stdio.h> |
| 49 |
#include <stdlib.h> |
| 50 |
#include <string.h> |
| 51 |
#include <paths.h> |
| 52 |
|
| 53 |
#include "libfakechroot.h" |
| 54 |
|
| 55 |
#define _MUTEX_LOCK(a) |
| 56 |
#define _MUTEX_UNLOCK(a) |
| 57 |
|
| 58 |
static struct pid { |
| 59 |
struct pid *next; |
| 60 |
FILE *fp; |
| 61 |
pid_t pid; |
| 62 |
} *pidlist; |
| 63 |
|
| 64 |
|
| 65 |
#if 0 |
| 66 |
static void *pidlist_lock = NULL; |
| 67 |
#endif |
| 68 |
|
| 69 |
FILE * |
| 70 |
popen(const char *program, const char *type) |
| 71 |
{ |
| 72 |
struct pid * volatile cur; |
| 73 |
FILE *iop; |
| 74 |
int pdes[2]; |
| 75 |
pid_t pid; |
| 76 |
|
| 77 |
debug("popen(\"%s\", \"%s\")", program, type); |
| 78 |
|
| 79 |
if ((*type != 'r' && *type != 'w') || type[1] != '\0') { |
| 80 |
errno = EINVAL; |
| 81 |
return (NULL); |
| 82 |
} |
| 83 |
|
| 84 |
if ((cur = malloc(sizeof(struct pid))) == NULL) |
| 85 |
return (NULL); |
| 86 |
|
| 87 |
if (pipe(pdes) < 0) { |
| 88 |
free(cur); |
| 89 |
return (NULL); |
| 90 |
} |
| 91 |
|
| 92 |
_MUTEX_LOCK(&pidlist_lock); |
| 93 |
switch (pid = vfork()) { |
| 94 |
case -1: /* Error. */ |
| 95 |
_MUTEX_UNLOCK(&pidlist_lock); |
| 96 |
(void)close(pdes[0]); |
| 97 |
(void)close(pdes[1]); |
| 98 |
free(cur); |
| 99 |
return (NULL); |
| 100 |
/* NOTREACHED */ |
| 101 |
case 0: /* Child. */ |
| 102 |
{ |
| 103 |
struct pid *pcur; |
| 104 |
/* |
| 105 |
* because vfork() instead of fork(), must leak FILE *, |
| 106 |
* but luckily we are terminally headed for an execl() |
| 107 |
*/ |
| 108 |
for (pcur = pidlist; pcur; pcur = pcur->next) |
| 109 |
close(fileno(pcur->fp)); |
| 110 |
|
| 111 |
if (*type == 'r') { |
| 112 |
int tpdes1 = pdes[1]; |
| 113 |
|
| 114 |
(void) close(pdes[0]); |
| 115 |
/* |
| 116 |
* We must NOT modify pdes, due to the |
| 117 |
* semantics of vfork. |
| 118 |
*/ |
| 119 |
if (tpdes1 != STDOUT_FILENO) { |
| 120 |
(void)dup2(tpdes1, STDOUT_FILENO); |
| 121 |
(void)close(tpdes1); |
| 122 |
tpdes1 = STDOUT_FILENO; |
| 123 |
} |
| 124 |
} else { |
| 125 |
(void)close(pdes[1]); |
| 126 |
if (pdes[0] != STDIN_FILENO) { |
| 127 |
(void)dup2(pdes[0], STDIN_FILENO); |
| 128 |
(void)close(pdes[0]); |
| 129 |
} |
| 130 |
} |
| 131 |
execl(_PATH_BSHELL, "sh", "-c", program, (char *)NULL); |
| 132 |
_exit(127); |
| 133 |
/* NOTREACHED */ |
| 134 |
} |
| 135 |
break; |
| 136 |
} |
| 137 |
_MUTEX_UNLOCK(&pidlist_lock); |
| 138 |
|
| 139 |
/* Parent; assume fdopen can't fail. */ |
| 140 |
if (*type == 'r') { |
| 141 |
iop = fdopen(pdes[0], type); |
| 142 |
(void)close(pdes[1]); |
| 143 |
} else { |
| 144 |
iop = fdopen(pdes[1], type); |
| 145 |
(void)close(pdes[0]); |
| 146 |
} |
| 147 |
|
| 148 |
/* Link into list of file descriptors. */ |
| 149 |
cur->fp = iop; |
| 150 |
cur->pid = pid; |
| 151 |
_MUTEX_LOCK(&pidlist_lock); |
| 152 |
cur->next = pidlist; |
| 153 |
pidlist = cur; |
| 154 |
_MUTEX_UNLOCK(&pidlist_lock); |
| 155 |
|
| 156 |
return (iop); |
| 157 |
} |
| 158 |
|
| 159 |
/* |
| 160 |
* pclose -- |
| 161 |
* Pclose returns -1 if stream is not associated with a `popened' command, |
| 162 |
* if already `pclosed', or waitpid returns an error. |
| 163 |
*/ |
| 164 |
int |
| 165 |
pclose(FILE *iop) |
| 166 |
{ |
| 167 |
struct pid *cur, *last; |
| 168 |
int pstat; |
| 169 |
pid_t pid; |
| 170 |
|
| 171 |
debug("popen(iop)"); |
| 172 |
|
| 173 |
/* Find the appropriate file pointer. */ |
| 174 |
_MUTEX_LOCK(&pidlist_lock); |
| 175 |
for (last = NULL, cur = pidlist; cur; last = cur, cur = cur->next) |
| 176 |
if (cur->fp == iop) |
| 177 |
break; |
| 178 |
|
| 179 |
if (cur == NULL) { |
| 180 |
_MUTEX_UNLOCK(&pidlist_lock); |
| 181 |
return (-1); |
| 182 |
} |
| 183 |
|
| 184 |
/* Remove the entry from the linked list. */ |
| 185 |
if (last == NULL) |
| 186 |
pidlist = cur->next; |
| 187 |
else |
| 188 |
last->next = cur->next; |
| 189 |
_MUTEX_UNLOCK(&pidlist_lock); |
| 190 |
|
| 191 |
(void)fclose(iop); |
| 192 |
|
| 193 |
do { |
| 194 |
pid = waitpid(cur->pid, &pstat, 0); |
| 195 |
} while (pid == -1 && errno == EINTR); |
| 196 |
|
| 197 |
free(cur); |
| 198 |
|
| 199 |
return (pid == -1 ? -1 : pstat); |
| 200 |
} |
| 201 |
|
| 202 |
#else |
| 203 |
typedef int empty_translation_unit; |
| 204 |
#endif |