Skip to content

Commit 50e98b3

Browse files
committed
Avoid calling tcgetpgrp if unneeded
1 parent 81fc87f commit 50e98b3

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

Sources/ProcessInvocation/ProcessInvocation.swift

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -454,11 +454,15 @@ public struct ProcessInvocation : AsyncSequence {
454454
switch stdinRedirect {
455455
case .none(let setFgPgID):
456456
p.standardInput = FileHandle.standardInput /* Already the case in theory as it is the default, but let’s be explicit. */
457-
let originalPgrp = tcgetpgrp(FileDescriptor.standardInput.rawValue)
458-
if originalPgrp == -1 {
459-
Conf.logger?.warning("Cannot determine the process group ID of the process that owns standard input; skipping foreground process group ID setting.", metadata: ["error": "\(Errno(rawValue: errno))"])
457+
if setFgPgID {
458+
let originalPgrp = tcgetpgrp(FileDescriptor.standardInput.rawValue)
459+
if originalPgrp == -1 {
460+
Conf.logger?.warning("Cannot determine the process group ID of the process that owns standard input; skipping foreground process group ID setting.", metadata: ["error": "\(Errno(rawValue: errno))"])
461+
}
462+
fgPgIDSetInfo = (originalPgrp != -1 ? (FileDescriptor.standardInput, originalPgrp) : nil)
463+
} else {
464+
fgPgIDSetInfo = nil
460465
}
461-
fgPgIDSetInfo = (setFgPgID && originalPgrp != -1 ? (FileDescriptor.standardInput, originalPgrp) : nil)
462466

463467
case .fromNull:
464468
p.standardInput = nil
@@ -470,11 +474,15 @@ public struct ProcessInvocation : AsyncSequence {
470474
assert(fileDescriptorsToSend.isEmpty, "Giving ownership to fd on stdin is not allowed when launching the process via the bridge. This is because stdin has to be sent via the bridge and we get only pain and race conditions to properly close the fd.")
471475
fdsToCloseAfterRun.insert(fd)
472476
}
473-
let originalPgrp = tcgetpgrp(fd.rawValue)
474-
if originalPgrp == -1 {
475-
Conf.logger?.warning("Cannot determine the process group ID of the process that owns the given fd; skipping foreground process group ID setting.", metadata: ["error": "\(Errno(rawValue: errno))", "fd": "\(fd)"])
477+
if setFgPgID {
478+
let originalPgrp = tcgetpgrp(fd.rawValue)
479+
if originalPgrp == -1 {
480+
Conf.logger?.warning("Cannot determine the process group ID of the process that owns the given fd; skipping foreground process group ID setting.", metadata: ["error": "\(Errno(rawValue: errno))"])
481+
}
482+
fgPgIDSetInfo = (originalPgrp != -1 ? (fd, originalPgrp) : nil)
483+
} else {
484+
fgPgIDSetInfo = nil
476485
}
477-
fgPgIDSetInfo = (setFgPgID && originalPgrp != -1 ? (fd, originalPgrp) : nil)
478486

479487
case .sendFromReader(let reader):
480488
assert(fileDescriptorsToSend.isEmpty, "Sending data to stdin via a reader is not allowed when launching the process via the bridge. This is because stdin has to be sent via the bridge and we get only pain and race conditions to properly close the fd.")

0 commit comments

Comments
 (0)