-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessUNIX.c
2957 lines (2654 loc) · 85.1 KB
/
ProcessUNIX.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
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
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
#if !defined(_WIN32) && !defined(__APPLE__) && !defined(__OpenBSD__)
/* NOLINTNEXTLINE(bugprone-reserved-identifier) */
# define _XOPEN_SOURCE 600
#endif
#include "kwsysPrivate.h"
#include KWSYS_HEADER(Process.h)
#include KWSYS_HEADER(System.h)
/* Work-around CMake dependency scanning limitation. This must
duplicate the above list of headers. */
#if 0
# include "Process.h.in"
# include "System.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.
*/
#if defined(__CYGWIN__)
/* Increase the file descriptor limit for select() before including
related system headers. (Default: 64) */
# define FD_SETSIZE 16384
#elif defined(__APPLE__)
/* Increase the file descriptor limit for select() before including
related system headers. (Default: 1024) */
# define _DARWIN_UNLIMITED_SELECT
# include <limits.h> /* OPEN_MAX */
# define FD_SETSIZE OPEN_MAX
#endif
#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 */
#include <stddef.h> /* ptrdiff_t */
#include <stdio.h> /* snprintf */
#include <stdlib.h> /* malloc, free */
#include <string.h> /* strdup, strerror, memset */
#include <sys/stat.h> /* open mode */
#include <sys/time.h> /* struct timeval */
#include <sys/types.h> /* pid_t, fd_set */
#include <sys/wait.h> /* waitpid */
#include <time.h> /* gettimeofday */
#include <unistd.h> /* pipe, close, fork, execvp, select, _exit */
#if defined(__VMS)
# define KWSYSPE_VMS_NONBLOCK , O_NONBLOCK
#else
# define KWSYSPE_VMS_NONBLOCK
#endif
#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__) && !defined(__VMS) && !defined(__MINT__) && \
!defined(KWSYSPE_USE_SELECT)
# define KWSYSPE_USE_SELECT 1
#endif
/* Some platforms do not have siginfo on their signal handlers. */
#if defined(SA_SIGINFO) && !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
#if defined(__NVCOMPILER)
# pragma diag_suppress 550 /* variable set but never used (in FD_ZERO) */
#endif
/* 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 void kwsysProcessVolatileFree(volatile void* p);
static int kwsysProcessInitialize(kwsysProcess* cp);
static void kwsysProcessCleanup(kwsysProcess* cp, int error);
static void kwsysProcessCleanupDescriptor(int* pfd);
static void kwsysProcessClosePipes(kwsysProcess* cp);
static int kwsysProcessSetNonBlocking(int fd);
static int kwsysProcessCreate(kwsysProcess* cp, int prIndex,
kwsysProcessCreateInformation* si);
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,
const double* userTimeout,
kwsysProcessTime* timeoutTime);
static int kwsysProcessGetTimeoutLeft(kwsysProcessTime* timeoutTime,
const double* userTimeout,
kwsysProcessTimeNative* timeoutLength,
int zeroIsExpired);
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 kwsysProcessSetExitExceptionByIndex(kwsysProcess* cp, int sig,
int idx);
static void kwsysProcessChildErrorExit(int errorPipe);
static void kwsysProcessRestoreDefaultSignalHandlers(void);
static pid_t kwsysProcessFork(kwsysProcess* cp,
kwsysProcessCreateInformation* si);
static void kwsysProcessKill(pid_t process_id);
#if defined(__VMS)
static int kwsysProcessSetVMSFeature(const char* name, int value);
#endif
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
/* 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];
};
/* Structure containing data used to implement the child's execution. */
struct kwsysProcess_s
{
/* The command lines to execute. */
char*** Commands;
volatile int NumberOfCommands;
/* Descriptors for the read ends of the child's output pipes and
the signal pipe. */
int PipeReadEnds[KWSYSPE_PIPE_COUNT];
/* Descriptors for the child's ends of the pipes.
Used temporarily during process creation. */
int PipeChildStd[3];
/* 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. 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;
/* Flag for whether the children were terminated by a failed 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;
/* Whether to merge stdout/stderr of the child. */
int MergeOutput;
/* Whether to create the process in a new process group. */
volatile sig_atomic_t CreateProcessGroup;
/* 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 status of the process structure. Must be atomic because
the signal handler checks this to avoid a race. */
volatile sig_atomic_t State;
/* Whether the process was killed. */
volatile sig_atomic_t Killed;
/* Buffer for error message in case of failure. */
char ErrorMessage[KWSYSPE_PIPE_BUFFER_SIZE + 1];
/* process results. */
kwsysProcessResults* ProcessResults;
/* 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);
free(cp->CommandExitCodes);
free(cp->ProcessResults);
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**) * (size_t)(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] =
kwsysSystem_Parse_CommandForUnix(*command, 0);
if (!newCommands[cp->NumberOfCommands] ||
!newCommands[cp->NumberOfCommands][0]) {
/* Out of memory or no command parsed. */
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((size_t)(n + 1) * sizeof(char*));
if (!newCommands[cp->NumberOfCommands]) {
/* Out of memory. */
free(newCommands);
return 0;
}
for (i = 0; i < n; ++i) {
assert(command[i]); /* Quiet Clang scan-build. */
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;
}
// Force recomputation of TimeoutTime.
cp->TimeoutTime.tv_sec = -1;
}
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 = strdup(dir);
if (!cp->WorkingDirectory) {
return 0;
}
}
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 = strdup(file);
if (!*pfile) {
return 0;
}
}
/* 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, const 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_MergeOutput:
return cp->MergeOutput;
case kwsysProcess_Option_Verbatim:
return cp->Verbatim;
case kwsysProcess_Option_CreateProcessGroup:
return cp->CreateProcessGroup;
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_MergeOutput:
cp->MergeOutput = value;
break;
case kwsysProcess_Option_Verbatim:
cp->Verbatim = value;
break;
case kwsysProcess_Option_CreateProcessGroup:
cp->CreateProcessGroup = value;
break;
default:
break;
}
}
int kwsysProcess_GetState(kwsysProcess* cp)
{
return cp ? cp->State : kwsysProcess_State_Error;
}
int kwsysProcess_GetExitException(kwsysProcess* cp)
{
return (cp && cp->ProcessResults && (cp->NumberOfCommands > 0))
? cp->ProcessResults[cp->NumberOfCommands - 1].ExitException
: kwsysProcess_Exception_Other;
}
int kwsysProcess_GetExitCode(kwsysProcess* cp)
{
return (cp && cp->ProcessResults && (cp->NumberOfCommands > 0))
? cp->ProcessResults[cp->NumberOfCommands - 1].ExitCode
: 0;
}
int kwsysProcess_GetExitValue(kwsysProcess* cp)
{
return (cp && cp->ProcessResults && (cp->NumberOfCommands > 0))
? cp->ProcessResults[cp->NumberOfCommands - 1].ExitValue
: -1;
}
const char* kwsysProcess_GetErrorString(kwsysProcess* cp)
{
if (!cp) {
return "Process management structure could not be allocated";
}
if (cp->State == kwsysProcess_State_Error) {
return cp->ErrorMessage;
}
return "Success";
}
const char* kwsysProcess_GetExceptionString(kwsysProcess* cp)
{
if (!(cp && cp->ProcessResults && (cp->NumberOfCommands > 0))) {
return "GetExceptionString called with NULL process management structure";
}
if (cp->State == kwsysProcess_State_Exception) {
return cp->ProcessResults[cp->NumberOfCommands - 1].ExitExceptionString;
}
return "No exception";
}
/* 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
void kwsysProcess_Execute(kwsysProcess* cp)
{
int i;
/* Do not execute a second copy simultaneously. */
if (!cp || cp->State == kwsysProcess_State_Executing) {
return;
}
/* Make sure we have something to run. */
if (cp->NumberOfCommands < 1) {
strcpy(cp->ErrorMessage, "No command");
cp->State = kwsysProcess_State_Error;
return;
}
/* Initialize the control structure for a new process. */
if (!kwsysProcessInitialize(cp)) {
strcpy(cp->ErrorMessage, "Out of memory");
cp->State = kwsysProcess_State_Error;
return;
}
#if defined(__VMS)
/* Make sure pipes behave like streams on VMS. */
if (!kwsysProcessSetVMSFeature("DECC$STREAM_PIPE", 1)) {
kwsysProcessCleanup(cp, 1);
return;
}
#endif
/* 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,
(size_t)(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 stdin pipe for the first process. */
if (cp->PipeFileSTDIN) {
/* Open a file for the child's stdin to read. */
cp->PipeChildStd[0] = open(cp->PipeFileSTDIN, O_RDONLY);
if (cp->PipeChildStd[0] < 0) {
kwsysProcessCleanup(cp, 1);
return;
}
/* Set close-on-exec flag on the pipe's end. */
if (fcntl(cp->PipeChildStd[0], F_SETFD, FD_CLOEXEC) < 0) {
kwsysProcessCleanup(cp, 1);
return;
}
} else if (cp->PipeSharedSTDIN) {
cp->PipeChildStd[0] = 0;
} else if (cp->PipeNativeSTDIN[0] >= 0) {
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. */
if ((fcntl(cp->PipeNativeSTDIN[0], F_SETFD, FD_CLOEXEC) < 0) ||
(fcntl(cp->PipeNativeSTDIN[1], F_SETFD, FD_CLOEXEC) < 0)) {
kwsysProcessCleanup(cp, 1);
return;
}
} else {
cp->PipeChildStd[0] = -1;
}
/* 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. */
{
/* Create the pipe. */
int p[2];
if (pipe(p KWSYSPE_VMS_NONBLOCK) < 0) {
kwsysProcessCleanup(cp, 1);
return;
}
/* Store the pipe. */
cp->PipeReadEnds[KWSYSPE_PIPE_STDOUT] = p[0];
cp->PipeChildStd[1] = 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);
return;
}
/* Set to non-blocking in case select lies, or for the polling
implementation. */
if (!kwsysProcessSetNonBlocking(p[0])) {
kwsysProcessCleanup(cp, 1);
return;
}
}
if (cp->PipeFileSTDOUT) {
/* Use a file for stdout. */
if (!kwsysProcessSetupOutputPipeFile(&cp->PipeChildStd[1],
cp->PipeFileSTDOUT)) {
kwsysProcessCleanup(cp, 1);
return;
}
} else if (cp->PipeSharedSTDOUT) {
/* Use the parent stdout. */
kwsysProcessCleanupDescriptor(&cp->PipeChildStd[1]);
cp->PipeChildStd[1] = 1;
} else if (cp->PipeNativeSTDOUT[1] >= 0) {
/* Use the given descriptor for stdout. */
if (!kwsysProcessSetupOutputPipeNative(&cp->PipeChildStd[1],
cp->PipeNativeSTDOUT)) {
kwsysProcessCleanup(cp, 1);
return;
}
}
/* 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. */
{
/* Create the pipe. */
int p[2];
if (pipe(p KWSYSPE_VMS_NONBLOCK) < 0) {
kwsysProcessCleanup(cp, 1);
return;
}
/* Store the pipe. */
cp->PipeReadEnds[KWSYSPE_PIPE_STDERR] = p[0];
cp->PipeChildStd[2] = 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);
return;
}
/* Set to non-blocking in case select lies, or for the polling
implementation. */
if (!kwsysProcessSetNonBlocking(p[0])) {
kwsysProcessCleanup(cp, 1);
return;
}
}
if (cp->PipeFileSTDERR) {
/* Use a file for stderr. */
if (!kwsysProcessSetupOutputPipeFile(&cp->PipeChildStd[2],
cp->PipeFileSTDERR)) {
kwsysProcessCleanup(cp, 1);
return;
}
} else if (cp->PipeSharedSTDERR) {
/* Use the parent stderr. */
kwsysProcessCleanupDescriptor(&cp->PipeChildStd[2]);
cp->PipeChildStd[2] = 2;
} else if (cp->PipeNativeSTDERR[1] >= 0) {
/* Use the given handle for stderr. */
if (!kwsysProcessSetupOutputPipeNative(&cp->PipeChildStd[2],
cp->PipeNativeSTDERR)) {
kwsysProcessCleanup(cp, 1);
return;
}
}
/* The timeout period starts now. */
cp->StartTime = kwsysProcessTimeGetCurrent();
cp->TimeoutTime.tv_sec = -1;
cp->TimeoutTime.tv_usec = -1;
/* Create the pipeline of processes. */
{
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);
}
kwsysProcessCleanup(cp, 1);
return;
}
/* 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);
}
kwsysProcessCleanup(cp, 1);
return;
}
nextStdIn = p[0];
si.StdOut = p[1];
}
si.StdErr = cp->MergeOutput ? cp->PipeChildStd[1] : cp->PipeChildStd[2];
{
int res = kwsysProcessCreate(cp, i, &si);
/* Close our copies of pipes used between children. */