Skip to content

Commit

Permalink
fix several IO#reopen_path bugs particularly improper Errno usage
Browse files Browse the repository at this point in the history
  • Loading branch information
chuckremes committed Jan 30, 2015
1 parent 9009136 commit a9be154
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions kernel/common/io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def initialize(fd, stat)

if acc_mode < 0
# Assume it's closed.
if Errno.eql?(Errno::EBADF)
if Errno.eql?(Errno::EBADF::Errno)
@descriptor = -1
end

Expand Down Expand Up @@ -178,7 +178,7 @@ def read(length, output_string=nil)
bytes_read = read_into_storage(length, storage)

if bytes_read == -1
if Errno.eql?(Errno::EAGAIN) || Errno.eql?(Errno::EINTR)
if Errno.eql?(Errno::EAGAIN::Errno) || Errno.eql?(Errno::EINTR::Errno)
redo
else
Errno.handle "read(2) failed"
Expand Down Expand Up @@ -212,7 +212,7 @@ def read_into_storage(count, storage)
if bytes_read == -1
errno = Errno.errno

if errno == Errno::EAGAIN || errno == Errno::EINTR
if errno == Errno::EAGAIN::Errno || errno == Errno::EINTR::Errno
ensure_open
next
else
Expand Down Expand Up @@ -240,10 +240,10 @@ def write(str)

if bytes_written == -1
errno = Errno.errno
if errno == Errno::EINTR || errno == Errno::EAGAIN
if errno == Errno::EINTR::Errno || errno == Errno::EAGAIN::Errno
# do a #select and wait for descriptor to become writable
continue
elsif errno == Errno::EPIPE
elsif errno == Errno::EPIPE::Errno
if @descriptor == 1 || @descriptor == 2
return buf_size
end
Expand Down Expand Up @@ -367,22 +367,20 @@ def reopen(other_fd)

def reopen_path(path, mode)
current_fd = @descriptor

other_fd = -1
other_fd = FileDescriptor.open_with_cloexec(path, mode, 0666)

SystemCallError.errno_error("could not reopen path", Errno.errno, "reopen_path") if other_fd < 0

FileDescriptor.new_open_fd(other_fd)
Errno.handle("could not reopen path \"#{path}\"") if other_fd < 0

if FFI.call_failed?(FFI::Platform::POSIX.dup2(other_fd, current_fd))
if Errno.eql?(Errno::EBADF)
if Errno.eql?(Errno::EBADF::Errno)
# means current_fd is closed, so set ourselves to use the new fd and continue
@descriptor = other_fd
else
SystemCallError.errno_error("could not reopen path", Errno.errno, "reopen_path")
return nil
FFI::Platform::POSIX.close(other_fd) if other_fd > 0
Errno.handle("could not reopen path \"#{path}\"")
end
else
FFI::Platform::POSIX.close(other_fd)
end

# FIXME: figure out a way to reset the buffer in BufferedFileDescriptor
Expand Down

0 comments on commit a9be154

Please sign in to comment.