/
runProcess.c
666 lines (576 loc) · 23.5 KB
/
runProcess.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
/* ----------------------------------------------------------------------------
(c) The University of Glasgow 2004-2022
Support for System.Process
------------------------------------------------------------------------- */
#define UNICODE
/* XXX This is a nasty hack; should put everything necessary in this package */
#include "HsBase.h"
#include "Rts.h"
#include "runProcess.h"
#include <assert.h>
#include <windows.h>
#include <io.h>
#include <objbase.h>
#include <wchar.h>
/* ----------------------------------------------------------------------------
Win32 versions
------------------------------------------------------------------------- */
/* -------------------- WINDOWS VERSION --------------------- */
/*
* Function: mkAnonPipe
*
* Purpose: create an anonymous pipe with read and write ends being
* optionally (non-)inheritable.
*/
static BOOL
mkAnonPipe (HANDLE* pHandleIn, BOOL isInheritableIn,
HANDLE* pHandleOut, BOOL isInheritableOut)
{
HANDLE hTemporaryIn = NULL;
HANDLE hTemporaryOut = NULL;
/* Create the anon pipe with both ends inheritable */
if (!CreatePipe(&hTemporaryIn, &hTemporaryOut, NULL, 0))
{
maperrno();
*pHandleIn = NULL;
*pHandleOut = NULL;
return FALSE;
}
if (isInheritableIn) {
// SetHandleInformation requires at least Win2k
if (!SetHandleInformation(hTemporaryIn,
HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT))
{
maperrno();
*pHandleIn = NULL;
*pHandleOut = NULL;
CloseHandle(hTemporaryIn);
CloseHandle(hTemporaryOut);
return FALSE;
}
}
*pHandleIn = hTemporaryIn;
if (isInheritableOut) {
if (!SetHandleInformation(hTemporaryOut,
HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT))
{
maperrno();
*pHandleIn = NULL;
*pHandleOut = NULL;
CloseHandle(hTemporaryIn);
CloseHandle(hTemporaryOut);
return FALSE;
}
}
*pHandleOut = hTemporaryOut;
return TRUE;
}
/*
* Function: mkNamedPipe
*
* Purpose: create an named pipe with read and write ends being
* optionally (non-)inheritable. Named pipes can be read
* asynchronously while anonymous pipes require blocking calls.
*/
BOOL
mkNamedPipe (HANDLE* pHandleIn, BOOL isInheritableIn, BOOL isOverlappedIn,
HANDLE* pHandleOut, BOOL isInheritableOut, BOOL isOverlappedOut)
{
HANDLE hTemporaryIn = INVALID_HANDLE_VALUE;
HANDLE hTemporaryOut = INVALID_HANDLE_VALUE;
RPC_WSTR guidStr = NULL;
GUID guid;
/* First we create a new GUID to make the name of the pipe unique. Since
GUID are guaranteed to be unique system wide we don't need to retry. */
ZeroMemory (&guid, sizeof (guid));
if (CoCreateGuid (&guid) != S_OK)
goto fail;
if (UuidToStringW ((UUID*)&guid, &guidStr) != S_OK)
goto fail;
/* Now we create the pipe name. */
wchar_t pipeName[MAX_PATH];
if (-1 == swprintf_s (&pipeName[0], MAX_PATH, L"\\\\.\\pipe\\haskell:process:%ls\n", guidStr))
goto fail;
const int buffer_size = 8 * 1024;
RpcStringFreeW (&guidStr);
SECURITY_ATTRIBUTES secAttr;
ZeroMemory (&secAttr, sizeof(secAttr));
secAttr.nLength = sizeof(SECURITY_ATTRIBUTES);
secAttr.lpSecurityDescriptor = NULL;
secAttr.bInheritHandle = isInheritableIn;
/* Create one end of the pipe. Named pipes are a bit less secure than
anonymous pipes. Because of this we restrict the pipe's access to only
one client and also only the local host. This means after we create the
other end of the pipe it should be as secure as an anonymous pipe.
"When you operate on named pipes, you have a choice of opening them in
PIPE_WAIT mode or PIPE_NOWAIT mode. When you read from a PIPE_WAIT pipe,
the read blocks until data becomes available in the pipe. When you read
from a PIPE_NOWAIT pipe, then the read completes immediately even if
there is no data in the pipe. But how is this different from a PIPE_WAIT
pipe opened in asynchronous mode by passing FILE_FLAG_OVERLAPPED? The
difference is in when the I/O is deemed to have completed. When you issue
an overlapped read against a PIPE_WAIT pipe, the call to ReadFile returns
immediately, but the completion actions do not occur until there is data
available in the pipe. (Completion actions are things like setting the
event, running the completion routine, or queueing a completion to an I/O
completion port.) On the other hand, when you issue a read against a
PIPE_NOWAIT pipe, the call to ReadFile returns immediately with
completion—if the pipe is empty, the read completes with a read of zero
bytes and the error ERROR_NO_DATA."[0]
[0] https://devblogs.microsoft.com/oldnewthing/20110114-00/?p=11753 */
DWORD inAttr = isOverlappedIn ? FILE_FLAG_OVERLAPPED : 0;
hTemporaryIn
= CreateNamedPipeW (pipeName,
PIPE_ACCESS_INBOUND | inAttr | FILE_FLAG_FIRST_PIPE_INSTANCE,
PIPE_TYPE_MESSAGE | PIPE_REJECT_REMOTE_CLIENTS | PIPE_READMODE_MESSAGE | PIPE_WAIT,
1, buffer_size, buffer_size,
0,
&secAttr);
if (hTemporaryIn == INVALID_HANDLE_VALUE)
goto fail;
/* And now open the other end, using the inverse access permissions. This
will give us the read and write ends of the pipe. */
secAttr.bInheritHandle = isInheritableOut;
hTemporaryOut
= CreateFileW (pipeName,
GENERIC_WRITE,
FILE_SHARE_WRITE,
&secAttr,
OPEN_EXISTING,
isOverlappedOut
? FILE_FLAG_OVERLAPPED
: FILE_ATTRIBUTE_NORMAL,
NULL);
if (hTemporaryOut == INVALID_HANDLE_VALUE)
goto fail;
/* Ensure that read and write ends are set to the same mode. MESSAGE mode
will honor data boundaries as a whole. That is, if n bytes are posted
at once, n bytes are received together at the other wide as long as n
is smaller than buffer size. Otherwise it's up to buffer size. BYTE
mode is essentially streaming mode. Typically Haskell would benefit from
both modes, cabal from byte streaming and iserv from message. Let's
default to MESSAGE. */
DWORD pipeFlags = PIPE_READMODE_MESSAGE;
if (!SetNamedPipeHandleState (hTemporaryOut, &pipeFlags, NULL, NULL))
goto fail;
/* Set some optimization flags to make the I/O manager operate more
efficiently on these handles. These mirrors those in
`optimizeFileAccess` but we set them here to do so before any data has
been put in the HANDLEs. However these don't always work for sockets and
pipes. So we set them, but can't rely on it. */
#if defined(FILE_SKIP_SET_EVENT_ON_HANDLE) && \
defined(FILE_SKIP_COMPLETION_PORT_ON_SUCCESS)
UCHAR flags = FILE_SKIP_COMPLETION_PORT_ON_SUCCESS
| FILE_SKIP_SET_EVENT_ON_HANDLE;
SetFileCompletionNotificationModes (hTemporaryIn, flags);
SetFileCompletionNotificationModes (hTemporaryOut, flags);
#endif
/* Everything has succeeded so now copy the pointers to the results. */
*pHandleIn = hTemporaryIn;
*pHandleOut = hTemporaryOut;
return TRUE;
fail:
/* We have to save the current error before we do another API call. */
maperrno();
RpcStringFreeW (&guidStr);
if (INVALID_HANDLE_VALUE != hTemporaryIn ) CloseHandle (hTemporaryIn);
if (INVALID_HANDLE_VALUE != hTemporaryOut) CloseHandle (hTemporaryOut);
return FALSE;
}
static HANDLE
createJob ()
{
HANDLE hJob = CreateJobObject (NULL, NULL);
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jeli;
ZeroMemory(&jeli, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION));
// Configure all child processes associated with the job to terminate when the
// Last process in the job terminates. This prevent half dead processes.
jeli.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (SetInformationJobObject (hJob, JobObjectExtendedLimitInformation,
&jeli, sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION)))
{
return hJob;
}
maperrno();
return NULL;
}
/* Small helper function that determines how the std handle should be used.
if _STDHANDLE is:
-1: A new pipe is created. If ASYNCHRONOUS an asynchronous pipe is created
with FILE_FLAG_OVERLAPPED set. If not then an anonymouse pipe is
created without that flag.
-2: No handle is created, DESTINATION is NULL.
std: If the handle matches the default std handle for the type (i.e. if the
handle for input _stdHandle mathed STDIN) then set DESTINATION to that
handle.
otherwise: We just duplicate the handle to make it inheritable and pass it
on. */
static inline bool
setStdHandleInfo (LPHANDLE destination, HANDLE _stdhandle,
LPHANDLE hStdRead, LPHANDLE hStdWrite, HANDLE defaultStd,
BOOL isInheritableIn, BOOL isInheritableOut, BOOL asynchronous)
{
BOOL status;
assert (destination);
assert (hStdRead);
assert (hStdWrite);
LPHANDLE tmpHandle = isInheritableOut ? hStdWrite : hStdRead;
if (_stdhandle == (HANDLE)-1) {
if (!asynchronous
&& !mkAnonPipe(hStdRead, isInheritableIn, hStdWrite, isInheritableOut))
return false;
if (asynchronous
&& !mkNamedPipe(hStdRead, isInheritableIn, !isInheritableIn, hStdWrite, isInheritableOut, !isInheritableOut))
return false;
*destination = *tmpHandle;
} else if (_stdhandle == (HANDLE)-2) {
*destination = NULL;
} else if (_stdhandle == defaultStd) {
// Don't duplicate standard handle, as console handles cannot be
// duplicated and inherited. urg.
*destination = defaultStd;
} else {
// The handle might not be inheritable, so duplicate it
status = DuplicateHandle(GetCurrentProcess(),
_stdhandle,
GetCurrentProcess(), tmpHandle,
0,
TRUE, /* inheritable */
DUPLICATE_SAME_ACCESS);
if (!status) return false;
*destination = *tmpHandle;
}
return true;
}
/* Common functionality between the Posix FD version and native HANDLE version
of runInteractiveProcess. The main difference lies in the use of
ASYNCHRONOUS which indicates whether the pipes that are created allow for
asynchronous access or not. */
static ProcHandle
runInteractiveProcessWrapper (
wchar_t *cmd, wchar_t *workingDirectory,
wchar_t *environment,
HANDLE _stdin, HANDLE _stdout, HANDLE _stderr,
HANDLE *pStdInput, HANDLE *pStdOutput, HANDLE *pStdError,
int flags, bool useJobObject, HANDLE *hJob, bool asynchronous)
{
STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;
HANDLE hStdInputRead = INVALID_HANDLE_VALUE;
HANDLE hStdInputWrite = INVALID_HANDLE_VALUE;
HANDLE hStdOutputRead = INVALID_HANDLE_VALUE;
HANDLE hStdOutputWrite = INVALID_HANDLE_VALUE;
HANDLE hStdErrorRead = INVALID_HANDLE_VALUE;
HANDLE hStdErrorWrite = INVALID_HANDLE_VALUE;
BOOL close_fds = ((flags & RUN_PROCESS_IN_CLOSE_FDS) != 0);
// We always pass a wide environment block, so we MUST set this flag
DWORD dwFlags = CREATE_UNICODE_ENVIRONMENT;
BOOL inherit;
ZeroMemory(&sInfo, sizeof(sInfo));
sInfo.cb = sizeof(sInfo);
sInfo.dwFlags = STARTF_USESTDHANDLES;
ZeroMemory(&pInfo, sizeof(pInfo));
HANDLE defaultStdIn = GetStdHandle(STD_INPUT_HANDLE);
HANDLE defaultStdOutput = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE defaultStdError = GetStdHandle(STD_ERROR_HANDLE);
if (!setStdHandleInfo (&sInfo.hStdInput, _stdin, &hStdInputRead,
&hStdInputWrite, defaultStdIn, TRUE, FALSE,
asynchronous))
goto cleanup_err;
if (!setStdHandleInfo (&sInfo.hStdOutput, _stdout, &hStdOutputRead,
&hStdOutputWrite, defaultStdOutput, FALSE, TRUE,
asynchronous))
goto cleanup_err;
if (!setStdHandleInfo (&sInfo.hStdError, _stderr, &hStdErrorRead,
&hStdErrorWrite, defaultStdError, FALSE, TRUE,
asynchronous))
goto cleanup_err;
if (sInfo.hStdInput != defaultStdIn
&& sInfo.hStdOutput != defaultStdOutput
&& sInfo.hStdError != defaultStdError
&& (flags & RUN_PROCESS_IN_NEW_GROUP) == 0)
dwFlags |= CREATE_NO_WINDOW; // Run without console window only when both output and error are redirected
// See #3231
if (close_fds
&& _stdin == defaultStdIn
&& _stdout == defaultStdOutput
&& _stderr == defaultStdError) {
inherit = FALSE;
} else {
inherit = TRUE;
}
if ((flags & RUN_PROCESS_IN_NEW_GROUP) != 0) {
dwFlags |= CREATE_NEW_PROCESS_GROUP;
}
if ((flags & RUN_PROCESS_DETACHED) != 0) {
dwFlags |= DETACHED_PROCESS;
}
if ((flags & RUN_PROCESS_NEW_CONSOLE) != 0) {
dwFlags |= CREATE_NEW_CONSOLE;
}
/* If we're going to use a job object, then we have to create
the thread suspended.
See Note [Windows exec interaction]. */
if (useJobObject)
{
dwFlags |= CREATE_SUSPENDED;
*hJob = createJob();
if (!*hJob)
{
goto cleanup_err;
}
} else {
*hJob = NULL;
}
if (!CreateProcess(NULL, cmd, NULL, NULL, inherit, dwFlags, environment, workingDirectory, &sInfo, &pInfo))
{
goto cleanup_err;
}
if (useJobObject && hJob && *hJob)
{
// Then associate the process and the job;
if (!AssignProcessToJobObject (*hJob, pInfo.hProcess))
{
goto cleanup_err;
}
// And now that we've associated the new process with the job
// we can actively resume it.
ResumeThread (pInfo.hThread);
}
CloseHandle(pInfo.hThread);
// Close the ends of the pipes that were inherited by the
// child process. This is important, otherwise we won't see
// EOF on these pipes when the child process exits.
if (hStdInputRead != INVALID_HANDLE_VALUE) CloseHandle(hStdInputRead);
if (hStdOutputWrite != INVALID_HANDLE_VALUE) CloseHandle(hStdOutputWrite);
if (hStdErrorWrite != INVALID_HANDLE_VALUE) CloseHandle(hStdErrorWrite);
// Return the pointers to the handles we need.
*pStdInput = hStdInputWrite;
*pStdOutput = hStdOutputRead;
*pStdError = hStdErrorRead;
return pInfo.hProcess;
cleanup_err:
if (hStdInputRead != INVALID_HANDLE_VALUE) CloseHandle(hStdInputRead);
if (hStdInputWrite != INVALID_HANDLE_VALUE) CloseHandle(hStdInputWrite);
if (hStdOutputRead != INVALID_HANDLE_VALUE) CloseHandle(hStdOutputRead);
if (hStdOutputWrite != INVALID_HANDLE_VALUE) CloseHandle(hStdOutputWrite);
if (hStdErrorRead != INVALID_HANDLE_VALUE) CloseHandle(hStdErrorRead);
if (hStdErrorWrite != INVALID_HANDLE_VALUE) CloseHandle(hStdErrorWrite);
if (useJobObject && hJob && *hJob ) CloseHandle(*hJob);
maperrno();
return NULL;
}
/* Note [Windows exec interaction]
The basic issue that process jobs tried to solve is this:
Say you have two programs A and B. Now A calls B. There are two ways to do this.
1) You can use the normal CreateProcess API, which is what normal Windows code do.
Using this approach, the current waitForProcess works absolutely fine.
2) You can call the emulated POSIX function _exec, which of course is supposed to
allow the child process to replace the parent.
With approach 2) waitForProcess falls apart because the Win32's process model does
not allow this the same way as linux. _exec is emulated by first making a call to
CreateProcess to spawn B and then immediately exiting from A. So you have two
different processes.
waitForProcess is waiting on the termination of A. Because A is immediately killed,
waitForProcess will return even though B is still running. This is why for instance
the GHC testsuite on Windows had lots of file locked errors.
This approach creates a new Job and assigned A to the job, but also all future
processes spawned by A. This allows us to listen in on events, such as, when all
processes in the job are finished, but also allows us to propagate exit codes from
_exec calls.
The only reason we need this at all is because we don't interact with just actual
native code on Windows, and instead have a lot of ported POSIX code.
The Job handle is returned to the user because Jobs have additional benefits as well,
such as allowing you to specify resource limits on the to be spawned process.
*/
ProcHandle
runInteractiveProcess (wchar_t *cmd, wchar_t *workingDirectory,
wchar_t *environment,
int fdStdIn, int fdStdOut, int fdStdErr,
int *pfdStdInput, int *pfdStdOutput, int *pfdStdError,
int flags, bool useJobObject, HANDLE *hJob)
{
HANDLE pStdInput = INVALID_HANDLE_VALUE;
HANDLE pStdOutput = INVALID_HANDLE_VALUE;
HANDLE pStdError = INVALID_HANDLE_VALUE;
ProcHandle result
= runInteractiveProcessWrapper (cmd, workingDirectory, environment,
(HANDLE) (fdStdIn < 0 ? fdStdIn : _get_osfhandle(fdStdIn)),
(HANDLE) (fdStdOut < 0 ? fdStdOut : _get_osfhandle(fdStdOut)),
(HANDLE) (fdStdErr < 0 ? fdStdErr : _get_osfhandle(fdStdErr)),
&pStdInput, &pStdOutput, &pStdError,
flags, useJobObject, hJob, FALSE);
if (result) {
*pfdStdInput = _open_osfhandle((intptr_t) pStdInput, _O_WRONLY);
*pfdStdOutput = _open_osfhandle((intptr_t) pStdOutput, _O_RDONLY);
*pfdStdError = _open_osfhandle((intptr_t) pStdError, _O_RDONLY);
}
return result;
}
/* This function is the same as runInteractiveProcess except it works directly
on Windows HANDLE rather than pseudo FDs. This allows us to use the pipes
returned here asynchronously and also need less system calls while working
with the new I/O manager. */
ProcHandle
runInteractiveProcessHANDLE (
wchar_t *cmd, wchar_t *workingDirectory,
wchar_t *environment,
HANDLE _stdin, HANDLE _stdout, HANDLE _stderr,
HANDLE *pStdInput, HANDLE *pStdOutput, HANDLE *pStdError,
int flags, bool useJobObject, HANDLE *hJob)
{
return runInteractiveProcessWrapper (cmd, workingDirectory, environment,
_stdin, _stdout, _stderr,
pStdInput, pStdOutput, pStdError,
flags, useJobObject, hJob, TRUE);
}
int
terminateProcess (ProcHandle handle)
{
if (!TerminateProcess ((HANDLE) handle, 1)) {
DWORD e = GetLastError();
DWORD exitCode;
/*
This is a crude workaround that is taken from libuv. For some reason
TerminateProcess() can fail with ERROR_ACCESS_DENIED if the process
already terminated. This situation can be detected by using
GetExitCodeProcess() to check if the exit code is availble. Unfortunately
this function succeeds and gives exit code 259 (STILL_ACTIVE) if the
process is still running. So there is no way to ditinguish a process
that exited with 259 and a process that did not exit because we had
insufficient access to terminate it.
One would expect WaitForSingleObject() to be the solid solution. But this
function does return WAIT_TIMEOUT in that situation. Even if called
after GetExitCodeProcess().
*/
if (e == ERROR_ACCESS_DENIED && GetExitCodeProcess((HANDLE) handle, &exitCode) && exitCode != STILL_ACTIVE)
return 0;
SetLastError(e);
maperrno();
return -1;
}
return 0;
}
int
terminateJob (ProcHandle handle)
{
if (!TerminateJobObject ((HANDLE)handle, 1)) {
maperrno();
return -1;
}
return 0;
}
int
getProcessExitCode (ProcHandle handle, int *pExitCode)
{
*pExitCode = 0;
if (WaitForSingleObject((HANDLE) handle, 1) == WAIT_OBJECT_0)
{
if (GetExitCodeProcess((HANDLE) handle, (DWORD *) pExitCode) == 0)
{
maperrno();
return -1;
}
return 1;
}
return 0;
}
int
waitForProcess (ProcHandle handle, int *pret)
{
DWORD retCode;
if (WaitForSingleObject((HANDLE) handle, INFINITE) == WAIT_OBJECT_0)
{
if (GetExitCodeProcess((HANDLE) handle, &retCode) == 0)
{
maperrno();
return -1;
}
*pret = retCode;
return 0;
}
maperrno();
return -1;
}
// Returns true on success.
int
waitForJobCompletion ( HANDLE hJob )
{
int process_count = 16;
JOBOBJECT_BASIC_PROCESS_ID_LIST *pid_list = NULL;
while (true) {
size_t pid_list_size = sizeof(JOBOBJECT_BASIC_PROCESS_ID_LIST) + sizeof(ULONG_PTR) * (process_count - 1);
if (pid_list == NULL) {
pid_list = malloc(pid_list_size);
if (pid_list == NULL) {
errno = ENOMEM;
return false;
}
pid_list->NumberOfAssignedProcesses = process_count;
}
// Find a process in the job...
bool success = QueryInformationJobObject(
hJob,
JobObjectBasicProcessIdList,
pid_list,
pid_list_size,
NULL);
if (!success && GetLastError() == ERROR_MORE_DATA) {
process_count *= 2;
free(pid_list);
pid_list = NULL;
continue;
} else if (!success) {
free(pid_list);
maperrno();
return false;
}
if (pid_list->NumberOfProcessIdsInList == 0) {
// We're done
free(pid_list);
return true;
}
HANDLE pHwnd;
bool found_p;
/* Find the first non-terminated handle. */
for (DWORD i = 0; i < pid_list->NumberOfProcessIdsInList; i++) {
pHwnd = OpenProcess(SYNCHRONIZE, FALSE, pid_list->ProcessIdList[i]);
int code = 0;
found_p = pHwnd && getProcessExitCode (pHwnd, &code) != -1 && code != STILL_ACTIVE;
if (found_p)
break;
}
/* All processes were terminated already. */
if (!found_p)
return true;
if (pHwnd == NULL) {
switch (GetLastError()) {
case ERROR_INVALID_PARAMETER:
case ERROR_INVALID_HANDLE:
// Presumably the process terminated; try again.
continue;
default:
free(pid_list);
maperrno();
return false;
}
}
// Wait for it to finish...
DWORD result = WaitForSingleObject(pHwnd, 200);
if (result == WAIT_TIMEOUT) {
int code = 0;
/* Current process is waiting to terminate. skip. */
if (getProcessExitCode (pHwnd, &code) != -1 && code != STILL_ACTIVE)
continue ;
} else if (result != WAIT_OBJECT_0) {
free(pid_list);
maperrno();
CloseHandle(pHwnd);
return false;
}
// The process signalled, loop again to try the next process.
CloseHandle(pHwnd);
}
}