Skip to content

Commit 22bd886

Browse files
committed
Do not log a warning if failure of retrieving the pgrp of a fd is due to the fd not being a TTY
1 parent 9578a1e commit 22bd886

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

Sources/ProcessInvocation/ProcessInvocation.swift

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -447,22 +447,29 @@ public struct ProcessInvocation : AsyncSequence {
447447
}
448448
}
449449

450+
func getFgPgIDSetInfo(for fd: FileDescriptor) -> (destFd: FileDescriptor, originalValue: Int32)? {
451+
let originalPgrp = tcgetpgrp(fd.rawValue)
452+
guard originalPgrp != -1 else {
453+
if errno != ENOTTY {
454+
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))"])
455+
} else {
456+
/* The ENOTTY error means the given fd does not have a TTY.
457+
* Setting the fg process group of a fd w/o a TTY does not make sense, so we skip it with an info message instead of a warning. */
458+
Conf.logger?.info("The given file descriptor does not have a TTY; skipping foreground process group ID setting.")
459+
}
460+
return nil
461+
}
462+
return (fd, originalPgrp)
463+
}
464+
450465
var fdsToCloseAfterRun = Set<FileDescriptor>()
451466
var fdRedirects = [FileDescriptor: FileDescriptor]()
452467
var outputFileDescriptors = additionalOutputFileDescriptors
453468
let fgPgIDSetInfo: (destFd: FileDescriptor, originalValue: Int32)?
454469
switch stdinRedirect {
455470
case .none(let setFgPgID):
456471
p.standardInput = FileHandle.standardInput /* Already the case in theory as it is the default, but let’s be explicit. */
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
465-
}
472+
fgPgIDSetInfo = (setFgPgID ? getFgPgIDSetInfo(for: FileDescriptor.standardInput) : nil)
466473

467474
case .fromNull:
468475
p.standardInput = nil
@@ -474,15 +481,7 @@ public struct ProcessInvocation : AsyncSequence {
474481
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.")
475482
fdsToCloseAfterRun.insert(fd)
476483
}
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
485-
}
484+
fgPgIDSetInfo = (setFgPgID ? getFgPgIDSetInfo(for: fd) : nil)
486485

487486
case .sendFromReader(let reader):
488487
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)