ProcessUNIX.c 84 KB
Newer Older
1 2
/* Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
   file Copyright.txt or https://cmake.org/licensing#kwsys for details.  */
3 4
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Process.h)
5
#include KWSYS_HEADER(System.h)
6

7 8 9
/* Work-around CMake dependency scanning limitation.  This must
   duplicate the above list of headers.  */
#if 0
10 11
#include "Process.h.in"
#include "System.h.in"
12 13
#endif

14 15 16
/*

Implementation for UNIX
17

18 19 20 21 22 23 24 25 26
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.

27 28
*/

29 30 31 32 33 34 35 36 37 38 39
/*

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.

*/

40 41 42
#if defined(__CYGWIN__)
/* Increase the file descriptor limit for select() before including
   related system headers. (Default: 64) */
43
#define FD_SETSIZE 16384
44 45
#endif

46 47 48 49 50 51
#include <assert.h>    /* assert */
#include <ctype.h>     /* isspace */
#include <dirent.h>    /* DIR, dirent */
#include <errno.h>     /* errno */
#include <fcntl.h>     /* fcntl */
#include <signal.h>    /* sigaction */
52
#include <stddef.h>    /* ptrdiff_t */
53 54 55
#include <stdio.h>     /* snprintf */
#include <stdlib.h>    /* malloc, free */
#include <string.h>    /* strdup, strerror, memset */
56
#include <sys/stat.h>  /* open mode */
57 58 59 60
#include <sys/time.h>  /* struct timeval */
#include <sys/types.h> /* pid_t, fd_set */
#include <sys/wait.h>  /* waitpid */
#include <time.h>      /* gettimeofday */
61
#include <unistd.h>    /* pipe, close, fork, execvp, select, _exit */
62

63
#if defined(__VMS)
64
#define KWSYSPE_VMS_NONBLOCK , O_NONBLOCK
65
#else
66
#define KWSYSPE_VMS_NONBLOCK
67 68
#endif

69 70 71 72 73 74 75 76 77 78 79 80
#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

81
#if defined(__BEOS__) && !defined(__ZETA__)
82
/* BeOS 5 doesn't have usleep(), but it has snooze(), which is identical. */
83
#include <be/kernel/OS.h>
84 85 86 87 88
static inline void kwsysProcess_usleep(unsigned int msec)
{
  snooze(msec);
}
#else
89
#define kwsysProcess_usleep usleep
90 91 92 93 94 95 96 97 98 99 100 101
#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().
 */
102 103
#if !defined(__BEOS__) && !defined(__VMS) && !defined(__MINT__) &&            \
  !defined(KWSYSPE_USE_SELECT)
104
#define KWSYSPE_USE_SELECT 1
105 106
#endif

107 108
/* Some platforms do not have siginfo on their signal handlers.  */
#if defined(SA_SIGINFO) && !defined(__BEOS__)
109
#define KWSYSPE_USE_SIGINFO 1
110 111
#endif

112
/* The number of pipes for the child's output.  The standard stdout
113 114 115 116
   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.  */
117 118 119
#define KWSYSPE_PIPE_COUNT 3
#define KWSYSPE_PIPE_STDOUT 0
#define KWSYSPE_PIPE_STDERR 1
120
#define KWSYSPE_PIPE_SIGNAL 2
121 122 123 124

/* The maximum amount to read from a pipe at a time.  */
#define KWSYSPE_PIPE_BUFFER_SIZE 1024

125 126 127 128 129 130 131 132 133 134
/* 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;
};
135

136 137
typedef struct kwsysProcessCreateInformation_s
{
138 139 140 141
  int StdIn;
  int StdOut;
  int StdErr;
  int ErrorPipe[2];
142 143
} kwsysProcessCreateInformation;

144
static void kwsysProcessVolatileFree(volatile void* p);
145
static int kwsysProcessInitialize(kwsysProcess* cp);
146 147
static void kwsysProcessCleanup(kwsysProcess* cp, int error);
static void kwsysProcessCleanupDescriptor(int* pfd);
148
static void kwsysProcessClosePipes(kwsysProcess* cp);
149
static int kwsysProcessSetNonBlocking(int fd);
150
static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
151
                              kwsysProcessCreateInformation* si);
152
static void kwsysProcessDestroy(kwsysProcess* cp);
153
static int kwsysProcessSetupOutputPipeFile(int* p, const char* name);
154
static int kwsysProcessSetupOutputPipeNative(int* p, int des[2]);
155 156 157
static int kwsysProcessGetTimeoutTime(kwsysProcess* cp, double* userTimeout,
                                      kwsysProcessTime* timeoutTime);
static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
158
                                      double* userTimeout,
159 160
                                      kwsysProcessTimeNative* timeoutLength,
                                      int zeroIsExpired);
161
static kwsysProcessTime kwsysProcessTimeGetCurrent(void);
162 163 164
static double kwsysProcessTimeToDouble(kwsysProcessTime t);
static kwsysProcessTime kwsysProcessTimeFromDouble(double d);
static int kwsysProcessTimeLess(kwsysProcessTime in1, kwsysProcessTime in2);
165 166 167 168
static kwsysProcessTime kwsysProcessTimeAdd(kwsysProcessTime in1,
                                            kwsysProcessTime in2);
static kwsysProcessTime kwsysProcessTimeSubtract(kwsysProcessTime in1,
                                                 kwsysProcessTime in2);
169 170
static void kwsysProcessSetExitExceptionByIndex(kwsysProcess* cp, int sig,
                                                int idx);
171
static void kwsysProcessChildErrorExit(int errorPipe);
172
static void kwsysProcessRestoreDefaultSignalHandlers(void);
173 174
static pid_t kwsysProcessFork(kwsysProcess* cp,
                              kwsysProcessCreateInformation* si);
175
static void kwsysProcessKill(pid_t process_id);
176
#if defined(__VMS)
177
static int kwsysProcessSetVMSFeature(const char* name, int value);
178
#endif
179 180
static int kwsysProcessesAdd(kwsysProcess* cp);
static void kwsysProcessesRemove(kwsysProcess* cp);
181
#if KWSYSPE_USE_SIGINFO
182 183
static void kwsysProcessesSignalHandler(int signum, siginfo_t* info,
                                        void* ucontext);
184 185 186
#else
static void kwsysProcessesSignalHandler(int signum);
#endif
187

188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
/* A structure containing results data for each process.  */
typedef struct kwsysProcessResults_s kwsysProcessResults;
struct kwsysProcessResults_s
{
  /* The status of the child process. */
  int State;

  /* The exceptional behavior that terminated the process, if any.  */
  int ExitException;

  /* The process exit code.  */
  int ExitCode;

  /* The process return code, if any.  */
  int ExitValue;

  /* Description for the ExitException.  */
  char ExitExceptionString[KWSYSPE_PIPE_BUFFER_SIZE + 1];
};

208 209 210
/* Structure containing data used to implement the child's execution.  */
struct kwsysProcess_s
{
211 212
  /* The command lines to execute.  */
  char*** Commands;
213
  volatile int NumberOfCommands;
214

215 216
  /* Descriptors for the read ends of the child's output pipes and
     the signal pipe. */
217
  int PipeReadEnds[KWSYSPE_PIPE_COUNT];
218

219 220 221 222
  /* Descriptors for the child's ends of the pipes.
     Used temporarily during process creation.  */
  int PipeChildStd[3];

223 224 225
  /* Write descriptor for child termination signal pipe.  */
  int SignalPipe;

226 227 228
  /* Buffer for pipe data.  */
  char PipeBuffer[KWSYSPE_PIPE_BUFFER_SIZE];

229 230 231 232
  /* Process IDs returned by the calls to fork.  Everything is volatile
     because the signal handler accesses them.  You must be very careful
     when reaping PIDs or modifying this array to avoid race conditions.  */
  volatile pid_t* volatile ForkPIDs;
233

234
  /* Flag for whether the children were terminated by a failed select.  */
235
  int SelectError;
236

237
  /* The timeout length.  */
238
  double Timeout;
239 240 241

  /* The working directory for the process. */
  char* WorkingDirectory;
242

243 244 245 246 247 248
  /* Whether to create the child as a detached process.  */
  int OptionDetach;

  /* Whether the child was created as a detached process.  */
  int Detached;

249 250 251
  /* Whether to treat command lines as verbatim.  */
  int Verbatim;

252 253 254
  /* Whether to merge stdout/stderr of the child.  */
  int MergeOutput;

255 256 257
  /* Whether to create the process in a new process group.  */
  volatile sig_atomic_t CreateProcessGroup;

258 259
  /* Time at which the child started.  Negative for no timeout.  */
  kwsysProcessTime StartTime;
260

261 262
  /* Time at which the child will timeout.  Negative for no timeout.  */
  kwsysProcessTime TimeoutTime;
263

264 265
  /* Flag for whether the timeout expired.  */
  int TimeoutExpired;
266

267 268
  /* The number of pipes left open during execution.  */
  int PipesLeft;
269

270
#if KWSYSPE_USE_SELECT
271 272
  /* File descriptor set for call to select.  */
  fd_set PipeSet;
273
#endif
274

275 276 277
  /* The number of children still executing.  */
  int CommandsLeft;

278
  /* The status of the process structure.  Must be atomic because
279 280
     the signal handler checks this to avoid a race.  */
  volatile sig_atomic_t State;
281

282
  /* Whether the process was killed.  */
283
  volatile sig_atomic_t Killed;
284

285
  /* Buffer for error message in case of failure.  */
286
  char ErrorMessage[KWSYSPE_PIPE_BUFFER_SIZE + 1];
287

288 289
  /* process results.  */
  kwsysProcessResults* ProcessResults;
290

291 292
  /* The exit codes of each child process in the pipeline.  */
  int* CommandExitCodes;
293 294 295 296 297 298

  /* Name of files to which stdin and stdout pipes are attached.  */
  char* PipeFileSTDIN;
  char* PipeFileSTDOUT;
  char* PipeFileSTDERR;

299 300 301 302 303
  /* Whether each pipe is shared with the parent process.  */
  int PipeSharedSTDIN;
  int PipeSharedSTDOUT;
  int PipeSharedSTDERR;

304 305 306 307 308
  /* Native pipes provided by the user.  */
  int PipeNativeSTDIN[2];
  int PipeNativeSTDOUT[2];
  int PipeNativeSTDERR[2];

309 310 311
  /* The real working directory of this process.  */
  int RealWorkingDirectoryLength;
  char* RealWorkingDirectory;
312 313
};

314
kwsysProcess* kwsysProcess_New(void)
315 316 317
{
  /* Allocate a process control structure.  */
  kwsysProcess* cp = (kwsysProcess*)malloc(sizeof(kwsysProcess));
318
  if (!cp) {
319
    return 0;
320
  }
321
  memset(cp, 0, sizeof(kwsysProcess));
322 323 324 325

  /* Share stdin with the parent process by default.  */
  cp->PipeSharedSTDIN = 1;

326 327 328 329 330 331 332 333
  /* 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;

334
  /* Set initial status.  */
335
  cp->State = kwsysProcess_State_Starting;
336

337 338 339 340 341
  return cp;
}

void kwsysProcess_Delete(kwsysProcess* cp)
{
342
  /* Make sure we have an instance.  */
343
  if (!cp) {
344
    return;
345
  }
346

347
  /* If the process is executing, wait for it to finish.  */
348 349
  if (cp->State == kwsysProcess_State_Executing) {
    if (cp->Detached) {
350
      kwsysProcess_Disown(cp);
351
    } else {
352
      kwsysProcess_WaitForExit(cp, 0);
353
    }
354
  }
355

356 357
  /* Free memory.  */
  kwsysProcess_SetCommand(cp, 0);
358
  kwsysProcess_SetWorkingDirectory(cp, 0);
359 360 361
  kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDIN, 0);
  kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDOUT, 0);
  kwsysProcess_SetPipeFile(cp, kwsysProcess_Pipe_STDERR, 0);
362
  free(cp->CommandExitCodes);
363
  free(cp->ProcessResults);
364 365 366
  free(cp);
}

367
int kwsysProcess_SetCommand(kwsysProcess* cp, char const* const* command)
368
{
369
  int i;
370
  if (!cp) {
371
    return 0;
372 373
  }
  for (i = 0; i < cp->NumberOfCommands; ++i) {
374
    char** c = cp->Commands[i];
375
    while (*c) {
376
      free(*c++);
377
    }
378 379
    free(cp->Commands[i]);
  }
380
  cp->NumberOfCommands = 0;
381
  if (cp->Commands) {
382 383
    free(cp->Commands);
    cp->Commands = 0;
384 385
  }
  if (command) {
386
    return kwsysProcess_AddCommand(cp, command);
387
  }
388 389 390 391 392 393 394 395 396
  return 1;
}

int kwsysProcess_AddCommand(kwsysProcess* cp, char const* const* command)
{
  int newNumberOfCommands;
  char*** newCommands;

  /* Make sure we have a command to add.  */
397
  if (!cp || !command || !*command) {
398
    return 0;
399
  }
400 401 402

  /* Allocate a new array for command pointers.  */
  newNumberOfCommands = cp->NumberOfCommands + 1;
403 404
  if (!(newCommands =
          (char***)malloc(sizeof(char**) * (size_t)(newNumberOfCommands)))) {
405 406
    /* Out of memory.  */
    return 0;
407
  }
408 409 410

  /* Copy any existing commands into the new array.  */
  {
411 412 413
    int i;
    for (i = 0; i < cp->NumberOfCommands; ++i) {
      newCommands[i] = cp->Commands[i];
414 415 416 417
    }
  }

  /* Add the new command.  */
418
  if (cp->Verbatim) {
419 420 421
    /* In order to run the given command line verbatim we need to
       parse it.  */
    newCommands[cp->NumberOfCommands] =
422
      kwsysSystem_Parse_CommandForUnix(*command, 0);
423 424
    if (!newCommands[cp->NumberOfCommands] ||
        !newCommands[cp->NumberOfCommands][0]) {
425
      /* Out of memory or no command parsed.  */
426 427
      free(newCommands);
      return 0;
428
    }
429
  } else {
430 431
    /* Copy each argument string individually.  */
    char const* const* c = command;
432 433
    kwsysProcess_ptrdiff_t n = 0;
    kwsysProcess_ptrdiff_t i = 0;
434 435
    while (*c++)
      ;
436
    n = c - command - 1;
Francois Bertel's avatar
Francois Bertel committed
437
    newCommands[cp->NumberOfCommands] =
438 439
      (char**)malloc((size_t)(n + 1) * sizeof(char*));
    if (!newCommands[cp->NumberOfCommands]) {
440 441 442
      /* Out of memory.  */
      free(newCommands);
      return 0;
443 444
    }
    for (i = 0; i < n; ++i) {
445
      assert(command[i]); /* Quiet Clang scan-build. */
446
      newCommands[cp->NumberOfCommands][i] = strdup(command[i]);
447
      if (!newCommands[cp->NumberOfCommands][i]) {
448 449
        break;
      }
450 451
    }
    if (i < n) {
452
      /* Out of memory.  */
453 454 455
      for (; i > 0; --i) {
        free(newCommands[cp->NumberOfCommands][i - 1]);
      }
456 457
      free(newCommands);
      return 0;
458
    }
459 460
    newCommands[cp->NumberOfCommands][n] = 0;
  }
461 462 463 464 465 466 467

  /* Successfully allocated new command array.  Free the old array. */
  free(cp->Commands);
  cp->Commands = newCommands;
  cp->NumberOfCommands = newNumberOfCommands;

  return 1;
468 469 470 471
}

void kwsysProcess_SetTimeout(kwsysProcess* cp, double timeout)
{
472
  if (!cp) {
473
    return;
474
  }
475
  cp->Timeout = timeout;
476
  if (cp->Timeout < 0) {
477
    cp->Timeout = 0;
478
  }
479 480
  // Force recomputation of TimeoutTime.
  cp->TimeoutTime.tv_sec = -1;
481 482
}

483
int kwsysProcess_SetWorkingDirectory(kwsysProcess* cp, const char* dir)
484
{
485
  if (!cp) {
486
    return 0;
487 488
  }
  if (cp->WorkingDirectory == dir) {
489
    return 1;
490 491
  }
  if (cp->WorkingDirectory && dir && strcmp(cp->WorkingDirectory, dir) == 0) {
492
    return 1;
493 494
  }
  if (cp->WorkingDirectory) {
495 496
    free(cp->WorkingDirectory);
    cp->WorkingDirectory = 0;
497 498
  }
  if (dir) {
499
    cp->WorkingDirectory = strdup(dir);
500
    if (!cp->WorkingDirectory) {
501
      return 0;
502
    }
503
  }
504 505 506
  return 1;
}

507
int kwsysProcess_SetPipeFile(kwsysProcess* cp, int prPipe, const char* file)
508 509
{
  char** pfile;
510
  if (!cp) {
511
    return 0;
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
  }
  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) {
527 528
    free(*pfile);
    *pfile = 0;
529 530
  }
  if (file) {
531
    *pfile = strdup(file);
532
    if (!*pfile) {
533 534
      return 0;
    }
535
  }
536

537 538
  /* If we are redirecting the pipe, do not share it or use a native
     pipe.  */
539
  if (*pfile) {
540
    kwsysProcess_SetPipeNative(cp, prPipe, 0);
541
    kwsysProcess_SetPipeShared(cp, prPipe, 0);
542
  }
543
  return 1;
544 545
}

546
void kwsysProcess_SetPipeShared(kwsysProcess* cp, int prPipe, int shared)
547
{
548
  if (!cp) {
549
    return;
550
  }
551

552 553 554 555 556 557 558 559 560 561 562 563 564
  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;
  }
565

566 567
  /* If we are sharing the pipe, do not redirect it to a file or use a
     native pipe.  */
568
  if (shared) {
569
    kwsysProcess_SetPipeFile(cp, prPipe, 0);
570
    kwsysProcess_SetPipeNative(cp, prPipe, 0);
571
  }
572 573 574 575 576 577
}

void kwsysProcess_SetPipeNative(kwsysProcess* cp, int prPipe, int p[2])
{
  int* pPipeNative = 0;

578
  if (!cp) {
579
    return;
580
  }
581

582 583 584 585 586 587 588 589 590 591 592 593 594
  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;
  }
595 596

  /* Copy the native pipe descriptors provided.  */
597
  if (p) {
598 599
    pPipeNative[0] = p[0];
    pPipeNative[1] = p[1];
600
  } else {
601 602
    pPipeNative[0] = -1;
    pPipeNative[1] = -1;
603
  }
604 605 606

  /* If we are using a native pipe, do not share it or redirect it to
     a file.  */
607
  if (p) {
608 609
    kwsysProcess_SetPipeFile(cp, prPipe, 0);
    kwsysProcess_SetPipeShared(cp, prPipe, 0);
610
  }
611 612
}

613 614
int kwsysProcess_GetOption(kwsysProcess* cp, int optionId)
{
615
  if (!cp) {
616
    return 0;
617
  }
618

619 620 621 622 623 624 625
  switch (optionId) {
    case kwsysProcess_Option_Detach:
      return cp->OptionDetach;
    case kwsysProcess_Option_MergeOutput:
      return cp->MergeOutput;
    case kwsysProcess_Option_Verbatim:
      return cp->Verbatim;
626 627
    case kwsysProcess_Option_CreateProcessGroup:
      return cp->CreateProcessGroup;
628 629 630
    default:
      return 0;
  }
631 632 633 634
}

void kwsysProcess_SetOption(kwsysProcess* cp, int optionId, int value)
{
635
  if (!cp) {
636
    return;
637
  }
638

639 640 641 642 643 644 645 646 647 648
  switch (optionId) {
    case kwsysProcess_Option_Detach:
      cp->OptionDetach = value;
      break;
    case kwsysProcess_Option_MergeOutput:
      cp->MergeOutput = value;
      break;
    case kwsysProcess_Option_Verbatim:
      cp->Verbatim = value;
      break;
649
    case kwsysProcess_Option_CreateProcessGroup:
650 651 652 653 654
      cp->CreateProcessGroup = value;
      break;
    default:
      break;
  }
655 656
}

657 658
int kwsysProcess_GetState(kwsysProcess* cp)
{
659
  return cp ? cp->State : kwsysProcess_State_Error;
660 661
}

662 663
int kwsysProcess_GetExitException(kwsysProcess* cp)
{
664 665 666
  return (cp && cp->ProcessResults && (cp->NumberOfCommands > 0))
    ? cp->ProcessResults[cp->NumberOfCommands - 1].ExitException
    : kwsysProcess_Exception_Other;
667 668
}

669 670
int kwsysProcess_GetExitCode(kwsysProcess* cp)
{
671 672 673
  return (cp && cp->ProcessResults && (cp->NumberOfCommands > 0))
    ? cp->ProcessResults[cp->NumberOfCommands - 1].ExitCode
    : 0;
674 675
}

676 677
int kwsysProcess_GetExitValue(kwsysProcess* cp)
{
678 679 680
  return (cp && cp->ProcessResults && (cp->NumberOfCommands > 0))
    ? cp->ProcessResults[cp->NumberOfCommands - 1].ExitValue
    : -1;
681 682
}

683 684
const char* kwsysProcess_GetErrorString(kwsysProcess* cp)
{
685
  if (!cp) {
686
    return "Process management structure could not be allocated";
687
  } else if (cp->State == kwsysProcess_State_Error) {
688
    return cp->ErrorMessage;
689
  }
690 691 692 693 694
  return "Success";
}

const char* kwsysProcess_GetExceptionString(kwsysProcess* cp)
{
695
  if (!(cp && cp->ProcessResults && (cp->NumberOfCommands > 0))) {
696
    return "GetExceptionString called with NULL process management structure";
697
  } else if (cp->State == kwsysProcess_State_Exception) {
698
    return cp->ProcessResults[cp->NumberOfCommands - 1].ExitExceptionString;
699
  }
700
  return "No exception";
701 702
}

703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744
/* the index should be in array bound. */
#define KWSYSPE_IDX_CHK(RET)                                                  \
  if (!cp || idx >= cp->NumberOfCommands || idx < 0) {                        \
    return RET;                                                               \
  }

int kwsysProcess_GetStateByIndex(kwsysProcess* cp, int idx)
{
  KWSYSPE_IDX_CHK(kwsysProcess_State_Error)
  return cp->ProcessResults[idx].State;
}

int kwsysProcess_GetExitExceptionByIndex(kwsysProcess* cp, int idx)
{
  KWSYSPE_IDX_CHK(kwsysProcess_Exception_Other)
  return cp->ProcessResults[idx].ExitException;
}

int kwsysProcess_GetExitValueByIndex(kwsysProcess* cp, int idx)
{
  KWSYSPE_IDX_CHK(-1)
  return cp->ProcessResults[idx].ExitValue;
}

int kwsysProcess_GetExitCodeByIndex(kwsysProcess* cp, int idx)
{
  KWSYSPE_IDX_CHK(-1)
  return cp->CommandExitCodes[idx];
}

const char* kwsysProcess_GetExceptionStringByIndex(kwsysProcess* cp, int idx)
{
  KWSYSPE_IDX_CHK("GetExceptionString called with NULL process management "
                  "structure or index out of bound")
  if (cp->ProcessResults[idx].State == kwsysProcess_StateByIndex_Exception) {
    return cp->ProcessResults[idx].ExitExceptionString;
  }
  return "No exception";
}

#undef KWSYSPE_IDX_CHK

745 746 747
void kwsysProcess_Execute(kwsysProcess* cp)
{
  int i;
748

749
  /* Do not execute a second copy simultaneously.  */
750
  if (!cp || cp->State == kwsysProcess_State_Executing) {
751
    return;
752
  }
753

754
  /* Make sure we have something to run.  */
755
  if (cp->NumberOfCommands < 1) {
756 757 758
    strcpy(cp->ErrorMessage, "No command");
    cp->State = kwsysProcess_State_Error;
    return;
759
  }
760

761
  /* Initialize the control structure for a new process.  */
762
  if (!kwsysProcessInitialize(cp)) {
763 764 765
    strcpy(cp->ErrorMessage, "Out of memory");
    cp->State = kwsysProcess_State_Error;
    return;
766
  }
767

768
#if defined(__VMS)
769
  /* Make sure pipes behave like streams on VMS.  */
770
  if (!kwsysProcessSetVMSFeature("DECC$STREAM_PIPE", 1)) {
771 772
    kwsysProcessCleanup(cp, 1);
    return;
773
  }
774
#endif
775

776 777 778
  /* 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.  */
779
  if (cp->WorkingDirectory) {
780
    int r;
781 782
    if (!getcwd(cp->RealWorkingDirectory,
                (size_t)(cp->RealWorkingDirectoryLength))) {
783 784
      kwsysProcessCleanup(cp, 1);
      return;
785
    }
786 787 788

    /* Some platforms specify that the chdir call may be
       interrupted.  Repeat the call until it finishes.  */
789 790 791
    while (((r = chdir(cp->WorkingDirectory)) < 0) && (errno == EINTR))
      ;
    if (r < 0) {
792
      kwsysProcessCleanup(cp, 1);
793
      return;
794
    }
795
  }
796

797 798 799
  /* 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.  */
800 801
  if (!cp->OptionDetach) {
    if (!kwsysProcessesAdd(cp)) {
802 803
      kwsysProcessCleanup(cp, 1);
      return;
804
    }
805
  }
806

807
  /* Setup the stdin pipe for the first process.  */
808
  if (cp->PipeFileSTDIN) {
809 810
    /* Open a file for the child's stdin to read.  */
    cp->PipeChildStd[0] = open(cp->PipeFileSTDIN, O_RDONLY);
811
    if (cp->PipeChildStd[0] < 0) {
812 813
      kwsysProcessCleanup(cp, 1);
      return;
814
    }
815 816

    /* Set close-on-exec flag on the pipe's end.  */
817
    if (fcntl(cp->PipeChildStd[0], F_SETFD, FD_CLOEXEC) < 0) {
818 819 820
      kwsysProcessCleanup(cp, 1);
      return;
    }
821
  } else if (cp->PipeSharedSTDIN) {
822
    cp->PipeChildStd[0] = 0;
823
  } else if (cp->PipeNativeSTDIN[0] >= 0) {
824 825 826 827 828
    cp->PipeChildStd[0] = 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.  */
829 830
    if ((fcntl(cp->PipeNativeSTDIN[0], F_SETFD, FD_CLOEXEC) < 0) ||
        (fcntl(cp->PipeNativeSTDIN[1], F_SETFD, FD_CLOEXEC) < 0)) {
831 832 833
      kwsysProcessCleanup(cp, 1);
      return;
    }
834
  } else {
835
    cp->PipeChildStd[0] = -1;
836
  }
837 838 839 840

  /* Create the output pipe for the last process.
     We always create this so the pipe can be passed to select even if
     it will report closed immediately.  */
841
  {
842 843 844 845 846
    /* Create the pipe.  */
    int p[2];
    if (pipe(p KWSYSPE_VMS_NONBLOCK) < 0) {
      kwsysProcessCleanup(cp, 1);
      return;
847
    }
848

849 850 851
    /* Store the pipe.  */
    cp->PipeReadEnds[KWSYSPE_PIPE_STDOUT] = p[0];
    cp->PipeChildStd[1] = p[1];
852

853 854 855 856 857
    /* 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);
      return;
858
    }
859

860 861 862 863 864
    /* Set to non-blocking in case select lies, or for the polling
       implementation.  */
    if (!kwsysProcessSetNonBlocking(p[0])) {
      kwsysProcessCleanup(cp, 1);
      return;
865
    }
866
  }
867

868
  if (cp->PipeFileSTDOUT) {
869
    /* Use a file for stdout.  */
870 871
    if (!kwsysProcessSetupOutputPipeFile(&cp->PipeChildStd[1],
                                         cp->PipeFileSTDOUT)) {
872 873 874
      kwsysProcessCleanup(cp, 1);
      return;
    }
875
  } else if (cp->PipeSharedSTDOUT) {
876 877 878
    /* Use the parent stdout.  */
    kwsysProcessCleanupDescriptor(&cp->PipeChildStd[1]);
    cp->PipeChildStd[1] = 1;
879
  } else if (cp->PipeNativeSTDOUT[1] >= 0) {
880
    /* Use the given descriptor for stdout.  */
881 882
    if (!kwsysProcessSetupOutputPipeNative(&cp->PipeChildStd[1],
                                           cp->PipeNativeSTDOUT)) {
883 884 885
      kwsysProcessCleanup(cp, 1);
      return;
    }
886
  }
887

888 889 890 891
  /* Create stderr pipe to be shared by all processes in the pipeline.
     We always create this so the pipe can be passed to select even if
     it will report closed immediately.  */
  {
892 893 894 895 896
    /* Create the pipe.  */
    int p[2];
    if (pipe(p KWSYSPE_VMS_NONBLOCK) < 0) {
      kwsysProcessCleanup(cp, 1);
      return;
897 898
    }

899 900 901
    /* Store the pipe.  */
    cp->PipeReadEnds[KWSYSPE_PIPE_STDERR] = p[0];
    cp->PipeChildStd[2] = p[1];
902

903 904 905 906 907
    /* 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);
      return;
908 909
    }

910 911 912 913 914
    /* Set to non-blocking in case select lies, or for the polling
       implementation.  */
    if (!kwsysProcessSetNonBlocking(p[0])) {
      kwsysProcessCleanup(cp, 1);
      return;
915
    }
916
  }
917

918
  if (cp->PipeFileSTDERR) {
919
    /* Use a file for stderr.  */
920 921
    if (!kwsysProcessSetupOutputPipeFile(&cp->PipeChildStd[2],
                                         cp->PipeFileSTDERR)) {
922 923 924
      kwsysProcessCleanup(cp, 1);
      return;
    }
925
  } else if (cp->PipeSharedSTDERR) {
926 927 928
    /* Use the parent stderr.  */
    kwsysProcessCleanupDescriptor(&cp->PipeChildStd[2]);
    cp->PipeChildStd[2] = 2;
929
  } else if (cp->PipeNativeSTDERR[1] >= 0) {
930
    /* Use the given handle for stderr.  */
931 932
    if (!kwsysProcessSetupOutputPipeNative(&cp->PipeChildStd[2],
                                           cp->PipeNativeSTDERR)) {
933 934 935
      kwsysProcessCleanup(cp, 1);
      return;
    }
936
  }
937

938 939 940 941
  /* The timeout period starts now.  */
  cp->StartTime = kwsysProcessTimeGetCurrent();
  cp->TimeoutTime.tv_sec = -1;
  cp->TimeoutTime.tv_usec = -1;
942

943 944
  /* Create the pipeline of processes.  */
  {
945 946 947 948 949 950 951 952 953 954 955 956 957 958
    kwsysProcessCreateInformation si = { -1, -1, -1, { -1, -1 } };
    int nextStdIn = cp->PipeChildStd[0];
    for (i = 0; i < cp->NumberOfCommands; ++i) {
      /* Setup the process's pipes.  */
      si.StdIn = nextStdIn;
      if (i == cp->NumberOfCommands - 1) {
        nextStdIn = -1;
        si.StdOut = cp->PipeChildStd[1];
      } else {
        /* Create a pipe to sit between the children.  */
        int p[2] = { -1, -1 };
        if (pipe(p KWSYSPE_VMS_NONBLOCK) < 0) {
          if (nextStdIn != cp->PipeChildStd[0]) {
            kwsysProcessCleanupDescriptor(&nextStdIn);
959
          }
960 961
          kwsysProcessCleanup(cp, 1);
          return;
962
        }
963

964 965 966 967 968 969 970
        /* 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)) {
          close(p[0]);
          close(p[1]);
          if (nextStdIn != cp->PipeChildStd[0]) {
            kwsysProcessCleanupDescriptor(&nextStdIn);
971
          }
972 973
          kwsysProcessCleanup(cp, 1);
          return;
974
        }
975 976
        nextStdIn = p[0];
        si.StdOut = p[1];
977
      }
978
      si.StdErr = cp->MergeOutput ? cp->PipeChildStd[1] : cp->PipeChildStd[2];
979 980

      {
981
        int res = kwsysProcessCreate(cp, i, &si);
982

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
        /* Close our copies of pipes used between children.  */
        if (si.StdIn != cp->PipeChildStd[0]) {
          kwsysProcessCleanupDescriptor(&si.StdIn);
        }
        if (si.StdOut != cp->PipeChildStd[1]) {
          kwsysProcessCleanupDescriptor(&si.StdOut);
        }
        if (si.StdErr != cp->PipeChildStd[2] && !cp->MergeOutput) {
          kwsysProcessCleanupDescriptor(&si.StdErr);
        }

        if (!res) {
          kwsysProcessCleanupDescriptor(&si.ErrorPipe[0]);
          kwsysProcessCleanupDescriptor(&si.ErrorPipe[1]);
          if (nextStdIn != cp->PipeChildStd[0]) {
            kwsysProcessCleanupDescriptor(&nextStdIn);
          }
          kwsysProcessCleanup(cp, 1);
          return;
1002
        }
1003
      }
1004
    }
1005
  }
1006

1007
  /* The parent process does not need the child's pipe ends.  */
1008
  for (i = 0; i < 3; ++i) {
1009
    kwsysProcessCleanupDescriptor(&cp->PipeChildStd[i]);
1010
  }
1011

1012
  /* Restore the working directory. */
1013
  if (cp->RealWorkingDirectory) {
1014 1015
    /* Some platforms specify that the chdir call may be
       interrupted.  Repeat the call until it finishes.  */
1016 1017
    while ((chdir(cp->RealWorkingDirectory) < 0) && (errno == EINTR))
      ;
1018 1019
    free(cp->RealWorkingDirectory);
    cp->RealWorkingDirectory = 0;
1020