2003-06-10 23:46:31 +04:00
|
|
|
/*=========================================================================
|
|
|
|
|
|
|
|
Program: KWSys - Kitware System Library
|
|
|
|
Module: $RCSfile$
|
|
|
|
Language: C++
|
|
|
|
Date: $Date$
|
|
|
|
Version: $Revision$
|
|
|
|
|
|
|
|
Copyright (c) 2002 Kitware, Inc., Insight Consortium. All rights reserved.
|
|
|
|
See http://www.cmake.org/HTML/Copyright.html for details.
|
|
|
|
|
2003-11-28 17:48:42 +03:00
|
|
|
This software is distributed WITHOUT ANY WARRANTY; without even
|
|
|
|
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
2003-06-10 23:46:31 +04:00
|
|
|
PURPOSE. See the above copyright notices for more information.
|
|
|
|
|
|
|
|
=========================================================================*/
|
|
|
|
#define KWSYS_IN_PROCESS_C
|
2003-07-10 22:32:30 +04:00
|
|
|
#include "kwsysPrivate.h"
|
|
|
|
#include KWSYS_HEADER(Process.h)
|
2003-06-10 23:46:31 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Implementation for UNIX
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
On UNIX, a child process is forked to exec the program. Three
|
|
|
|
output pipes from the child 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 error pipe
|
|
|
|
that has two purposes. First, if the child cannot exec the program,
|
|
|
|
the error is reported through the error pipe. Second, the error
|
|
|
|
pipe is left open until the child exits. 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.
|
|
|
|
*/
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
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 */
|
|
|
|
#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 */
|
|
|
|
|
|
|
|
/* 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
|
2003-12-03 17:20:05 +03:00
|
|
|
#define KWSYSPE_PIPE_TERM 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
|
|
|
|
|
|
|
|
typedef struct timeval kwsysProcessTime;
|
|
|
|
|
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 TermPipe;
|
|
|
|
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);
|
2003-12-03 17:20:05 +03:00
|
|
|
static int kwsysProcessCreate(kwsysProcess* cp, int index,
|
|
|
|
kwsysProcessCreateInformation* si, int* readEnd);
|
2003-06-10 23:46:31 +04:00
|
|
|
static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
|
|
|
|
kwsysProcessTime* timeoutTime);
|
|
|
|
static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
|
|
|
|
kwsysProcessTime* timeoutLength);
|
|
|
|
static kwsysProcessTime kwsysProcessTimeGetCurrent();
|
|
|
|
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-03 17:20:05 +03:00
|
|
|
static void kwsysProcessChildErrorExit(int errorPipe);
|
2003-06-10 23:46:31 +04:00
|
|
|
static void kwsysProcessRestoreDefaultSignalHandlers();
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* Descriptors for the read ends of the child's output pipes. */
|
|
|
|
int PipeReadEnds[KWSYSPE_PIPE_COUNT];
|
2003-11-28 17:48:42 +03:00
|
|
|
|
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
|
|
|
|
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 old SIGCHLD handler. */
|
|
|
|
struct sigaction OldSigChldAction;
|
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
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* File descriptor set for call to select. */
|
|
|
|
fd_set PipeSet;
|
|
|
|
|
|
|
|
/* 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
|
|
|
|
|
|
|
/* The exit codes of each child process in the pipeline. */
|
|
|
|
int* CommandExitCodes;
|
2003-06-10 23:46:31 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
kwsysProcess* kwsysProcess_New()
|
|
|
|
{
|
|
|
|
/* Allocate a process control structure. */
|
|
|
|
kwsysProcess* cp = (kwsysProcess*)malloc(sizeof(kwsysProcess));
|
|
|
|
if(!cp)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(cp, 0, sizeof(kwsysProcess));
|
2003-06-19 01:06:21 +04:00
|
|
|
cp->State = kwsysProcess_State_Starting;
|
2003-06-10 23:46:31 +04:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_Delete(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
{
|
|
|
|
kwsysProcess_WaitForExit(cp, 0);
|
|
|
|
}
|
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-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;
|
|
|
|
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. */
|
|
|
|
if(!command)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate a new array for command pointers. */
|
|
|
|
newNumberOfCommands = cp->NumberOfCommands + 1;
|
|
|
|
if(!(newCommands = (char***)malloc(sizeof(char**) * newNumberOfCommands)))
|
|
|
|
{
|
|
|
|
/* 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. */
|
|
|
|
{
|
|
|
|
char const* const* c = command;
|
|
|
|
int n = 0;
|
|
|
|
int i = 0;
|
|
|
|
while(*c++);
|
|
|
|
n = c - command - 1;
|
|
|
|
newCommands[cp->NumberOfCommands] = (char**)malloc((n+1)*sizeof(char*));
|
|
|
|
if(!newCommands[cp->NumberOfCommands])
|
|
|
|
{
|
|
|
|
/* Out of memory. */
|
|
|
|
free(newCommands);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
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)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
free(newCommands[cp->NumberOfCommands][i-1]);
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
free(newCommands);
|
|
|
|
return 0;
|
2003-11-28 17:48:42 +03:00
|
|
|
}
|
2003-12-03 17:20:05 +03:00
|
|
|
newCommands[cp->NumberOfCommands][n] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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)
|
|
|
|
{
|
|
|
|
cp->Timeout = timeout;
|
|
|
|
if(cp->Timeout < 0)
|
|
|
|
{
|
|
|
|
cp->Timeout = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-07-07 16:36:40 +04:00
|
|
|
void kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir)
|
|
|
|
{
|
|
|
|
if(cp->WorkingDirectory == dir)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(cp->WorkingDirectory && dir && strcmp(cp->WorkingDirectory, dir) == 0)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
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-07-07 16:36:40 +04:00
|
|
|
strcpy(cp->WorkingDirectory, dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-08-05 22:27:47 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetOption(kwsysProcess* cp, int optionId)
|
|
|
|
{
|
|
|
|
(void)cp;
|
|
|
|
(void)optionId;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_SetOption(kwsysProcess* cp, int optionId, int value)
|
|
|
|
{
|
|
|
|
(void)cp;
|
|
|
|
(void)optionId;
|
|
|
|
(void)value;
|
|
|
|
}
|
|
|
|
|
2003-07-07 16:36:40 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-10 23:46:31 +04:00
|
|
|
int kwsysProcess_GetState(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
return cp->State;
|
|
|
|
}
|
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetExitException(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
return cp->ExitException;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetExitCode(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
return cp->ExitCode;
|
|
|
|
}
|
|
|
|
|
2003-06-19 01:06:21 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_GetExitValue(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
return cp->ExitValue;
|
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
const char* kwsysProcess_GetErrorString(kwsysProcess* cp)
|
|
|
|
{
|
2003-06-19 01:06:21 +04:00
|
|
|
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-06-19 01:06:21 +04:00
|
|
|
return 0;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void kwsysProcess_Execute(kwsysProcess* cp)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct sigaction newSigChldAction;
|
2003-12-03 17:20:05 +03:00
|
|
|
kwsysProcessCreateInformation si = {-1, -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-06-19 01:06:21 +04:00
|
|
|
if(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-06-10 23:46:31 +04:00
|
|
|
/* We want no special handling of SIGCHLD. Repeat call until it is
|
|
|
|
not interrupted. */
|
2003-08-05 19:34:39 +04:00
|
|
|
memset(&newSigChldAction, 0, sizeof(struct sigaction));
|
2003-06-10 23:46:31 +04:00
|
|
|
newSigChldAction.sa_handler = SIG_DFL;
|
|
|
|
while((sigaction(SIGCHLD, &newSigChldAction, &cp->OldSigChldAction) < 0) &&
|
|
|
|
(errno == EINTR));
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Setup the stderr and termination pipes to be shared by all processes. */
|
|
|
|
for(i=KWSYSPE_PIPE_STDERR; i < KWSYSPE_PIPE_COUNT; ++i)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
/* Create the pipe. */
|
2003-12-03 17:20:05 +03:00
|
|
|
int p[2];
|
2003-06-10 23:46:31 +04:00
|
|
|
if(pipe(p) < 0)
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
|
|
|
return;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Store the pipe. */
|
|
|
|
cp->PipeReadEnds[i] = p[0];
|
|
|
|
if(i == KWSYSPE_PIPE_STDERR)
|
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
si.StdErr = p[1];
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
si.TermPipe = p[1];
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
|
|
|
|
2003-06-10 23:46:31 +04: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))
|
|
|
|
{
|
|
|
|
kwsysProcessCleanup(cp, 1);
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.TermPipe);
|
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
|
|
|
/* 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. */
|
|
|
|
{
|
|
|
|
int readEnd = 0;
|
|
|
|
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))
|
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. */
|
|
|
|
kwsysProcessCleanupDescriptor(&readEnd);
|
|
|
|
if(i > 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-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si.StdOut);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.TermPipe);
|
|
|
|
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. */
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si.StdErr);
|
|
|
|
kwsysProcessCleanupDescriptor(&si.TermPipe);
|
2003-11-28 17:48:42 +03:00
|
|
|
|
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;
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-03 17:20:05 +03:00
|
|
|
int kwsysProcess_WaitForData(kwsysProcess* cp, int pipes, char** data,
|
|
|
|
int* length, double* userTimeout)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int max = -1;
|
|
|
|
kwsysProcessTime* timeout = 0;
|
|
|
|
kwsysProcessTime timeoutLength;
|
|
|
|
kwsysProcessTime timeoutTime;
|
|
|
|
kwsysProcessTime userStartTime;
|
|
|
|
int user = 0;
|
|
|
|
int expired = 0;
|
|
|
|
int pipeId = 0;
|
|
|
|
int numReady = 0;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
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. */
|
|
|
|
user = kwsysProcessGetTimeoutTime(cp, userTimeout, &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. */
|
|
|
|
while(cp->PipesLeft > 0)
|
|
|
|
{
|
|
|
|
/* Check for any open pipes with data reported ready by the last
|
|
|
|
call to select. */
|
|
|
|
for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
|
|
|
|
{
|
|
|
|
if(cp->PipeReadEnds[i] >= 0 &&
|
|
|
|
FD_ISSET(cp->PipeReadEnds[i], &cp->PipeSet))
|
|
|
|
{
|
|
|
|
int n;
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* We are handling this pipe now. Remove it from the set. */
|
|
|
|
FD_CLR(cp->PipeReadEnds[i], &cp->PipeSet);
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* 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. */
|
2003-12-03 17:20:05 +03:00
|
|
|
if(i == KWSYSPE_PIPE_TERM)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
/* This is data on the special termination pipe. Ignore it. */
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
|
|
|
else if(pipes & (1 << i))
|
|
|
|
{
|
|
|
|
/* Caller wants this data. Report it. */
|
|
|
|
*data = cp->PipeBuffer;
|
|
|
|
*length = n;
|
|
|
|
pipeId = (1 << i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* We are done reading from this pipe. */
|
|
|
|
kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]);
|
|
|
|
--cp->PipesLeft;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* If we have data, break early. */
|
|
|
|
if(pipeId)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Make sure the set is empty (it should always be empty here
|
|
|
|
anyway). */
|
|
|
|
FD_ZERO(&cp->PipeSet);
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* 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];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Make sure we have a non-empty set. */
|
|
|
|
if(max < 0)
|
|
|
|
{
|
|
|
|
/* All pipes have closed. Child has terminated. */
|
|
|
|
break;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Setup a timeout if required. */
|
|
|
|
if(timeoutTime.tv_sec < 0)
|
|
|
|
{
|
|
|
|
timeout = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
timeout = &timeoutLength;
|
|
|
|
}
|
|
|
|
if(kwsysProcessGetTimeoutLeft(&timeoutTime, &timeoutLength))
|
|
|
|
{
|
|
|
|
/* Timeout has already expired. */
|
|
|
|
expired = 1;
|
|
|
|
break;
|
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* 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));
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Check result of select. */
|
|
|
|
if(numReady == 0)
|
|
|
|
{
|
|
|
|
/* Select's timeout expired. */
|
|
|
|
expired = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else if(numReady < 0)
|
|
|
|
{
|
|
|
|
/* Select returned an error. Leave the error description in the
|
|
|
|
pipe buffer. */
|
2003-07-10 00:18:14 +04:00
|
|
|
strncpy(cp->ErrorMessage, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE);
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Kill the children now. */
|
2003-06-10 23:46:31 +04:00
|
|
|
kwsysProcess_Kill(cp);
|
|
|
|
cp->Killed = 0;
|
2003-12-03 17:20:05 +03:00
|
|
|
cp->SelectError = 1;
|
2003-06-10 23:46:31 +04:00
|
|
|
cp->PipesLeft = 0;
|
|
|
|
}
|
|
|
|
}
|
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,
|
|
|
|
userStartTime);
|
|
|
|
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. */
|
|
|
|
if(pipeId)
|
|
|
|
{
|
|
|
|
/* Data are ready on a pipe. */
|
|
|
|
return pipeId;
|
|
|
|
}
|
|
|
|
else if(expired)
|
|
|
|
{
|
|
|
|
/* A timeout has expired. */
|
|
|
|
if(user)
|
|
|
|
{
|
|
|
|
/* 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;
|
|
|
|
cp->PipesLeft = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* No pipes are left open. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
int kwsysProcess_WaitForExit(kwsysProcess* cp, double* userTimeout)
|
|
|
|
{
|
|
|
|
int result = 0;
|
|
|
|
int status = 0;
|
|
|
|
int pipe = 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-06-19 01:06:21 +04:00
|
|
|
if(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. */
|
|
|
|
while((pipe = kwsysProcess_WaitForData(cp, 0, 0, 0, userTimeout)) > 0)
|
|
|
|
{
|
2003-06-19 01:06:21 +04:00
|
|
|
if(pipe == kwsysProcess_Pipe_Timeout)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/* Wait for each child to terminate. The process should have
|
|
|
|
already exited because KWSYSPE_PIPE_TERM has been closed by this
|
|
|
|
point. Repeat the call until it is not interrupted. */
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-12-03 17:20:05 +03:00
|
|
|
while(((result = waitpid(cp->ForkPIDs[i],
|
|
|
|
&cp->CommandExitCodes[i], 0)) < 0) &&
|
|
|
|
(errno == EINTR));
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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;
|
|
|
|
switch ((int)WTERMSIG(status))
|
|
|
|
{
|
|
|
|
#ifdef SIGSEGV
|
|
|
|
case SIGSEGV: cp->ExitException = kwsysProcess_Exception_Fault; break;
|
|
|
|
#endif
|
2003-08-13 22:21:39 +04:00
|
|
|
#ifdef SIGBUS
|
|
|
|
case SIGBUS: cp->ExitException = kwsysProcess_Exception_Fault; break;
|
|
|
|
#endif
|
2003-06-19 01:06:21 +04:00
|
|
|
#ifdef SIGFPE
|
|
|
|
case SIGFPE: cp->ExitException = kwsysProcess_Exception_Numerical; break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGILL
|
|
|
|
case SIGILL: cp->ExitException = kwsysProcess_Exception_Illegal; break;
|
|
|
|
#endif
|
|
|
|
#ifdef SIGINT
|
|
|
|
case SIGINT: cp->ExitException = kwsysProcess_Exception_Interrupt; break;
|
|
|
|
#endif
|
|
|
|
default: cp->ExitException = kwsysProcess_Exception_Other; break;
|
|
|
|
}
|
|
|
|
cp->ExitCode = 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-06-19 01:06:21 +04:00
|
|
|
if(cp->State != kwsysProcess_State_Executing)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2003-11-28 17:48:42 +03: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)
|
|
|
|
{
|
|
|
|
if(cp->ForkPIDs[i])
|
|
|
|
{
|
|
|
|
kill(cp->ForkPIDs[i], SIGKILL);
|
|
|
|
}
|
|
|
|
}
|
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;
|
|
|
|
}
|
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;
|
|
|
|
FD_ZERO(&cp->PipeSet);
|
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-03 17:20:05 +03:00
|
|
|
|
|
|
|
if(cp->ForkPIDs)
|
|
|
|
{
|
|
|
|
free(cp->ForkPIDs);
|
|
|
|
}
|
|
|
|
cp->ForkPIDs = (pid_t*)malloc(sizeof(pid_t)*cp->NumberOfCommands);
|
|
|
|
if(!cp->ForkPIDs)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(cp->ForkPIDs, 0, sizeof(pid_t)*cp->NumberOfCommands);
|
|
|
|
|
|
|
|
if(cp->CommandExitCodes)
|
|
|
|
{
|
|
|
|
free(cp->CommandExitCodes);
|
|
|
|
}
|
|
|
|
cp->CommandExitCodes = (int*)malloc(sizeof(int)*cp->NumberOfCommands);
|
|
|
|
if(!cp->CommandExitCodes)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
memset(cp->CommandExitCodes, 0, sizeof(int)*cp->NumberOfCommands);
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
for(i=0; i < cp->NumberOfCommands; ++i)
|
|
|
|
{
|
|
|
|
if(cp->ForkPIDs[i])
|
|
|
|
{
|
|
|
|
kill(cp->ForkPIDs[i], SIGKILL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-06-10 23:46:31 +04:00
|
|
|
}
|
2003-11-28 17:48:42 +03:00
|
|
|
|
2003-06-10 23:46:31 +04:00
|
|
|
/* Restore the SIGCHLD handler. */
|
|
|
|
while((sigaction(SIGCHLD, &cp->OldSigChldAction, 0) < 0) &&
|
|
|
|
(errno == EINTR));
|
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-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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-03 17:20:05 +03:00
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-12-04 21:56:00 +03:00
|
|
|
static int kwsysProcessCreate(kwsysProcess* cp, int index,
|
|
|
|
kwsysProcessCreateInformation* si, int* readEnd)
|
2003-12-03 17:20:05 +03:00
|
|
|
{
|
|
|
|
/* Setup the process's stdin. */
|
|
|
|
if(index > 0)
|
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
si->StdIn = *readEnd;
|
2003-12-03 17:20:05 +03:00
|
|
|
*readEnd = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
si->StdIn = 0;
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
cp->ForkPIDs[index] = fork();
|
|
|
|
if(cp->ForkPIDs[index] < 0)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(cp->ForkPIDs[index] == 0)
|
|
|
|
{
|
|
|
|
/* 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. */
|
|
|
|
if(index > 0)
|
|
|
|
{
|
2003-12-03 21:37:21 +03:00
|
|
|
dup2(si->StdIn, 0);
|
2003-12-03 17:20:05 +03:00
|
|
|
}
|
2003-12-03 21:37:21 +03:00
|
|
|
dup2(si->StdOut, 1);
|
|
|
|
dup2(si->StdErr, 2);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Clear the close-on-exec flag for stdin, stdout, and stderr.
|
|
|
|
Also clear it for the termination pipe. All other pipe handles
|
|
|
|
will be closed when exec succeeds. */
|
|
|
|
fcntl(0, F_SETFD, 0);
|
|
|
|
fcntl(1, F_SETFD, 0);
|
|
|
|
fcntl(2, F_SETFD, 0);
|
2003-12-03 21:37:21 +03:00
|
|
|
fcntl(si->TermPipe, F_SETFD, 0);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
/* Restore all default signal handlers. */
|
|
|
|
kwsysProcessRestoreDefaultSignalHandlers();
|
|
|
|
|
|
|
|
/* Change to the working directory specified, if any. */
|
|
|
|
if(cp->WorkingDirectory)
|
|
|
|
{
|
|
|
|
/* Some platforms specify that the chdir call may be
|
|
|
|
interrupted. Repeat the call until it finishes. */
|
|
|
|
int r;
|
|
|
|
while(((r = chdir(cp->WorkingDirectory)) < 0) && (errno == EINTR));
|
|
|
|
if(r < 0)
|
|
|
|
{
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Execute the real process. If successful, this does not return. */
|
|
|
|
execvp(cp->Commands[index][0], cp->Commands[index]);
|
|
|
|
|
|
|
|
/* 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
|
|
|
}
|
|
|
|
|
|
|
|
/* 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. */
|
|
|
|
{
|
|
|
|
int total = 0;
|
|
|
|
int n = 1;
|
|
|
|
/* 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,
|
2003-12-03 17:20:05 +03:00
|
|
|
KWSYSPE_PIPE_BUFFER_SIZE-total)) < 0) &&
|
|
|
|
(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. */
|
|
|
|
if(index > 0)
|
|
|
|
{
|
|
|
|
/* 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. */
|
2003-12-03 21:37:21 +03:00
|
|
|
kwsysProcessCleanupDescriptor(&si->StdOut);
|
2003-12-03 17:20:05 +03:00
|
|
|
|
|
|
|
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,
|
|
|
|
userTimeoutLength);
|
|
|
|
if(kwsysProcessTimeLess(userTimeoutTime, *timeoutTime))
|
|
|
|
{
|
|
|
|
*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,
|
|
|
|
kwsysProcessTime* 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();
|
|
|
|
*timeoutLength = kwsysProcessTimeSubtract(*timeoutTime, currentTime);
|
|
|
|
if(timeoutLength->tv_sec < 0)
|
|
|
|
{
|
|
|
|
/* Timeout has already expired. */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* There is some time left. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static kwsysProcessTime kwsysProcessTimeGetCurrent()
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
|
|
|
kwsysProcessTime current;
|
|
|
|
gettimeofday(¤t, 0);
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
2003-06-11 00:55:50 +04:00
|
|
|
static double kwsysProcessTimeToDouble(kwsysProcessTime t)
|
2003-06-10 23:46:31 +04:00
|
|
|
{
|
2003-11-28 17:48:42 +03:00
|
|
|
return (double)t.tv_sec + 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;
|
|
|
|
t.tv_usec = (long)((d-t.tv_sec)*1000000);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
/* 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. */
|
2003-12-03 17:20:05 +03:00
|
|
|
write(errorPipe, buffer, strlen(buffer));
|
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. */
|
2003-06-11 00:55:50 +04:00
|
|
|
static void kwsysProcessRestoreDefaultSignalHandlers()
|
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
|
|
|
|
}
|