Skip to content

Instantly share code, notes, and snippets.

@headius
Created September 14, 2010 18:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save headius/579505 to your computer and use it in GitHub Desktop.
Save headius/579505 to your computer and use it in GitHub Desktop.
~/projects/jruby ➔ jruby -rreal_unix -e 'trap("INT") { puts :there; exit }; puts :here; select(nil, nil, nil, nil)'
here
^Cthere
~/projects/jruby ➔ jruby -rreal_unix -e 'puts $$; exec "echo $$"'
461
461
~/projects/jruby ➔ cat real_unix.rb
require 'ffi'
module RealUnix
extend FFI::Library
ffi_lib 'c'
attach_function :execv, [ :string, :buffer_in ], :int
attach_function :exit, [ :int ], :void
def self.exec(*args)
args.unshift "/bin/sh", "-c"
cmd = args[0]
exec_args = FFI::MemoryPointer.new(:pointer, args.length + 1)
args.each_with_index do |arg, i|
exec_args[i].put_pointer(0, FFI::MemoryPointer.from_string(arg))
end
execv(cmd, exec_args)
end
end
module Kernel
alias :old_exec :exec
alias :old_exit :exit
def exec(*args); RealUnix.exec(*args); end
def exit(code = 0); RealUnix.exit(code); end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment