examples/jexec.rb
changeset 7 4460fc10c6a3
parent 0 92d00ff32c56
equal deleted inserted replaced
6:2d52adc4adcc 7:4460fc10c6a3
     1 #!/usr/bin/ruby
     1 #!/usr/bin/env ruby
       
     2 #
       
     3 # An example 'jexec' in ruby.
       
     4 #
     2 
     5 
     3 BEGIN {
     6 BEGIN {
     4 	require 'pathname'
     7 	require 'pathname'
     5 	basedir = Pathname.new( __FILE__ ).dirname.parent
     8 	basedir = Pathname.new( __FILE__ ).dirname.parent
     6 	
     9 
     7 	$LOAD_PATH.unshift basedir + "ext" unless 
    10 	$LOAD_PATH.unshift basedir + "ext" unless 
     8 		$LOAD_PATH.include? basedir + "ext"
    11 		$LOAD_PATH.include? basedir + "ext"
     9 }
    12 }
    10 
    13 
       
    14 require 'jail'
    11 
    15 
    12 require 'bsdjail'
    16 BSD::Jail.attach( ARGV.shift.to_i )
       
    17 exec( *ARGV )
    13 
    18 
    14 jid = Integer( ARGV.shift )
       
    15 args = ARGV
       
    16 
       
    17 $deferr.puts "In process #{Process.pid}, about to jail_attach() with a block"
       
    18 
       
    19 childpid = BSD::Jail.attach( jid ) do
       
    20 	Dir.chdir( "/" )
       
    21 	$deferr.puts "Child #{Process.pid} exec()ing:", "  " + args.join(" ")
       
    22 	exec( *args )
       
    23 end
       
    24 
       
    25 $deferr.puts "Parent: waiting on imprisoned child #{childpid}"
       
    26 Process.waitpid( childpid )
       
    27 
       
    28 $deferr.puts "Child exited with exit code: %d" % [ $?.exitstatus ]
       
    29