examples/jexec.rb
author Mahlon E. Smith <mahlon@martini.nu>
Sat, 28 Feb 2009 04:54:46 +0000
branchmahlon-misc
changeset 12 5fd07a9e7e7b
parent 0 92d00ff32c56
child 7 4460fc10c6a3
permissions -rw-r--r--
* Added jail header dependencies for mkmf Makefile. * Renamed header file (bsdjail -> jail) * Fixed copyright * Initial commit of primary functionality * Attach to a running jail * Create a new jail * List and instantiate existing jails * Fetch information about existing jails * Still needs... * Attach "in a block" fleshed out * Documentation and better comments * Tests (of some kind, this will be tough) * Update for not-yet-MFC'ed multiple IP jail patch in FBSD 7.2-STABLE
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     1
#!/usr/bin/ruby
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     2
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     3
BEGIN {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     4
	require 'pathname'
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     5
	basedir = Pathname.new( __FILE__ ).dirname.parent
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     6
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     7
	$LOAD_PATH.unshift basedir + "ext" unless 
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     8
		$LOAD_PATH.include? basedir + "ext"
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     9
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    10
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    11
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    12
require 'bsdjail'
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    13
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    14
jid = Integer( ARGV.shift )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    15
args = ARGV
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    16
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    17
$deferr.puts "In process #{Process.pid}, about to jail_attach() with a block"
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    18
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    19
childpid = BSD::Jail.attach( jid ) do
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    20
	Dir.chdir( "/" )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    21
	$deferr.puts "Child #{Process.pid} exec()ing:", "  " + args.join(" ")
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    22
	exec( *args )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    23
end
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    24
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    25
$deferr.puts "Parent: waiting on imprisoned child #{childpid}"
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    26
Process.waitpid( childpid )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    27
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    28
$deferr.puts "Child exited with exit code: %d" % [ $?.exitstatus ]
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    29