Skip to content

Commit 482cc7c

Browse files
committed
---
yaml --- r: 347875 b: refs/heads/master c: 8bd2867 h: refs/heads/master i: 347873: 5360ad3 347871: b6fc417
1 parent ff5241f commit 482cc7c

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
refs/heads/master: a7f43a9984ff0bb8c6705b1aa8fa0735d1452a50
2+
refs/heads/master: 8bd28674564967504bc5a77e606e68b2dd3dac33
33
refs/heads/master-next: 203b3026584ecad859eb328b2e12490099409cd5
44
refs/tags/osx-passed: b6b74147ef8a386f532cf9357a1bde006e552c54
55
refs/tags/swift-2.2-SNAPSHOT-2015-12-01-a: 6bb18e013c2284f2b45f5f84f2df2887dc0f7dea

trunk/stdlib/private/SwiftPrivateLibcExtras/Subprocess.swift

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ public func spawnChild(_ args: [String])
217217
if pid == 0 {
218218
// pid of 0 means we are now in the child process.
219219
// Capture the output before executing the program.
220+
close(childStdout.readFD)
221+
close(childStdin.writeFD)
222+
close(childStderr.readFD)
223+
close(childToParentPipe.readFD)
220224
dup2(childStdout.writeFD, STDOUT_FILENO)
221225
dup2(childStdin.readFD, STDIN_FILENO)
222226
dup2(childStderr.writeFD, STDERR_FILENO)
@@ -261,6 +265,41 @@ public func spawnChild(_ args: [String])
261265

262266
// Close the pipe when we're done writing the error.
263267
close(childToParentPipe.writeFD)
268+
} else {
269+
close(childToParentPipe.writeFD)
270+
271+
// Figure out if the child’s call to execve was successful or not.
272+
var readfds = _stdlib_fd_set()
273+
readfds.set(childToParentPipe.readFD)
274+
var writefds = _stdlib_fd_set()
275+
var errorfds = _stdlib_fd_set()
276+
errorfds.set(childToParentPipe.readFD)
277+
278+
var ret: CInt
279+
repeat {
280+
ret = _stdlib_select(&readfds, &writefds, &errorfds, nil)
281+
} while ret == -1 && errno == EINTR
282+
if ret <= 0 {
283+
fatalError("select() returned an error: \(errno)")
284+
}
285+
286+
if readfds.isset(childToParentPipe.readFD) || errorfds.isset(childToParentPipe.readFD) {
287+
var childErrno: CInt = 0
288+
let readResult: ssize_t = withUnsafeMutablePointer(to: &childErrno) {
289+
return read(childToParentPipe.readFD, $0, MemoryLayout.size(ofValue: $0.pointee))
290+
}
291+
if readResult == 0 {
292+
// We read an EOF indicating that the child's call to execve was successful.
293+
} else if readResult < 0 {
294+
fatalError("read() returned error: \(errno)")
295+
} else {
296+
// We read an error from the child.
297+
print(String(cString: strerror(childErrno)))
298+
preconditionFailure("execve() failed")
299+
}
300+
}
301+
302+
close(childToParentPipe.readFD)
264303
}
265304
#else
266305
var fileActions = _make_posix_spawn_file_actions_t()

0 commit comments

Comments
 (0)