ext/jail.c
author Mahlon E. Smith <mahlon@martini.nu>
Tue, 03 Mar 2009 22:23:45 +0000
changeset 7 4460fc10c6a3
parent 6 2d52adc4adcc
permissions -rw-r--r--
* Now with 87% more hot jail action! * Predeclared all C methods in jail.h, so they could be arranged in logical order in jail.c * Fixed the extconf namespace. * Added rdoc. * Added usage examples, demonstrating jls, jexec, and jail ruby equivalents. * Re-added the "attach and execute within a block" code. * Added Enumerable and Comparable support. * Return 'path' as a Pathname object. TODO: * Create the actual 'jParallel' shell binary, now that we have a good backend framework. * Tests? How? * Add support for recently committed (will be part of 7.2-RELEASE) multiple IPs per jail, and jail labels.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     1
/*
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
     2
 *  jail.c - Ruby jParallel
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     3
 *
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     4
 *  vim: set nosta noet ts=4 sw=4:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     5
 *
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     6
 *  $Id$
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     7
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     8
 *  Authors:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
     9
 *	* Michael Granger <ged@FaerieMUD.org>
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    10
 *	* Mahlon E. Smith <mahlon@martini.nu>
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    11
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    12
 *  Copyright (c) 2008, Michael Granger and Mahlon E. Smith
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    13
 *  All rights reserved.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    14
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    15
 *  Redistribution and use in source and binary forms, with or without
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    16
 *  modification, are permitted provided that the following conditions are met:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    17
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    18
 *	* Redistributions of source code must retain the above copyright notice,
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    19
 *	  this list of conditions and the following disclaimer.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    20
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    21
 *	* Redistributions in binary form must reproduce the above copyright notice,
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    22
 *	  this list of conditions and the following disclaimer in the documentation
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    23
 *	  and/or other materials provided with the distribution.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    24
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    25
 *	* Neither the name of the author/s, nor the names of the project's
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    26
 *	  contributors may be used to endorse or promote products derived from this
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    27
 *	  software without specific prior written permission.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    28
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    29
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    30
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    31
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    32
 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    33
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    34
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    35
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    36
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    37
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    38
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    39
 *  
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    40
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    41
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    42
#include "jail.h"
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    43
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    44
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
    45
/* --------------------------------------------------------------
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
    46
 * Utility functions
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
    47
 * -------------------------------------------------------------- */
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    48
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    49
/*
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    50
 *  Debug logging function
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    51
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    52
void
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    53
#ifdef HAVE_STDARG_PROTOTYPES
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    54
rbjail_debug(const char *fmt, ...)
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    55
#else
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    56
rbjail_debug( const char *fmt, va_dcl )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    57
#endif
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    58
{
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    59
	char buf[BUFSIZ], buf2[BUFSIZ];
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    60
	va_list args;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    61
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    62
	if ( !RTEST(ruby_debug) ) return;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    63
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    64
	snprintf( buf, BUFSIZ, "Debug>>> %s", fmt );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    65
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    66
	va_init_list( args, fmt );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    67
	vsnprintf( buf2, BUFSIZ, buf, args );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    68
	fputs( buf2, stderr );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    69
	fputs( "\n", stderr );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    70
	fflush( stderr );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    71
	va_end( args );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    72
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    73
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    74
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    75
/*
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    76
 * Object validity checker. Returns the data pointer.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    77
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    78
static struct xprison *
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    79
rbjail_check_jail( VALUE self ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    80
	debugMsg(( "Checking a BSD::Jail object (%d).", self ));
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    81
	Check_Type( self, T_DATA );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    82
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    83
	if ( !rb_obj_is_kind_of(self, rbjail_cBSDJail) ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    84
		rb_raise( rb_eTypeError, "wrong argument type %s (expected BSD::Jail)",
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
    85
			rb_obj_classname( self ));
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    86
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    87
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    88
	return DATA_PTR( self );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    89
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    90
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    91
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    92
/*
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    93
 * Fetch the data pointer and check it for sanity.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    94
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    95
static struct xprison *
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    96
rbjail_get_jailptr( VALUE self ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    97
	struct xprison *ptr = rbjail_check_jail( self );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    98
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
    99
	debugMsg(( "Fetching a Jail (%p).", ptr ));
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   100
	if ( !ptr )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   101
		rb_raise( rb_eRuntimeError, "uninitialized Jail" );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   102
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   103
	return ptr;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   104
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   105
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   106
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   107
/*
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   108
 * Attach to a running jail and chdir to the root.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   109
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   110
static int
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   111
rbjail_do_attach( int jid )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   112
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   113
	int attach_status = jail_attach(jid);
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   114
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   115
	if ( attach_status == -1 )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   116
		rb_sys_fail( "jail_attach" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   117
	if ( chdir("/") == -1 ) rb_sys_fail( "chdir" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   118
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   119
	return attach_status;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   120
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   121
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   122
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   123
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   124
 * Attach to a running jail from within a block, forking first
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   125
 * and returning the child pid.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   126
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   127
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   128
rbjail_attach_block( int jid )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   129
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   130
	int pid;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   131
	int status;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   132
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   133
	if ( ! rb_block_given_p() ) return Qnil;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   134
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   135
	rb_secure(2);
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   136
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   137
	fflush(stdout);
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   138
	fflush(stderr);
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   139
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   140
	switch ( pid = fork() ) {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   141
		case 0:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   142
			rb_thread_atfork();
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   143
			rbjail_do_attach( jid );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   144
			rb_protect( rb_yield, Qundef, &status );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   145
			rb_exit( status );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   146
			return Qnil;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   147
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   148
		case -1:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   149
			rb_sys_fail( "fork(2)" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   150
			return Qnil;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   151
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   152
		default:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   153
			return INT2FIX( pid );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   154
	}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   155
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   156
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   157
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   158
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   159
/* --------------------------------------------------------------
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   160
 * Memory-management functions
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   161
 * -------------------------------------------------------------- */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   162
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   163
/*
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   164
 * Copy memory from the given 'xp' to a ruby managed object.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   165
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   166
static VALUE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   167
rbjail_alloc( VALUE class, struct xprison *xp )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   168
{
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   169
	struct xprison *rbjail_xp = ALLOC( struct xprison );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   170
	VALUE rbjail = rb_funcall( class, rb_intern("allocate"), 0 );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   171
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   172
	// replace the null pointer obj with an xprison ptr.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   173
	//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   174
	memcpy( rbjail_xp, xp, sizeof( struct xprison ) );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   175
	DATA_PTR( rbjail ) = rbjail_xp;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   176
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   177
	return rbjail;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   178
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   179
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   180
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   181
/*
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   182
 * GC Free function
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   183
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   184
static void
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   185
rbjail_gc_free( struct xprison *ptr ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   186
	if ( ptr ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   187
		xfree( ptr );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   188
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   189
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   190
	else {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   191
		debugMsg(( "Not freeing an uninitialized jail" ));
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   192
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   193
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   194
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   195
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   196
/*
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   197
 * Allocate a new ruby object with a NULL pointer.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   198
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   199
static VALUE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   200
rbjail_s_alloc( VALUE class )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   201
{
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   202
	return Data_Wrap_Struct( class, NULL, rbjail_gc_free, NULL );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   203
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   204
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   205
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   206
/* --------------------------------------------------------------
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   207
 * Class methods
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   208
 * -------------------------------------------------------------- */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   209
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   210
/*
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   211
 *  call-seq:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   212
 *     BSD::Jail.new( ip, path, host )   => jail_id
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   213
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   214
 *  Create a new BSD::Jail object from required +ip+, +path+, and +host+
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   215
 *  arguments.  You can optionally pass a +securelevel+ fourth argument.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   216
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   217
 *  Returns the +id+ of the newly created jail.
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   218
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   219
static VALUE
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   220
rbjail_jail( int argc, VALUE *argv, VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   221
{
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   222
	struct jail j;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   223
	struct in_addr in;
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   224
	VALUE ip, path, host, sec_level;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   225
	int id;
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   226
	int securelevel = -1;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   227
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   228
	rb_scan_args( argc, argv, "31", &ip, &path, &host, &sec_level );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   229
	if ( sec_level != Qnil ) securelevel = FIX2INT( sec_level );
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   230
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   231
	if ( inet_aton( RSTRING_PTR( rb_obj_as_string(ip) ), &in ) == 0 )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   232
		rb_raise( rb_eArgError, "Could not make sense of ip number: %s", ip );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   233
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   234
	SafeStringValue(path);
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   235
	SafeStringValue(host);
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   236
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   237
	j.version   = 0;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   238
	j.path	    = RSTRING_PTR( path );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   239
	j.hostname  = RSTRING_PTR( host );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   240
	j.ip_number = ntohl( in.s_addr );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   241
	id = jail(&j);
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   242
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   243
	if ( id == -1 ) rb_sys_fail( "jail" );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   244
	if ( chdir("/") == -1 ) rb_sys_fail( "chdir" );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   245
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   246
	if ( securelevel > -1 && securelevel < 4 ) {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   247
		debugMsg(( "Setting securelevel to: %d", securelevel ));
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   248
		if ( sysctlbyname("kern.securelevel", NULL, 0, &securelevel, sizeof(securelevel)) )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   249
			rb_sys_fail( "securelevel" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   250
	}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   251
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   252
	debugMsg(( "New jail created with id: %d\n", id ));
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   253
	return INT2FIX( id );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   254
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   255
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   256
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   257
/*
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   258
 *  call-seq:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   259
 *     BSD::Jail.find_by_jid( jid ) => BSD::Jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   260
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   261
 *  Iterate over the currently instantiated jails, returning a jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   262
 *  object that matches the given +jid+, or nil if no jail is found.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   263
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   264
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   265
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   266
rbjail_find_by_jid( VALUE self, VALUE jid )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   267
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   268
	VALUE args[0];
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   269
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   270
	if ( TYPE(jid) != T_FIXNUM )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   271
		rb_raise( rb_eTypeError, "invalid argument to find_by_jid(): %s",
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   272
			RSTRING_PTR( rb_inspect(jid)) );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   273
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   274
	args[0] = jid;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   275
	return rbjail_find( 1, args, self );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   276
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   277
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   278
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   279
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   280
 *  call-seq:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   281
 *     BSD::Jail.search( id )                      => BSD::Jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   282
 *     BSD::Jail.search( hostname )                => [ BSD::Jail, ... ]
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   283
 *     BSD::Jail.search( IPAddr.new('127.0.0.1') ) => [ BSD::Jail, ... ]
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   284
 *     BSD::Jail.search( Pathname.new('/tmp') )    => [ BSD::Jail, ... ]
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   285
 *     BSD::Jail.search                            => [ BSD::Jail, BSD::Jail, ... ]
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   286
 *     BSD::Jail.search { |obj| block }            => obj
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   287
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   288
 *  Iterate over the currently instantiated jails, returning a jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   289
 *  object that matches the given argument. 
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   290
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   291
 *  If the argument is an integer, it is assumed to be a JID.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   292
 *  Otherwise, it is converted to a string, and compared against
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   293
 *  IP addresses, hostnames, and paths.  JIDs are unique, so there is
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   294
 *  only one object that can match.  Only the matching object (or nil)
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   295
 *  is returned in that event.  IPs, hostnames, and paths can be
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   296
 *  shared between jails, so searching on those return an array populated
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   297
 *  with matched jail objects -- or if there are no valid matches, an empty array.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   298
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   299
 *  Without an argument, return an array of all jails as objects.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   300
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   301
 */
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   302
static VALUE
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   303
rbjail_find( int argc, VALUE *argv, VALUE self )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   304
{ 
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   305
	struct xprison *sxp, *xp;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   306
	struct in_addr in;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   307
	size_t i, len;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   308
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   309
	VALUE arg, rbjail;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   310
	VALUE jails = rb_ary_new();
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   311
	int jid = 0, compare = 0;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   312
	char *str = "";
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   313
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   314
	rb_scan_args( argc, argv, "01", &arg );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   315
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   316
	// An argument was passed, so let's figure out what it was
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   317
	// and try to compare it to the current jails.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   318
	//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   319
	if ( argc == 1 ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   320
		switch ( TYPE(arg) ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   321
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   322
			// find by JID
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   323
			//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   324
			case T_FIXNUM:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   325
				jid = FIX2INT( arg );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   326
				break;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   327
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   328
			// find by IP/hostname/path
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   329
			//
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   330
			case T_OBJECT:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   331
			case T_DATA:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   332
			case T_STRING:
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   333
				str = RSTRING_PTR( rb_obj_as_string(arg) );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   334
				compare = 1;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   335
				break;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   336
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   337
			default:
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   338
				rb_raise( rb_eTypeError, "invalid argument: %s",
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   339
						RSTRING_PTR( rb_inspect(arg)) );
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   340
		}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   341
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   342
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   343
	// Get the size of the xprison and allocate memory to it.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   344
	//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   345
	if ( sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1 )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   346
		rb_sys_fail("sysctlbyname(): security.jail.list");
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   347
	if ( len <= 0 ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   348
		rb_sys_fail("sysctlbyname(): unable to determine xprison size");
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   349
		return Qnil;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   350
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   351
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   352
	sxp = xp = malloc( len );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   353
	if ( sxp == NULL ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   354
		rb_sys_fail("sysctlbyname(): unable to allocate memory");
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   355
		return Qnil;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   356
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   357
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   358
	// Get and sanity check the current prison list
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   359
	//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   360
	if ( sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1 ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   361
		if ( errno == ENOMEM ) free( sxp );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   362
		rb_sys_fail("sysctlbyname(): out of memory");
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   363
		return Qnil;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   364
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   365
	if ( len < sizeof(*xp) || len % sizeof(*xp) || xp->pr_version != XPRISON_VERSION )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   366
		rb_fatal("Kernel and userland out of sync");
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   367
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   368
	// No arguments to find() -- yield each successive jail,
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   369
	// and return an array of all jail objects.
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   370
	//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   371
	if ( argc == 0 ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   372
		for ( i = 0; i < len / sizeof(*xp); i++ ) {
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   373
			rbjail = rbjail_alloc( self, xp );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   374
			if ( rb_block_given_p() ) rb_yield( rbjail );
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   375
			rb_ary_push( jails, rbjail_alloc( self, xp ) );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   376
			xp++;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   377
		}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   378
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   379
		free( sxp );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   380
		return jails;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   381
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   382
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   383
	// Argument passed to find(): walk the jail list, comparing the arg
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   384
	// with each current jail.  Return all matches.
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   385
	//
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   386
	for ( i = 0; i < len / sizeof(*xp); i++ ) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   387
		in.s_addr = ntohl(xp->pr_ip);
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   388
		if (( compare == 0 && xp->pr_id == jid ) ||
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   389
				( compare == 1 &&
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   390
				  (( strcmp( str, inet_ntoa(in) ) == 0 ) ||
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   391
				   ( strcmp( str, xp->pr_host )   == 0 ) ||
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   392
				   ( strcmp( str, xp->pr_path )   == 0 ))
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   393
				)) {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   394
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   395
			debugMsg(( "Located jail: %d", xp->pr_id ));
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   396
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   397
			// If the user already knows the JID, there can only be
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   398
			// one match.  Return it immediately.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   399
			//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   400
			if ( compare == 0 ) {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   401
				free( sxp );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   402
				return rbjail_alloc( self, xp );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   403
			}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   404
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   405
			// If searching for anything other than JID, the argument
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   406
			// isn't unique.  Put matching jails into an array.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   407
			//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   408
			rb_ary_push( jails, rbjail_alloc( self, xp ) );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   409
			xp++;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   410
		}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   411
		else {
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   412
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   413
			xp++;
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   414
		}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   415
	}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   416
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   417
	free( sxp );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   418
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   419
	if ( compare == 0 && rb_ary_shift( jails ) == Qnil )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   420
		return Qnil;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   421
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   422
	return jails;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   423
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   424
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   425
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   426
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   427
 *  call-seq:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   428
 *     BSD::Jail.attach( id )       => true
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   429
 *     BSD::Jail.attach( hostname ) => true
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   430
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   431
 *  Attach to a currently running jail instance directly.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   432
 *  Operates under the same rules as BSD::Jail#search -- you may
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   433
 *  specify a jail by IP Address, hostname, or jid.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   434
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   435
 *  Please note that attaching your process into a jail is a one way
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   436
 *  operation that requires root privileges.  You must fork() if
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   437
 *  your process needs to continue in the host environment.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   438
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   439
 */
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   440
static VALUE
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   441
rbjail_class_attach( VALUE self, VALUE arg )
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   442
{
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   443
	int jid = 0;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   444
	VALUE jails;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   445
	VALUE find_args [0];
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   446
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   447
	switch ( TYPE(arg) ) {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   448
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   449
		// The user knows the JID already, attach directly.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   450
		//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   451
		case T_FIXNUM:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   452
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   453
			jid = FIX2INT( arg );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   454
			rbjail_do_attach( jid );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   455
			break;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   456
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   457
		// Find the JID to attach to.  First match wins.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   458
		//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   459
		case T_OBJECT:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   460
		case T_DATA:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   461
		case T_STRING:
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   462
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   463
			find_args[0] = arg;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   464
			jails = rbjail_find( 1, find_args, self );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   465
			jid = FIX2INT( rbjail_get_jid( rb_ary_shift(jails) ));
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   466
			rbjail_do_attach( jid );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   467
			break;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   468
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   469
		default:
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   470
			rb_raise( rb_eTypeError, "invalid argument to attach(): %s",
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   471
						RSTRING_PTR( rb_inspect(arg)) );
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   472
	}
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   473
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   474
	return Qtrue;
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   475
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   476
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   477
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   478
/* --------------------------------------------------------------
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   479
 * Instance methods
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   480
 * -------------------------------------------------------------- */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   481
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   482
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   483
 *  call-seq:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   484
 *     BSD::Jail <=> BSD::Jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   485
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   486
 *  Interface for Comparable.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   487
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   488
 */
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   489
static VALUE
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   490
rbjail_compare( VALUE self, VALUE other )
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   491
{
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   492
	debugMsg(("self id: %s, other id: %s",
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   493
				RSTRING_PTR(rb_inspect( self )),
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   494
				RSTRING_PTR(rb_inspect( other ))));
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   495
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   496
	return rb_funcall( 
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   497
		rbjail_get_jid( self ),
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   498
		rb_intern("<=>"),
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   499
		1,
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   500
		rbjail_get_jid( other )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   501
	);
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   502
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   503
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   504
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   505
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   506
 * Fetch the configured IP address for the jail.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   507
 * Returns an IPAddr object.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   508
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   509
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   510
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   511
rbjail_get_ip( VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   512
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   513
	struct xprison *xp = rbjail_get_jailptr( self );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   514
	struct in_addr in;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   515
	VALUE args [0];
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   516
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   517
	in.s_addr = ntohl( xp->pr_ip );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   518
	args[0] = rb_str_new2( inet_ntoa(in) );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   519
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   520
	return rb_class_new_instance( 1, args, rbjail_cIPAddr );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   521
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   522
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   523
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   524
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   525
 * Fetch the assigned JID for the jail.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   526
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   527
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   528
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   529
rbjail_get_jid( VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   530
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   531
	struct xprison *xp = rbjail_get_jailptr( self );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   532
	return INT2FIX( xp->pr_id );
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   533
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   534
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   535
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   536
/*
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   537
 * Fetch the configured hostname for the jail as a string.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   538
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   539
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   540
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   541
rbjail_get_host( VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   542
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   543
	struct xprison *xp = rbjail_get_jailptr( self );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   544
	return rb_str_new2( xp->pr_host );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   545
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   546
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   547
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   548
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   549
 * Fetch the configured path for the jail.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   550
 * Returns a Pathname object.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   551
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   552
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   553
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   554
rbjail_get_path( VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   555
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   556
	struct xprison *xp = rbjail_get_jailptr( self );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   557
	VALUE args[0];
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   558
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   559
	args[0] = rb_str_new2( xp->pr_path );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   560
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   561
	return rb_class_new_instance( 1, args, rbjail_cPathname );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   562
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   563
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   564
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   565
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   566
 * Return a human readable version of the object.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   567
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   568
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   569
rbjail_inspect( VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   570
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   571
	char inspect_str[BUFSIZ];
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   572
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   573
	sprintf( inspect_str, "#<%s:0x%07x jid:%d (%s)>",
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   574
		rb_obj_classname( self ),
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   575
		NUM2UINT(rb_obj_id( self )) * 2,
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   576
		FIX2INT(rbjail_get_jid( self )),
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   577
		RSTRING_PTR(rbjail_get_host( self ))
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   578
	);
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   579
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   580
	return rb_str_new2( inspect_str );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   581
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   582
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   583
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   584
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   585
 *  call-seq:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   586
 *     jail.attach
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   587
 *     jail.attach do ... end  => child pid
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   588
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   589
 *  Attach to the jail.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   590
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   591
 *  Please note that attaching your process into a jail is a one way
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   592
 *  operation that requires root privileges.  You must fork() if
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   593
 *  your process needs to continue in the host environment.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   594
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   595
 * 	In the block form, a fork() is performed automatically.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   596
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   597
 */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   598
static VALUE
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   599
rbjail_instance_attach( VALUE self )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   600
{
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   601
	int jid = FIX2INT( rbjail_get_jid( self ) );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   602
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   603
	if ( rb_block_given_p() ) {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   604
		return rbjail_attach_block( jid );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   605
	}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   606
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   607
	else {
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   608
		rbjail_do_attach( jid );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   609
	}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   610
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   611
	return Qtrue;
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   612
}
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   613
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   614
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   615
/* --------------------------------------------------------------
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   616
 * Initalizer
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   617
 * -------------------------------------------------------------- */
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   618
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   619
/*
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   620
 *  BSD::Jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   621
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   622
 *  Ruby bindings for the FreeBSD jail(2) subsystem.
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   623
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   624
 *  Example usage:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   625
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   626
 *  	require 'bsd/jail'
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   627
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   628
 *      # create a new jail
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   629
 *      jid = BSD::Jail.create( '127.0.0.1', '/tmp', 'testjail' )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   630
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   631
 *      # find existing jail(s)
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   632
 *      jail  = BSD::Jail.find_by_jid( jid )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   633
 *      jails = BSD::Jail.search( hostname )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   634
 *      jails = BSD::Jail.search( IPAddr.new('127.0.0.1') )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   635
 *      jails = BSD::Jail.search( Pathname.new('/tmp') )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   636
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   637
 *      # attach to jails
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   638
 *      BSD::Jail.attach( jid )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   639
 *      jail.attach do
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   640
 *     	    ... do something fancy!
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   641
 *      end
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   642
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   643
 *  BSD::Jail includes behaviors from Enumerable and Comparable,
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   644
 *  so you can do things such as the following:
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   645
 *
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   646
 *      # Are these two instantiated jails the same jid?
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   647
 *      BSD::Jail.search( '/tmp' ) == BSD::Jail.search( 'testjail' )
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   648
 *      
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   649
 *      # Alternative interface for finding a jail by jid
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   650
 *      BSD::Jail.find { |j| j.jid == 3 }
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   651
 *      
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   652
 *      # Return all jails as an array
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   653
 *      jails = BSD::Jail.find_all
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   654
 *      
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   655
 *      # What jails have IPs within the 192.168.16.0/24 class C netblock?
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   656
 *      nb = IPAddr.new( '192.168.16.0/24' );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   657
 *      jails = BSD::Jail.find? { |j| nb.include?(j.ip) }
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   658
 *
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   659
 */
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   660
void
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   661
Init_jail( void )
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   662
{
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   663
	// namespacing
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   664
	//
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   665
	rbjail_mBSD	= rb_define_module( "BSD" );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   666
	rbjail_cBSDJail = rb_define_class_under( rbjail_mBSD, "Jail", rb_cObject );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   667
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   668
	// struct wrapping function
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   669
	rb_define_alloc_func( rbjail_cBSDJail, rbjail_s_alloc );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   670
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   671
	// Make the 'new' method private.
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   672
	rb_funcall( rbjail_cBSDJail, rb_intern("private_class_method"), 1, ID2SYM(rb_intern("new")) );
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   673
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   674
	// class methods
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   675
	//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   676
	rb_define_singleton_method( rbjail_cBSDJail, "attach", rbjail_class_attach, 1 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   677
	rb_define_singleton_method( rbjail_cBSDJail, "create", rbjail_jail, -1 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   678
	rb_define_singleton_method( rbjail_cBSDJail, "search", rbjail_find, -1 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   679
	rb_define_singleton_method( rbjail_cBSDJail, "find_by_jid", rbjail_find_by_jid, 1 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   680
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   681
	// instance methods
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   682
	//
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   683
	rb_define_method( rbjail_cBSDJail, "attach", rbjail_instance_attach, 0 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   684
	rb_define_method( rbjail_cBSDJail, "host", rbjail_get_host, 0 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   685
	rb_define_alias(  rbjail_cBSDJail, "hostname", "host" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   686
	rb_define_method( rbjail_cBSDJail, "inspect", rbjail_inspect, 0 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   687
	rb_define_method( rbjail_cBSDJail, "ip",   rbjail_get_ip,   0 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   688
	rb_define_method( rbjail_cBSDJail, "jid",  rbjail_get_jid,  0 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   689
	rb_define_method( rbjail_cBSDJail, "path", rbjail_get_path, 0 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   690
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   691
	// Additional (base) modules for accessor wrapping
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   692
	//
7
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   693
	rb_require( "ipaddr" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   694
	rb_require( "pathname" );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   695
	rbjail_cIPAddr   = rb_const_get( rb_cObject, rb_intern("IPAddr") );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   696
	rbjail_cPathname = rb_const_get( rb_cObject, rb_intern("Pathname") );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   697
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   698
	// Enumerable!
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   699
	//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   700
	rb_define_singleton_method( rbjail_cBSDJail, "each", rbjail_find, -1 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   701
	rb_extend_object( rbjail_cBSDJail, rb_const_get( rb_cObject, rb_intern("Enumerable") ));
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   702
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   703
	// Comparable!
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   704
	//
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   705
	rb_define_method( rbjail_cBSDJail, "<=>", rbjail_compare, 1 );
4460fc10c6a3 * Now with 87% more hot jail action!
Mahlon E. Smith <mahlon@martini.nu>
parents: 6
diff changeset
   706
	rb_include_module( rbjail_cBSDJail, rb_const_get( rb_cObject, rb_intern("Comparable") ));
6
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   707
}
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   708
2d52adc4adcc * Merge of the mahlon experimental branch.
Mahlon E. Smith <mahlon@martini.nu>
parents:
diff changeset
   709