1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
|
/*******************************************************************************
ex: set tabstop=4 shiftwidth=4 autoindent:
* (C) Xenadyne Inc. 2002. All Rights Reserved
*
* Permission to use, copy, modify and distribute this software for
* any purpose and without fee is hereby granted, provided that the
* above copyright notice appears in all copies. Also note the
* University of California copyright below.
*
* XENADYNE INC DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS.
* IN NO EVENT SHALL XENADYNE BE LIABLE FOR ANY SPECIAL, INDIRECT OR
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM THE
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
* File: nft_popen.c
*
* Description: A thread-safe replacement for popen()/pclose().
*
* This is a thread-safe variant of popen that does unbuffered IO, to
* avoid running afoul of Solaris's inability to fdopen when fd > 255.
*
*******************************************************************************
*/
/*
* Copyright (c) 1988, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software written by Ken Arnold and
* published in UNIX Review, Vol. 6, No. 8.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "common.h"
#include "spine.h"
/* An instance of this struct is created for each popen() fd. */
static struct pid
{
struct pid *next;
int fd;
pid_t pid;
} * PidList;
/* Serialize access to PidList. */
static pthread_mutex_t ListMutex = PTHREAD_MUTEX_INITIALIZER;
static void close_cleanup(void *);
/*! ------------------------------------------------------------------------------
*
* nft_popen
*
* The nft_popen() function forks a command in a child process, and returns
* a pipe that is connected to the child's standard input and output. It is
* like the standard popen() call, except that it does not dfopen() the pipe
* file descriptor in order to return a stdio FILE *. This is useful if you
* wish to use select()- or poll()-driven IO.
*
* The mode argument is defined as in standard popen().
*
* On success, returns a file descriptor, or -1 on error.
* On failure, returns -1, with errno set to one of:
* EINVAL The mode argument is incorrect.
* EMFILE pipe() failed.
* ENFILE pipe() failed.
* ENOMEM malloc() failed.
* EAGAIN fork() failed.
*
*------------------------------------------------------------------------------
*/
int nft_popen(const char * command, const char * type) {
struct pid *cur;
struct pid *p;
int pdes[2];
int fd, pid, twoway;
char *argv[4];
int cancel_state;
extern char **environ;
int retry_count = 0;
/* On platforms where pipe() is bidirectional,
* "r+" gives two-way communication.
*/
if (strchr(type, '+')) {
twoway = 1;
type = "r+";
}else {
twoway = 0;
if ((*type != 'r' && *type != 'w') || type[1]) {
errno = EINVAL;
return -1;
}
}
if (pipe(pdes) < 0)
return -1;
/* Disable thread cancellation from this point forward. */
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cancel_state);
if ((cur = malloc(sizeof(struct pid))) == NULL) {
(void)close(pdes[0]);
(void)close(pdes[1]);
pthread_setcancelstate(cancel_state, NULL);
return -1;
}
argv[0] = "sh";
argv[1] = "-c";
argv[2] = (char *)command;
argv[3] = NULL;
/* Lock the list mutex prior to forking, to ensure that
* the child process sees PidList in a consistent list state.
*/
pthread_mutex_lock(&ListMutex);
/* Fork. */
retry:
switch (pid = vfork()) {
case -1: /* Error. */
switch (errno) {
case EAGAIN:
if (retry_count < 3) {
retry_count++;
#ifndef SOLAR_THREAD
/* take a moment */
usleep(50000);
#endif
goto retry;
}else{
SPINE_LOG(("ERROR: SCRIPT: Cound not fork. Out of Resources nft_popen.c"));
}
case ENOMEM:
if (retry_count < 3) {
retry_count++;
#ifndef SOLAR_THREAD
/* take a moment */
usleep(50000);
#endif
goto retry;
}else{
SPINE_LOG(("ERROR: SCRIPT Cound not fork. Out of Memory nft_popen.c"));
}
default:
SPINE_LOG(("ERROR: SCRIPT Cound not fork. Unknown Reason nft_popen.c"));
}
(void)close(pdes[0]);
(void)close(pdes[1]);
pthread_mutex_unlock(&ListMutex);
pthread_setcancelstate(cancel_state, NULL);
return -1;
/* NOTREACHED */
case 0: /* Child. */
if (*type == 'r') {
/* The dup2() to STDIN_FILENO is repeated to avoid
* writing to pdes[1], which might corrupt the
* parent's copy. This isn't good enough in
* general, since the _exit() is no return, so
* the compiler is free to corrupt all the local
* variables.
*/
(void)close(pdes[0]);
if (pdes[1] != STDOUT_FILENO) {
(void)dup2(pdes[1], STDOUT_FILENO);
(void)close(pdes[1]);
if (twoway)
(void)dup2(STDOUT_FILENO, STDIN_FILENO);
}else if (twoway && (pdes[1] != STDIN_FILENO))
(void)dup2(pdes[1], STDIN_FILENO);
}else {
if (pdes[0] != STDIN_FILENO) {
(void)dup2(pdes[0], STDIN_FILENO);
(void)close(pdes[0]);
}
(void)close(pdes[1]);
}
/* Close all the other pipes in the child process.
* Posix.2 requires this, tho I don't know why.
*/
for (p = PidList; p; p = p->next)
(void)close(p->fd);
/* Execute the command. */
#if defined(__CYGWIN__)
if (set.cygwinshloc == 0) {
execve("sh.exe", argv, environ);
}else{
execve("/bin/sh", argv, environ);
}
#else
execve("/bin/sh", argv, environ);
#endif
_exit(127);
/* NOTREACHED */
}
/* Parent. */
if (*type == 'r') {
fd = pdes[0];
(void)close(pdes[1]);
}else {
fd = pdes[1];
(void)close(pdes[0]);
}
/* Link into list of file descriptors. */
cur->fd = fd;
cur->pid = pid;
cur->next = PidList;
PidList = cur;
/* Unlock the mutex, and restore caller's cancellation state. */
pthread_mutex_unlock(&ListMutex);
pthread_setcancelstate(cancel_state, NULL);
return fd;
}
/*! ------------------------------------------------------------------------------
*
* nft_pchild
*
* Get the pid of the child process for an fd created by ntf_popen().
*
* On success, the pid of the child process is returned.
* On failure, nft_pchild() returns -1, with errno set to:
*
* EBADF The fd is not an active nft_popen() file descriptor.
*
*------------------------------------------------------------------------------
*/
int nft_pchild(int fd) {
struct pid *cur;
pid_t pid = 0;
/* Find the appropriate file descriptor. */
pthread_mutex_lock(&ListMutex);
for (cur = PidList; cur; cur = cur->next)
if (cur->fd == fd) {
pid = cur->pid;
break;
}
pthread_mutex_unlock(&ListMutex);
if (cur == NULL) {
errno = EBADF;
return -1;
}
return pid;
}
/*! ------------------------------------------------------------------------------
*
* nft_pclose
*
* Close the pipe and wait for the status of the child process.
*
* On success, the exit status of the child process is returned.
* On failure, nft_pclose() returns -1, with errno set to:
*
* EBADF The fd is not an active popen() file descriptor.
* ECHILD The waitpid() call failed.
*
* This call is cancellable.
*
*------------------------------------------------------------------------------
*/
int
nft_pclose(int fd)
{
struct pid *cur;
int pstat;
pid_t pid;
/* Find the appropriate file descriptor. */
pthread_mutex_lock(&ListMutex);
for (cur = PidList; cur; cur = cur->next)
if (cur->fd == fd) break;
pthread_mutex_unlock(&ListMutex);
if (cur == NULL) {
errno = EBADF;
return -1;
}
/* The close and waitpid calls below are cancellation points.
* We want to ensure that the fd is closed and the PidList
* entry freed despite cancellation, so push a cleanup handler.
*/
pthread_cleanup_push(close_cleanup, cur);
/* end the process nicely and then forcefully */
(void)close(fd);
cur->fd = -1; /* Prevent the fd being closed twice. */
do { pid = waitpid(cur->pid, &pstat, 0);
} while (pid == -1 && errno == EINTR);
pthread_cleanup_pop(1); /* Execute the cleanup handler. */
return (pid == -1 ? -1 : pstat);
}
/*! ------------------------------------------------------------------------------
* close_cleanup - close the pipe and free the pidlist entry.
*------------------------------------------------------------------------------
*/
static void
close_cleanup(void * arg)
{
struct pid * cur = arg;
struct pid * prev;
/* Close the pipe fd if necessary. */
if (cur->fd >= 0) {
(void)close(cur->fd);
}
/* Remove the entry from the linked list. */
pthread_mutex_lock(&ListMutex);
if (PidList == cur) {
PidList = cur->next;
}else{
for (prev = PidList; prev; prev = prev->next)
if (prev->next == cur) {
prev->next = cur->next;
break;
}
assert(prev != NULL); /* Search should not fail */
}
pthread_mutex_unlock(&ListMutex);
free(cur);
}
|