-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubprocess.h
1180 lines (995 loc) · 34.9 KB
/
subprocess.h
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
/*
The latest version of this library is available on GitHub;
https://github.com/sheredom/subprocess.h
*/
/*
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
*/
#ifndef SHEREDOM_SUBPROCESS_H_INCLUDED
#define SHEREDOM_SUBPROCESS_H_INCLUDED
#if defined(_MSC_VER)
#pragma warning(push, 1)
/* disable warning: '__cplusplus' is not defined as a preprocessor macro,
* replacing with '0' for '#if/#elif' */
#pragma warning(disable : 4668)
#endif
#include <stdio.h>
#include <string.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if defined(_MSC_VER)
#define subprocess_pure
#define subprocess_weak __inline
#define subprocess_tls __declspec(thread)
#elif defined(__MINGW32__)
#define subprocess_pure __attribute__((pure))
#define subprocess_weak static __attribute__((used))
#define subprocess_tls __thread
#elif defined(__clang__) || defined(__GNUC__)
#define subprocess_pure __attribute__((pure))
#define subprocess_weak __attribute__((weak))
#define subprocess_tls __thread
#else
#error Non clang, non gcc, non MSVC compiler found!
#endif
struct subprocess_s;
enum subprocess_option_e {
// stdout and stderr are the same FILE.
subprocess_option_combined_stdout_stderr = 0x1,
// The child process should inherit the environment variables of the parent.
subprocess_option_inherit_environment = 0x2,
// Enable asynchronous reading of stdout/stderr before it has completed.
subprocess_option_enable_async = 0x4,
// Enable the child process to be spawned with no window visible if supported
// by the platform.
subprocess_option_no_window = 0x8,
// Search for program names in the PATH variable. Always enabled on Windows.
// Note: this will **not** search for paths in any provided custom environment
// and instead uses the PATH of the spawning process.
subprocess_option_search_user_path = 0x10
};
#if defined(__cplusplus)
extern "C" {
#endif
/// @brief Create a process.
/// @param command_line An array of strings for the command line to execute for
/// this process. The last element must be NULL to signify the end of the array.
/// The memory backing this parameter only needs to persist until this function
/// returns.
/// @param options A bit field of subprocess_option_e's to pass.
/// @param out_process The newly created process.
/// @return On success zero is returned.
subprocess_weak int subprocess_create(const char *const command_line[],
int options,
struct subprocess_s *const out_process);
/// @brief Create a process (extended create).
/// @param command_line An array of strings for the command line to execute for
/// this process. The last element must be NULL to signify the end of the array.
/// The memory backing this parameter only needs to persist until this function
/// returns.
/// @param options A bit field of subprocess_option_e's to pass.
/// @param environment An optional array of strings for the environment to use
/// for a child process (each element of the form FOO=BAR). The last element
/// must be NULL to signify the end of the array.
/// @param out_process The newly created process.
/// @return On success zero is returned.
///
/// If `options` contains `subprocess_option_inherit_environment`, then
/// `environment` must be NULL.
subprocess_weak int
subprocess_create_ex(const char *const command_line[], int options,
const char *const environment[],
struct subprocess_s *const out_process);
/// @brief Get the standard input file for a process.
/// @param process The process to query.
/// @return The file for standard input of the process.
///
/// The file returned can be written to by the parent process to feed data to
/// the standard input of the process.
subprocess_pure subprocess_weak FILE *
subprocess_stdin(const struct subprocess_s *const process);
/// @brief Get the standard output file for a process.
/// @param process The process to query.
/// @return The file for standard output of the process.
///
/// The file returned can be read from by the parent process to read data from
/// the standard output of the child process.
subprocess_pure subprocess_weak FILE *
subprocess_stdout(const struct subprocess_s *const process);
/// @brief Get the standard error file for a process.
/// @param process The process to query.
/// @return The file for standard error of the process.
///
/// The file returned can be read from by the parent process to read data from
/// the standard error of the child process.
///
/// If the process was created with the subprocess_option_combined_stdout_stderr
/// option bit set, this function will return NULL, and the subprocess_stdout
/// function should be used for both the standard output and error combined.
subprocess_pure subprocess_weak FILE *
subprocess_stderr(const struct subprocess_s *const process);
/// @brief Wait for a process to finish execution.
/// @param process The process to wait for.
/// @param out_return_code The return code of the returned process (can be
/// NULL).
/// @return On success zero is returned.
///
/// Joining a process will close the stdin pipe to the process.
subprocess_weak int subprocess_join(struct subprocess_s *const process,
int *const out_return_code);
/// @brief Destroy a previously created process.
/// @param process The process to destroy.
/// @return On success zero is returned.
///
/// If the process to be destroyed had not finished execution, it may out live
/// the parent process.
subprocess_weak int subprocess_destroy(struct subprocess_s *const process);
/// @brief Terminate a previously created process.
/// @param process The process to terminate.
/// @return On success zero is returned.
///
/// If the process to be destroyed had not finished execution, it will be
/// terminated (i.e killed).
subprocess_weak int subprocess_terminate(struct subprocess_s *const process);
/// @brief Read the standard output from the child process.
/// @param process The process to read from.
/// @param buffer The buffer to read into.
/// @param size The maximum number of bytes to read.
/// @return The number of bytes actually read into buffer. Can only be 0 if the
/// process has complete.
///
/// The only safe way to read from the standard output of a process during it's
/// execution is to use the `subprocess_option_enable_async` option in
/// conjunction with this method.
subprocess_weak unsigned
subprocess_read_stdout(struct subprocess_s *const process, char *const buffer,
unsigned size);
/// @brief Read the standard error from the child process.
/// @param process The process to read from.
/// @param buffer The buffer to read into.
/// @param size The maximum number of bytes to read.
/// @return The number of bytes actually read into buffer. Can only be 0 if the
/// process has complete.
///
/// The only safe way to read from the standard error of a process during it's
/// execution is to use the `subprocess_option_enable_async` option in
/// conjunction with this method.
subprocess_weak unsigned
subprocess_read_stderr(struct subprocess_s *const process, char *const buffer,
unsigned size);
/// @brief Returns if the subprocess is currently still alive and executing.
/// @param process The process to check.
/// @return If the process is still alive non-zero is returned.
subprocess_weak int subprocess_alive(struct subprocess_s *const process);
#if defined(__cplusplus)
#define SUBPROCESS_CAST(type, x) static_cast<type>(x)
#define SUBPROCESS_PTR_CAST(type, x) reinterpret_cast<type>(x)
#define SUBPROCESS_CONST_CAST(type, x) const_cast<type>(x)
#define SUBPROCESS_NULL NULL
#else
#define SUBPROCESS_CAST(type, x) ((type)(x))
#define SUBPROCESS_PTR_CAST(type, x) ((type)(x))
#define SUBPROCESS_CONST_CAST(type, x) ((type)(x))
#define SUBPROCESS_NULL 0
#endif
#if !defined(_WIN32)
#include <signal.h>
#include <spawn.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#endif
#if defined(_WIN32)
#if (_MSC_VER < 1920)
#ifdef _WIN64
typedef __int64 subprocess_intptr_t;
typedef unsigned __int64 subprocess_size_t;
#else
typedef int subprocess_intptr_t;
typedef unsigned int subprocess_size_t;
#endif
#else
#include <inttypes.h>
typedef intptr_t subprocess_intptr_t;
typedef size_t subprocess_size_t;
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-identifier"
#endif
typedef struct _PROCESS_INFORMATION *LPPROCESS_INFORMATION;
typedef struct _SECURITY_ATTRIBUTES *LPSECURITY_ATTRIBUTES;
typedef struct _STARTUPINFOA *LPSTARTUPINFOA;
typedef struct _OVERLAPPED *LPOVERLAPPED;
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef _MSC_VER
#pragma warning(push, 1)
#endif
#ifdef __MINGW32__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
struct subprocess_subprocess_information_s {
void *hProcess;
void *hThread;
unsigned long dwProcessId;
unsigned long dwThreadId;
};
struct subprocess_security_attributes_s {
unsigned long nLength;
void *lpSecurityDescriptor;
int bInheritHandle;
};
struct subprocess_startup_info_s {
unsigned long cb;
char *lpReserved;
char *lpDesktop;
char *lpTitle;
unsigned long dwX;
unsigned long dwY;
unsigned long dwXSize;
unsigned long dwYSize;
unsigned long dwXCountChars;
unsigned long dwYCountChars;
unsigned long dwFillAttribute;
unsigned long dwFlags;
unsigned short wShowWindow;
unsigned short cbReserved2;
unsigned char *lpReserved2;
void *hStdInput;
void *hStdOutput;
void *hStdError;
};
struct subprocess_overlapped_s {
uintptr_t Internal;
uintptr_t InternalHigh;
union {
struct {
unsigned long Offset;
unsigned long OffsetHigh;
} DUMMYSTRUCTNAME;
void *Pointer;
} DUMMYUNIONNAME;
void *hEvent;
};
#ifdef __MINGW32__
#pragma GCC diagnostic pop
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
__declspec(dllimport) unsigned long __stdcall GetLastError(void);
__declspec(dllimport) int __stdcall SetHandleInformation(void *, unsigned long,
unsigned long);
__declspec(dllimport) int __stdcall CreatePipe(void **, void **,
LPSECURITY_ATTRIBUTES,
unsigned long);
__declspec(dllimport) void *__stdcall CreateNamedPipeA(
const char *, unsigned long, unsigned long, unsigned long, unsigned long,
unsigned long, unsigned long, LPSECURITY_ATTRIBUTES);
__declspec(dllimport) int __stdcall ReadFile(void *, void *, unsigned long,
unsigned long *, LPOVERLAPPED);
__declspec(dllimport) unsigned long __stdcall GetCurrentProcessId(void);
__declspec(dllimport) unsigned long __stdcall GetCurrentThreadId(void);
__declspec(dllimport) void *__stdcall CreateFileA(const char *, unsigned long,
unsigned long,
LPSECURITY_ATTRIBUTES,
unsigned long, unsigned long,
void *);
__declspec(dllimport) void *__stdcall CreateEventA(LPSECURITY_ATTRIBUTES, int,
int, const char *);
__declspec(dllimport) int __stdcall CreateProcessA(
const char *, char *, LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, int,
unsigned long, void *, const char *, LPSTARTUPINFOA, LPPROCESS_INFORMATION);
__declspec(dllimport) int __stdcall CloseHandle(void *);
__declspec(dllimport) unsigned long __stdcall WaitForSingleObject(
void *, unsigned long);
__declspec(dllimport) int __stdcall GetExitCodeProcess(
void *, unsigned long *lpExitCode);
__declspec(dllimport) int __stdcall TerminateProcess(void *, unsigned int);
__declspec(dllimport) unsigned long __stdcall WaitForMultipleObjects(
unsigned long, void *const *, int, unsigned long);
__declspec(dllimport) int __stdcall GetOverlappedResult(void *, LPOVERLAPPED,
unsigned long *, int);
#if defined(_DLL)
#define SUBPROCESS_DLLIMPORT __declspec(dllimport)
#else
#define SUBPROCESS_DLLIMPORT
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wreserved-identifier"
#endif
SUBPROCESS_DLLIMPORT int __cdecl _fileno(FILE *);
SUBPROCESS_DLLIMPORT int __cdecl _open_osfhandle(subprocess_intptr_t, int);
SUBPROCESS_DLLIMPORT subprocess_intptr_t __cdecl _get_osfhandle(int);
#ifndef __MINGW32__
void *__cdecl _alloca(subprocess_size_t);
#else
#include <malloc.h>
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#else
typedef size_t subprocess_size_t;
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
#endif
struct subprocess_s {
FILE *stdin_file;
FILE *stdout_file;
FILE *stderr_file;
#if defined(_WIN32)
void *hProcess;
void *hStdInput;
void *hEventOutput;
void *hEventError;
#else
pid_t child;
int return_status;
#endif
subprocess_size_t alive;
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#if defined(_WIN32)
subprocess_weak int subprocess_create_named_pipe_helper(void **rd, void **wr);
int subprocess_create_named_pipe_helper(void **rd, void **wr) {
const unsigned long pipeAccessInbound = 0x00000001;
const unsigned long fileFlagOverlapped = 0x40000000;
const unsigned long pipeTypeByte = 0x00000000;
const unsigned long pipeWait = 0x00000000;
const unsigned long genericWrite = 0x40000000;
const unsigned long openExisting = 3;
const unsigned long fileAttributeNormal = 0x00000080;
const void *const invalidHandleValue =
SUBPROCESS_PTR_CAST(void *, ~(SUBPROCESS_CAST(subprocess_intptr_t, 0)));
struct subprocess_security_attributes_s saAttr = {sizeof(saAttr),
SUBPROCESS_NULL, 1};
char name[256] = {0};
static subprocess_tls long index = 0;
const long unique = index++;
#if defined(_MSC_VER) && _MSC_VER < 1900
#pragma warning(push, 1)
#pragma warning(disable : 4996)
_snprintf(name, sizeof(name) - 1,
"\\\\.\\pipe\\sheredom_subprocess_h.%08lx.%08lx.%ld",
GetCurrentProcessId(), GetCurrentThreadId(), unique);
#pragma warning(pop)
#else
snprintf(name, sizeof(name) - 1,
"\\\\.\\pipe\\sheredom_subprocess_h.%08lx.%08lx.%ld",
GetCurrentProcessId(), GetCurrentThreadId(), unique);
#endif
*rd =
CreateNamedPipeA(name, pipeAccessInbound | fileFlagOverlapped,
pipeTypeByte | pipeWait, 1, 4096, 4096, SUBPROCESS_NULL,
SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr));
if (invalidHandleValue == *rd) {
return -1;
}
*wr = CreateFileA(name, genericWrite, SUBPROCESS_NULL,
SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr),
openExisting, fileAttributeNormal, SUBPROCESS_NULL);
if (invalidHandleValue == *wr) {
return -1;
}
return 0;
}
#endif
int subprocess_create(const char *const commandLine[], int options,
struct subprocess_s *const out_process) {
return subprocess_create_ex(commandLine, options, SUBPROCESS_NULL,
out_process);
}
int subprocess_create_ex(const char *const commandLine[], int options,
const char *const environment[],
struct subprocess_s *const out_process) {
#if defined(_WIN32)
int fd;
void *rd, *wr;
char *commandLineCombined;
subprocess_size_t len;
int i, j;
int need_quoting;
unsigned long flags = 0;
const unsigned long startFUseStdHandles = 0x00000100;
const unsigned long handleFlagInherit = 0x00000001;
const unsigned long createNoWindow = 0x08000000;
struct subprocess_subprocess_information_s processInfo;
struct subprocess_security_attributes_s saAttr = {sizeof(saAttr),
SUBPROCESS_NULL, 1};
char *used_environment = SUBPROCESS_NULL;
struct subprocess_startup_info_s startInfo = {0,
SUBPROCESS_NULL,
SUBPROCESS_NULL,
SUBPROCESS_NULL,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
SUBPROCESS_NULL,
SUBPROCESS_NULL,
SUBPROCESS_NULL,
SUBPROCESS_NULL};
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = startFUseStdHandles;
if (subprocess_option_no_window == (options & subprocess_option_no_window)) {
flags |= createNoWindow;
}
if (subprocess_option_inherit_environment !=
(options & subprocess_option_inherit_environment)) {
if (SUBPROCESS_NULL == environment) {
used_environment = SUBPROCESS_CONST_CAST(char *, "\0\0");
} else {
// We always end with two null terminators.
len = 2;
for (i = 0; environment[i]; i++) {
for (j = 0; '\0' != environment[i][j]; j++) {
len++;
}
// For the null terminator too.
len++;
}
used_environment = SUBPROCESS_CAST(char *, _alloca(len));
// Re-use len for the insertion position
len = 0;
for (i = 0; environment[i]; i++) {
for (j = 0; '\0' != environment[i][j]; j++) {
used_environment[len++] = environment[i][j];
}
used_environment[len++] = '\0';
}
// End with the two null terminators.
used_environment[len++] = '\0';
used_environment[len++] = '\0';
}
} else {
if (SUBPROCESS_NULL != environment) {
return -1;
}
}
if (!CreatePipe(&rd, &wr, SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr),
0)) {
return -1;
}
if (!SetHandleInformation(wr, handleFlagInherit, 0)) {
return -1;
}
fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, wr), 0);
if (-1 != fd) {
out_process->stdin_file = _fdopen(fd, "wb");
if (SUBPROCESS_NULL == out_process->stdin_file) {
return -1;
}
}
startInfo.hStdInput = rd;
if (options & subprocess_option_enable_async) {
if (subprocess_create_named_pipe_helper(&rd, &wr)) {
return -1;
}
} else {
if (!CreatePipe(&rd, &wr,
SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 0)) {
return -1;
}
}
if (!SetHandleInformation(rd, handleFlagInherit, 0)) {
return -1;
}
fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, rd), 0);
if (-1 != fd) {
out_process->stdout_file = _fdopen(fd, "rb");
if (SUBPROCESS_NULL == out_process->stdout_file) {
return -1;
}
}
startInfo.hStdOutput = wr;
if (subprocess_option_combined_stdout_stderr ==
(options & subprocess_option_combined_stdout_stderr)) {
out_process->stderr_file = out_process->stdout_file;
startInfo.hStdError = startInfo.hStdOutput;
} else {
if (options & subprocess_option_enable_async) {
if (subprocess_create_named_pipe_helper(&rd, &wr)) {
return -1;
}
} else {
if (!CreatePipe(&rd, &wr,
SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 0)) {
return -1;
}
}
if (!SetHandleInformation(rd, handleFlagInherit, 0)) {
return -1;
}
fd = _open_osfhandle(SUBPROCESS_PTR_CAST(subprocess_intptr_t, rd), 0);
if (-1 != fd) {
out_process->stderr_file = _fdopen(fd, "rb");
if (SUBPROCESS_NULL == out_process->stderr_file) {
return -1;
}
}
startInfo.hStdError = wr;
}
if (options & subprocess_option_enable_async) {
out_process->hEventOutput =
CreateEventA(SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 1, 1,
SUBPROCESS_NULL);
out_process->hEventError =
CreateEventA(SUBPROCESS_PTR_CAST(LPSECURITY_ATTRIBUTES, &saAttr), 1, 1,
SUBPROCESS_NULL);
} else {
out_process->hEventOutput = SUBPROCESS_NULL;
out_process->hEventError = SUBPROCESS_NULL;
}
// Combine commandLine together into a single string
len = 0;
for (i = 0; commandLine[i]; i++) {
// for the trailing \0
len++;
// Quote the argument if it has a space in it
if (strpbrk(commandLine[i], "\t\v ") != SUBPROCESS_NULL)
len += 2;
for (j = 0; '\0' != commandLine[i][j]; j++) {
switch (commandLine[i][j]) {
default:
break;
case '\\':
if (commandLine[i][j + 1] == '"') {
len++;
}
break;
case '"':
len++;
break;
}
len++;
}
}
commandLineCombined = SUBPROCESS_CAST(char *, _alloca(len));
if (!commandLineCombined) {
return -1;
}
// Gonna re-use len to store the write index into commandLineCombined
len = 0;
for (i = 0; commandLine[i]; i++) {
if (0 != i) {
commandLineCombined[len++] = ' ';
}
need_quoting = strpbrk(commandLine[i], "\t\v ") != SUBPROCESS_NULL;
if (need_quoting) {
commandLineCombined[len++] = '"';
}
for (j = 0; '\0' != commandLine[i][j]; j++) {
switch (commandLine[i][j]) {
default:
break;
case '\\':
if (commandLine[i][j + 1] == '"') {
commandLineCombined[len++] = '\\';
}
break;
case '"':
commandLineCombined[len++] = '\\';
break;
}
commandLineCombined[len++] = commandLine[i][j];
}
if (need_quoting) {
commandLineCombined[len++] = '"';
}
}
commandLineCombined[len] = '\0';
if (!CreateProcessA(
SUBPROCESS_NULL,
commandLineCombined, // command line
SUBPROCESS_NULL, // process security attributes
SUBPROCESS_NULL, // primary thread security attributes
1, // handles are inherited
flags, // creation flags
used_environment, // used environment
SUBPROCESS_NULL, // use parent's current directory
SUBPROCESS_PTR_CAST(LPSTARTUPINFOA,
&startInfo), // STARTUPINFO pointer
SUBPROCESS_PTR_CAST(LPPROCESS_INFORMATION, &processInfo))) {
return -1;
}
out_process->hProcess = processInfo.hProcess;
out_process->hStdInput = startInfo.hStdInput;
// We don't need the handle of the primary thread in the called process.
CloseHandle(processInfo.hThread);
if (SUBPROCESS_NULL != startInfo.hStdOutput) {
CloseHandle(startInfo.hStdOutput);
if (startInfo.hStdError != startInfo.hStdOutput) {
CloseHandle(startInfo.hStdError);
}
}
out_process->alive = 1;
return 0;
#else
int stdinfd[2];
int stdoutfd[2];
int stderrfd[2];
pid_t child;
extern char **environ;
char *const empty_environment[1] = {SUBPROCESS_NULL};
posix_spawn_file_actions_t actions;
char *const *used_environment;
if (subprocess_option_inherit_environment ==
(options & subprocess_option_inherit_environment)) {
if (SUBPROCESS_NULL != environment) {
return -1;
}
}
if (0 != pipe(stdinfd)) {
return -1;
}
if (0 != pipe(stdoutfd)) {
return -1;
}
if (subprocess_option_combined_stdout_stderr !=
(options & subprocess_option_combined_stdout_stderr)) {
if (0 != pipe(stderrfd)) {
return -1;
}
}
if (environment) {
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-qual"
#pragma clang diagnostic ignored "-Wold-style-cast"
#endif
used_environment = (char *const *)environment;
#ifdef __clang__
#pragma clang diagnostic pop
#endif
} else if (subprocess_option_inherit_environment ==
(options & subprocess_option_inherit_environment)) {
used_environment = environ;
} else {
used_environment = empty_environment;
}
if (0 != posix_spawn_file_actions_init(&actions)) {
return -1;
}
// Close the stdin write end
if (0 != posix_spawn_file_actions_addclose(&actions, stdinfd[1])) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
// Map the read end to stdin
if (0 !=
posix_spawn_file_actions_adddup2(&actions, stdinfd[0], STDIN_FILENO)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
// Close the stdout read end
if (0 != posix_spawn_file_actions_addclose(&actions, stdoutfd[0])) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
// Map the write end to stdout
if (0 !=
posix_spawn_file_actions_adddup2(&actions, stdoutfd[1], STDOUT_FILENO)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
if (subprocess_option_combined_stdout_stderr ==
(options & subprocess_option_combined_stdout_stderr)) {
if (0 != posix_spawn_file_actions_adddup2(&actions, STDOUT_FILENO,
STDERR_FILENO)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
} else {
// Close the stderr read end
if (0 != posix_spawn_file_actions_addclose(&actions, stderrfd[0])) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
// Map the write end to stdout
if (0 != posix_spawn_file_actions_adddup2(&actions, stderrfd[1],
STDERR_FILENO)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
}
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wcast-qual"
#pragma clang diagnostic ignored "-Wold-style-cast"
#endif
if (subprocess_option_search_user_path ==
(options & subprocess_option_search_user_path)) {
if (0 != posix_spawnp(&child, commandLine[0], &actions, SUBPROCESS_NULL,
(char *const *)commandLine, used_environment)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
} else {
if (0 != posix_spawn(&child, commandLine[0], &actions, SUBPROCESS_NULL,
(char *const *)commandLine, used_environment)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
}
#ifdef __clang__
#pragma clang diagnostic pop
#endif
// Close the stdin read end
close(stdinfd[0]);
// Store the stdin write end
out_process->stdin_file = fdopen(stdinfd[1], "wb");
// Close the stdout write end
close(stdoutfd[1]);
// Store the stdout read end
out_process->stdout_file = fdopen(stdoutfd[0], "rb");
if (subprocess_option_combined_stdout_stderr ==
(options & subprocess_option_combined_stdout_stderr)) {
out_process->stderr_file = out_process->stdout_file;
} else {
// Close the stderr write end
close(stderrfd[1]);
// Store the stderr read end
out_process->stderr_file = fdopen(stderrfd[0], "rb");
}
// Store the child's pid
out_process->child = child;
out_process->alive = 1;
posix_spawn_file_actions_destroy(&actions);
return 0;
#endif
}
FILE *subprocess_stdin(const struct subprocess_s *const process) {
return process->stdin_file;
}
FILE *subprocess_stdout(const struct subprocess_s *const process) {
return process->stdout_file;
}
FILE *subprocess_stderr(const struct subprocess_s *const process) {
if (process->stdout_file != process->stderr_file) {
return process->stderr_file;
} else {
return SUBPROCESS_NULL;
}
}
int subprocess_join(struct subprocess_s *const process,
int *const out_return_code) {
#if defined(_WIN32)
const unsigned long infinite = 0xFFFFFFFF;
if (process->stdin_file) {
fclose(process->stdin_file);
process->stdin_file = SUBPROCESS_NULL;
}
if (process->hStdInput) {
CloseHandle(process->hStdInput);
process->hStdInput = SUBPROCESS_NULL;
}
WaitForSingleObject(process->hProcess, infinite);
if (out_return_code) {
if (!GetExitCodeProcess(
process->hProcess,
SUBPROCESS_PTR_CAST(unsigned long *, out_return_code))) {
return -1;
}
}
process->alive = 0;
return 0;
#else
int status;
if (process->stdin_file) {
fclose(process->stdin_file);
process->stdin_file = SUBPROCESS_NULL;
}
if (process->child) {
if (process->child != waitpid(process->child, &status, 0)) {
return -1;
}
process->child = 0;
if (WIFEXITED(status)) {
process->return_status = WEXITSTATUS(status);
} else {
process->return_status = EXIT_FAILURE;
}
process->alive = 0;
}
if (out_return_code) {
*out_return_code = process->return_status;
}
return 0;
#endif
}
int subprocess_destroy(struct subprocess_s *const process) {
if (process->stdin_file) {
fclose(process->stdin_file);
process->stdin_file = SUBPROCESS_NULL;
}
if (process->stdout_file) {
fclose(process->stdout_file);
if (process->stdout_file != process->stderr_file) {
fclose(process->stderr_file);
}
process->stdout_file = SUBPROCESS_NULL;
process->stderr_file = SUBPROCESS_NULL;
}