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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
|
/* pr.c -- Process creation and tracking support
* Created: Sun Jan 7 13:34:08 1996 by r.faith@ieee.org
* Revised: Wed Mar 19 13:04:19 1997 by faith@cs.unc.edu
* Copyright 1996 Rickard E. Faith (r.faith@ieee.org)
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of the GNU Library General Public License as published
* by the Free Software Foundation; either version 2 of the License, or (at
* your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: pr.c,v 1.9 1997/03/19 21:10:23 faith Exp $
*
* \section{Process Management Routines}
*
* \intro The process management routines are designed to facilitate the
* creation and management of child processes, coprocesses, and associated
* pipelines and I/O. Some support for daemons and socket connections is
* also provided.
*
*/
#include "maaP.h"
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#if HAVE_SYS_PARAM_H
# include <sys/param.h>
#endif
#if HAVE_SYS_WAIT_H
# include <sys/wait.h>
#endif
#ifndef WEXITSTATUS
# define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
#endif
#ifndef WIFEXITED
# define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
#endif
typedef struct _pr_Obj {
int pid;
} *_pr_Obj;
static _pr_Obj _pr_objects = NULL;
#if 0
static void _pr_check( pr_Object object, const char *function )
{
Obj o = (Obj)object;
if (!o) err_internal( function, "object is null\n" );
#if MAA_MAGIC
if (o->magic != PR_MAGIC)
err_internal( function,
"Magic match failed: 0x%08x (should be 0x%08x)\n",
o->magic,
PR_MAGIC );
#endif
}
#endif
/* The idea for the max_fd call is from W. Richard Stevens; Advanced
Programming in the UNIX Environment (Addison-Wesley Publishing Co.,
1992); page 43. The implementation here, however, is different from
that provided by Stevens for his open_max routine. */
static int max_fd( void )
{
static int maxFd = 0;
if (maxFd) return maxFd;
#if HAVE_SYSCONF
if ((maxFd = sysconf( _SC_OPEN_MAX )) > 0) return maxFd;
#endif
#ifdef NOFILE
return maxFd = NOFILE; /* Usually in sys/params.h */
#else
# ifdef _NFILE
return maxFd = _NFILE;
# else
return maxFd = 256; /* best guess */
# endif
#endif
}
static void _pr_init( void )
{
if (!_pr_objects)
_pr_objects = xcalloc( max_fd(), sizeof( struct _pr_Obj ) );
}
void _pr_shutdown( void )
{
int i;
if (_pr_objects) {
for (i = 0; i < max_fd(); i++) {
/* FIXME: blah */;
if (_pr_objects[i].pid) {
kill( _pr_objects[i].pid, SIGKILL ); /* FIXME! be gentler. */
pr_wait( _pr_objects[i].pid );
_pr_objects[i].pid = 0;
}
}
xfree( _pr_objects );
_pr_objects = NULL;
}
}
int pr_open( const char *command, int flags, int *infd, int *outfd, int *errfd)
{
int pid;
int fdin[2];
int fdout[2];
int fderr[2];
arg_List list;
int argc;
char **argv;
int null;
_pr_init();
if (flags & ~(PR_USE_STDIN | PR_USE_STDOUT | PR_USE_STDERR
| PR_CREATE_STDIN | PR_CREATE_STDOUT | PR_CREATE_STDERR
| PR_STDERR_TO_STDOUT))
err_internal( __FUNCTION__, "Illegal flags: 0x%08x\n", flags );
if ((flags & PR_USE_STDIN) && (flags & PR_CREATE_STDIN))
err_internal( __FUNCTION__, "Cannot both use and create stdin\n" );
if ((flags & PR_USE_STDOUT) && (flags & PR_CREATE_STDOUT))
err_internal( __FUNCTION__, "Cannot both use and create stdout\n" );
if ((flags & PR_USE_STDERR) && (flags & PR_CREATE_STDERR))
err_internal( __FUNCTION__, "Cannot both use and create stderr\n" );
if ((flags & PR_STDERR_TO_STDOUT)
&& ((flags & PR_USE_STDERR) || (flags & PR_CREATE_STDERR)))
err_internal( __FUNCTION__,
"Cannot use/create stderr when duping to stdout\n" );
list = arg_argify( command, 0 );
arg_get_vector( list, &argc, &argv );
PRINTF(MAA_PR,("Execing %s with \"%s\"\n", argv[0], command ));
if ((flags & PR_CREATE_STDIN) && pipe( fdin ) < 0)
err_fatal_errno( __FUNCTION__, "Cannot create pipe for stdin\n" );
if ((flags & PR_CREATE_STDOUT) && pipe( fdout ) < 0)
err_fatal_errno( __FUNCTION__, "Cannot create pipe for stdout\n" );
if ((flags & PR_CREATE_STDERR) && pipe( fderr ) < 0)
err_fatal_errno( __FUNCTION__, "Cannot create pipe for stderr\n" );
if ((pid = fork()) < 0)
err_fatal_errno( __FUNCTION__, "Cannot fork\n" );
if (pid == 0) { /* child */
int i;
#define CHILD(CREATE,USE,fds,writefd,readfd,fd,FILENO,flag) \
if (flags & CREATE) { \
close( fds[writefd] ); \
dup2( fds[readfd], FILENO ); \
close( fds[readfd] ); \
} else if (flags & USE) { \
if (fd && *fd) { \
dup2( *fd, FILENO ); \
close( *fd ); \
} else { \
if ((null = open( "/dev/null", flag )) >= 0) { \
dup2( null, FILENO ); \
close( null ); \
} \
} \
}
CHILD( PR_CREATE_STDIN, PR_USE_STDIN, fdin, 1, 0, infd,
STDIN_FILENO, O_RDONLY );
CHILD( PR_CREATE_STDOUT, PR_USE_STDOUT, fdout, 0, 1, outfd,
STDOUT_FILENO, O_WRONLY );
CHILD( PR_CREATE_STDERR, PR_USE_STDERR, fderr, 0, 1, errfd,
STDERR_FILENO, O_WRONLY );
#undef CHILD
if (flags & PR_STDERR_TO_STDOUT)
dup2( STDOUT_FILENO, STDERR_FILENO );
for (i = 0; i < max_fd(); i++)
if (_pr_objects[i].pid > 0) close( i );
execvp( argv[0], argv );
_exit(127);
}
/* parent */
#define PARENT(CREATE,USE,fds,readfd,writefd,fd,flag,name) \
if (flags & CREATE) { \
close( fds[ readfd ] ); \
*fd = fds[ writefd ]; \
_pr_objects[ *fd ].pid = pid; \
PRINTF(MAA_PR,(name " = %d; ",*fd)); \
} else if (flags & USE) { \
if (fd && *fd) { \
PRINTF(MAA_PR,(name " = %d*; ",*fd)); \
_pr_objects[ *fd ].pid =0; \
close( *fd ); \
} \
}
PARENT( PR_CREATE_STDIN, PR_USE_STDIN, fdin, 0, 1,
infd, "w", "stdin" );
PARENT( PR_CREATE_STDOUT, PR_USE_STDOUT, fdout, 1, 0,
outfd, "r", "stdout" );
PARENT( PR_CREATE_STDERR, PR_USE_STDERR, fderr, 1, 0,
errfd, "r", "stderr" );
#undef PARENT
PRINTF(MAA_PR,("child pid = %d\n",pid));
arg_destroy( list );
return pid;
}
int pr_wait( int pid )
{
int exitStatus = 0;
int status;
PRINTF(MAA_PR,("waiting on pid %d\n",pid));
while (waitpid( pid, &status, 0 ) < 0) {
if (errno != EINTR) {
if (errno == ECHILD) return 0; /* We've already waited */
/* This is really bad... */
PRINTF(MAA_PR,("waitpid() < 0, errno = %d\n", errno ));
perror( __FUNCTION__ );
return -1;
}
}
if (WIFEXITED( status ))
exitStatus |= WEXITSTATUS( status );
/* SIGPIPE is ok here, since tar may
shutdown early. Anything else is a
problem. */
if (WIFSIGNALED( status ) && WTERMSIG( status ) != SIGPIPE)
exitStatus |= 128 + WTERMSIG( status ); /* like bash :-) */
PRINTF(MAA_PR,("Child %d exited with status 0x%04x\n",pid,exitStatus));
return exitStatus;
}
int pr_close_nowait( int fd )
{
int pid;
if (!_pr_objects)
err_internal( __FUNCTION__, "No previous call to pr_open()\n" );
if (!(pid = _pr_objects[ fd ].pid))
err_internal( __FUNCTION__, "File (%d) not created by pr_open()\n", fd );
_pr_objects[ fd ].pid = 0;
close( fd );
return pid;
}
int pr_close( int fd )
{
int pid = pr_close_nowait( fd );
return pr_wait( pid );
}
int pr_readwrite( int in, int out,
const char *inBuffer, int inLen,
char *outBuffer, int outMaxLen )
{
long flags;
const char *inPt = inBuffer;
char *outPt = outBuffer;
int outLen = 0;
fd_set rfds, wfds, efds;
struct timeval tv;
int n;
int count;
int retval;
int status;
if ((flags = fcntl( in, F_GETFL )) < 0)
err_fatal_errno( __FUNCTION__, "Can't get flags for output stream\n" );
#ifdef O_NONBLOCK
flags |= O_NONBLOCK;
#else
flags |= FNDELAY;
#endif
fcntl( in, F_SETFL, flags );
if ((flags = fcntl( out, F_GETFL )) < 0)
err_fatal_errno( __FUNCTION__, "Can't get flags for input stream\n" );
#ifdef O_NONBLOCK
flags |= O_NONBLOCK;
#else
flags |= FNDELAY;
#endif
fcntl( out, F_SETFL, flags );
n = max( in, out ) + 1;
for (;;) {
tv.tv_sec = 5;
tv.tv_usec = 0;
FD_ZERO( &rfds );
FD_ZERO( &wfds );
FD_ZERO( &efds );
FD_SET( out, &rfds );
FD_SET( out, &efds );
if (inLen) {
FD_SET( in, &wfds );
FD_SET( in, &efds );
}
switch ((retval = select( n, &rfds, &wfds, &efds, &tv )))
{
case -1: err_fatal_errno( __FUNCTION__, "Filter failed\n" ); break;
/* case 0: err_fatal( __FUNCTION__, "Filter hung\n" ); break; */
default:
if (dbg_test(MAA_PR)) {
printf( "select(2) returns %d,"
" inLen = %d, outLen = %d, outMaxLen = %d\n",
retval, inLen, outLen, outMaxLen );
if (FD_ISSET( in, &rfds )) printf( " in/read\n" );
if (FD_ISSET( out, &rfds )) printf( " out/read\n" );
if (FD_ISSET( in, &wfds )) printf( " in/write\n" );
if (FD_ISSET( out, &wfds )) printf( " out/write\n" );
if (FD_ISSET( in, &efds )) printf( " in/error\n" );
if (FD_ISSET( out, &efds )) printf( " out/error\n" );
}
if (inLen) {
if ((count = write( in, inPt, inLen )) <= 0) {
if (errno != EAGAIN)
err_fatal_errno( __FUNCTION__, "Error writing to filter\n" );
} else {
PRINTF(MAA_PR,(" wrote %d\n",count));
inLen -= count;
inPt += count;
if (!inLen) {
pr_close_nowait( in );
n = out + 1;
}
}
}
if ((count = read( out, outPt, outMaxLen )) <= 0) {
if (!count) {
if (inLen)
err_fatal( __FUNCTION__,
"End of output, but input not flushed\n" );
if ((status = pr_close( out )))
err_warning( __FUNCTION__,
"Filter had non-zero exit status: 0x%x\n",
status );
return outLen;
} else if (errno != EAGAIN)
err_fatal_errno( __FUNCTION__, "Error reading from filter\n" );
} else {
PRINTF(MAA_PR,(" read %d\n",count));
outLen += count;
outPt += count;
if ((outMaxLen -= count) < 0)
err_fatal( __FUNCTION__, "Output buffer overflow\n" );
}
break;
}
}
}
int pr_filter( const char *command,
const char *inBuffer, int inLen,
char *outBuffer, int outMaxLen )
{
int pid;
int in, out;
pid = pr_open( command, PR_CREATE_STDIN | PR_CREATE_STDOUT,
&in, &out, NULL );
return pr_readwrite( in, out, inBuffer, inLen, outBuffer, outMaxLen );
}
int pr_spawn( const char *command )
{
arg_List list;
int argc;
char **argv;
int pid;
int status;
int exitStatus = 0;
_pr_init();
list = arg_argify( command, 0 );
arg_get_vector( list, &argc, &argv );
PRINTF(MAA_PR,("Execing %s with \"%s\"\n", argv[0], command ));
if ((pid = fork()) < 0)
err_fatal_errno( __FUNCTION__, "Cannot fork\n" );
if (pid == 0) { /* child */
execvp( argv[0], argv );
_exit(127);
}
/* parent */
PRINTF(MAA_PR,("child pid = %d\n",pid));
arg_destroy( list );
PRINTF(MAA_PR,("waiting on pid %d\n",pid));
while (waitpid( pid, &status, 0 ) < 0) {
if (errno != EINTR) {
if (errno == ECHILD) return 0; /* We've already waited */
/* This is really bad... */
PRINTF(MAA_PR,("waitpid() < 0, errno = %d\n", errno ));
perror( __FUNCTION__ );
return -1;
}
}
if (WIFEXITED( status ))
exitStatus |= WEXITSTATUS( status );
/* SIGPIPE is ok here, since tar may
shutdown early. Anything else is a
problem. */
if (WIFSIGNALED( status ) && WTERMSIG( status ) != SIGPIPE)
exitStatus |= 128 + WTERMSIG( status ); /* like bash :-) */
PRINTF(MAA_PR,("Child %d exited with status 0x%04x\n",pid,exitStatus));
return exitStatus;
}
|