2003-06-10 23:46:31 +04:00
|
|
|
/*=========================================================================
|
|
|
|
|
2003-12-16 00:30:46 +03:00
|
|
|
Program: KWSys - Kitware System Library
|
|
|
|
Module: $RCSfile$
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2003-12-16 00:30:46 +03:00
|
|
|
Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2003-12-16 00:30:46 +03:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
|
|
PURPOSE. See the above copyright notices for more information.
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
=========================================================================*/
|
2003-07-10 22:32:30 +04:00
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
#include KWSYS_HEADER(Process.h)
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2005-04-14 00:46:09 +04:00
|
|
|
/* Work-around CMake dependency scanning limitation. This must
|
|
|
|
duplicate the above list of headers. */
|
|
|
|
#if 0
|
|
|
|
# include "Process.h.in"
|
|
|
|
#endif
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*
|
|
|
|
|
|
|
|
Implementation for UNIX
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
On UNIX, a child process is forked to exec the program. Three output
|
|
|
|
pipes are read by the parent process using a select call to block
|
|
|
|
until data are ready. Two of the pipes are stdout and stderr for the
|
|
|
|
child. The third is a special pipe populated by a signal handler to
|
|
|
|
indicate that a child has terminated. This is used in conjunction
|
|
|
|
with the timeout on the select call to implement a timeout for program
|
|
|
|
even when it closes stdout and stderr and at the same time avoiding
|
|
|
|
races.
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
*/
|
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/*
|
|
|
|
|
|
|
|
TODO:
|
|
|
|
|
|
|
|
We cannot create the pipeline of processes in suspended states. How
|
|
|
|
do we cleanup processes already started when one fails to load? Right
|
|
|
|
now we are just killing them, which is probably not the right thing to
|
|
|
|
do.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2006-10-05 01:27:17 +04:00
|
|
|
#include <stddef.h> /* ptrdiff_t */
|
2003-06-10 23:46:31 +04:00
|
|
|
#include <stdio.h> /* snprintf */
|
|
|
|
#include <stdlib.h> /* malloc, free */
|
|
|
|
#include <string.h> /* strdup, strerror, memset */
|
|
|
|
#include <sys/time.h> /* struct timeval */
|
|
|
|
#include <sys/types.h> /* pid_t, fd_set */
|
|
|
|
#include <sys/wait.h> /* waitpid */
|
2003-12-13 22:19:45 +03:00
|
|
|
#include <sys/stat.h> /* open mode */
|
2003-06-10 23:46:31 +04:00
|
|
|
#include <unistd.h> /* pipe, close, fork, execvp, select, _exit */
|
|
|
|
#include <fcntl.h> /* fcntl */
|
|
|
|
#include <errno.h> /* errno */
|
|
|
|
#include <time.h> /* gettimeofday */
|
|
|
|
#include <signal.h> /* sigaction */
|
2004-07-03 00:39:29 +04:00
|
|
|
#include <dirent.h> /* DIR, dirent */
|
2006-06-15 22:40:42 +04:00
|
|
|
#include <ctype.h> /* isspace */
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2008-09-22 22:05:13 +04:00
|
|
|
#ifdef __HAIKU__
|
|
|
|
#undef __BEOS__
|
|
|
|
#endif
|
|
|
|
|
2006-10-05 01:27:17 +04:00
|
|
|
#if defined(KWSYS_C_HAS_PTRDIFF_T) && KWSYS_C_HAS_PTRDIFF_T
|
|
|
|
typedef ptrdiff_t kwsysProcess_ptrdiff_t;
|
|
|
|
#else
|
|
|
|
typedef int kwsysProcess_ptrdiff_t;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(KWSYS_C_HAS_SSIZE_T) && KWSYS_C_HAS_SSIZE_T
|
|
|
|
typedef ssize_t kwsysProcess_ssize_t;
|
|
|
|
#else
|
|
|
|
typedef int kwsysProcess_ssize_t;
|
|
|
|
#endif
|
|
|
|
|
2008-09-16 01:53:28 +04:00
|
|
|
#if defined(__BEOS__) && !defined(__ZETA__)
|
2006-12-04 22:42:47 +03:00
|
|
|
/* BeOS 5 doesn't have usleep(), but it has snooze(), which is identical. */
|
|
|
|
# include <be/kernel/OS.h>
|
|
|
|
static inline void kwsysProcess_usleep(unsigned int msec)
|
|
|
|
{
|
|
|
|
snooze(msec);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
# define kwsysProcess_usleep usleep
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* BeOS's select() works like WinSock: it's for networking only, and
|
|
|
|
* doesn't work with Unix file handles...socket and file handles are
|
|
|
|
* different namespaces (the same descriptor means different things in
|
|
|
|
* each context!)
|
|
|
|
*
|
|
|
|
* So on Unix-like systems where select() is flakey, we'll set the
|
|
|
|
* pipes' file handles to be non-blocking and just poll them directly
|
|
|
|
* without select().
|
|
|
|
*/
|
|
|
|
#if !defined(__BEOS__)
|
|
|
|
# define KWSYSPE_USE_SELECT 1
|
|
|
|
#endif
|
|
|
|
|
2007-06-27 19:39:16 +04:00
|
|
|
/* Some platforms do not have siginfo on their signal handlers. */
|
|
|
|
#if defined(SA_SIGINFO) && !defined(__BEOS__)
|
2006-12-04 22:42:47 +03:00
|
|
|
# define KWSYSPE_USE_SIGINFO 1
|
|
|
|
#endif
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The number of pipes for the child's output. The standard stdout
|
2003-12-03 17:20:05 +03:00
|
|
|
and stderr pipes are the first two. One more pipe is used to
|
|
|
|
detect when the child process has terminated. The third pipe is
|
|
|
|
not given to the child process, so it cannot close it until it
|
|
|
|
terminates. */
|
2003-06-10 23:46:31 +04:00
|
|
|
#define KWSYSPE_PIPE_COUNT 3
|
|
|
|
#define KWSYSPE_PIPE_STDOUT 0
|
|
|
|
#define KWSYSPE_PIPE_STDERR 1
|
2006-05-23 19:48:15 +04:00
|
|
|
#define KWSYSPE_PIPE_SIGNAL 2
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
/* The maximum amount to read from a pipe at a time. */
|
|
|
|
#define KWSYSPE_PIPE_BUFFER_SIZE 1024
|
|
|
|
|
2005-05-11 00:36:49 +04:00
|
|
|
/* Keep track of times using a signed representation. Switch to the
|
|
|
|
native (possibly unsigned) representation only when calling native
|
|
|
|
functions. */
|
|
|
|
typedef struct timeval kwsysProcessTimeNative;
|
|
|
|
typedef struct kwsysProcessTime_s kwsysProcessTime;
|
|
|
|
struct kwsysProcessTime_s
|
|
|
|
{
|
|
|
|
long tv_sec;
|
|
|
|
long tv_usec;
|
|
|
|
};
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
typedef struct kwsysProcessCreateInformation_s
|
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
int StdIn;
|
|
|
|
int StdOut;
|
|
|
|
int StdErr;
|
|
|
|
int ErrorPipe[2];
|
2003-12-03 17:20:05 +03:00
|
|
|
} kwsysProcessCreateInformation;
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-03 17:20:05 +03:00
|
|
|
static int kwsysProcessInitialize(kwsysProcess* cp);
|
2003-06-10 23:46:31 +04:00
|
|
|
static void kwsysProcessCleanup(kwsysProcess* cp, int error);
|
|
|
|
static void kwsysProcessCleanupDescriptor(int* pfd);
|
2008-01-11 02:32:38 +03:00
|
|
|
static void kwsysProcessClosePipes(kwsysProcess* cp);
|
2006-12-04 22:42:47 +03:00
|
|
|
static int kwsysProcessSetNonBlocking(int fd);
|
2005-02-28 01:36:00 +03:00
|
|
|
static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
|
2003-12-03 17:20:05 +03:00
|
|
|
kwsysProcessCreateInformation* si, int* readEnd);
|
2006-05-23 19:48:15 +04:00
|
|
|
static void kwsysProcessDestroy(kwsysProcess* cp);
|
2003-12-13 22:13:54 +03:00
|
|
|
static int kwsysProcessSetupOutputPipeFile(int* p, const char* name);
|
2006-10-03 17:10:03 +04:00
|
|
|
static int kwsysProcessSetupOutputPipeNative(int* p, int des[2]);
|
2003-06-10 23:46:31 +04:00
|
|
|
static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
|
|
|
|
kwsysProcessTime* timeoutTime);
|
|
|
|
static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
|
2005-06-17 21:57:27 +04:00
|
|
|
double* userTimeout,
|
2005-05-11 00:36:49 +04:00
|
|
|
kwsysProcessTimeNative* timeoutLength);
|
2004-01-22 22:44:44 +03:00
|
|
|
static kwsysProcessTime kwsysProcessTimeGetCurrent(void);
|
2003-06-10 23:46:31 +04:00
|
|
|
static double kwsysProcessTimeToDouble(kwsysProcessTime t);
|
|
|
|
static kwsysProcessTime kwsysProcessTimeFromDouble(double d);
|
|
|
|
static int kwsysProcessTimeLess(kwsysProcessTime in1, kwsysProcessTime in2);
|
|
|
|
static kwsysProcessTime kwsysProcessTimeAdd(kwsysProcessTime in1, kwsysProcessTime in2);
|
|
|
|
static kwsysProcessTime kwsysProcessTimeSubtract(kwsysProcessTime in1, kwsysProcessTime in2);
|
2003-12-31 00:23:16 +03:00
|
|
|
static void kwsysProcessSetExitException(kwsysProcess* cp, int sig);
|
2003-12-03 17:20:05 +03:00
|
|
|
static void kwsysProcessChildErrorExit(int errorPipe);
|
2004-01-22 22:44:44 +03:00
|
|
|
static void kwsysProcessRestoreDefaultSignalHandlers(void);
|
2004-07-08 01:27:50 +04:00
|
|
|
static pid_t kwsysProcessFork(kwsysProcess* cp,
|
|
|
|
kwsysProcessCreateInformation* si);
|
2004-07-03 00:39:29 +04:00
|
|
|
static void kwsysProcessKill(pid_t process_id);
|
2006-05-23 19:48:15 +04:00
|
|
|
static int kwsysProcessesAdd(kwsysProcess* cp);
|
|
|
|
static void kwsysProcessesRemove(kwsysProcess* cp);
|
2006-12-04 22:42:47 +03:00
|
|
|
#if KWSYSPE_USE_SIGINFO
|
2006-05-23 19:48:15 +04:00
|
|
|
static void kwsysProcessesSignalHandler(int signum, siginfo_t* info,
|
|
|
|
void* ucontext);
|
2006-12-04 22:42:47 +03:00
|
|
|
#else
|
|
|
|
static void kwsysProcessesSignalHandler(int signum);
|
|
|
|
#endif
|
2006-06-15 22:40:42 +04:00
|
|
|
static char** kwsysProcessParseVerbatimCommand(const char* command);
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Structure containing data used to implement the child's execution. */
|
|
|
|
struct kwsysProcess_s
|
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
/* The command lines to execute. */
|
|
|
|
char*** Commands;
|
|
|
|
int NumberOfCommands;
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Descriptors for the read ends of the child's output pipes and
|
|
|
|
the signal pipe. */
|
2003-06-10 23:46:31 +04:00
|
|
|
int PipeReadEnds[KWSYSPE_PIPE_COUNT];
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Write descriptor for child termination signal pipe. */
|
|
|
|
int SignalPipe;
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Buffer for pipe data. */
|
|
|
|
char PipeBuffer[KWSYSPE_PIPE_BUFFER_SIZE];
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Process IDs returned by the calls to fork. */
|
|
|
|
pid_t* ForkPIDs;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Flag for whether the children were terminated by a faild select. */
|
|
|
|
int SelectError;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The timeout length. */
|
2003-06-19 01:06:21 +04:00
|
|
|
double Timeout;
|
2003-07-07 16:36:40 +04:00
|
|
|
|
|
|
|
/* The working directory for the process. */
|
|
|
|
char* WorkingDirectory;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2004-07-08 01:27:50 +04:00
|
|
|
/* Whether to create the child as a detached process. */
|
|
|
|
int OptionDetach;
|
|
|
|
|
|
|
|
/* Whether the child was created as a detached process. */
|
|
|
|
int Detached;
|
|
|
|
|
2006-06-15 22:40:42 +04:00
|
|
|
/* Whether to treat command lines as verbatim. */
|
|
|
|
int Verbatim;
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Time at which the child started. Negative for no timeout. */
|
|
|
|
kwsysProcessTime StartTime;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Time at which the child will timeout. Negative for no timeout. */
|
|
|
|
kwsysProcessTime TimeoutTime;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Flag for whether the timeout expired. */
|
|
|
|
int TimeoutExpired;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The number of pipes left open during execution. */
|
|
|
|
int PipesLeft;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2006-12-04 22:42:47 +03:00
|
|
|
#if KWSYSPE_USE_SELECT
|
2003-06-10 23:46:31 +04:00
|
|
|
/* File descriptor set for call to select. */
|
|
|
|
fd_set PipeSet;
|
2006-12-04 22:42:47 +03:00
|
|
|
#endif
|
2003-06-10 23:46:31 +04:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* The number of children still executing. */
|
|
|
|
int CommandsLeft;
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The current status of the child process. */
|
|
|
|
int State;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/* The exceptional behavior that terminated the child process, if
|
|
|
|
* any. */
|
|
|
|
int ExitException;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/* The exit code of the child process. */
|
2003-06-10 23:46:31 +04:00
|
|
|
int ExitCode;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/* The exit value of the child process, if any. */
|
|
|
|
int ExitValue;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Whether the process was killed. */
|
|
|
|
int Killed;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Buffer for error message in case of failure. */
|
|
|
|
char ErrorMessage[KWSYSPE_PIPE_BUFFER_SIZE+1];
|
2003-12-03 17:20:05 +03:00
|
|
|
|
2003-12-31 00:23:16 +03:00
|
|
|
/* Description for the ExitException. */
|
|
|
|
char ExitExceptionString[KWSYSPE_PIPE_BUFFER_SIZE+1];
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* The exit codes of each child process in the pipeline. */
|
|
|
|
int* CommandExitCodes;
|
2003-12-13 22:13:54 +03:00
|
|
|
|
|
|
|
/* Name of files to which stdin and stdout pipes are attached. */
|
|
|
|
char* PipeFileSTDIN;
|
|
|
|
char* PipeFileSTDOUT;
|
|
|
|
char* PipeFileSTDERR;
|
|
|
|
|
2003-12-17 01:20:01 +03:00
|
|
|
/* Whether each pipe is shared with the parent process. */
|
|
|
|
int PipeSharedSTDIN;
|
|
|
|
int PipeSharedSTDOUT;
|
|
|
|
int PipeSharedSTDERR;
|
|
|
|
|
2006-10-03 17:10:03 +04:00
|
|
|
/* Native pipes provided by the user. */
|
|
|
|
int PipeNativeSTDIN[2];
|
|
|
|
int PipeNativeSTDOUT[2];
|
|
|
|
int PipeNativeSTDERR[2];
|
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/* The real working directory of this process. */
|
|
|
|
int RealWorkingDirectoryLength;
|
|
|
|
char* RealWorkingDirectory;
|
2003-06-10 23:46:31 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2004-01-22 22:44:44 +03:00
|
|
|
kwsysProcess* kwsysProcess_New(void)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* Allocate a process control structure. */
|
|
|
|
kwsysProcess* cp = (kwsysProcess*)malloc(sizeof(kwsysProcess));
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(cp, 0, sizeof(kwsysProcess));
|
2003-12-17 01:20:01 +03:00
|
|
|
|
|
|
|
/* Share stdin with the parent process by default. */
|
|
|
|
cp->PipeSharedSTDIN = 1;
|
|
|
|
|
2006-10-03 18:26:32 +04:00
|
|
|
/* No native pipes by default. */
|
|
|
|
cp->PipeNativeSTDIN[0] = -1;
|
|
|
|
cp->PipeNativeSTDIN[1] = -1;
|
|
|
|
cp->PipeNativeSTDOUT[0] = -1;
|
|
|
|
cp->PipeNativeSTDOUT[1] = -1;
|
|
|
|
cp->PipeNativeSTDERR[0] = -1;
|
|
|
|
cp->PipeNativeSTDERR[1] = -1;
|
|
|
|
|
2003-12-17 01:20:01 +03:00
|
|
|
/* Set initial status. */
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Starting;
|
2003-12-17 01:20:01 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_Delete(kwsysProcess* cp)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
/* Make sure we have an instance. */
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* If the process is executing, wait for it to finish. */
|
2003-06-19 01:06:21 +04:00
|
|
|
if(cp->State == kwsysProcess_State_Executing)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2004-07-08 01:27:50 +04:00
|
|
|
if(cp->Detached)
|
|
|
|
{
|
|
|
|
kwsysProcess_Disown(cp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
kwsysProcess_WaitForExit(cp, 0);
|
|
|
|
}
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Free memory. */
|
|
|
|
kwsysProcess_SetCommand(cp, 0);
|
2003-07-07 17:12:34 +04:00
|
|
|
kwsysProcess_SetWorkingDirectory(cp, 0);
|
2003-12-13 22:13:54 +03:00
|
|
|
kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDIN, 0);
|
|
|
|
kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDOUT, 0);
|
|
|
|
kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDERR, 0);
|
2003-12-03 17:20:05 +03:00
|
|
|
if(cp->CommandExitCodes)
|
|
|
|
{
|
|
|
|
free(cp->CommandExitCodes);
|
|
|
|
}
|
2003-06-10 23:46:31 +04:00
|
|
|
free(cp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-03 17:20:05 +03:00
|
|
|
int kwsysProcess_SetCommand(kwsysProcess* cp, char const* const* command)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
int i;
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
char** c = cp->Commands[i];
|
2003-06-10 23:46:31 +04:00
|
|
|
while(*c)
|
|
|
|
{
|
|
|
|
free(*c++);
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
free(cp->Commands[i]);
|
|
|
|
}
|
|
|
|
cp->NumberOfCommands = 0;
|
|
|
|
if(cp->Commands)
|
|
|
|
{
|
|
|
|
free(cp->Commands);
|
|
|
|
cp->Commands = 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
if(command)
|
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
return kwsysProcess_AddCommand(cp, command);
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_AddCommand(kwsysProcess* cp, char const* const* command)
|
|
|
|
{
|
|
|
|
int newNumberOfCommands;
|
|
|
|
char*** newCommands;
|
|
|
|
|
|
|
|
/* Make sure we have a command to add. */
|
2006-06-15 22:40:42 +04:00
|
|
|
if(!cp || !command || !*command)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new array for command pointers. */
|
|
|
|
newNumberOfCommands = cp->NumberOfCommands + 1;
|
2009-03-16 22:02:25 +03:00
|
|
|
if(!(newCommands =
|
|
|
|
(char***)malloc(sizeof(char**) *(size_t)(newNumberOfCommands))))
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
/* Out of memory. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy any existing commands into the new array. */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
|
|
|
{
|
|
|
|
newCommands[i] = cp->Commands[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the new command. */
|
2006-06-15 22:40:42 +04:00
|
|
|
if(cp->Verbatim)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
2006-06-15 22:40:42 +04:00
|
|
|
/* In order to run the given command line verbatim we need to
|
|
|
|
parse it. */
|
|
|
|
newCommands[cp->NumberOfCommands] =
|
|
|
|
kwsysProcessParseVerbatimCommand(*command);
|
|
|
|
if(!newCommands[cp->NumberOfCommands])
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
2006-06-15 22:40:42 +04:00
|
|
|
/* Out of memory. */
|
|
|
|
free(newCommands);
|
|
|
|
return 0;
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
|
|
|
}
|
2006-06-15 22:40:42 +04:00
|
|
|
else
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
2006-06-15 22:40:42 +04:00
|
|
|
/* Copy each argument string individually. */
|
|
|
|
char const* const* c = command;
|
2006-10-05 01:27:17 +04:00
|
|
|
kwsysProcess_ptrdiff_t n = 0;
|
|
|
|
kwsysProcess_ptrdiff_t i = 0;
|
2006-06-15 22:40:42 +04:00
|
|
|
while(*c++);
|
|
|
|
n = c - command - 1;
|
2009-03-16 22:02:25 +03:00
|
|
|
newCommands[cp->NumberOfCommands] =
|
|
|
|
(char**)malloc((size_t)(n+1)*sizeof(char*));
|
2006-06-15 22:40:42 +04:00
|
|
|
if(!newCommands[cp->NumberOfCommands])
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2006-06-15 22:40:42 +04:00
|
|
|
/* Out of memory. */
|
|
|
|
free(newCommands);
|
|
|
|
return 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2006-06-15 22:40:42 +04:00
|
|
|
for(i=0; i < n; ++i)
|
|
|
|
{
|
|
|
|
newCommands[cp->NumberOfCommands][i] = strdup(command[i]);
|
|
|
|
if(!newCommands[cp->NumberOfCommands][i])
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(i < n)
|
|
|
|
{
|
|
|
|
/* Out of memory. */
|
|
|
|
for(;i > 0; --i)
|
|
|
|
{
|
|
|
|
free(newCommands[cp->NumberOfCommands][i-1]);
|
|
|
|
}
|
|
|
|
free(newCommands);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
newCommands[cp->NumberOfCommands][n] = 0;
|
2003-11-28 17:48:42 +03:00
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Successfully allocated new command array. Free the old array. */
|
|
|
|
free(cp->Commands);
|
|
|
|
cp->Commands = newCommands;
|
|
|
|
cp->NumberOfCommands = newNumberOfCommands;
|
|
|
|
|
|
|
|
return 1;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2003-06-10 23:46:31 +04:00
|
|
|
cp->Timeout = timeout;
|
|
|
|
if(cp->Timeout < 0)
|
|
|
|
{
|
|
|
|
cp->Timeout = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-13 22:13:54 +03:00
|
|
|
int kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir)
|
2003-07-07 16:36:40 +04:00
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp)
|
|
|
|
{
|
2003-12-13 22:13:54 +03:00
|
|
|
return 0;
|
2003-12-13 18:36:22 +03:00
|
|
|
}
|
2003-07-07 16:36:40 +04:00
|
|
|
if(cp->WorkingDirectory == dir)
|
|
|
|
{
|
2003-12-13 22:13:54 +03:00
|
|
|
return 1;
|
2003-07-07 16:36:40 +04:00
|
|
|
}
|
|
|
|
if(cp->WorkingDirectory && dir && strcmp(cp->WorkingDirectory, dir) == 0)
|
|
|
|
{
|
2003-12-13 22:13:54 +03:00
|
|
|
return 1;
|
2003-07-07 16:36:40 +04:00
|
|
|
}
|
|
|
|
if(cp->WorkingDirectory)
|
|
|
|
{
|
|
|
|
free(cp->WorkingDirectory);
|
|
|
|
cp->WorkingDirectory = 0;
|
|
|
|
}
|
|
|
|
if(dir)
|
|
|
|
{
|
2003-07-07 17:12:34 +04:00
|
|
|
cp->WorkingDirectory = (char*)malloc(strlen(dir) + 1);
|
2003-12-13 22:13:54 +03:00
|
|
|
if(!cp->WorkingDirectory)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2003-07-07 16:36:40 +04:00
|
|
|
strcpy(cp->WorkingDirectory, dir);
|
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2005-02-28 01:36:00 +03:00
|
|
|
int kwsysProcess_SetPipeFile(kwsysProcess* cp, int prPipe, const char* file)
|
2003-12-13 22:13:54 +03:00
|
|
|
{
|
|
|
|
char** pfile;
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2005-02-28 01:36:00 +03:00
|
|
|
switch(prPipe)
|
2003-12-13 22:13:54 +03:00
|
|
|
{
|
|
|
|
case kwsysProcess_Pipe_STDIN: pfile = &cp->PipeFileSTDIN; break;
|
|
|
|
case kwsysProcess_Pipe_STDOUT: pfile = &cp->PipeFileSTDOUT; break;
|
|
|
|
case kwsysProcess_Pipe_STDERR: pfile = &cp->PipeFileSTDERR; break;
|
|
|
|
default: return 0;
|
|
|
|
}
|
|
|
|
if(*pfile)
|
|
|
|
{
|
|
|
|
free(*pfile);
|
|
|
|
*pfile = 0;
|
|
|
|
}
|
|
|
|
if(file)
|
|
|
|
{
|
|
|
|
*pfile = malloc(strlen(file)+1);
|
|
|
|
if(!*pfile)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
strcpy(*pfile, file);
|
|
|
|
}
|
2003-12-17 01:20:01 +03:00
|
|
|
|
2006-10-03 17:10:03 +04:00
|
|
|
/* If we are redirecting the pipe, do not share it or use a native
|
|
|
|
pipe. */
|
2003-12-17 01:20:01 +03:00
|
|
|
if(*pfile)
|
|
|
|
{
|
2006-10-03 17:10:03 +04:00
|
|
|
kwsysProcess_SetPipeNative(cp, prPipe, 0);
|
2005-02-28 01:36:00 +03:00
|
|
|
kwsysProcess_SetPipeShared(cp, prPipe, 0);
|
2003-12-17 01:20:01 +03:00
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
return 1;
|
2003-07-07 16:36:40 +04:00
|
|
|
}
|
|
|
|
|
2003-12-17 01:20:01 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2005-02-28 01:36:00 +03:00
|
|
|
void kwsysProcess_SetPipeShared(kwsysProcess* cp, int prPipe, int shared)
|
2003-12-17 01:20:01 +03:00
|
|
|
{
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2005-02-28 01:36:00 +03:00
|
|
|
switch(prPipe)
|
2003-12-17 01:20:01 +03:00
|
|
|
{
|
|
|
|
case kwsysProcess_Pipe_STDIN: cp->PipeSharedSTDIN = shared?1:0; break;
|
|
|
|
case kwsysProcess_Pipe_STDOUT: cp->PipeSharedSTDOUT = shared?1:0; break;
|
|
|
|
case kwsysProcess_Pipe_STDERR: cp->PipeSharedSTDERR = shared?1:0; break;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:10:03 +04:00
|
|
|
/* If we are sharing the pipe, do not redirect it to a file or use a
|
|
|
|
native pipe. */
|
2003-12-17 01:20:01 +03:00
|
|
|
if(shared)
|
|
|
|
{
|
2005-02-28 01:36:00 +03:00
|
|
|
kwsysProcess_SetPipeFile(cp, prPipe, 0);
|
2006-10-03 17:10:03 +04:00
|
|
|
kwsysProcess_SetPipeNative(cp, prPipe, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_SetPipeNative(kwsysProcess* cp, int prPipe, int p[2])
|
|
|
|
{
|
|
|
|
int* pPipeNative = 0;
|
|
|
|
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(prPipe)
|
|
|
|
{
|
|
|
|
case kwsysProcess_Pipe_STDIN: pPipeNative = cp->PipeNativeSTDIN; break;
|
|
|
|
case kwsysProcess_Pipe_STDOUT: pPipeNative = cp->PipeNativeSTDOUT; break;
|
|
|
|
case kwsysProcess_Pipe_STDERR: pPipeNative = cp->PipeNativeSTDERR; break;
|
|
|
|
default: return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the native pipe descriptors provided. */
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
pPipeNative[0] = p[0];
|
|
|
|
pPipeNative[1] = p[1];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
pPipeNative[0] = -1;
|
|
|
|
pPipeNative[1] = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are using a native pipe, do not share it or redirect it to
|
|
|
|
a file. */
|
|
|
|
if(p)
|
|
|
|
{
|
|
|
|
kwsysProcess_SetPipeFile(cp, prPipe, 0);
|
|
|
|
kwsysProcess_SetPipeShared(cp, prPipe, 0);
|
2003-12-17 01:20:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 22:27:47 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetOption(kwsysProcess* cp, int optionId)
|
|
|
|
{
|
2004-07-08 01:27:50 +04:00
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(optionId)
|
|
|
|
{
|
|
|
|
case kwsysProcess_Option_Detach: return cp->OptionDetach;
|
2006-06-15 22:40:42 +04:00
|
|
|
case kwsysProcess_Option_Verbatim: return cp->Verbatim;
|
2004-07-08 01:27:50 +04:00
|
|
|
default: return 0;
|
|
|
|
}
|
2003-08-05 22:27:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_SetOption(kwsysProcess* cp, int optionId, int value)
|
|
|
|
{
|
2004-07-08 01:27:50 +04:00
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch(optionId)
|
|
|
|
{
|
|
|
|
case kwsysProcess_Option_Detach: cp->OptionDetach = value; break;
|
2006-06-15 22:40:42 +04:00
|
|
|
case kwsysProcess_Option_Verbatim: cp->Verbatim = value; break;
|
2004-07-08 01:27:50 +04:00
|
|
|
default: break;
|
|
|
|
}
|
2003-08-05 22:27:47 +04:00
|
|
|
}
|
|
|
|
|
2003-07-07 16:36:40 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-10 23:46:31 +04:00
|
|
|
int kwsysProcess_GetState(kwsysProcess* cp)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
return cp? cp->State : kwsysProcess_State_Error;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetExitException(kwsysProcess* cp)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
return cp? cp->ExitException : kwsysProcess_Exception_Other;
|
2003-06-19 01:06:21 +04:00
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetExitCode(kwsysProcess* cp)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
return cp? cp->ExitCode : 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetExitValue(kwsysProcess* cp)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
return cp? cp->ExitValue : -1;
|
2003-06-19 01:06:21 +04:00
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
const char* kwsysProcess_GetErrorString(kwsysProcess* cp)
|
|
|
|
{
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp)
|
|
|
|
{
|
2003-12-31 00:23:16 +03:00
|
|
|
return "Process management structure could not be allocated";
|
2003-12-13 18:36:22 +03:00
|
|
|
}
|
|
|
|
else if(cp->State == kwsysProcess_State_Error)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-08-05 17:07:54 +04:00
|
|
|
return cp->ErrorMessage;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-12-31 00:23:16 +03:00
|
|
|
return "Success";
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
const char* kwsysProcess_GetExceptionString(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return "GetExceptionString called with NULL process management structure";
|
|
|
|
}
|
|
|
|
else if(cp->State == kwsysProcess_State_Exception)
|
|
|
|
{
|
|
|
|
return cp->ExitExceptionString;
|
|
|
|
}
|
|
|
|
return "No exception";
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_Execute(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
int i;
|
2006-05-23 19:48:15 +04:00
|
|
|
kwsysProcessCreateInformation si = {-1, -1, -1, {-1, -1}};
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Do not execute a second copy simultaneously. */
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp || cp->State == kwsysProcess_State_Executing)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Initialize the control structure for a new process. */
|
2003-12-03 17:20:05 +03:00
|
|
|
if(!kwsysProcessInitialize(cp))
|
|
|
|
{
|
|
|
|
strcpy(cp->ErrorMessage, "Out of memory");
|
|
|
|
cp->State = kwsysProcess_State_Error;
|
|
|
|
return;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/* Save the real working directory of this process and change to
|
|
|
|
the working directory for the child processes. This is needed
|
|
|
|
to make pipe file paths evaluate correctly. */
|
|
|
|
if(cp->WorkingDirectory)
|
|
|
|
{
|
|
|
|
int r;
|
2008-12-16 01:19:26 +03:00
|
|
|
if(!getcwd(cp->RealWorkingDirectory,
|
|
|
|
(size_t)(cp->RealWorkingDirectoryLength)))
|
2003-12-13 22:13:54 +03:00
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Some platforms specify that the chdir call may be
|
|
|
|
interrupted. Repeat the call until it finishes. */
|
|
|
|
while(((r = chdir(cp->WorkingDirectory)) < 0) && (errno == EINTR));
|
|
|
|
if(r < 0)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
2004-01-22 19:16:43 +03:00
|
|
|
return;
|
2003-12-13 22:13:54 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* If not running a detached child, add this object to the global
|
|
|
|
set of process objects that wish to be notified when a child
|
|
|
|
exits. */
|
|
|
|
if(!cp->OptionDetach)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2006-05-23 19:48:15 +04:00
|
|
|
if(!kwsysProcessesAdd(cp))
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
return;
|
|
|
|
}
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Setup the stderr pipe to be shared by all processes. */
|
|
|
|
{
|
|
|
|
/* Create the pipe. */
|
|
|
|
int p[2];
|
|
|
|
if(pipe(p) < 0)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
return;
|
|
|
|
}
|
2006-05-21 18:26:28 +04:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Store the pipe. */
|
|
|
|
cp->PipeReadEnds[KWSYSPE_PIPE_STDERR] = p[0];
|
|
|
|
si.StdErr = p[1];
|
|
|
|
|
|
|
|
/* Set close-on-exec flag on the pipe's ends. */
|
|
|
|
if((fcntl(p[0], F_SETFD, FD_CLOEXEC) < 0) ||
|
|
|
|
(fcntl(p[1], F_SETFD, FD_CLOEXEC) < 0))
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
return;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2006-12-04 22:42:47 +03:00
|
|
|
|
2008-08-20 17:57:09 +04:00
|
|
|
/* Set to non-blocking in case select lies, or for the polling
|
|
|
|
implementation. */
|
2006-12-04 22:42:47 +03:00
|
|
|
if(!kwsysProcessSetNonBlocking(p[0]))
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
return;
|
|
|
|
}
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/* Replace the stderr pipe with a file if requested. In this case
|
|
|
|
the select call will report that stderr is closed immediately. */
|
|
|
|
if(cp->PipeFileSTDERR)
|
|
|
|
{
|
|
|
|
if(!kwsysProcessSetupOutputPipeFile(&si.StdErr, cp->PipeFileSTDERR))
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-17 01:20:01 +03:00
|
|
|
/* Replace the stderr pipe with the parent's if requested. In this
|
|
|
|
case the select call will report that stderr is closed
|
|
|
|
immediately. */
|
|
|
|
if(cp->PipeSharedSTDERR)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
si.StdErr = 2;
|
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
|
2006-10-03 17:10:03 +04:00
|
|
|
/* Replace the stderr pipe with the native pipe provided if any. In
|
|
|
|
this case the select call will report that stderr is closed
|
|
|
|
immediately. */
|
|
|
|
if(cp->PipeNativeSTDERR[1] >= 0)
|
|
|
|
{
|
|
|
|
if(!kwsysProcessSetupOutputPipeNative(&si.StdErr, cp->PipeNativeSTDERR))
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The timeout period starts now. */
|
|
|
|
cp->StartTime = kwsysProcessTimeGetCurrent();
|
|
|
|
cp->TimeoutTime.tv_sec = -1;
|
|
|
|
cp->TimeoutTime.tv_usec = -1;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Create the pipeline of processes. */
|
|
|
|
{
|
2003-12-17 01:20:01 +03:00
|
|
|
int readEnd = -1;
|
2006-12-04 22:42:47 +03:00
|
|
|
int failed = 0;
|
2003-12-03 17:20:05 +03:00
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
if(!kwsysProcessCreate(cp, i, &si, &readEnd))
|
2006-12-04 22:42:47 +03:00
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
|
2008-08-20 17:57:09 +04:00
|
|
|
/* Set the output pipe of the last process to be non-blocking in
|
|
|
|
case select lies, or for the polling implementation. */
|
|
|
|
if(i == (cp->NumberOfCommands-1) && !kwsysProcessSetNonBlocking(readEnd))
|
2006-12-04 22:42:47 +03:00
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(failed)
|
2003-07-07 17:12:34 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
|
|
|
|
/* Release resources that may have been allocated for this
|
|
|
|
process before an error occurred. */
|
2003-12-17 01:20:01 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&readEnd);
|
|
|
|
if(si.StdIn != 0)
|
2003-07-07 17:12:34 +04:00
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si.StdIn);
|
2003-07-07 17:12:34 +04:00
|
|
|
}
|
2003-12-17 01:20:01 +03:00
|
|
|
if(si.StdOut != 1)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdOut);
|
|
|
|
}
|
|
|
|
if(si.StdErr != 2)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
}
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si.ErrorPipe[0]);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.ErrorPipe[1]);
|
2003-12-03 17:20:05 +03:00
|
|
|
return;
|
2003-07-07 17:12:34 +04:00
|
|
|
}
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Save a handle to the output pipe for the last process. */
|
|
|
|
cp->PipeReadEnds[KWSYSPE_PIPE_STDOUT] = readEnd;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* The parent process does not need the output pipe write ends. */
|
2005-04-22 00:05:09 +04:00
|
|
|
if(si.StdErr != 2)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/* Restore the working directory. */
|
|
|
|
if(cp->RealWorkingDirectory)
|
|
|
|
{
|
|
|
|
/* Some platforms specify that the chdir call may be
|
|
|
|
interrupted. Repeat the call until it finishes. */
|
|
|
|
while((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR));
|
|
|
|
free(cp->RealWorkingDirectory);
|
|
|
|
cp->RealWorkingDirectory = 0;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* All the pipes are now open. */
|
|
|
|
cp->PipesLeft = KWSYSPE_PIPE_COUNT;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* The process has now started. */
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Executing;
|
2004-07-08 01:27:50 +04:00
|
|
|
cp->Detached = cp->OptionDetach;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
kwsysEXPORT void kwsysProcess_Disown(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
/* Make sure a detached child process is running. */
|
2004-07-08 01:46:34 +04:00
|
|
|
if(!cp || !cp->Detached || cp->State != kwsysProcess_State_Executing ||
|
|
|
|
cp->TimeoutExpired || cp->Killed)
|
2004-07-08 01:27:50 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-01-11 02:32:38 +03:00
|
|
|
/* Close all the pipes safely. */
|
|
|
|
kwsysProcessClosePipes(cp);
|
2004-07-08 01:27:50 +04:00
|
|
|
|
2004-07-08 01:46:34 +04:00
|
|
|
/* We will not wait for exit, so cleanup now. */
|
|
|
|
kwsysProcessCleanup(cp, 0);
|
|
|
|
|
|
|
|
/* The process has been disowned. */
|
2004-07-08 01:27:50 +04:00
|
|
|
cp->State = kwsysProcess_State_Disowned;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
2006-12-04 22:42:47 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
typedef struct kwsysProcessWaitData_s
|
|
|
|
{
|
|
|
|
int Expired;
|
|
|
|
int PipeId;
|
|
|
|
int User;
|
|
|
|
double* UserTimeout;
|
|
|
|
kwsysProcessTime TimeoutTime;
|
|
|
|
} kwsysProcessWaitData;
|
|
|
|
static int kwsysProcessWaitForPipe(kwsysProcess* cp, char** data, int* length,
|
|
|
|
kwsysProcessWaitData* wd);
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-05 19:53:17 +03:00
|
|
|
int kwsysProcess_WaitForData(kwsysProcess* cp, char** data, int* length,
|
|
|
|
double* userTimeout)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2005-05-05 17:05:15 +04:00
|
|
|
kwsysProcessTime userStartTime = {0, 0};
|
2006-12-04 22:42:47 +03:00
|
|
|
kwsysProcessWaitData wd =
|
|
|
|
{
|
|
|
|
0,
|
|
|
|
kwsysProcess_Pipe_None,
|
|
|
|
0,
|
2006-12-05 16:39:22 +03:00
|
|
|
0,
|
2006-12-04 22:42:47 +03:00
|
|
|
{0, 0}
|
|
|
|
};
|
2006-12-05 16:39:22 +03:00
|
|
|
wd.UserTimeout = userTimeout;
|
2003-12-13 18:36:22 +03:00
|
|
|
/* Make sure we are executing a process. */
|
|
|
|
if(!cp || cp->State != kwsysProcess_State_Executing || cp->Killed ||
|
|
|
|
cp->TimeoutExpired)
|
|
|
|
{
|
|
|
|
return kwsysProcess_Pipe_None;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Record the time at which user timeout period starts. */
|
|
|
|
if(userTimeout)
|
|
|
|
{
|
|
|
|
userStartTime = kwsysProcessTimeGetCurrent();
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Calculate the time at which a timeout will expire, and whether it
|
|
|
|
is the user or process timeout. */
|
2006-12-04 22:42:47 +03:00
|
|
|
wd.User = kwsysProcessGetTimeoutTime(cp, userTimeout,
|
|
|
|
&wd.TimeoutTime);
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Data can only be available when pipes are open. If the process
|
|
|
|
is not running, cp->PipesLeft will be 0. */
|
2006-12-04 22:42:47 +03:00
|
|
|
while(cp->PipesLeft > 0 &&
|
|
|
|
!kwsysProcessWaitForPipe(cp, data, length, &wd)) {}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Update the user timeout. */
|
|
|
|
if(userTimeout)
|
|
|
|
{
|
|
|
|
kwsysProcessTime userEndTime = kwsysProcessTimeGetCurrent();
|
|
|
|
kwsysProcessTime difference = kwsysProcessTimeSubtract(userEndTime,
|
2006-12-04 22:42:47 +03:00
|
|
|
userStartTime);
|
2003-06-10 23:46:31 +04:00
|
|
|
double d = kwsysProcessTimeToDouble(difference);
|
|
|
|
*userTimeout -= d;
|
|
|
|
if(*userTimeout < 0)
|
|
|
|
{
|
|
|
|
*userTimeout = 0;
|
|
|
|
}
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Check what happened. */
|
2006-12-04 22:42:47 +03:00
|
|
|
if(wd.PipeId)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* Data are ready on a pipe. */
|
2006-12-04 22:42:47 +03:00
|
|
|
return wd.PipeId;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2006-12-04 22:42:47 +03:00
|
|
|
else if(wd.Expired)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* A timeout has expired. */
|
2006-12-04 22:42:47 +03:00
|
|
|
if(wd.User)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* The user timeout has expired. It has no time left. */
|
2003-06-19 01:06:21 +04:00
|
|
|
return kwsysProcess_Pipe_Timeout;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
/* The process timeout has expired. Kill the children now. */
|
2003-06-10 23:46:31 +04:00
|
|
|
kwsysProcess_Kill(cp);
|
|
|
|
cp->Killed = 0;
|
|
|
|
cp->TimeoutExpired = 1;
|
2003-12-07 22:09:58 +03:00
|
|
|
return kwsysProcess_Pipe_None;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No pipes are left open. */
|
2003-12-07 22:09:58 +03:00
|
|
|
return kwsysProcess_Pipe_None;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-04 22:42:47 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static int kwsysProcessWaitForPipe(kwsysProcess* cp, char** data, int* length,
|
|
|
|
kwsysProcessWaitData* wd)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
kwsysProcessTimeNative timeoutLength;
|
|
|
|
|
|
|
|
#if KWSYSPE_USE_SELECT
|
|
|
|
int numReady = 0;
|
|
|
|
int max = -1;
|
|
|
|
kwsysProcessTimeNative* timeout = 0;
|
|
|
|
|
|
|
|
/* Check for any open pipes with data reported ready by the last
|
|
|
|
call to select. According to "man select_tut" we must deal
|
|
|
|
with all descriptors reported by a call to select before
|
|
|
|
passing them to another select call. */
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
if(cp->PipeReadEnds[i] >= 0 &&
|
|
|
|
FD_ISSET(cp->PipeReadEnds[i], &cp->PipeSet))
|
|
|
|
{
|
|
|
|
kwsysProcess_ssize_t n;
|
|
|
|
|
|
|
|
/* We are handling this pipe now. Remove it from the set. */
|
|
|
|
FD_CLR(cp->PipeReadEnds[i], &cp->PipeSet);
|
|
|
|
|
|
|
|
/* The pipe is ready to read without blocking. Keep trying to
|
|
|
|
read until the operation is not interrupted. */
|
|
|
|
while(((n = read(cp->PipeReadEnds[i], cp->PipeBuffer,
|
|
|
|
KWSYSPE_PIPE_BUFFER_SIZE)) < 0) && (errno == EINTR));
|
|
|
|
if(n > 0)
|
|
|
|
{
|
|
|
|
/* We have data on this pipe. */
|
|
|
|
if(i == KWSYSPE_PIPE_SIGNAL)
|
|
|
|
{
|
|
|
|
/* A child process has terminated. */
|
|
|
|
kwsysProcessDestroy(cp);
|
|
|
|
}
|
|
|
|
else if(data && length)
|
|
|
|
{
|
|
|
|
/* Report this data. */
|
|
|
|
*data = cp->PipeBuffer;
|
2009-03-16 22:02:25 +03:00
|
|
|
*length = (int)(n);
|
2006-12-04 22:42:47 +03:00
|
|
|
switch(i)
|
|
|
|
{
|
|
|
|
case KWSYSPE_PIPE_STDOUT:
|
|
|
|
wd->PipeId = kwsysProcess_Pipe_STDOUT; break;
|
|
|
|
case KWSYSPE_PIPE_STDERR:
|
|
|
|
wd->PipeId = kwsysProcess_Pipe_STDERR; break;
|
|
|
|
};
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2008-08-20 17:57:09 +04:00
|
|
|
else if(n < 0 && errno == EAGAIN)
|
|
|
|
{
|
|
|
|
/* No data are really ready. The select call lied. See the
|
|
|
|
"man select" page on Linux for cases when this occurs. */
|
|
|
|
}
|
2006-12-04 22:42:47 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are done reading from this pipe. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]);
|
|
|
|
--cp->PipesLeft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have data, break early. */
|
|
|
|
if(wd->PipeId)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure the set is empty (it should always be empty here
|
|
|
|
anyway). */
|
|
|
|
FD_ZERO(&cp->PipeSet);
|
|
|
|
|
|
|
|
/* Setup a timeout if required. */
|
|
|
|
if(wd->TimeoutTime.tv_sec < 0)
|
|
|
|
{
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeout = &timeoutLength;
|
|
|
|
}
|
|
|
|
if(kwsysProcessGetTimeoutLeft(&wd->TimeoutTime,
|
|
|
|
wd->User?wd->UserTimeout:0,
|
|
|
|
&timeoutLength))
|
|
|
|
{
|
|
|
|
/* Timeout has already expired. */
|
|
|
|
wd->Expired = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Add the pipe reading ends that are still open. */
|
|
|
|
max = -1;
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
if(cp->PipeReadEnds[i] >= 0)
|
|
|
|
{
|
|
|
|
FD_SET(cp->PipeReadEnds[i], &cp->PipeSet);
|
|
|
|
if(cp->PipeReadEnds[i] > max)
|
|
|
|
{
|
|
|
|
max = cp->PipeReadEnds[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Make sure we have a non-empty set. */
|
|
|
|
if(max < 0)
|
|
|
|
{
|
|
|
|
/* All pipes have closed. Child has terminated. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Run select to block until data are available. Repeat call
|
|
|
|
until it is not interrupted. */
|
|
|
|
while(((numReady = select(max+1, &cp->PipeSet, 0, 0, timeout)) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
|
|
|
|
/* Check result of select. */
|
|
|
|
if(numReady == 0)
|
|
|
|
{
|
|
|
|
/* Select's timeout expired. */
|
|
|
|
wd->Expired = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if(numReady < 0)
|
|
|
|
{
|
|
|
|
/* Select returned an error. Leave the error description in the
|
|
|
|
pipe buffer. */
|
|
|
|
strncpy(cp->ErrorMessage, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE);
|
|
|
|
|
|
|
|
/* Kill the children now. */
|
|
|
|
kwsysProcess_Kill(cp);
|
|
|
|
cp->Killed = 0;
|
|
|
|
cp->SelectError = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
#else
|
|
|
|
/* Poll pipes for data since we do not have select. */
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
if(cp->PipeReadEnds[i] >= 0)
|
|
|
|
{
|
|
|
|
const int fd = cp->PipeReadEnds[i];
|
|
|
|
int n = read(fd, cp->PipeBuffer, KWSYSPE_PIPE_BUFFER_SIZE);
|
|
|
|
if(n > 0)
|
|
|
|
{
|
|
|
|
/* We have data on this pipe. */
|
|
|
|
if(i == KWSYSPE_PIPE_SIGNAL)
|
|
|
|
{
|
|
|
|
/* A child process has terminated. */
|
|
|
|
kwsysProcessDestroy(cp);
|
|
|
|
}
|
|
|
|
else if(data && length)
|
|
|
|
{
|
|
|
|
/* Report this data. */
|
|
|
|
*data = cp->PipeBuffer;
|
|
|
|
*length = n;
|
|
|
|
switch(i)
|
|
|
|
{
|
|
|
|
case KWSYSPE_PIPE_STDOUT:
|
|
|
|
wd->PipeId = kwsysProcess_Pipe_STDOUT; break;
|
|
|
|
case KWSYSPE_PIPE_STDERR:
|
|
|
|
wd->PipeId = kwsysProcess_Pipe_STDERR; break;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else if (n == 0) /* EOF */
|
|
|
|
{
|
|
|
|
/* We are done reading from this pipe. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]);
|
|
|
|
--cp->PipesLeft;
|
|
|
|
}
|
|
|
|
else if (n < 0) /* error */
|
|
|
|
{
|
|
|
|
if((errno != EINTR) && (errno != EAGAIN))
|
|
|
|
{
|
|
|
|
strncpy(cp->ErrorMessage,strerror(errno),
|
|
|
|
KWSYSPE_PIPE_BUFFER_SIZE);
|
|
|
|
/* Kill the children now. */
|
|
|
|
kwsysProcess_Kill(cp);
|
|
|
|
cp->Killed = 0;
|
|
|
|
cp->SelectError = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we have data, break early. */
|
|
|
|
if(wd->PipeId)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(kwsysProcessGetTimeoutLeft(&wd->TimeoutTime, wd->User?wd->UserTimeout:0,
|
|
|
|
&timeoutLength))
|
|
|
|
{
|
|
|
|
/* Timeout has already expired. */
|
|
|
|
wd->Expired = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if((timeoutLength.tv_sec == 0) && (timeoutLength.tv_usec == 0))
|
|
|
|
{
|
|
|
|
/* Timeout has already expired. */
|
|
|
|
wd->Expired = 1;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Sleep a little, try again. */
|
|
|
|
{
|
|
|
|
unsigned int msec = ((timeoutLength.tv_sec * 1000) +
|
|
|
|
(timeoutLength.tv_usec / 1000));
|
|
|
|
if (msec > 100000)
|
|
|
|
{
|
|
|
|
msec = 100000; /* do not sleep more than 100 milliseconds at a time */
|
|
|
|
}
|
|
|
|
kwsysProcess_usleep(msec);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_WaitForExit(kwsysProcess* cp, double* userTimeout)
|
|
|
|
{
|
|
|
|
int status = 0;
|
2005-02-28 01:36:00 +03:00
|
|
|
int prPipe = 0;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Make sure we are executing a process. */
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp || cp->State != kwsysProcess_State_Executing)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Wait for all the pipes to close. Ignore all data. */
|
2005-02-28 01:36:00 +03:00
|
|
|
while((prPipe = kwsysProcess_WaitForData(cp, 0, 0, userTimeout)) > 0)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2005-02-28 01:36:00 +03:00
|
|
|
if(prPipe == kwsysProcess_Pipe_Timeout)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Check if there was an error in one of the waitpid calls. */
|
|
|
|
if(cp->State == kwsysProcess_State_Error)
|
|
|
|
{
|
|
|
|
/* The error message is already in its buffer. Tell
|
|
|
|
kwsysProcessCleanup to not create it. */
|
|
|
|
kwsysProcessCleanup(cp, 0);
|
2003-06-10 23:46:31 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Check whether the child reported an error invoking the process. */
|
2003-12-03 17:20:05 +03:00
|
|
|
if(cp->SelectError)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* The error message is already in its buffer. Tell
|
|
|
|
kwsysProcessCleanup to not create it. */
|
|
|
|
kwsysProcessCleanup(cp, 0);
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Error;
|
2003-06-10 23:46:31 +04:00
|
|
|
return 1;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Use the status of the last process in the pipeline. */
|
|
|
|
status = cp->CommandExitCodes[cp->NumberOfCommands-1];
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Determine the outcome. */
|
|
|
|
if(cp->Killed)
|
|
|
|
{
|
|
|
|
/* We killed the child. */
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Killed;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else if(cp->TimeoutExpired)
|
|
|
|
{
|
|
|
|
/* The timeout expired. */
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Expired;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else if(WIFEXITED(status))
|
|
|
|
{
|
2003-06-19 01:06:21 +04:00
|
|
|
/* The child exited normally. */
|
|
|
|
cp->State = kwsysProcess_State_Exited;
|
|
|
|
cp->ExitException = kwsysProcess_Exception_None;
|
|
|
|
cp->ExitCode = status;
|
|
|
|
cp->ExitValue = (int)WEXITSTATUS(status);
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else if(WIFSIGNALED(status))
|
|
|
|
{
|
|
|
|
/* The child received an unhandled signal. */
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Exception;
|
|
|
|
cp->ExitCode = status;
|
2003-12-31 00:23:16 +03:00
|
|
|
kwsysProcessSetExitException(cp, (int)WTERMSIG(status));
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Error getting the child return code. */
|
|
|
|
strcpy(cp->ErrorMessage, "Error getting child return code.");
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Error;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Normal cleanup. */
|
|
|
|
kwsysProcessCleanup(cp, 0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_Kill(kwsysProcess* cp)
|
2003-11-28 17:48:42 +03:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
int i;
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Make sure we are executing a process. */
|
2003-12-13 18:36:22 +03:00
|
|
|
if(!cp || cp->State != kwsysProcess_State_Executing)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2008-01-11 02:32:38 +03:00
|
|
|
/* First close the child exit report pipe write end to avoid causing a
|
|
|
|
SIGPIPE when the child terminates and our signal handler tries to
|
|
|
|
report it after we have already closed the read end. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->SignalPipe);
|
|
|
|
|
|
|
|
#if !defined(__APPLE__)
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Close all the pipe read ends. Do this before killing the
|
|
|
|
children because Cygwin has problems killing processes that are
|
2008-01-11 02:32:38 +03:00
|
|
|
blocking to wait for writing to their output pipes. */
|
|
|
|
kwsysProcessClosePipes(cp);
|
|
|
|
#endif
|
2006-05-23 19:48:15 +04:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Kill the children. */
|
2003-06-10 23:46:31 +04:00
|
|
|
cp->Killed = 1;
|
2003-12-03 17:20:05 +03:00
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
|
|
|
{
|
2006-05-23 19:48:15 +04:00
|
|
|
int status;
|
2003-12-03 17:20:05 +03:00
|
|
|
if(cp->ForkPIDs[i])
|
|
|
|
{
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Kill the child. */
|
2004-07-03 00:39:29 +04:00
|
|
|
kwsysProcessKill(cp->ForkPIDs[i]);
|
2005-04-19 19:52:25 +04:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* Reap the child. Keep trying until the call is not
|
|
|
|
interrupted. */
|
|
|
|
while((waitpid(cp->ForkPIDs[i], &status, 0) < 0) && (errno == EINTR));
|
|
|
|
}
|
2005-04-19 19:52:25 +04:00
|
|
|
}
|
2008-01-11 02:32:38 +03:00
|
|
|
|
|
|
|
#if defined(__APPLE__)
|
|
|
|
/* Close all the pipe read ends. Do this after killing the
|
|
|
|
children because OS X has problems closing pipe read ends whose
|
|
|
|
pipes are full and still have an open write end. */
|
|
|
|
kwsysProcessClosePipes(cp);
|
|
|
|
#endif
|
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
cp->CommandsLeft = 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Initialize a process control structure for kwsysProcess_Execute. */
|
2003-12-03 17:20:05 +03:00
|
|
|
static int kwsysProcessInitialize(kwsysProcess* cp)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
cp->PipeReadEnds[i] = -1;
|
|
|
|
}
|
2006-05-23 19:48:15 +04:00
|
|
|
cp->SignalPipe = -1;
|
2003-12-03 17:20:05 +03:00
|
|
|
cp->SelectError = 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
cp->StartTime.tv_sec = -1;
|
|
|
|
cp->StartTime.tv_usec = -1;
|
|
|
|
cp->TimeoutTime.tv_sec = -1;
|
|
|
|
cp->TimeoutTime.tv_usec = -1;
|
|
|
|
cp->TimeoutExpired = 0;
|
|
|
|
cp->PipesLeft = 0;
|
2006-05-23 19:48:15 +04:00
|
|
|
cp->CommandsLeft = 0;
|
2006-12-04 22:42:47 +03:00
|
|
|
#if KWSYSPE_USE_SELECT
|
2003-06-10 23:46:31 +04:00
|
|
|
FD_ZERO(&cp->PipeSet);
|
2006-12-04 22:42:47 +03:00
|
|
|
#endif
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Starting;
|
2003-06-10 23:46:31 +04:00
|
|
|
cp->Killed = 0;
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->ExitException = kwsysProcess_Exception_None;
|
|
|
|
cp->ExitCode = 1;
|
|
|
|
cp->ExitValue = 1;
|
2003-06-10 23:46:31 +04:00
|
|
|
cp->ErrorMessage[0] = 0;
|
2003-12-31 00:23:16 +03:00
|
|
|
strcpy(cp->ExitExceptionString, "No exception");
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
if(cp->ForkPIDs)
|
|
|
|
{
|
|
|
|
free(cp->ForkPIDs);
|
|
|
|
}
|
2009-03-16 22:02:25 +03:00
|
|
|
cp->ForkPIDs = (pid_t*)malloc(sizeof(pid_t)*(size_t)(cp->NumberOfCommands));
|
2003-12-03 17:20:05 +03:00
|
|
|
if(!cp->ForkPIDs)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-16 22:02:25 +03:00
|
|
|
memset(cp->ForkPIDs, 0, sizeof(pid_t)*(size_t)(cp->NumberOfCommands));
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
if(cp->CommandExitCodes)
|
|
|
|
{
|
|
|
|
free(cp->CommandExitCodes);
|
|
|
|
}
|
2009-03-16 22:02:25 +03:00
|
|
|
cp->CommandExitCodes = (int*)malloc(sizeof(int)*
|
|
|
|
(size_t)(cp->NumberOfCommands));
|
2003-12-03 17:20:05 +03:00
|
|
|
if(!cp->CommandExitCodes)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-16 22:02:25 +03:00
|
|
|
memset(cp->CommandExitCodes, 0, sizeof(int)*(size_t)(cp->NumberOfCommands));
|
2003-12-03 17:20:05 +03:00
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/* Allocate memory to save the real working directory. */
|
2004-01-21 21:38:09 +03:00
|
|
|
if ( cp->WorkingDirectory )
|
|
|
|
{
|
2003-12-13 22:13:54 +03:00
|
|
|
#if defined(MAXPATHLEN)
|
2004-01-21 21:38:09 +03:00
|
|
|
cp->RealWorkingDirectoryLength = MAXPATHLEN;
|
2003-12-13 22:13:54 +03:00
|
|
|
#elif defined(PATH_MAX)
|
2004-01-21 21:38:09 +03:00
|
|
|
cp->RealWorkingDirectoryLength = PATH_MAX;
|
2003-12-13 22:13:54 +03:00
|
|
|
#else
|
2004-01-21 21:38:09 +03:00
|
|
|
cp->RealWorkingDirectoryLength = 4096;
|
2003-12-13 22:13:54 +03:00
|
|
|
#endif
|
2008-12-16 01:19:26 +03:00
|
|
|
cp->RealWorkingDirectory =
|
|
|
|
malloc((size_t)(cp->RealWorkingDirectoryLength));
|
2004-01-21 21:38:09 +03:00
|
|
|
if(!cp->RealWorkingDirectory)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
return 1;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Free all resources used by the given kwsysProcess instance that were
|
|
|
|
allocated by kwsysProcess_Execute. */
|
2003-06-11 00:55:50 +04:00
|
|
|
static void kwsysProcessCleanup(kwsysProcess* cp, int error)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
int i;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
if(error)
|
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
/* We are cleaning up due to an error. Report the error message
|
|
|
|
if one has not been provided already. */
|
|
|
|
if(cp->ErrorMessage[0] == 0)
|
|
|
|
{
|
|
|
|
strncpy(cp->ErrorMessage, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set the error state. */
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Error;
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Kill any children already started. */
|
|
|
|
if(cp->ForkPIDs)
|
|
|
|
{
|
2005-12-31 20:59:31 +03:00
|
|
|
int status;
|
2003-12-03 17:20:05 +03:00
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
|
|
|
{
|
|
|
|
if(cp->ForkPIDs[i])
|
|
|
|
{
|
2005-12-31 20:59:31 +03:00
|
|
|
/* Kill the child. */
|
2004-07-03 00:39:29 +04:00
|
|
|
kwsysProcessKill(cp->ForkPIDs[i]);
|
2006-05-23 19:48:15 +04:00
|
|
|
|
2005-12-31 20:59:31 +03:00
|
|
|
/* Reap the child. Keep trying until the call is not
|
|
|
|
interrupted. */
|
|
|
|
while((waitpid(cp->ForkPIDs[i], &status, 0) < 0) &&
|
|
|
|
(errno == EINTR));
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
|
|
|
|
/* Restore the working directory. */
|
|
|
|
if(cp->RealWorkingDirectory)
|
|
|
|
{
|
|
|
|
while((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR));
|
|
|
|
}
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* If not creating a detached child, remove this object from the
|
|
|
|
global set of process objects that wish to be notified when a
|
|
|
|
child exits. */
|
|
|
|
if(!cp->OptionDetach)
|
|
|
|
{
|
|
|
|
kwsysProcessesRemove(cp);
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Free memory. */
|
|
|
|
if(cp->ForkPIDs)
|
|
|
|
{
|
|
|
|
free(cp->ForkPIDs);
|
|
|
|
cp->ForkPIDs = 0;
|
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
if(cp->RealWorkingDirectory)
|
|
|
|
{
|
|
|
|
free(cp->RealWorkingDirectory);
|
|
|
|
cp->RealWorkingDirectory = 0;
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Close pipe handles. */
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Close the given file descriptor if it is open. Reset its value to -1. */
|
2003-06-11 00:55:50 +04:00
|
|
|
static void kwsysProcessCleanupDescriptor(int* pfd)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
if(pfd && *pfd >= 0)
|
|
|
|
{
|
|
|
|
/* Keep trying to close until it is not interrupted by a
|
|
|
|
* signal. */
|
|
|
|
while((close(*pfd) < 0) && (errno == EINTR));
|
|
|
|
*pfd = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-04 22:42:47 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2008-01-11 02:32:38 +03:00
|
|
|
static void kwsysProcessClosePipes(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Close any pipes that are still open. */
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
if(cp->PipeReadEnds[i] >= 0)
|
|
|
|
{
|
|
|
|
#if KWSYSPE_USE_SELECT
|
|
|
|
/* If the pipe was reported by the last call to select, we must
|
|
|
|
read from it. This is needed to satisfy the suggestions from
|
|
|
|
"man select_tut" and is not needed for the polling
|
|
|
|
implementation. Ignore the data. */
|
|
|
|
if(FD_ISSET(cp->PipeReadEnds[i], &cp->PipeSet))
|
|
|
|
{
|
|
|
|
/* We are handling this pipe now. Remove it from the set. */
|
|
|
|
FD_CLR(cp->PipeReadEnds[i], &cp->PipeSet);
|
|
|
|
|
|
|
|
/* The pipe is ready to read without blocking. Keep trying to
|
|
|
|
read until the operation is not interrupted. */
|
|
|
|
while((read(cp->PipeReadEnds[i], cp->PipeBuffer,
|
|
|
|
KWSYSPE_PIPE_BUFFER_SIZE) < 0) && (errno == EINTR));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* We are done reading from this pipe. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]);
|
|
|
|
--cp->PipesLeft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2006-12-04 22:42:47 +03:00
|
|
|
static int kwsysProcessSetNonBlocking(int fd)
|
|
|
|
{
|
|
|
|
int flags = fcntl(fd, F_GETFL);
|
|
|
|
if(flags >= 0)
|
|
|
|
{
|
|
|
|
flags = fcntl(fd, F_SETFL, flags | O_NONBLOCK);
|
|
|
|
}
|
|
|
|
return flags >= 0;
|
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2005-02-28 01:36:00 +03:00
|
|
|
static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
|
2003-12-04 21:56:00 +03:00
|
|
|
kwsysProcessCreateInformation* si, int* readEnd)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
/* Setup the process's stdin. */
|
2005-02-28 01:36:00 +03:00
|
|
|
if(prIndex > 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
si->StdIn = *readEnd;
|
2003-12-03 17:20:05 +03:00
|
|
|
*readEnd = 0;
|
|
|
|
}
|
2003-12-13 22:13:54 +03:00
|
|
|
else if(cp->PipeFileSTDIN)
|
|
|
|
{
|
|
|
|
/* Open a file for the child's stdin to read. */
|
|
|
|
si->StdIn = open(cp->PipeFileSTDIN, O_RDONLY);
|
|
|
|
if(si->StdIn < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set close-on-exec flag on the pipe's end. */
|
|
|
|
if(fcntl(si->StdIn, F_SETFD, FD_CLOEXEC) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2003-12-17 01:20:01 +03:00
|
|
|
else if(cp->PipeSharedSTDIN)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
si->StdIn = 0;
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
2006-10-03 17:10:03 +04:00
|
|
|
else if(cp->PipeNativeSTDIN[0] >= 0)
|
|
|
|
{
|
|
|
|
si->StdIn = cp->PipeNativeSTDIN[0];
|
|
|
|
|
|
|
|
/* Set close-on-exec flag on the pipe's ends. The read end will
|
|
|
|
be dup2-ed into the stdin descriptor after the fork but before
|
|
|
|
the exec. */
|
|
|
|
if((fcntl(cp->PipeNativeSTDIN[0], F_SETFD, FD_CLOEXEC) < 0) ||
|
|
|
|
(fcntl(cp->PipeNativeSTDIN[1], F_SETFD, FD_CLOEXEC) < 0))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2003-12-17 01:20:01 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
si->StdIn = -1;
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Setup the process's stdout. */
|
|
|
|
{
|
|
|
|
/* Create the pipe. */
|
|
|
|
int p[2];
|
|
|
|
if(pipe(p) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
*readEnd = p[0];
|
2003-12-03 21:37:21 +03:00
|
|
|
si->StdOut = p[1];
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Set close-on-exec flag on the pipe's ends. */
|
|
|
|
if((fcntl(p[0], F_SETFD, FD_CLOEXEC) < 0) ||
|
|
|
|
(fcntl(p[1], F_SETFD, FD_CLOEXEC) < 0))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/* Replace the stdout pipe with a file if requested. In this case
|
|
|
|
the select call will report that stdout is closed immediately. */
|
2005-02-28 01:36:00 +03:00
|
|
|
if(prIndex == cp->NumberOfCommands-1 && cp->PipeFileSTDOUT)
|
2003-12-13 22:13:54 +03:00
|
|
|
{
|
|
|
|
if(!kwsysProcessSetupOutputPipeFile(&si->StdOut, cp->PipeFileSTDOUT))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-17 01:20:01 +03:00
|
|
|
/* Replace the stdout pipe with the parent's if requested. In this
|
|
|
|
case the select call will report that stderr is closed
|
|
|
|
immediately. */
|
2005-02-28 01:36:00 +03:00
|
|
|
if(prIndex == cp->NumberOfCommands-1 && cp->PipeSharedSTDOUT)
|
2003-12-17 01:20:01 +03:00
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&si->StdOut);
|
|
|
|
si->StdOut = 1;
|
|
|
|
}
|
|
|
|
|
2006-10-03 17:10:03 +04:00
|
|
|
/* Replace the stdout pipe with the native pipe provided if any. In
|
|
|
|
this case the select call will report that stdout is closed
|
|
|
|
immediately. */
|
|
|
|
if(prIndex == cp->NumberOfCommands-1 && cp->PipeNativeSTDOUT[1] >= 0)
|
|
|
|
{
|
|
|
|
if(!kwsysProcessSetupOutputPipeNative(&si->StdOut, cp->PipeNativeSTDOUT))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Create the error reporting pipe. */
|
2003-12-03 21:37:21 +03:00
|
|
|
if(pipe(si->ErrorPipe) < 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set close-on-exec flag on the error pipe's write end. */
|
2003-12-03 21:37:21 +03:00
|
|
|
if(fcntl(si->ErrorPipe[1], F_SETFD, FD_CLOEXEC) < 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Fork off a child process. */
|
2005-02-28 01:36:00 +03:00
|
|
|
cp->ForkPIDs[prIndex] = kwsysProcessFork(cp, si);
|
|
|
|
if(cp->ForkPIDs[prIndex] < 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2005-02-28 01:36:00 +03:00
|
|
|
if(cp->ForkPIDs[prIndex] == 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
/* Close the read end of the error reporting pipe. */
|
2003-12-03 21:37:21 +03:00
|
|
|
close(si->ErrorPipe[0]);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Setup the stdin, stdout, and stderr pipes. */
|
2003-12-17 01:20:01 +03:00
|
|
|
if(si->StdIn > 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
dup2(si->StdIn, 0);
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
2003-12-17 01:20:01 +03:00
|
|
|
else if(si->StdIn < 0)
|
|
|
|
{
|
|
|
|
close(0);
|
|
|
|
}
|
|
|
|
if(si->StdOut != 1)
|
|
|
|
{
|
|
|
|
dup2(si->StdOut, 1);
|
|
|
|
}
|
|
|
|
if(si->StdErr != 2)
|
|
|
|
{
|
|
|
|
dup2(si->StdErr, 2);
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Clear the close-on-exec flag for stdin, stdout, and stderr.
|
2006-05-23 19:48:15 +04:00
|
|
|
All other pipe handles will be closed when exec succeeds. */
|
2003-12-03 17:20:05 +03:00
|
|
|
fcntl(0, F_SETFD, 0);
|
|
|
|
fcntl(1, F_SETFD, 0);
|
|
|
|
fcntl(2, F_SETFD, 0);
|
|
|
|
|
|
|
|
/* Restore all default signal handlers. */
|
|
|
|
kwsysProcessRestoreDefaultSignalHandlers();
|
|
|
|
|
|
|
|
/* Execute the real process. If successful, this does not return. */
|
2005-02-28 01:36:00 +03:00
|
|
|
execvp(cp->Commands[prIndex][0], cp->Commands[prIndex]);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Failure. Report error to parent and terminate. */
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessChildErrorExit(si->ErrorPipe[1]);
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/* A child has been created. */
|
|
|
|
++cp->CommandsLeft;
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* We are done with the error reporting pipe write end. */
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si->ErrorPipe[1]);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Block until the child's exec call succeeds and closes the error
|
|
|
|
pipe or writes data to the pipe to report an error. */
|
|
|
|
{
|
2006-10-05 01:27:17 +04:00
|
|
|
kwsysProcess_ssize_t total = 0;
|
|
|
|
kwsysProcess_ssize_t n = 1;
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Read the entire error message up to the length of our buffer. */
|
|
|
|
while(total < KWSYSPE_PIPE_BUFFER_SIZE && n > 0)
|
|
|
|
{
|
|
|
|
/* Keep trying to read until the operation is not interrupted. */
|
2003-12-03 21:37:21 +03:00
|
|
|
while(((n = read(si->ErrorPipe[0], cp->ErrorMessage+total,
|
2008-12-16 01:19:26 +03:00
|
|
|
(size_t)(KWSYSPE_PIPE_BUFFER_SIZE-total))) < 0) &&
|
2003-12-03 17:20:05 +03:00
|
|
|
(errno == EINTR));
|
|
|
|
if(n > 0)
|
|
|
|
{
|
|
|
|
total += n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We are done with the error reporting pipe read end. */
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si->ErrorPipe[0]);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
if(total > 0)
|
|
|
|
{
|
|
|
|
/* The child failed to execute the process. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Successfully created this child process. */
|
2005-02-28 01:36:00 +03:00
|
|
|
if(prIndex > 0 || si->StdIn > 0)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
/* The parent process does not need the input pipe read end. */
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si->StdIn);
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* The parent process does not need the output pipe write ends. */
|
2005-04-22 00:05:09 +04:00
|
|
|
if(si->StdOut != 1)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanupDescriptor(&si->StdOut);
|
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void kwsysProcessDestroy(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
/* A child process has terminated. Reap it if it is one handled by
|
|
|
|
this object. */
|
|
|
|
int i;
|
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
|
|
|
{
|
|
|
|
if(cp->ForkPIDs[i])
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
while(((result = waitpid(cp->ForkPIDs[i],
|
|
|
|
&cp->CommandExitCodes[i], WNOHANG)) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
if(result > 0)
|
|
|
|
{
|
|
|
|
/* This child has termianted. */
|
|
|
|
cp->ForkPIDs[i] = 0;
|
|
|
|
if(--cp->CommandsLeft == 0)
|
|
|
|
{
|
|
|
|
/* All children have terminated. Close the signal pipe
|
|
|
|
write end so that no more notifications are sent to this
|
|
|
|
object. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->SignalPipe);
|
|
|
|
|
|
|
|
/* TODO: Once the children have terminated, switch
|
|
|
|
WaitForData to use a non-blocking read to get the
|
|
|
|
rest of the data from the pipe. This is needed when
|
|
|
|
grandchildren keep the output pipes open. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(result < 0 && cp->State != kwsysProcess_State_Error)
|
|
|
|
{
|
|
|
|
/* Unexpected error. Report the first time this happens. */
|
|
|
|
strncpy(cp->ErrorMessage, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE);
|
|
|
|
cp->State = kwsysProcess_State_Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-13 22:13:54 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static int kwsysProcessSetupOutputPipeFile(int* p, const char* name)
|
|
|
|
{
|
|
|
|
int fout;
|
|
|
|
if(!name)
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the existing descriptor. */
|
|
|
|
kwsysProcessCleanupDescriptor(p);
|
|
|
|
|
|
|
|
/* Open a file for the pipe to write (permissions 644). */
|
|
|
|
if((fout = open(name, O_WRONLY | O_CREAT | O_TRUNC,
|
|
|
|
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH)) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Set close-on-exec flag on the pipe's end. */
|
|
|
|
if(fcntl(fout, F_SETFD, FD_CLOEXEC) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign the replacement descriptor. */
|
|
|
|
*p = fout;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2006-10-03 17:10:03 +04:00
|
|
|
static int kwsysProcessSetupOutputPipeNative(int* p, int des[2])
|
|
|
|
{
|
|
|
|
/* Close the existing descriptor. */
|
|
|
|
kwsysProcessCleanupDescriptor(p);
|
|
|
|
|
|
|
|
/* Set close-on-exec flag on the pipe's ends. The proper end will
|
|
|
|
be dup2-ed into the standard descriptor number after fork but
|
|
|
|
before exec. */
|
|
|
|
if((fcntl(des[0], F_SETFD, FD_CLOEXEC) < 0) ||
|
|
|
|
(fcntl(des[1], F_SETFD, FD_CLOEXEC) < 0))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Assign the replacement descriptor. */
|
|
|
|
*p = des[1];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Get the time at which either the process or user timeout will
|
|
|
|
expire. Returns 1 if the user timeout is first, and 0 otherwise. */
|
2003-06-11 00:55:50 +04:00
|
|
|
static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
|
|
|
|
kwsysProcessTime* timeoutTime)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* The first time this is called, we need to calculate the time at
|
|
|
|
which the child will timeout. */
|
|
|
|
if(cp->Timeout && cp->TimeoutTime.tv_sec < 0)
|
|
|
|
{
|
|
|
|
kwsysProcessTime length = kwsysProcessTimeFromDouble(cp->Timeout);
|
|
|
|
cp->TimeoutTime = kwsysProcessTimeAdd(cp->StartTime, length);
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Start with process timeout. */
|
|
|
|
*timeoutTime = cp->TimeoutTime;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Check if the user timeout is earlier. */
|
|
|
|
if(userTimeout)
|
|
|
|
{
|
|
|
|
kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent();
|
|
|
|
kwsysProcessTime userTimeoutLength = kwsysProcessTimeFromDouble(*userTimeout);
|
|
|
|
kwsysProcessTime userTimeoutTime = kwsysProcessTimeAdd(currentTime,
|
2005-06-17 21:57:27 +04:00
|
|
|
userTimeoutLength);
|
|
|
|
if(timeoutTime->tv_sec < 0 ||
|
|
|
|
kwsysProcessTimeLess(userTimeoutTime, *timeoutTime))
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
*timeoutTime = userTimeoutTime;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Get the length of time before the given timeout time arrives.
|
|
|
|
Returns 1 if the time has already arrived, and 0 otherwise. */
|
2003-06-11 00:55:50 +04:00
|
|
|
static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
|
2005-06-17 21:57:27 +04:00
|
|
|
double* userTimeout,
|
2005-05-11 00:36:49 +04:00
|
|
|
kwsysProcessTimeNative* timeoutLength)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
if(timeoutTime->tv_sec < 0)
|
|
|
|
{
|
|
|
|
/* No timeout time has been requested. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Calculate the remaining time. */
|
|
|
|
kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent();
|
2005-05-11 00:36:49 +04:00
|
|
|
kwsysProcessTime timeLeft = kwsysProcessTimeSubtract(*timeoutTime,
|
|
|
|
currentTime);
|
2005-06-17 21:57:27 +04:00
|
|
|
if(timeLeft.tv_sec < 0 && userTimeout && *userTimeout <= 0)
|
|
|
|
{
|
|
|
|
/* Caller has explicitly requested a zero timeout. */
|
|
|
|
timeLeft.tv_sec = 0;
|
|
|
|
timeLeft.tv_usec = 0;
|
|
|
|
}
|
|
|
|
|
2005-05-11 00:36:49 +04:00
|
|
|
if(timeLeft.tv_sec < 0)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* Timeout has already expired. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There is some time left. */
|
2005-05-11 00:36:49 +04:00
|
|
|
timeoutLength->tv_sec = timeLeft.tv_sec;
|
|
|
|
timeoutLength->tv_usec = timeLeft.tv_usec;
|
2003-06-10 23:46:31 +04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2004-01-22 22:44:44 +03:00
|
|
|
static kwsysProcessTime kwsysProcessTimeGetCurrent(void)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
kwsysProcessTime current;
|
2005-05-11 00:36:49 +04:00
|
|
|
kwsysProcessTimeNative current_native;
|
|
|
|
gettimeofday(¤t_native, 0);
|
|
|
|
current.tv_sec = (long)current_native.tv_sec;
|
|
|
|
current.tv_usec = (long)current_native.tv_usec;
|
2003-06-10 23:46:31 +04:00
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static double kwsysProcessTimeToDouble(kwsysProcessTime t)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2009-03-16 22:02:25 +03:00
|
|
|
return (double)t.tv_sec + (double)(t.tv_usec)*0.000001;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static kwsysProcessTime kwsysProcessTimeFromDouble(double d)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
kwsysProcessTime t;
|
|
|
|
t.tv_sec = (long)d;
|
2009-03-16 22:02:25 +03:00
|
|
|
t.tv_usec = (long)((d-(double)(t.tv_sec))*1000000);
|
2003-06-10 23:46:31 +04:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static int kwsysProcessTimeLess(kwsysProcessTime in1, kwsysProcessTime in2)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return ((in1.tv_sec < in2.tv_sec) ||
|
|
|
|
((in1.tv_sec == in2.tv_sec) && (in1.tv_usec < in2.tv_usec)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static kwsysProcessTime kwsysProcessTimeAdd(kwsysProcessTime in1, kwsysProcessTime in2)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
kwsysProcessTime out;
|
|
|
|
out.tv_sec = in1.tv_sec + in2.tv_sec;
|
|
|
|
out.tv_usec = in1.tv_usec + in2.tv_usec;
|
|
|
|
if(out.tv_usec > 1000000)
|
|
|
|
{
|
|
|
|
out.tv_usec -= 1000000;
|
|
|
|
out.tv_sec += 1;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static kwsysProcessTime kwsysProcessTimeSubtract(kwsysProcessTime in1, kwsysProcessTime in2)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
kwsysProcessTime out;
|
|
|
|
out.tv_sec = in1.tv_sec - in2.tv_sec;
|
|
|
|
out.tv_usec = in1.tv_usec - in2.tv_usec;
|
|
|
|
if(out.tv_usec < 0)
|
|
|
|
{
|
|
|
|
out.tv_usec += 1000000;
|
|
|
|
out.tv_sec -= 1;
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2003-12-31 00:23:16 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
#define KWSYSPE_CASE(type, str) \
|
|
|
|
cp->ExitException = kwsysProcess_Exception_##type; \
|
|
|
|
strcpy(cp->ExitExceptionString, str)
|
|
|
|
static void kwsysProcessSetExitException(kwsysProcess* cp, int sig)
|
|
|
|
{
|
|
|
|
switch (sig)
|
|
|
|
{
|
|
|
|
#ifdef SIGSEGV
|
|
|
|
case SIGSEGV: KWSYSPE_CASE(Fault, "Segmentation fault"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
2004-03-12 22:43:32 +03:00
|
|
|
# if !defined(SIGSEGV) || SIGBUS != SIGSEGV
|
2003-12-31 00:23:16 +03:00
|
|
|
case SIGBUS: KWSYSPE_CASE(Fault, "Bus error"); break;
|
2004-03-12 22:43:32 +03:00
|
|
|
# endif
|
2003-12-31 00:23:16 +03:00
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
|
|
|
case SIGFPE: KWSYSPE_CASE(Numerical, "Floating-point exception"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
|
|
|
case SIGILL: KWSYSPE_CASE(Illegal, "Illegal instruction"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
case SIGINT: KWSYSPE_CASE(Interrupt, "User interrupt"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
|
|
|
case SIGABRT: KWSYSPE_CASE(Other, "Child aborted"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGKILL
|
|
|
|
case SIGKILL: KWSYSPE_CASE(Other, "Child killed"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
case SIGTERM: KWSYSPE_CASE(Other, "Child terminated"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGHUP
|
|
|
|
case SIGHUP: KWSYSPE_CASE(Other, "SIGHUP"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
case SIGQUIT: KWSYSPE_CASE(Other, "SIGQUIT"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
case SIGTRAP: KWSYSPE_CASE(Other, "SIGTRAP"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
|
|
|
# if !defined(SIGABRT) || SIGIOT != SIGABRT
|
|
|
|
case SIGIOT: KWSYSPE_CASE(Other, "SIGIOT"); break;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
case SIGUSR1: KWSYSPE_CASE(Other, "SIGUSR1"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
case SIGUSR2: KWSYSPE_CASE(Other, "SIGUSR2"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
case SIGPIPE: KWSYSPE_CASE(Other, "SIGPIPE"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
case SIGALRM: KWSYSPE_CASE(Other, "SIGALRM"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
case SIGSTKFLT: KWSYSPE_CASE(Other, "SIGSTKFLT"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
case SIGCHLD: KWSYSPE_CASE(Other, "SIGCHLD"); break;
|
|
|
|
#elif defined(SIGCLD)
|
|
|
|
case SIGCLD: KWSYSPE_CASE(Other, "SIGCLD"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
|
|
|
case SIGCONT: KWSYSPE_CASE(Other, "SIGCONT"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTOP
|
|
|
|
case SIGSTOP: KWSYSPE_CASE(Other, "SIGSTOP"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
case SIGTSTP: KWSYSPE_CASE(Other, "SIGTSTP"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
|
|
case SIGTTIN: KWSYSPE_CASE(Other, "SIGTTIN"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
|
|
|
case SIGTTOU: KWSYSPE_CASE(Other, "SIGTTOU"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
|
|
|
case SIGURG: KWSYSPE_CASE(Other, "SIGURG"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
case SIGXCPU: KWSYSPE_CASE(Other, "SIGXCPU"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
case SIGXFSZ: KWSYSPE_CASE(Other, "SIGXFSZ"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
|
|
|
case SIGVTALRM: KWSYSPE_CASE(Other, "SIGVTALRM"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
|
|
case SIGPROF: KWSYSPE_CASE(Other, "SIGPROF"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
case SIGWINCH: KWSYSPE_CASE(Other, "SIGWINCH"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
case SIGPOLL: KWSYSPE_CASE(Other, "SIGPOLL"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
|
|
|
# if !defined(SIGPOLL) || SIGIO != SIGPOLL
|
|
|
|
case SIGIO: KWSYSPE_CASE(Other, "SIGIO"); break;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
case SIGPWR: KWSYSPE_CASE(Other, "SIGPWR"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
|
|
|
case SIGSYS: KWSYSPE_CASE(Other, "SIGSYS"); break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUNUSED
|
|
|
|
# if !defined(SIGSYS) || SIGUNUSED != SIGSYS
|
|
|
|
case SIGUNUSED: KWSYSPE_CASE(Other, "SIGUNUSED"); break;
|
|
|
|
# endif
|
|
|
|
#endif
|
|
|
|
default:
|
|
|
|
cp->ExitException = kwsysProcess_Exception_Other;
|
|
|
|
sprintf(cp->ExitExceptionString, "Signal %d", sig);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#undef KWSYSPE_CASE
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* When the child process encounters an error before its program is
|
|
|
|
invoked, this is called to report the error to the parent and
|
|
|
|
exit. */
|
2003-12-03 17:20:05 +03:00
|
|
|
static void kwsysProcessChildErrorExit(int errorPipe)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* Construct the error message. */
|
|
|
|
char buffer[KWSYSPE_PIPE_BUFFER_SIZE];
|
2003-07-10 00:18:14 +04:00
|
|
|
strncpy(buffer, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE);
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Report the error to the parent through the special pipe. */
|
2009-03-17 00:13:14 +03:00
|
|
|
kwsysProcess_ssize_t result=write(errorPipe, buffer, strlen(buffer));
|
|
|
|
(void)result;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Terminate without cleanup. */
|
|
|
|
_exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Restores all signal handlers to their default values. */
|
2004-01-22 22:44:44 +03:00
|
|
|
static void kwsysProcessRestoreDefaultSignalHandlers(void)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
struct sigaction act;
|
2003-08-05 19:34:39 +04:00
|
|
|
memset(&act, 0, sizeof(struct sigaction));
|
2003-06-10 23:46:31 +04:00
|
|
|
act.sa_handler = SIG_DFL;
|
|
|
|
#ifdef SIGHUP
|
|
|
|
sigaction(SIGHUP, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
sigaction(SIGINT, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGQUIT
|
|
|
|
sigaction(SIGQUIT, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
|
|
|
sigaction(SIGILL, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTRAP
|
|
|
|
sigaction(SIGTRAP, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGABRT
|
|
|
|
sigaction(SIGABRT, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIOT
|
|
|
|
sigaction(SIGIOT, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGBUS
|
|
|
|
sigaction(SIGBUS, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGFPE
|
|
|
|
sigaction(SIGFPE, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR1
|
|
|
|
sigaction(SIGUSR1, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSEGV
|
|
|
|
sigaction(SIGSEGV, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUSR2
|
|
|
|
sigaction(SIGUSR2, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPIPE
|
|
|
|
sigaction(SIGPIPE, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGALRM
|
|
|
|
sigaction(SIGALRM, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTERM
|
|
|
|
sigaction(SIGTERM, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSTKFLT
|
|
|
|
sigaction(SIGSTKFLT, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCLD
|
|
|
|
sigaction(SIGCLD, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCHLD
|
|
|
|
sigaction(SIGCHLD, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGCONT
|
|
|
|
sigaction(SIGCONT, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTSTP
|
|
|
|
sigaction(SIGTSTP, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTIN
|
|
|
|
sigaction(SIGTTIN, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGTTOU
|
|
|
|
sigaction(SIGTTOU, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGURG
|
|
|
|
sigaction(SIGURG, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXCPU
|
|
|
|
sigaction(SIGXCPU, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGXFSZ
|
|
|
|
sigaction(SIGXFSZ, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGVTALRM
|
|
|
|
sigaction(SIGVTALRM, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPROF
|
|
|
|
sigaction(SIGPROF, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGWINCH
|
|
|
|
sigaction(SIGWINCH, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPOLL
|
|
|
|
sigaction(SIGPOLL, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGIO
|
|
|
|
sigaction(SIGIO, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGPWR
|
|
|
|
sigaction(SIGPWR, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGSYS
|
|
|
|
sigaction(SIGSYS, &act, 0);
|
|
|
|
#endif
|
|
|
|
#ifdef SIGUNUSED
|
|
|
|
sigaction(SIGUNUSED, &act, 0);
|
|
|
|
#endif
|
|
|
|
}
|
2004-07-03 00:39:29 +04:00
|
|
|
|
2007-05-03 21:03:13 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void kwsysProcessExit(void)
|
|
|
|
{
|
|
|
|
_exit(0);
|
|
|
|
}
|
|
|
|
|
2004-07-08 01:27:50 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static pid_t kwsysProcessFork(kwsysProcess* cp,
|
|
|
|
kwsysProcessCreateInformation* si)
|
|
|
|
{
|
|
|
|
/* Create a detached process if requested. */
|
|
|
|
if(cp->OptionDetach)
|
|
|
|
{
|
|
|
|
/* Create an intermediate process. */
|
|
|
|
pid_t middle_pid = fork();
|
|
|
|
if(middle_pid < 0)
|
|
|
|
{
|
|
|
|
/* Fork failed. Return as if we were not detaching. */
|
|
|
|
return middle_pid;
|
|
|
|
}
|
|
|
|
else if(middle_pid == 0)
|
|
|
|
{
|
|
|
|
/* This is the intermediate process. Create the real child. */
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
if(child_pid == 0)
|
|
|
|
{
|
|
|
|
/* This is the real child process. There is nothing to do here. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Use the error pipe to report the pid to the real parent. */
|
|
|
|
while((write(si->ErrorPipe[1], &child_pid, sizeof(child_pid)) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
|
|
|
|
/* Exit without cleanup. The parent holds all resources. */
|
2007-05-03 21:03:13 +04:00
|
|
|
kwsysProcessExit();
|
2004-07-09 19:49:04 +04:00
|
|
|
return 0; /* Never reached, but avoids SunCC warning. */
|
2004-07-08 01:27:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This is the original parent process. The intermediate
|
|
|
|
process will use the error pipe to report the pid of the
|
|
|
|
detached child. */
|
|
|
|
pid_t child_pid;
|
|
|
|
int status;
|
|
|
|
while((read(si->ErrorPipe[0], &child_pid, sizeof(child_pid)) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
|
|
|
|
/* Wait for the intermediate process to exit and clean it up. */
|
|
|
|
while((waitpid(middle_pid, &status, 0) < 0) && (errno == EINTR));
|
|
|
|
return child_pid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Not creating a detached process. Use normal fork. */
|
|
|
|
return fork();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-03-08 19:38:51 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2006-03-08 20:36:14 +03:00
|
|
|
/* We try to obtain process information by invoking the ps command.
|
|
|
|
Here we define the command to call on each platform and the
|
|
|
|
corresponding parsing format string. The parsing format should
|
|
|
|
have two integers to store: the pid and then the ppid. */
|
|
|
|
#if defined(__linux__) || defined(__APPLE__) || defined(__FreeBSD__)
|
2006-03-08 19:38:51 +03:00
|
|
|
# define KWSYSPE_PS_COMMAND "ps axo pid,ppid"
|
|
|
|
# define KWSYSPE_PS_FORMAT "%d %d\n"
|
2006-03-08 21:06:46 +03:00
|
|
|
#elif defined(__hpux) || defined(__sparc) || defined(__sgi) || defined(_AIX)
|
2006-03-08 19:38:51 +03:00
|
|
|
# define KWSYSPE_PS_COMMAND "ps -ef"
|
2006-03-08 20:12:37 +03:00
|
|
|
# define KWSYSPE_PS_FORMAT "%*s %d %d %*[^\n]\n"
|
2006-05-21 02:50:27 +04:00
|
|
|
#elif defined(__CYGWIN__)
|
|
|
|
# define KWSYSPE_PS_COMMAND "ps aux"
|
|
|
|
# define KWSYSPE_PS_FORMAT "%d %d %*[^\n]\n"
|
2006-03-08 19:38:51 +03:00
|
|
|
#endif
|
|
|
|
|
2004-07-03 00:39:29 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void kwsysProcessKill(pid_t process_id)
|
|
|
|
{
|
2006-05-21 02:50:27 +04:00
|
|
|
#if defined(__linux__) || defined(__CYGWIN__)
|
2004-07-03 00:39:29 +04:00
|
|
|
DIR* procdir;
|
2006-03-08 20:42:59 +03:00
|
|
|
#endif
|
2004-07-03 00:39:29 +04:00
|
|
|
|
2007-05-16 21:10:45 +04:00
|
|
|
/* Kill the process now to make sure it does not create more
|
|
|
|
children. Do not reap it yet so we can identify its existing
|
|
|
|
children. There is a small race condition here. If the child
|
|
|
|
forks after we begin looking for children below but before it
|
|
|
|
receives this kill signal we might miss a child. Also we might
|
|
|
|
not be able to catch up to a fork bomb. */
|
|
|
|
kill(process_id, SIGKILL);
|
2004-07-03 00:39:29 +04:00
|
|
|
|
2006-03-08 20:36:14 +03:00
|
|
|
/* Kill all children if we can find them. */
|
2006-05-21 02:50:27 +04:00
|
|
|
#if defined(__linux__) || defined(__CYGWIN__)
|
2006-03-08 20:36:14 +03:00
|
|
|
/* First try using the /proc filesystem. */
|
2004-07-03 00:39:29 +04:00
|
|
|
if((procdir = opendir("/proc")) != NULL)
|
|
|
|
{
|
|
|
|
#if defined(MAXPATHLEN)
|
|
|
|
char fname[MAXPATHLEN];
|
|
|
|
#elif defined(PATH_MAX)
|
|
|
|
char fname[PATH_MAX];
|
|
|
|
#else
|
|
|
|
char fname[4096];
|
|
|
|
#endif
|
2004-07-07 21:03:56 +04:00
|
|
|
char buffer[KWSYSPE_PIPE_BUFFER_SIZE+1];
|
2004-07-03 00:39:29 +04:00
|
|
|
struct dirent* d;
|
|
|
|
|
|
|
|
/* Each process has a directory in /proc whose name is the pid.
|
|
|
|
Within this directory is a file called stat that has the
|
|
|
|
following format:
|
|
|
|
|
|
|
|
pid (command line) status ppid ...
|
|
|
|
|
|
|
|
We want to get the ppid for all processes. Those that have
|
|
|
|
process_id as their parent should be recursively killed. */
|
|
|
|
for(d = readdir(procdir); d; d = readdir(procdir))
|
|
|
|
{
|
|
|
|
int pid;
|
|
|
|
if(sscanf(d->d_name, "%d", &pid) == 1 && pid != 0)
|
|
|
|
{
|
|
|
|
struct stat finfo;
|
|
|
|
sprintf(fname, "/proc/%d/stat", pid);
|
|
|
|
if(stat(fname, &finfo) == 0)
|
|
|
|
{
|
|
|
|
FILE* f = fopen(fname, "r");
|
|
|
|
if(f)
|
|
|
|
{
|
2009-03-16 22:02:25 +03:00
|
|
|
size_t nread = fread(buffer, 1, KWSYSPE_PIPE_BUFFER_SIZE, f);
|
2004-07-03 00:39:29 +04:00
|
|
|
buffer[nread] = '\0';
|
|
|
|
if(nread > 0)
|
|
|
|
{
|
|
|
|
const char* rparen = strrchr(buffer, ')');
|
|
|
|
int ppid;
|
|
|
|
if(rparen && (sscanf(rparen+1, "%*s %d", &ppid) == 1))
|
|
|
|
{
|
|
|
|
if(ppid == process_id)
|
|
|
|
{
|
|
|
|
/* Recursively kill this child and its children. */
|
|
|
|
kwsysProcessKill(pid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(procdir);
|
|
|
|
}
|
2006-03-08 19:38:51 +03:00
|
|
|
else
|
2006-03-08 20:36:14 +03:00
|
|
|
#endif
|
2006-03-08 19:38:51 +03:00
|
|
|
{
|
2006-05-21 02:50:27 +04:00
|
|
|
#if defined(KWSYSPE_PS_COMMAND)
|
2006-03-08 19:38:51 +03:00
|
|
|
/* Try running "ps" to get the process information. */
|
|
|
|
FILE* ps = popen(KWSYSPE_PS_COMMAND, "r");
|
|
|
|
|
|
|
|
/* Make sure the process started and provided a valid header. */
|
2006-03-08 20:12:37 +03:00
|
|
|
if(ps && fscanf(ps, "%*[^\n]\n") != EOF)
|
2006-03-08 19:38:51 +03:00
|
|
|
{
|
|
|
|
/* Look for processes whose parent is the process being killed. */
|
|
|
|
int pid, ppid;
|
|
|
|
while(fscanf(ps, KWSYSPE_PS_FORMAT, &pid, &ppid) == 2)
|
|
|
|
{
|
|
|
|
if(ppid == process_id)
|
|
|
|
{
|
2008-04-02 16:36:51 +04:00
|
|
|
/* Recursively kill this child and its children. */
|
2006-03-08 19:38:51 +03:00
|
|
|
kwsysProcessKill(pid);
|
|
|
|
}
|
|
|
|
}
|
2006-03-08 19:57:41 +03:00
|
|
|
}
|
2006-03-08 19:38:51 +03:00
|
|
|
|
2006-03-08 19:57:41 +03:00
|
|
|
/* We are done with the ps process. */
|
|
|
|
if(ps)
|
|
|
|
{
|
2006-03-08 19:38:51 +03:00
|
|
|
pclose(ps);
|
|
|
|
}
|
|
|
|
#endif
|
2006-05-21 02:50:27 +04:00
|
|
|
}
|
2004-07-03 00:39:29 +04:00
|
|
|
}
|
2006-05-23 19:48:15 +04:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* Global set of executing processes for use by the signal handler.
|
|
|
|
This global instance will be zero-initialized by the compiler. */
|
|
|
|
typedef struct kwsysProcessInstances_s
|
|
|
|
{
|
|
|
|
int Count;
|
|
|
|
int Size;
|
|
|
|
kwsysProcess** Processes;
|
|
|
|
} kwsysProcessInstances;
|
|
|
|
static kwsysProcessInstances kwsysProcesses;
|
|
|
|
|
|
|
|
/* The old SIGCHLD handler. */
|
|
|
|
static struct sigaction kwsysProcessesOldSigChldAction;
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void kwsysProcessesUpdate(kwsysProcessInstances* newProcesses)
|
|
|
|
{
|
|
|
|
/* Block SIGCHLD while we update the set of pipes to check.
|
|
|
|
TODO: sigprocmask is undefined for threaded apps. See
|
|
|
|
pthread_sigmask. */
|
|
|
|
sigset_t newset;
|
|
|
|
sigset_t oldset;
|
|
|
|
sigemptyset(&newset);
|
|
|
|
sigaddset(&newset, SIGCHLD);
|
|
|
|
sigprocmask(SIG_BLOCK, &newset, &oldset);
|
|
|
|
|
|
|
|
/* Store the new set in that seen by the signal handler. */
|
|
|
|
kwsysProcesses = *newProcesses;
|
|
|
|
|
|
|
|
/* Restore the signal mask to the previous setting. */
|
|
|
|
sigprocmask(SIG_SETMASK, &oldset, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static int kwsysProcessesAdd(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
/* Create a pipe through which the signal handler can notify the
|
|
|
|
given process object that a child has exited. */
|
|
|
|
{
|
|
|
|
/* Create the pipe. */
|
|
|
|
int p[2];
|
|
|
|
if(pipe(p) < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store the pipes now to be sure they are cleaned up later. */
|
|
|
|
cp->PipeReadEnds[KWSYSPE_PIPE_SIGNAL] = p[0];
|
|
|
|
cp->SignalPipe = p[1];
|
|
|
|
|
|
|
|
/* Switch the pipe to non-blocking mode so that reading a byte can
|
|
|
|
be an atomic test-and-set. */
|
2006-12-04 22:42:47 +03:00
|
|
|
if(!kwsysProcessSetNonBlocking(p[0]) ||
|
|
|
|
!kwsysProcessSetNonBlocking(p[1]))
|
2006-05-23 19:48:15 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* The children do not need this pipe. Set close-on-exec flag on
|
|
|
|
the pipe's ends. */
|
|
|
|
if((fcntl(p[0], F_SETFD, FD_CLOEXEC) < 0) ||
|
|
|
|
(fcntl(p[1], F_SETFD, FD_CLOEXEC) < 0))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Attempt to add the given signal pipe to the signal handler set. */
|
|
|
|
{
|
|
|
|
|
|
|
|
/* Make sure there is enough space for the new signal pipe. */
|
|
|
|
kwsysProcessInstances oldProcesses = kwsysProcesses;
|
|
|
|
kwsysProcessInstances newProcesses = oldProcesses;
|
|
|
|
if(oldProcesses.Count == oldProcesses.Size)
|
|
|
|
{
|
|
|
|
/* Start with enough space for a small number of process instances
|
|
|
|
and double the size each time more is needed. */
|
|
|
|
newProcesses.Size = oldProcesses.Size? oldProcesses.Size*2 : 4;
|
|
|
|
|
|
|
|
/* Try allocating the new block of memory. */
|
|
|
|
if((newProcesses.Processes = ((kwsysProcess**)
|
2009-03-16 22:02:25 +03:00
|
|
|
malloc((size_t)(newProcesses.Size)*
|
2006-05-23 19:48:15 +04:00
|
|
|
sizeof(kwsysProcess*)))))
|
|
|
|
{
|
|
|
|
/* Copy the old pipe set to the new memory. */
|
|
|
|
if(oldProcesses.Count > 0)
|
|
|
|
{
|
|
|
|
memcpy(newProcesses.Processes, oldProcesses.Processes,
|
2009-03-16 22:02:25 +03:00
|
|
|
((size_t)(oldProcesses.Count) * sizeof(kwsysProcess*)));
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Failed to allocate memory for the new signal pipe set. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Append the new signal pipe to the set. */
|
|
|
|
newProcesses.Processes[newProcesses.Count++] = cp;
|
|
|
|
|
|
|
|
/* Store the new set in that seen by the signal handler. */
|
|
|
|
kwsysProcessesUpdate(&newProcesses);
|
|
|
|
|
|
|
|
/* Free the original pipes if new ones were allocated. */
|
|
|
|
if(newProcesses.Processes != oldProcesses.Processes)
|
|
|
|
{
|
|
|
|
free(oldProcesses.Processes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this is the first process, enable the signal handler. */
|
|
|
|
if(newProcesses.Count == 1)
|
|
|
|
{
|
|
|
|
/* Install our handler for SIGCHLD. Repeat call until it is not
|
|
|
|
interrupted. */
|
|
|
|
struct sigaction newSigChldAction;
|
|
|
|
memset(&newSigChldAction, 0, sizeof(struct sigaction));
|
2006-12-04 22:42:47 +03:00
|
|
|
#if KWSYSPE_USE_SIGINFO
|
2006-05-23 19:48:15 +04:00
|
|
|
newSigChldAction.sa_sigaction = kwsysProcessesSignalHandler;
|
2006-08-01 22:28:03 +04:00
|
|
|
newSigChldAction.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
|
2006-12-04 22:42:47 +03:00
|
|
|
# ifdef SA_RESTART
|
2006-08-01 22:28:03 +04:00
|
|
|
newSigChldAction.sa_flags |= SA_RESTART;
|
2006-12-04 22:42:47 +03:00
|
|
|
# endif
|
|
|
|
#else
|
|
|
|
newSigChldAction.sa_handler = kwsysProcessesSignalHandler;
|
|
|
|
newSigChldAction.sa_flags = SA_NOCLDSTOP;
|
2006-08-01 22:28:03 +04:00
|
|
|
#endif
|
2006-05-23 19:48:15 +04:00
|
|
|
while((sigaction(SIGCHLD, &newSigChldAction,
|
|
|
|
&kwsysProcessesOldSigChldAction) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static void kwsysProcessesRemove(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
/* Attempt to remove the given signal pipe from the signal handler set. */
|
|
|
|
{
|
|
|
|
/* Find the given process in the set. */
|
|
|
|
kwsysProcessInstances newProcesses = kwsysProcesses;
|
|
|
|
int i;
|
|
|
|
for(i=0; i < newProcesses.Count; ++i)
|
|
|
|
{
|
|
|
|
if(newProcesses.Processes[i] == cp)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(i < newProcesses.Count)
|
|
|
|
{
|
|
|
|
/* Remove the process from the set. */
|
|
|
|
--newProcesses.Count;
|
|
|
|
for(; i < newProcesses.Count; ++i)
|
|
|
|
{
|
|
|
|
newProcesses.Processes[i] = newProcesses.Processes[i+1];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If this was the last process, disable the signal handler. */
|
|
|
|
if(newProcesses.Count == 0)
|
|
|
|
{
|
|
|
|
/* Restore the SIGCHLD handler. Repeat call until it is not
|
|
|
|
interrupted. */
|
|
|
|
while((sigaction(SIGCHLD, &kwsysProcessesOldSigChldAction, 0) < 0) &&
|
|
|
|
(errno == EINTR));
|
2006-05-24 18:13:18 +04:00
|
|
|
|
|
|
|
/* Free the table of process pointers since it is now empty.
|
|
|
|
This is safe because the signal handler has been removed. */
|
|
|
|
newProcesses.Size = 0;
|
|
|
|
free(newProcesses.Processes);
|
|
|
|
newProcesses.Processes = 0;
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
2006-05-24 18:13:18 +04:00
|
|
|
|
|
|
|
/* Store the new set in that seen by the signal handler. */
|
|
|
|
kwsysProcessesUpdate(&newProcesses);
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the pipe through which the signal handler may have notified
|
|
|
|
the given process object that a child has exited. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->SignalPipe);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2006-12-04 22:42:47 +03:00
|
|
|
static void kwsysProcessesSignalHandler(int signum
|
|
|
|
#if KWSYSPE_USE_SIGINFO
|
|
|
|
, siginfo_t* info, void* ucontext
|
|
|
|
#endif
|
|
|
|
)
|
2006-05-23 19:48:15 +04:00
|
|
|
{
|
|
|
|
(void)signum;
|
2006-12-04 22:42:47 +03:00
|
|
|
#if KWSYSPE_USE_SIGINFO
|
2006-05-23 19:48:15 +04:00
|
|
|
(void)info;
|
|
|
|
(void)ucontext;
|
2006-12-04 22:42:47 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Signal all process objects that a child has terminated. */
|
|
|
|
{
|
|
|
|
int i;
|
2006-05-23 19:48:15 +04:00
|
|
|
for(i=0; i < kwsysProcesses.Count; ++i)
|
|
|
|
{
|
|
|
|
/* Set the pipe in a signalled state. */
|
|
|
|
char buf = 1;
|
|
|
|
kwsysProcess* cp = kwsysProcesses.Processes[i];
|
2009-03-17 00:13:14 +03:00
|
|
|
kwsysProcess_ssize_t status=
|
|
|
|
read(cp->PipeReadEnds[KWSYSPE_PIPE_SIGNAL], &buf, 1);
|
|
|
|
status=write(cp->SignalPipe, &buf, 1);
|
|
|
|
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
2006-12-04 22:42:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#if !KWSYSPE_USE_SIGINFO
|
|
|
|
/* Re-Install our handler for SIGCHLD. Repeat call until it is not
|
|
|
|
interrupted. */
|
|
|
|
{
|
|
|
|
struct sigaction newSigChldAction;
|
|
|
|
memset(&newSigChldAction, 0, sizeof(struct sigaction));
|
|
|
|
newSigChldAction.sa_handler = kwsysProcessesSignalHandler;
|
|
|
|
newSigChldAction.sa_flags = SA_NOCLDSTOP;
|
|
|
|
while((sigaction(SIGCHLD, &newSigChldAction,
|
|
|
|
&kwsysProcessesOldSigChldAction) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
}
|
|
|
|
#endif
|
2006-05-23 19:48:15 +04:00
|
|
|
}
|
2006-06-15 22:40:42 +04:00
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static int kwsysProcessAppendByte(char* local,
|
|
|
|
char** begin, char** end,
|
|
|
|
int* size, char c)
|
|
|
|
{
|
|
|
|
/* Allocate space for the character. */
|
|
|
|
if((*end - *begin) >= *size)
|
|
|
|
{
|
2006-10-05 01:27:17 +04:00
|
|
|
kwsysProcess_ptrdiff_t length = *end - *begin;
|
2008-12-16 01:19:26 +03:00
|
|
|
char* newBuffer = (char*)malloc((size_t)(*size*2));
|
2006-06-15 22:40:42 +04:00
|
|
|
if(!newBuffer)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-16 22:02:25 +03:00
|
|
|
memcpy(newBuffer, *begin, (size_t)(length)*sizeof(char));
|
2006-06-15 22:40:42 +04:00
|
|
|
if(*begin != local)
|
|
|
|
{
|
|
|
|
free(*begin);
|
|
|
|
}
|
|
|
|
*begin = newBuffer;
|
|
|
|
*end = *begin + length;
|
|
|
|
*size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store the character. */
|
|
|
|
*(*end)++ = c;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
static int kwsysProcessAppendArgument(char** local,
|
|
|
|
char*** begin, char*** end,
|
|
|
|
int* size,
|
|
|
|
char* arg_local,
|
|
|
|
char** arg_begin, char** arg_end,
|
|
|
|
int* arg_size)
|
|
|
|
{
|
|
|
|
/* Append a null-terminator to the argument string. */
|
|
|
|
if(!kwsysProcessAppendByte(arg_local, arg_begin, arg_end, arg_size, '\0'))
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate space for the argument pointer. */
|
|
|
|
if((*end - *begin) >= *size)
|
|
|
|
{
|
2006-10-05 01:27:17 +04:00
|
|
|
kwsysProcess_ptrdiff_t length = *end - *begin;
|
2009-03-16 22:02:25 +03:00
|
|
|
char** newPointers = (char**)malloc((size_t)(*size)*2*sizeof(char*));
|
2006-06-15 22:40:42 +04:00
|
|
|
if(!newPointers)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
2009-03-16 22:02:25 +03:00
|
|
|
memcpy(newPointers, *begin, (size_t)(length)*sizeof(char*));
|
2006-06-15 22:40:42 +04:00
|
|
|
if(*begin != local)
|
|
|
|
{
|
|
|
|
free(*begin);
|
|
|
|
}
|
|
|
|
*begin = newPointers;
|
|
|
|
*end = *begin + length;
|
|
|
|
*size *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate space for the argument string. */
|
2008-12-16 01:19:26 +03:00
|
|
|
**end = (char*)malloc((size_t)(*arg_end - *arg_begin));
|
2006-06-15 22:40:42 +04:00
|
|
|
if(!**end)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Store the argument in the command array. */
|
2008-12-16 01:19:26 +03:00
|
|
|
memcpy(**end, *arg_begin,(size_t)(*arg_end - *arg_begin));
|
2006-06-15 22:40:42 +04:00
|
|
|
++(*end);
|
|
|
|
|
|
|
|
/* Reset the argument to be empty. */
|
|
|
|
*arg_end = *arg_begin;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
#define KWSYSPE_LOCAL_BYTE_COUNT 1024
|
|
|
|
#define KWSYSPE_LOCAL_ARGS_COUNT 32
|
|
|
|
static char** kwsysProcessParseVerbatimCommand(const char* command)
|
|
|
|
{
|
|
|
|
/* Create a buffer for argument pointers during parsing. */
|
|
|
|
char* local_pointers[KWSYSPE_LOCAL_ARGS_COUNT];
|
|
|
|
int pointers_size = KWSYSPE_LOCAL_ARGS_COUNT;
|
|
|
|
char** pointer_begin = local_pointers;
|
|
|
|
char** pointer_end = pointer_begin;
|
|
|
|
|
|
|
|
/* Create a buffer for argument strings during parsing. */
|
|
|
|
char local_buffer[KWSYSPE_LOCAL_BYTE_COUNT];
|
|
|
|
int buffer_size = KWSYSPE_LOCAL_BYTE_COUNT;
|
|
|
|
char* buffer_begin = local_buffer;
|
|
|
|
char* buffer_end = buffer_begin;
|
|
|
|
|
|
|
|
/* Parse the command string. Try to behave like a UNIX shell. */
|
|
|
|
char** newCommand = 0;
|
|
|
|
const char* c = command;
|
|
|
|
int in_argument = 0;
|
|
|
|
int in_escape = 0;
|
|
|
|
int in_single = 0;
|
|
|
|
int in_double = 0;
|
|
|
|
int failed = 0;
|
|
|
|
for(;*c; ++c)
|
|
|
|
{
|
|
|
|
if(in_escape)
|
|
|
|
{
|
|
|
|
/* This character is escaped so do no special handling. */
|
|
|
|
if(!in_argument)
|
|
|
|
{
|
|
|
|
in_argument = 1;
|
|
|
|
}
|
|
|
|
if(!kwsysProcessAppendByte(local_buffer, &buffer_begin,
|
|
|
|
&buffer_end, &buffer_size, *c))
|
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
in_escape = 0;
|
|
|
|
}
|
|
|
|
else if(*c == '\\' && !in_single)
|
|
|
|
{
|
|
|
|
/* The next character should be escaped. */
|
|
|
|
in_escape = 1;
|
|
|
|
}
|
|
|
|
else if(*c == '\'' && !in_double)
|
|
|
|
{
|
|
|
|
/* Enter or exit single-quote state. */
|
|
|
|
if(in_single)
|
|
|
|
{
|
|
|
|
in_single = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
in_single = 1;
|
|
|
|
if(!in_argument)
|
|
|
|
{
|
|
|
|
in_argument = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(*c == '"' && !in_single)
|
|
|
|
{
|
|
|
|
/* Enter or exit double-quote state. */
|
|
|
|
if(in_double)
|
|
|
|
{
|
|
|
|
in_double = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
in_double = 1;
|
|
|
|
if(!in_argument)
|
|
|
|
{
|
|
|
|
in_argument = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-05-13 13:18:51 +04:00
|
|
|
else if(isspace((unsigned char) *c))
|
2006-06-15 22:40:42 +04:00
|
|
|
{
|
|
|
|
if(in_argument)
|
|
|
|
{
|
|
|
|
if(in_single || in_double)
|
|
|
|
{
|
|
|
|
/* This space belongs to a quoted argument. */
|
|
|
|
if(!kwsysProcessAppendByte(local_buffer, &buffer_begin,
|
|
|
|
&buffer_end, &buffer_size, *c))
|
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This argument has been terminated by whitespace. */
|
|
|
|
if(!kwsysProcessAppendArgument(local_pointers, &pointer_begin,
|
|
|
|
&pointer_end, &pointers_size,
|
|
|
|
local_buffer, &buffer_begin,
|
|
|
|
&buffer_end, &buffer_size))
|
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
in_argument = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* This character belong to an argument. */
|
|
|
|
if(!in_argument)
|
|
|
|
{
|
|
|
|
in_argument = 1;
|
|
|
|
}
|
|
|
|
if(!kwsysProcessAppendByte(local_buffer, &buffer_begin,
|
|
|
|
&buffer_end, &buffer_size, *c))
|
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finish the last argument. */
|
|
|
|
if(in_argument)
|
|
|
|
{
|
|
|
|
if(!kwsysProcessAppendArgument(local_pointers, &pointer_begin,
|
|
|
|
&pointer_end, &pointers_size,
|
|
|
|
local_buffer, &buffer_begin,
|
|
|
|
&buffer_end, &buffer_size))
|
|
|
|
{
|
|
|
|
failed = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we still have memory allocate space for the new command
|
|
|
|
buffer. */
|
|
|
|
if(!failed)
|
|
|
|
{
|
2006-10-05 01:27:17 +04:00
|
|
|
kwsysProcess_ptrdiff_t n = pointer_end - pointer_begin;
|
2009-03-16 22:02:25 +03:00
|
|
|
newCommand = (char**)malloc((size_t)(n+1)*sizeof(char*));
|
2006-06-15 22:40:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(newCommand)
|
|
|
|
{
|
|
|
|
/* Copy the arguments into the new command buffer. */
|
2006-10-05 01:27:17 +04:00
|
|
|
kwsysProcess_ptrdiff_t n = pointer_end - pointer_begin;
|
2009-03-16 22:02:25 +03:00
|
|
|
memcpy(newCommand, pointer_begin, sizeof(char*)*(size_t)(n));
|
2006-06-15 22:40:42 +04:00
|
|
|
newCommand[n] = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Free arguments already allocated. */
|
|
|
|
while(pointer_end != pointer_begin)
|
|
|
|
{
|
|
|
|
free(*(--pointer_end));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Free temporary buffers. */
|
|
|
|
if(pointer_begin != local_pointers)
|
|
|
|
{
|
|
|
|
free(pointer_begin);
|
|
|
|
}
|
|
|
|
if(buffer_begin != local_buffer)
|
|
|
|
{
|
|
|
|
free(buffer_begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the final command buffer. */
|
|
|
|
return newCommand;
|
|
|
|
}
|
2006-12-04 22:42:47 +03:00
|
|
|
|