IO.for_fd strangeness?

IO.for_fd seems to work with $stdin.fileno, but it seems to not work for
others; specifically fd’s returned from inotify_init(2).

In the case of jruby 1.5.6, it tosses an exception (see link below). In
the
case of 1.6.0.RC2 it actually wraps the wrong fd. IO.for_fd(5).fileno ==
2 -
crazy, and wrong :wink:

Code and output here: https://gist.github.com/856207

(I would’ve filed this as a bug, but I can’t find the “sign up” link for
the
jira instance jruby uses just now)

-Jordan

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 05/03/11 07:41, Jordan S. wrote:

the jira instance jruby uses just now)

  • From http://www.ruby-forum.com/topic/566695 , I found JRuby can’t,
    doesn’t, know about real UNIX file descriptors, other than the
    predictable 0, 1 & 2. It fakes it internally, but I don’t think Java
    lets you reference real file descriptors from the outside, so JRuby
    can’t help you.

Matthew
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk1y1gQACgkQT2rVDg8aLXSIEgCeJFKK7MsqGKHB+VzzwgF6RvFX
48gAnj3n7a1/Y7ggTCn3rIAv2Ogti3SI
=EepF
-----END PGP SIGNATURE-----

On Sat, Mar 5, 2011 at 4:32 PM, Matthew B.
[email protected]wrote:

Yeah, walking around through the java6 docs doesn’t show anything
terribly
promising. There’s java.io.FileDescriptor but there’s no way to create
it
with an existing file descriptor.

I’ll probably invoke read(2) with FFI via libc now, instead. I’ll let
you
know what I come up with.

-Jordan

On Sat, Mar 5, 2011 at 7:09 PM, Jordan S. [email protected]
wrote:

doesn’t, know about real UNIX file descriptors, other than the
know what I come up with.

Yeah, exposing read(2) with FFI works as expected to read things I
would
normally use IO.for_fd(x) to read from.

It looks a bit like this:

module CFuncs
  extend FFI::Library
  ffi_lib FFI::Library::LIBC

  attach_function :read, [:int, :pointer, :size_t], :int
end

...
read_buffer = FFI::MemoryPointer.new(:char, 4096)
bytes = CFuncs.read(fd, read_buffer, 4096)

-Jordan

-Jordan

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs