| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881 |
- /*=========================================================================
- Program: KWSys - Kitware System Library
- Module: $RCSfile$
- Copyright (c) Kitware, Inc., Insight Consortium. All rights reserved.
- See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
- 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.
- =========================================================================*/
- #include "kwsysPrivate.h"
- #include KWSYS_HEADER(Process.h)
- /* Work-around CMake dependency scanning limitation. This must
- duplicate the above list of headers. */
- #if 0
- # include "Process.h.in"
- #endif
- /*
- Implementation for UNIX
- 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.
- */
- /*
- 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.
- */
- #include <stddef.h> /* ptrdiff_t */
- #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 <sys/stat.h> /* open mode */
- #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 */
- #include <dirent.h> /* DIR, dirent */
- #include <ctype.h> /* isspace */
- #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
- #if defined(__BEOS__) && !defined(__ZETA__)
- /* 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
- /* BeOS does not have siginfo on its signal handlers. */
- #if !defined(__BEOS__)
- # define KWSYSPE_USE_SIGINFO 1
- #endif
- /* The number of pipes for the child's output. The standard stdout
- 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. */
- #define KWSYSPE_PIPE_COUNT 3
- #define KWSYSPE_PIPE_STDOUT 0
- #define KWSYSPE_PIPE_STDERR 1
- #define KWSYSPE_PIPE_SIGNAL 2
- /* The maximum amount to read from a pipe at a time. */
- #define KWSYSPE_PIPE_BUFFER_SIZE 1024
- /* 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;
- };
- typedef struct kwsysProcessCreateInformation_s
- {
- int StdIn;
- int StdOut;
- int StdErr;
- int ErrorPipe[2];
- } kwsysProcessCreateInformation;
- /*--------------------------------------------------------------------------*/
- static int kwsysProcessInitialize(kwsysProcess* cp);
- static void kwsysProcessCleanup(kwsysProcess* cp, int error);
- static void kwsysProcessCleanupDescriptor(int* pfd);
- static int kwsysProcessSetNonBlocking(int fd);
- static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
- kwsysProcessCreateInformation* si, int* readEnd);
- static void kwsysProcessDestroy(kwsysProcess* cp);
- static int kwsysProcessSetupOutputPipeFile(int* p, const char* name);
- static int kwsysProcessSetupOutputPipeNative(int* p, int des[2]);
- static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
- kwsysProcessTime* timeoutTime);
- static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
- double* userTimeout,
- kwsysProcessTimeNative* timeoutLength);
- static kwsysProcessTime kwsysProcessTimeGetCurrent(void);
- 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);
- static void kwsysProcessSetExitException(kwsysProcess* cp, int sig);
- static void kwsysProcessChildErrorExit(int errorPipe);
- static void kwsysProcessRestoreDefaultSignalHandlers(void);
- static pid_t kwsysProcessFork(kwsysProcess* cp,
- kwsysProcessCreateInformation* si);
- static void kwsysProcessKill(pid_t process_id);
- static int kwsysProcessesAdd(kwsysProcess* cp);
- static void kwsysProcessesRemove(kwsysProcess* cp);
- #if KWSYSPE_USE_SIGINFO
- static void kwsysProcessesSignalHandler(int signum, siginfo_t* info,
- void* ucontext);
- #else
- static void kwsysProcessesSignalHandler(int signum);
- #endif
- static char** kwsysProcessParseVerbatimCommand(const char* command);
- /*--------------------------------------------------------------------------*/
- /* Structure containing data used to implement the child's execution. */
- struct kwsysProcess_s
- {
- /* The command lines to execute. */
- char*** Commands;
- int NumberOfCommands;
- /* Descriptors for the read ends of the child's output pipes and
- the signal pipe. */
- int PipeReadEnds[KWSYSPE_PIPE_COUNT];
- /* Write descriptor for child termination signal pipe. */
- int SignalPipe;
- /* Buffer for pipe data. */
- char PipeBuffer[KWSYSPE_PIPE_BUFFER_SIZE];
- /* Process IDs returned by the calls to fork. */
- pid_t* ForkPIDs;
- /* Flag for whether the children were terminated by a faild select. */
- int SelectError;
- /* The timeout length. */
- double Timeout;
- /* The working directory for the process. */
- char* WorkingDirectory;
- /* Whether to create the child as a detached process. */
- int OptionDetach;
- /* Whether the child was created as a detached process. */
- int Detached;
- /* Whether to treat command lines as verbatim. */
- int Verbatim;
- /* Time at which the child started. Negative for no timeout. */
- kwsysProcessTime StartTime;
- /* Time at which the child will timeout. Negative for no timeout. */
- kwsysProcessTime TimeoutTime;
- /* Flag for whether the timeout expired. */
- int TimeoutExpired;
- /* The number of pipes left open during execution. */
- int PipesLeft;
- #if KWSYSPE_USE_SELECT
- /* File descriptor set for call to select. */
- fd_set PipeSet;
- #endif
- /* The number of children still executing. */
- int CommandsLeft;
- /* The current status of the child process. */
- int State;
- /* The exceptional behavior that terminated the child process, if
- * any. */
- int ExitException;
- /* The exit code of the child process. */
- int ExitCode;
- /* The exit value of the child process, if any. */
- int ExitValue;
- /* Whether the process was killed. */
- int Killed;
- /* Buffer for error message in case of failure. */
- char ErrorMessage[KWSYSPE_PIPE_BUFFER_SIZE+1];
- /* Description for the ExitException. */
- char ExitExceptionString[KWSYSPE_PIPE_BUFFER_SIZE+1];
- /* The exit codes of each child process in the pipeline. */
- int* CommandExitCodes;
- /* Name of files to which stdin and stdout pipes are attached. */
- char* PipeFileSTDIN;
- char* PipeFileSTDOUT;
- char* PipeFileSTDERR;
- /* Whether each pipe is shared with the parent process. */
- int PipeSharedSTDIN;
- int PipeSharedSTDOUT;
- int PipeSharedSTDERR;
- /* Native pipes provided by the user. */
- int PipeNativeSTDIN[2];
- int PipeNativeSTDOUT[2];
- int PipeNativeSTDERR[2];
- /* The real working directory of this process. */
- int RealWorkingDirectoryLength;
- char* RealWorkingDirectory;
- };
- /*--------------------------------------------------------------------------*/
- kwsysProcess* kwsysProcess_New(void)
- {
- /* Allocate a process control structure. */
- kwsysProcess* cp = (kwsysProcess*)malloc(sizeof(kwsysProcess));
- if(!cp)
- {
- return 0;
- }
- memset(cp, 0, sizeof(kwsysProcess));
- /* Share stdin with the parent process by default. */
- cp->PipeSharedSTDIN = 1;
- /* 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;
- /* Set initial status. */
- cp->State = kwsysProcess_State_Starting;
- return cp;
- }
- /*--------------------------------------------------------------------------*/
- void kwsysProcess_Delete(kwsysProcess* cp)
- {
- /* Make sure we have an instance. */
- if(!cp)
- {
- return;
- }
- /* If the process is executing, wait for it to finish. */
- if(cp->State == kwsysProcess_State_Executing)
- {
- if(cp->Detached)
- {
- kwsysProcess_Disown(cp);
- }
- else
- {
- kwsysProcess_WaitForExit(cp, 0);
- }
- }
- /* Free memory. */
- kwsysProcess_SetCommand(cp, 0);
- kwsysProcess_SetWorkingDirectory(cp, 0);
- kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDIN, 0);
- kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDOUT, 0);
- kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDERR, 0);
- if(cp->CommandExitCodes)
- {
- free(cp->CommandExitCodes);
- }
- free(cp);
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_SetCommand(kwsysProcess* cp, char const* const* command)
- {
- int i;
- if(!cp)
- {
- return 0;
- }
- for(i=0; i < cp->NumberOfCommands; ++i)
- {
- char** c = cp->Commands[i];
- while(*c)
- {
- free(*c++);
- }
- free(cp->Commands[i]);
- }
- cp->NumberOfCommands = 0;
- if(cp->Commands)
- {
- free(cp->Commands);
- cp->Commands = 0;
- }
- if(command)
- {
- 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(!cp || !command || !*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. */
- if(cp->Verbatim)
- {
- /* In order to run the given command line verbatim we need to
- parse it. */
- newCommands[cp->NumberOfCommands] =
- kwsysProcessParseVerbatimCommand(*command);
- if(!newCommands[cp->NumberOfCommands])
- {
- /* Out of memory. */
- free(newCommands);
- return 0;
- }
- }
- else
- {
- /* Copy each argument string individually. */
- char const* const* c = command;
- kwsysProcess_ptrdiff_t n = 0;
- kwsysProcess_ptrdiff_t 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)
- {
- free(newCommands[cp->NumberOfCommands][i-1]);
- }
- free(newCommands);
- return 0;
- }
- 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;
- }
- /*--------------------------------------------------------------------------*/
- void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout)
- {
- if(!cp)
- {
- return;
- }
- cp->Timeout = timeout;
- if(cp->Timeout < 0)
- {
- cp->Timeout = 0;
- }
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir)
- {
- if(!cp)
- {
- return 0;
- }
- if(cp->WorkingDirectory == dir)
- {
- return 1;
- }
- if(cp->WorkingDirectory && dir && strcmp(cp->WorkingDirectory, dir) == 0)
- {
- return 1;
- }
- if(cp->WorkingDirectory)
- {
- free(cp->WorkingDirectory);
- cp->WorkingDirectory = 0;
- }
- if(dir)
- {
- cp->WorkingDirectory = (char*)malloc(strlen(dir) + 1);
- if(!cp->WorkingDirectory)
- {
- return 0;
- }
- strcpy(cp->WorkingDirectory, dir);
- }
- return 1;
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_SetPipeFile(kwsysProcess* cp, int prPipe, const char* file)
- {
- char** pfile;
- if(!cp)
- {
- return 0;
- }
- switch(prPipe)
- {
- 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);
- }
- /* If we are redirecting the pipe, do not share it or use a native
- pipe. */
- if(*pfile)
- {
- kwsysProcess_SetPipeNative(cp, prPipe, 0);
- kwsysProcess_SetPipeShared(cp, prPipe, 0);
- }
- return 1;
- }
- /*--------------------------------------------------------------------------*/
- void kwsysProcess_SetPipeShared(kwsysProcess* cp, int prPipe, int shared)
- {
- if(!cp)
- {
- return;
- }
- switch(prPipe)
- {
- 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;
- }
- /* If we are sharing the pipe, do not redirect it to a file or use a
- native pipe. */
- if(shared)
- {
- kwsysProcess_SetPipeFile(cp, prPipe, 0);
- 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);
- }
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_GetOption(kwsysProcess* cp, int optionId)
- {
- if(!cp)
- {
- return 0;
- }
- switch(optionId)
- {
- case kwsysProcess_Option_Detach: return cp->OptionDetach;
- case kwsysProcess_Option_Verbatim: return cp->Verbatim;
- default: return 0;
- }
- }
- /*--------------------------------------------------------------------------*/
- void kwsysProcess_SetOption(kwsysProcess* cp, int optionId, int value)
- {
- if(!cp)
- {
- return;
- }
- switch(optionId)
- {
- case kwsysProcess_Option_Detach: cp->OptionDetach = value; break;
- case kwsysProcess_Option_Verbatim: cp->Verbatim = value; break;
- default: break;
- }
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_GetState(kwsysProcess* cp)
- {
- return cp? cp->State : kwsysProcess_State_Error;
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_GetExitException(kwsysProcess* cp)
- {
- return cp? cp->ExitException : kwsysProcess_Exception_Other;
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_GetExitCode(kwsysProcess* cp)
- {
- return cp? cp->ExitCode : 0;
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_GetExitValue(kwsysProcess* cp)
- {
- return cp? cp->ExitValue : -1;
- }
- /*--------------------------------------------------------------------------*/
- const char* kwsysProcess_GetErrorString(kwsysProcess* cp)
- {
- if(!cp)
- {
- return "Process management structure could not be allocated";
- }
- else if(cp->State == kwsysProcess_State_Error)
- {
- return cp->ErrorMessage;
- }
- 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";
- }
- /*--------------------------------------------------------------------------*/
- void kwsysProcess_Execute(kwsysProcess* cp)
- {
- int i;
- kwsysProcessCreateInformation si = {-1, -1, -1, {-1, -1}};
- /* Do not execute a second copy simultaneously. */
- if(!cp || cp->State == kwsysProcess_State_Executing)
- {
- return;
- }
- /* Initialize the control structure for a new process. */
- if(!kwsysProcessInitialize(cp))
- {
- strcpy(cp->ErrorMessage, "Out of memory");
- cp->State = kwsysProcess_State_Error;
- return;
- }
- /* 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;
- if(!getcwd(cp->RealWorkingDirectory, cp->RealWorkingDirectoryLength))
- {
- 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);
- return;
- }
- }
- /* 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)
- {
- if(!kwsysProcessesAdd(cp))
- {
- kwsysProcessCleanup(cp, 1);
- return;
- }
- }
- /* Setup the stderr pipe to be shared by all processes. */
- {
- /* Create the pipe. */
- int p[2];
- if(pipe(p) < 0)
- {
- kwsysProcessCleanup(cp, 1);
- return;
- }
- /* 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;
- }
- #if !KWSYSPE_USE_SELECT
- if(!kwsysProcessSetNonBlocking(p[0]))
- {
- kwsysProcessCleanup(cp, 1);
- kwsysProcessCleanupDescriptor(&si.StdErr);
- return;
- }
- #endif
- }
- /* 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;
- }
- }
- /* 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;
- }
- /* 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;
- }
- }
- /* The timeout period starts now. */
- cp->StartTime = kwsysProcessTimeGetCurrent();
- cp->TimeoutTime.tv_sec = -1;
- cp->TimeoutTime.tv_usec = -1;
- /* Create the pipeline of processes. */
- {
- int readEnd = -1;
- int failed = 0;
- for(i=0; i < cp->NumberOfCommands; ++i)
- {
- if(!kwsysProcessCreate(cp, i, &si, &readEnd))
- {
- failed = 1;
- }
- #if !KWSYSPE_USE_SELECT
- /* Set the output pipe of the last process to be non-blocking so
- we can poll it. */
- if(i == cp->NumberOfCommands-1 && !kwsysProcessSetNonBlocking(readEnd))
- {
- failed = 1;
- }
- #endif
- if(failed)
- {
- kwsysProcessCleanup(cp, 1);
- /* Release resources that may have been allocated for this
- process before an error occurred. */
- kwsysProcessCleanupDescriptor(&readEnd);
- if(si.StdIn != 0)
- {
- kwsysProcessCleanupDescriptor(&si.StdIn);
- }
- if(si.StdOut != 1)
- {
- kwsysProcessCleanupDescriptor(&si.StdOut);
- }
- if(si.StdErr != 2)
- {
- kwsysProcessCleanupDescriptor(&si.StdErr);
- }
- kwsysProcessCleanupDescriptor(&si.ErrorPipe[0]);
- kwsysProcessCleanupDescriptor(&si.ErrorPipe[1]);
- return;
- }
- }
- /* Save a handle to the output pipe for the last process. */
- cp->PipeReadEnds[KWSYSPE_PIPE_STDOUT] = readEnd;
- }
- /* The parent process does not need the output pipe write ends. */
- if(si.StdErr != 2)
- {
- kwsysProcessCleanupDescriptor(&si.StdErr);
- }
- /* 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;
- }
- /* All the pipes are now open. */
- cp->PipesLeft = KWSYSPE_PIPE_COUNT;
- /* The process has now started. */
- cp->State = kwsysProcess_State_Executing;
- cp->Detached = cp->OptionDetach;
- }
- /*--------------------------------------------------------------------------*/
- kwsysEXPORT void kwsysProcess_Disown(kwsysProcess* cp)
- {
- int i;
- /* Make sure a detached child process is running. */
- if(!cp || !cp->Detached || cp->State != kwsysProcess_State_Executing ||
- cp->TimeoutExpired || cp->Killed)
- {
- return;
- }
- /* 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;
- }
- }
- /* We will not wait for exit, so cleanup now. */
- kwsysProcessCleanup(cp, 0);
- /* The process has been disowned. */
- cp->State = kwsysProcess_State_Disowned;
- }
- /*--------------------------------------------------------------------------*/
- 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);
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_WaitForData(kwsysProcess* cp, char** data, int* length,
- double* userTimeout)
- {
- kwsysProcessTime userStartTime = {0, 0};
- kwsysProcessWaitData wd =
- {
- 0,
- kwsysProcess_Pipe_None,
- 0,
- 0,
- {0, 0}
- };
- wd.UserTimeout = userTimeout;
- /* Make sure we are executing a process. */
- if(!cp || cp->State != kwsysProcess_State_Executing || cp->Killed ||
- cp->TimeoutExpired)
- {
- return kwsysProcess_Pipe_None;
- }
- /* Record the time at which user timeout period starts. */
- if(userTimeout)
- {
- userStartTime = kwsysProcessTimeGetCurrent();
- }
- /* Calculate the time at which a timeout will expire, and whether it
- is the user or process timeout. */
- wd.User = kwsysProcessGetTimeoutTime(cp, userTimeout,
- &wd.TimeoutTime);
- /* Data can only be available when pipes are open. If the process
- is not running, cp->PipesLeft will be 0. */
- while(cp->PipesLeft > 0 &&
- !kwsysProcessWaitForPipe(cp, data, length, &wd)) {}
- /* 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;
- }
- }
- /* Check what happened. */
- if(wd.PipeId)
- {
- /* Data are ready on a pipe. */
- return wd.PipeId;
- }
- else if(wd.Expired)
- {
- /* A timeout has expired. */
- if(wd.User)
- {
- /* The user timeout has expired. It has no time left. */
- return kwsysProcess_Pipe_Timeout;
- }
- else
- {
- /* The process timeout has expired. Kill the children now. */
- kwsysProcess_Kill(cp);
- cp->Killed = 0;
- cp->TimeoutExpired = 1;
- return kwsysProcess_Pipe_None;
- }
- }
- else
- {
- /* No pipes are left open. */
- return kwsysProcess_Pipe_None;
- }
- }
- /*--------------------------------------------------------------------------*/
- 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;
- *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
- {
- /* 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
- }
- /*--------------------------------------------------------------------------*/
- int kwsysProcess_WaitForExit(kwsysProcess* cp, double* userTimeout)
- {
- int status = 0;
- int prPipe = 0;
- /* Make sure we are executing a process. */
- if(!cp || cp->State != kwsysProcess_State_Executing)
- {
- return 1;
- }
- /* Wait for all the pipes to close. Ignore all data. */
- while((prPipe = kwsysProcess_WaitForData(cp, 0, 0, userTimeout)) > 0)
- {
- if(prPipe == kwsysProcess_Pipe_Timeout)
- {
- return 0;
- }
- }
- /* 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);
- return 1;
- }
- /* Check whether the child reported an error invoking the process. */
- if(cp->SelectError)
- {
- /* The error message is already in its buffer. Tell
- kwsysProcessCleanup to not create it. */
- kwsysProcessCleanup(cp, 0);
- cp->State = kwsysProcess_State_Error;
- return 1;
- }
- /* Use the status of the last process in the pipeline. */
- status = cp->CommandExitCodes[cp->NumberOfCommands-1];
- /* Determine the outcome. */
- if(cp->Killed)
- {
- /* We killed the child. */
- cp->State = kwsysProcess_State_Killed;
- }
- else if(cp->TimeoutExpired)
- {
- /* The timeout expired. */
- cp->State = kwsysProcess_State_Expired;
- }
- else if(WIFEXITED(status))
- {
- /* The child exited normally. */
- cp->State = kwsysProcess_State_Exited;
- cp->ExitException = kwsysProcess_Exception_None;
- cp->ExitCode = status;
- cp->ExitValue = (int)WEXITSTATUS(status);
- }
- else if(WIFSIGNALED(status))
- {
- /* The child received an unhandled signal. */
- cp->State = kwsysProcess_State_Exception;
- cp->ExitCode = status;
- kwsysProcessSetExitException(cp, (int)WTERMSIG(status));
- }
- else
- {
- /* Error getting the child return code. */
- strcpy(cp->ErrorMessage, "Error getting child return code.");
- cp->State = kwsysProcess_State_Error;
- }
- /* Normal cleanup. */
- kwsysProcessCleanup(cp, 0);
- return 1;
- }
- /*--------------------------------------------------------------------------*/
- void kwsysProcess_Kill(kwsysProcess* cp)
- {
- int i;
- /* Make sure we are executing a process. */
- if(!cp || cp->State != kwsysProcess_State_Executing)
- {
- return;
- }
- /* Close all the pipe read ends. Do this before killing the
- children because Cygwin has problems killing processes that are
- blocking to wait for writing to their output pipes. 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. */
- kwsysProcessCleanupDescriptor(&cp->SignalPipe);
- for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
- {
- kwsysProcessCleanupDescriptor(&cp->PipeReadEnds[i]);
- }
- cp->PipesLeft = 0;
- /* Kill the children. */
- cp->Killed = 1;
- for(i=0; i < cp->NumberOfCommands; ++i)
- {
- int status;
- if(cp->ForkPIDs[i])
- {
- /* Kill the child. */
- kwsysProcessKill(cp->ForkPIDs[i]);
- /* Reap the child. Keep trying until the call is not
- interrupted. */
- while((waitpid(cp->ForkPIDs[i], &status, 0) < 0) && (errno == EINTR));
- }
- }
- cp->CommandsLeft = 0;
- }
- /*--------------------------------------------------------------------------*/
- /* Initialize a process control structure for kwsysProcess_Execute. */
- static int kwsysProcessInitialize(kwsysProcess* cp)
- {
- int i;
- for(i=0; i < KWSYSPE_PIPE_COUNT; ++i)
- {
- cp->PipeReadEnds[i] = -1;
- }
- cp->SignalPipe = -1;
- cp->SelectError = 0;
- 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;
- cp->CommandsLeft = 0;
- #if KWSYSPE_USE_SELECT
- FD_ZERO(&cp->PipeSet);
- #endif
- cp->State = kwsysProcess_State_Starting;
- cp->Killed = 0;
- cp->ExitException = kwsysProcess_Exception_None;
- cp->ExitCode = 1;
- cp->ExitValue = 1;
- cp->ErrorMessage[0] = 0;
- strcpy(cp->ExitExceptionString, "No exception");
- 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);
- /* Allocate memory to save the real working directory. */
- if ( cp->WorkingDirectory )
- {
- #if defined(MAXPATHLEN)
- cp->RealWorkingDirectoryLength = MAXPATHLEN;
- #elif defined(PATH_MAX)
- cp->RealWorkingDirectoryLength = PATH_MAX;
- #else
- cp->RealWorkingDirectoryLength = 4096;
- #endif
- cp->RealWorkingDirectory = malloc(cp->RealWorkingDirectoryLength);
- if(!cp->RealWorkingDirectory)
- {
- return 0;
- }
- }
- return 1;
- }
- /*--------------------------------------------------------------------------*/
- /* Free all resources used by the given kwsysProcess instance that were
- allocated by kwsysProcess_Execute. */
- static void kwsysProcessCleanup(kwsysProcess* cp, int error)
- {
- int i;
- if(error)
- {
- /* 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. */
- cp->State = kwsysProcess_State_Error;
- /* Kill any children already started. */
- if(cp->ForkPIDs)
- {
- int status;
- for(i=0; i < cp->NumberOfCommands; ++i)
- {
- if(cp->ForkPIDs[i])
- {
- /* Kill the child. */
- kwsysProcessKill(cp->ForkPIDs[i]);
- /* Reap the child. Keep trying until the call is not
- interrupted. */
- while((waitpid(cp->ForkPIDs[i], &status, 0) < 0) &&
- (errno == EINTR));
- }
- }
- }
- /* Restore the working directory. */
- if(cp->RealWorkingDirectory)
- {
- while((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR));
- }
- }
- /* 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);
- }
- /* Free memory. */
- if(cp->ForkPIDs)
- {
- free(cp->ForkPIDs);
- cp->ForkPIDs = 0;
- }
- if(cp->RealWorkingDirectory)
- {
- free(cp->RealWorkingDirectory);
- cp->RealWorkingDirectory = 0;
- }
- /* 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. */
- static void kwsysProcessCleanupDescriptor(int* pfd)
- {
- if(pfd && *pfd >= 0)
- {
- /* Keep trying to close until it is not interrupted by a
- * signal. */
- while((close(*pfd) < 0) && (errno == EINTR));
- *pfd = -1;
- }
- }
- /*--------------------------------------------------------------------------*/
- 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;
- }
- /*--------------------------------------------------------------------------*/
- static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
- kwsysProcessCreateInformation* si, int* readEnd)
- {
- /* Setup the process's stdin. */
- if(prIndex > 0)
- {
- si->StdIn = *readEnd;
- *readEnd = 0;
- }
- 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;
- }
- }
- else if(cp->PipeSharedSTDIN)
- {
- si->StdIn = 0;
- }
- 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;
- }
- }
- else
- {
- si->StdIn = -1;
- }
- /* Setup the process's stdout. */
- {
- /* Create the pipe. */
- int p[2];
- if(pipe(p) < 0)
- {
- return 0;
- }
- *readEnd = p[0];
- si->StdOut = 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))
- {
- return 0;
- }
- }
- /* Replace the stdout pipe with a file if requested. In this case
- the select call will report that stdout is closed immediately. */
- if(prIndex == cp->NumberOfCommands-1 && cp->PipeFileSTDOUT)
- {
- if(!kwsysProcessSetupOutputPipeFile(&si->StdOut, cp->PipeFileSTDOUT))
- {
- return 0;
- }
- }
- /* Replace the stdout pipe with the parent's if requested. In this
- case the select call will report that stderr is closed
- immediately. */
- if(prIndex == cp->NumberOfCommands-1 && cp->PipeSharedSTDOUT)
- {
- kwsysProcessCleanupDescriptor(&si->StdOut);
- si->StdOut = 1;
- }
- /* 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;
- }
- }
- /* Create the error reporting pipe. */
- if(pipe(si->ErrorPipe) < 0)
- {
- return 0;
- }
- /* Set close-on-exec flag on the error pipe's write end. */
- if(fcntl(si->ErrorPipe[1], F_SETFD, FD_CLOEXEC) < 0)
- {
- return 0;
- }
- /* Fork off a child process. */
- cp->ForkPIDs[prIndex] = kwsysProcessFork(cp, si);
- if(cp->ForkPIDs[prIndex] < 0)
- {
- return 0;
- }
- if(cp->ForkPIDs[prIndex] == 0)
- {
- /* Close the read end of the error reporting pipe. */
- close(si->ErrorPipe[0]);
- /* Setup the stdin, stdout, and stderr pipes. */
- if(si->StdIn > 0)
- {
- dup2(si->StdIn, 0);
- }
- else if(si->StdIn < 0)
- {
- close(0);
- }
- if(si->StdOut != 1)
- {
- dup2(si->StdOut, 1);
- }
- if(si->StdErr != 2)
- {
- dup2(si->StdErr, 2);
- }
- /* Clear the close-on-exec flag for stdin, stdout, and stderr.
- 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);
- /* Restore all default signal handlers. */
- kwsysProcessRestoreDefaultSignalHandlers();
- /* Execute the real process. If successful, this does not return. */
- execvp(cp->Commands[prIndex][0], cp->Commands[prIndex]);
- /* Failure. Report error to parent and terminate. */
- kwsysProcessChildErrorExit(si->ErrorPipe[1]);
- }
- /* A child has been created. */
- ++cp->CommandsLeft;
- /* We are done with the error reporting pipe write end. */
- kwsysProcessCleanupDescriptor(&si->ErrorPipe[1]);
- /* Block until the child's exec call succeeds and closes the error
- pipe or writes data to the pipe to report an error. */
- {
- kwsysProcess_ssize_t total = 0;
- kwsysProcess_ssize_t 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. */
- while(((n = read(si->ErrorPipe[0], cp->ErrorMessage+total,
- KWSYSPE_PIPE_BUFFER_SIZE-total)) < 0) &&
- (errno == EINTR));
- if(n > 0)
- {
- total += n;
- }
- }
- /* We are done with the error reporting pipe read end. */
- kwsysProcessCleanupDescriptor(&si->ErrorPipe[0]);
- if(total > 0)
- {
- /* The child failed to execute the process. */
- return 0;
- }
- }
- /* Successfully created this child process. */
- if(prIndex > 0 || si->StdIn > 0)
- {
- /* The parent process does not need the input pipe read end. */
- kwsysProcessCleanupDescriptor(&si->StdIn);
- }
- /* The parent process does not need the output pipe write ends. */
- if(si->StdOut != 1)
- {
- kwsysProcessCleanupDescriptor(&si->StdOut);
- }
- return 1;
- }
- /*--------------------------------------------------------------------------*/
- 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;
- }
- }
- }
- }
- /*--------------------------------------------------------------------------*/
- 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;
- }
- /*--------------------------------------------------------------------------*/
- 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;
- }
- /*--------------------------------------------------------------------------*/
- /* Get the time at which either the process or user timeout will
- expire. Returns 1 if the user timeout is first, and 0 otherwise. */
- static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
- kwsysProcessTime* timeoutTime)
- {
- /* 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);
- }
- /* Start with process timeout. */
- *timeoutTime = cp->TimeoutTime;
- /* Check if the user timeout is earlier. */
- if(userTimeout)
- {
- kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent();
- kwsysProcessTime userTimeoutLength = kwsysProcessTimeFromDouble(*userTimeout);
- kwsysProcessTime userTimeoutTime = kwsysProcessTimeAdd(currentTime,
- userTimeoutLength);
- if(timeoutTime->tv_sec < 0 ||
- 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. */
- static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
- double* userTimeout,
- kwsysProcessTimeNative* timeoutLength)
- {
- if(timeoutTime->tv_sec < 0)
- {
- /* No timeout time has been requested. */
- return 0;
- }
- else
- {
- /* Calculate the remaining time. */
- kwsysProcessTime currentTime = kwsysProcessTimeGetCurrent();
- kwsysProcessTime timeLeft = kwsysProcessTimeSubtract(*timeoutTime,
- currentTime);
- if(timeLeft.tv_sec < 0 && userTimeout && *userTimeout <= 0)
- {
- /* Caller has explicitly requested a zero timeout. */
- timeLeft.tv_sec = 0;
- timeLeft.tv_usec = 0;
- }
- if(timeLeft.tv_sec < 0)
- {
- /* Timeout has already expired. */
- return 1;
- }
- else
- {
- /* There is some time left. */
- timeoutLength->tv_sec = timeLeft.tv_sec;
- timeoutLength->tv_usec = timeLeft.tv_usec;
- return 0;
- }
- }
- }
- /*--------------------------------------------------------------------------*/
- static kwsysProcessTime kwsysProcessTimeGetCurrent(void)
- {
- kwsysProcessTime current;
- kwsysProcessTimeNative current_native;
- gettimeofday(¤t_native, 0);
- current.tv_sec = (long)current_native.tv_sec;
- current.tv_usec = (long)current_native.tv_usec;
- return current;
- }
- /*--------------------------------------------------------------------------*/
- static double kwsysProcessTimeToDouble(kwsysProcessTime t)
- {
- return (double)t.tv_sec + t.tv_usec*0.000001;
- }
- /*--------------------------------------------------------------------------*/
- static kwsysProcessTime kwsysProcessTimeFromDouble(double d)
- {
- kwsysProcessTime t;
- t.tv_sec = (long)d;
- t.tv_usec = (long)((d-t.tv_sec)*1000000);
- return t;
- }
- /*--------------------------------------------------------------------------*/
- static int kwsysProcessTimeLess(kwsysProcessTime in1, kwsysProcessTime in2)
- {
- return ((in1.tv_sec < in2.tv_sec) ||
- ((in1.tv_sec == in2.tv_sec) && (in1.tv_usec < in2.tv_usec)));
- }
- /*--------------------------------------------------------------------------*/
- static kwsysProcessTime kwsysProcessTimeAdd(kwsysProcessTime in1, kwsysProcessTime in2)
- {
- 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;
- }
- /*--------------------------------------------------------------------------*/
- static kwsysProcessTime kwsysProcessTimeSubtract(kwsysProcessTime in1, kwsysProcessTime in2)
- {
- 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;
- }
- /*--------------------------------------------------------------------------*/
- #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
- # if !defined(SIGSEGV) || SIGBUS != SIGSEGV
- case SIGBUS: KWSYSPE_CASE(Fault, "Bus error"); break;
- # endif
- #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
- /*--------------------------------------------------------------------------*/
- /* When the child process encounters an error before its program is
- invoked, this is called to report the error to the parent and
- exit. */
- static void kwsysProcessChildErrorExit(int errorPipe)
- {
- /* Construct the error message. */
- char buffer[KWSYSPE_PIPE_BUFFER_SIZE];
- strncpy(buffer, strerror(errno), KWSYSPE_PIPE_BUFFER_SIZE);
- /* Report the error to the parent through the special pipe. */
- write(errorPipe, buffer, strlen(buffer));
- /* Terminate without cleanup. */
- _exit(1);
- }
- /*--------------------------------------------------------------------------*/
- /* Restores all signal handlers to their default values. */
- static void kwsysProcessRestoreDefaultSignalHandlers(void)
- {
- struct sigaction act;
- memset(&act, 0, sizeof(struct sigaction));
- 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
- }
- /*--------------------------------------------------------------------------*/
- 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. */
- _exit(0);
- return 0; /* Never reached, but avoids SunCC warning. */
- }
- }
- 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();
- }
- }
- /*--------------------------------------------------------------------------*/
- /* 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__)
- # define KWSYSPE_PS_COMMAND "ps axo pid,ppid"
- # define KWSYSPE_PS_FORMAT "%d %d\n"
- #elif defined(__hpux) || defined(__sparc) || defined(__sgi) || defined(_AIX)
- # define KWSYSPE_PS_COMMAND "ps -ef"
- # define KWSYSPE_PS_FORMAT "%*s %d %d %*[^\n]\n"
- #elif defined(__CYGWIN__)
- # define KWSYSPE_PS_COMMAND "ps aux"
- # define KWSYSPE_PS_FORMAT "%d %d %*[^\n]\n"
- #endif
- /*--------------------------------------------------------------------------*/
- static void kwsysProcessKill(pid_t process_id)
- {
- #if defined(__linux__) || defined(__CYGWIN__)
- DIR* procdir;
- #endif
- /* Suspend the process to be sure it will not create more children. */
- kill(process_id, SIGSTOP);
- /* Kill all children if we can find them. */
- #if defined(__linux__) || defined(__CYGWIN__)
- /* First try using the /proc filesystem. */
- if((procdir = opendir("/proc")) != NULL)
- {
- #if defined(MAXPATHLEN)
- char fname[MAXPATHLEN];
- #elif defined(PATH_MAX)
- char fname[PATH_MAX];
- #else
- char fname[4096];
- #endif
- char buffer[KWSYSPE_PIPE_BUFFER_SIZE+1];
- 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)
- {
- int nread = fread(buffer, 1, KWSYSPE_PIPE_BUFFER_SIZE, f);
- 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);
- }
- else
- #endif
- {
- #if defined(KWSYSPE_PS_COMMAND)
- /* 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. */
- if(ps && fscanf(ps, "%*[^\n]\n") != EOF)
- {
- /* 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)
- {
- /* Recursively kill this child aned its children. */
- kwsysProcessKill(pid);
- }
- }
- }
- /* We are done with the ps process. */
- if(ps)
- {
- pclose(ps);
- }
- #endif
- }
- /* Kill the process. */
- kill(process_id, SIGKILL);
- }
- /*--------------------------------------------------------------------------*/
- /* 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. */
- if(!kwsysProcessSetNonBlocking(p[0]) ||
- !kwsysProcessSetNonBlocking(p[1]))
- {
- 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**)
- malloc(newProcesses.Size*
- sizeof(kwsysProcess*)))))
- {
- /* Copy the old pipe set to the new memory. */
- if(oldProcesses.Count > 0)
- {
- memcpy(newProcesses.Processes, oldProcesses.Processes,
- (oldProcesses.Count * sizeof(kwsysProcess*)));
- }
- }
- 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));
- #if KWSYSPE_USE_SIGINFO
- newSigChldAction.sa_sigaction = kwsysProcessesSignalHandler;
- newSigChldAction.sa_flags = SA_NOCLDSTOP | SA_SIGINFO;
- # ifdef SA_RESTART
- newSigChldAction.sa_flags |= SA_RESTART;
- # endif
- #else
- newSigChldAction.sa_handler = kwsysProcessesSignalHandler;
- newSigChldAction.sa_flags = SA_NOCLDSTOP;
- #endif
- 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));
- /* 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;
- }
- /* Store the new set in that seen by the signal handler. */
- kwsysProcessesUpdate(&newProcesses);
- }
- }
- /* Close the pipe through which the signal handler may have notified
- the given process object that a child has exited. */
- kwsysProcessCleanupDescriptor(&cp->SignalPipe);
- }
- /*--------------------------------------------------------------------------*/
- static void kwsysProcessesSignalHandler(int signum
- #if KWSYSPE_USE_SIGINFO
- , siginfo_t* info, void* ucontext
- #endif
- )
- {
- (void)signum;
- #if KWSYSPE_USE_SIGINFO
- (void)info;
- (void)ucontext;
- #endif
- /* Signal all process objects that a child has terminated. */
- {
- int i;
- for(i=0; i < kwsysProcesses.Count; ++i)
- {
- /* Set the pipe in a signalled state. */
- char buf = 1;
- kwsysProcess* cp = kwsysProcesses.Processes[i];
- read(cp->PipeReadEnds[KWSYSPE_PIPE_SIGNAL], &buf, 1);
- write(cp->SignalPipe, &buf, 1);
- }
- }
- #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
- }
- /*--------------------------------------------------------------------------*/
- static int kwsysProcessAppendByte(char* local,
- char** begin, char** end,
- int* size, char c)
- {
- /* Allocate space for the character. */
- if((*end - *begin) >= *size)
- {
- kwsysProcess_ptrdiff_t length = *end - *begin;
- char* newBuffer = (char*)malloc(*size*2);
- if(!newBuffer)
- {
- return 0;
- }
- memcpy(newBuffer, *begin, length*sizeof(char));
- 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)
- {
- kwsysProcess_ptrdiff_t length = *end - *begin;
- char** newPointers = (char**)malloc(*size*2*sizeof(char*));
- if(!newPointers)
- {
- return 0;
- }
- memcpy(newPointers, *begin, length*sizeof(char*));
- if(*begin != local)
- {
- free(*begin);
- }
- *begin = newPointers;
- *end = *begin + length;
- *size *= 2;
- }
- /* Allocate space for the argument string. */
- **end = (char*)malloc(*arg_end - *arg_begin);
- if(!**end)
- {
- return 0;
- }
- /* Store the argument in the command array. */
- memcpy(**end, *arg_begin, *arg_end - *arg_begin);
- ++(*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;
- }
- }
- }
- else if(isspace(*c))
- {
- 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)
- {
- kwsysProcess_ptrdiff_t n = pointer_end - pointer_begin;
- newCommand = (char**)malloc((n+1)*sizeof(char*));
- }
- if(newCommand)
- {
- /* Copy the arguments into the new command buffer. */
- kwsysProcess_ptrdiff_t n = pointer_end - pointer_begin;
- memcpy(newCommand, pointer_begin, sizeof(char*)*n);
- 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;
- }
|