[FFI] how to bind to a function with variable number of args

O

Ollivier Robert

I'm looking at ruby-ffi 0.2.0 with MRI 1.8.7 (haven't tried with
rubinius yet) and I can get to attach to setproctitle(3) defined like
this:

void
setproctitle(const char *fmt, ...);

-----
require "ffi"
#
module LibC
extend FFI::Library

attach_function :setproctitle, [:string, :string], []
end

LibC.setproctitle("Ruby: executing %s", $0)
----
gives
-----
FFI::NotFoundError: Function 'setproctitle' not found in [[current
process]]
from
/opt/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/library.rb:65:in
`attach_function'
 
L

Luc Heinrich

I'm looking at ruby-ffi 0.2.0 with MRI 1.8.7 (haven't tried with
rubinius yet) and I can get to attach to setproctitle(3) defined like
this:

void
setproctitle(const char *fmt, ...);

This would be defined like this:

module LibC
extend FFI::Library
attach_function : setproctitle, [:string, :varargs], :void
end

And then used like this:

LibC.setproctitle("%d-%d", [:string, "foo", :int, 42])
 
O

Ollivier Robert

This would be defined like this:

The following does not work:
-----
module LibC
extend FFI::Library

attach_function :setproctitle, [:string, :varargs], :void
end

LibC.setproctitle("Ruby: executing %s", [:string, $0])
-----

Same error:
/opt/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/library.rb:65:in
`attach_function': Function 'setproctitle' not found in [[current process]]
(FFI::NotFoundError) from proc.rb:12

(OS X, 10.5.6, Ruby from MacPorts)

UPDATE: setproctitle may not be available in OS X. Retrying on FreeBSD leads
to this:

/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/types.rb:18:in `find_type': Unable to resolve type 'stringproc.rb' (FFI::TypeError)
from /usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/variadic.rb:23:in `call'
from (eval):3:in `setproctitle'
from proc.rb:15
 
L

Luc Heinrich

UPDATE: setproctitle may not be available in OS X.

Right. setproctitle is not available on OS X.
Retrying on FreeBSD leads
to this:

/usr/local/lib/ruby/gems/1.8/gems/ffi-0.2.0/lib/ffi/types.rb:18:in
`find_type': Unable to resolve type 'stringproc.rb' (FFI::TypeError)

Yes, and that's because I need some sleep ;)

This is the right way to call variadic functions using FFI (remove the
[ ]):

LibC.setproctitle("Ruby: executing %s", :string, $0)

Sorry about the wrong answers... :/
 
O

Ollivier Robert

This is the right way to call variadic functions using FFI (remove the
[ ]):

LibC.setproctitle("Ruby: executing %s", :string, $0)

.... and the winner is you :)

47629 ph S+ 0:00.26 ruby: Ruby: executing proc.rb (ruby)

(some redundancy there... :) )
Sorry about the wrong answers... :/

No worry, merci.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top