Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use blocking IO on a TTY if it can't be reopened #6660

Merged
merged 2 commits into from
Sep 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions src/io/file_descriptor.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,18 @@ class IO::FileDescriptor < IO

# :nodoc:
def self.from_stdio(fd)
# If we have a TTY for stdin/out/err, it is a shared terminal.
# If we have a TTY for stdin/out/err, it is possibly a shared terminal.
# We need to reopen it to use O_NONBLOCK without causing other programs to break

# Figure out the terminal TTY name. If ttyname fails we have a non-tty, or something strange.
path = uninitialized UInt8[256]
if LibC.ttyname_r(fd, path, 256) == 0
# Open a fresh handle to the TTY
fd = LibC.open(path, LibC::O_RDWR)
ret = LibC.ttyname_r(fd, path, 256)
return new(fd, blocking: true) unless ret == 0

new(fd).tap(&.close_on_exec = true)
else
new(fd, blocking: true)
end
clone_fd = LibC.open(path, LibC::O_RDWR)
return new(fd, blocking: true) if clone_fd == -1

new(clone_fd).tap(&.close_on_exec = true)
end

def blocking
Expand Down