Skip to content

Commit 908227d

Browse files
authored
Merge pull request #2928 from spevans/pr_sr_13861
2 parents 6897b19 + faef552 commit 908227d

File tree

3 files changed

+44
-10
lines changed

3 files changed

+44
-10
lines changed

Sources/Foundation/Process.swift

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -944,11 +944,17 @@ open class Process: NSObject {
944944
try _throwIfPosixError(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
945945
}
946946

947-
#if canImport(Darwin)
947+
#if canImport(Darwin)
948948
var spawnAttrs: posix_spawnattr_t? = nil
949949
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
950+
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_SETPGROUP)))
950951
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_CLOEXEC_DEFAULT)))
951-
#else
952+
#else
953+
var spawnAttrs: posix_spawnattr_t = posix_spawnattr_t()
954+
try _throwIfPosixError(posix_spawnattr_init(&spawnAttrs))
955+
try _throwIfPosixError(posix_spawnattr_setflags(&spawnAttrs, .init(POSIX_SPAWN_SETPGROUP)))
956+
957+
// POSIX_SPAWN_CLOEXEC_DEFAULT is an Apple extension so emulate it.
952958
for fd in 3 ... findMaximumOpenFD() {
953959
guard adddup2[fd] == nil &&
954960
!addclose.contains(fd) &&
@@ -957,7 +963,7 @@ open class Process: NSObject {
957963
}
958964
try _throwIfPosixError(_CFPosixSpawnFileActionsAddClose(fileActions, fd))
959965
}
960-
#endif
966+
#endif
961967

962968
let fileManager = FileManager()
963969
let previousDirectoryPath = fileManager.currentDirectoryPath
@@ -972,16 +978,10 @@ open class Process: NSObject {
972978

973979
// Launch
974980
var pid = pid_t()
975-
#if os(macOS)
976981
guard _CFPosixSpawn(&pid, launchPath, fileActions, &spawnAttrs, argv, envp) == 0 else {
977982
throw _NSErrorWithErrno(errno, reading: true, path: launchPath)
978983
}
979-
#else
980-
guard _CFPosixSpawn(&pid, launchPath, fileActions, nil, argv, envp) == 0 else {
981-
throw _NSErrorWithErrno(errno, reading: true, path: launchPath)
982-
}
983-
#endif
984-
984+
posix_spawnattr_destroy(&spawnAttrs)
985985

986986
// Close the write end of the input and output pipes.
987987
if let pipe = standardInput as? Pipe {

Tests/Foundation/Tests/TestProcess.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,35 @@ class TestProcess : XCTestCase {
806806
}
807807
}
808808

809+
func test_processGroup() throws {
810+
// The process group of the child process should be different to the parent's.
811+
let process = Process()
812+
813+
process.executableURL = xdgTestHelperURL()
814+
process.arguments = ["--pgrp"]
815+
let pipe = Pipe()
816+
process.standardOutput = pipe
817+
process.standardError = nil
818+
819+
try process.run()
820+
process.waitUntilExit()
821+
XCTAssertEqual(process.terminationStatus, 0)
822+
823+
let data = pipe.fileHandleForReading.availableData
824+
guard let string = String(data: data, encoding: .ascii) else {
825+
XCTFail("Could not read stdout")
826+
return
827+
}
828+
829+
let parts = string.trimmingCharacters(in: .newlines).components(separatedBy: ": ")
830+
guard parts.count == 2, parts[0] == "pgrp", let childPgrp = Int(parts[1]) else {
831+
XCTFail("Could not pgrp fron stdout")
832+
return
833+
}
834+
let parentPgrp = Int(getpgrp())
835+
XCTAssertNotEqual(parentPgrp, childPgrp, "Child process group \(parentPgrp) should not equal parent process group \(childPgrp)")
836+
}
837+
809838
static var allTests: [(String, (TestProcess) -> () throws -> Void)] {
810839
var tests = [
811840
("test_exit0" , test_exit0),
@@ -835,6 +864,7 @@ class TestProcess : XCTestCase {
835864
("test_currentDirectory", test_currentDirectory),
836865
("test_pipeCloseBeforeLaunch", test_pipeCloseBeforeLaunch),
837866
("test_multiProcesses", test_multiProcesses),
867+
("test_processGroup", test_processGroup),
838868
]
839869

840870
#if !os(Windows)

Tests/Tools/XDGTestHelper/main.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,10 @@ case "--signal-test":
274274

275275
case "--print-open-file-descriptors":
276276
printOpenFileDescriptors()
277+
278+
case "--pgrp":
279+
print("pgrp: \(getpgrp())")
280+
277281
#endif
278282

279283
default:

0 commit comments

Comments
 (0)