ext/bsdjail.c
author Mahlon E. Smith <mahlon@martini.nu>
Sat, 28 Feb 2009 06:52:48 +0000
changeset 6 2d52adc4adcc
parent 5 41a1542b3b10
permissions -rw-r--r--
* Merge of the mahlon experimental branch.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     1
/*
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
     2
 *  bsdjail.c - JParallel extension for FreeBSD jail(2) functions
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     3
 *  $Id$
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     4
 *  
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     5
 *  Authors:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     6
 *    * Michael Granger <ged@FaerieMUD.org>
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
     7
 *    * Mahlon E. Smith <mahlon@martini.nu>
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     8
 *  
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
     9
 *  Copyright (c) 2008, Michael Granger and Mahlon Smith
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    10
 *  All rights reserved.
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    11
 *  
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    12
 *  Redistribution and use in source and binary forms, with or without
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    13
 *  modification, are permitted provided that the following conditions are met:
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    14
 *  
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    15
 *    * Redistributions of source code must retain the above copyright notice,
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    16
 *      this list of conditions and the following disclaimer.
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    17
 *  
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    18
 *    * Redistributions in binary form must reproduce the above copyright notice,
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    19
 *      this list of conditions and the following disclaimer in the documentation
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    20
 *      and/or other materials provided with the distribution.
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    21
 *  
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    22
 *    * Neither the name of the author/s, nor the names of the project's
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    23
 *      contributors may be used to endorse or promote products derived from this
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    24
 *      software without specific prior written permission.
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    25
 *  
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    26
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    27
 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    28
 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    29
 *  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    30
 *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    31
 *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    32
 *  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    33
 *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    34
 *  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    35
 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    36
 *  
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    37
 */
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    38
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    39
#include "bsdjail.h"
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    40
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    41
VALUE rbjail_mBSD;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    42
VALUE rbjail_cBSDJail;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    43
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    44
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    45
/*
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    46
 *  Debug logging function
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    47
 */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    48
void
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    49
#ifdef HAVE_STDARG_PROTOTYPES
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    50
rbjail_debug(const char *fmt, ...)
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    51
#else
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    52
rbjail_debug( const char *fmt, va_dcl )
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    53
#endif
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    54
{
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    55
	char buf[BUFSIZ], buf2[BUFSIZ];
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    56
	va_list	args;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    57
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    58
	if ( !RTEST(ruby_debug) ) return;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    59
5
41a1542b3b10 Cleaned up bsdjail extension code a bit more and made the extconf check for
Michael Granger <ged@FaerieMUD.org>
parents: 2
diff changeset
    60
	snprintf( buf, BUFSIZ, "Debug>>> %s", fmt );
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    61
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    62
	va_init_list( args, fmt );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    63
	vsnprintf( buf2, BUFSIZ, buf, args );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    64
	fputs( buf2, stderr );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    65
	fputs( "\n", stderr );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    66
	fflush( stderr );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    67
	va_end( args );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    68
}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    69
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    70
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    71
/*
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    72
struct jail {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    73
	u_int32_t       version;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    74
	char            *path;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    75
	char            *hostname;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    76
	u_int32_t       ip_number;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    77
};
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    78
*/
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    79
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    80
/*
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    81
 * Allocation function
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    82
 */
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    83
static jail *
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
    84
rbjail_jail_alloc() {
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    85
	jail *ptr = ALLOC( jail );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    86
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    87
	ptr->version	= 0;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    88
	ptr->path		= NULL;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    89
	ptr->hostname	= NULL;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    90
	ptr->ip_number	= 0;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    91
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    92
	debugMsg(( "Initialized a jail pointer <%p>", ptr ));
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    93
	return ptr;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    94
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    95
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    96
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    97
/*
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    98
 * GC Free function
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    99
 */
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   100
static void
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   101
rbjail_gc_free( jail *ptr ) {
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   102
	if ( ptr ) {
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   103
		if ( ptr->path ) xfree( ptr->path );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   104
		if ( ptr->hostname ) xfree( ptr->hostname );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   105
		
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   106
		ptr->path		= NULL;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   107
		ptr->hostname	= NULL;
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   108
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   109
		xfree( ptr );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   110
	}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   111
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   112
	else {
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   113
		debugMsg(( "Not freeing an uninitialized jail" ));
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   114
	}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   115
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   116
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   117
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   118
/*
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   119
 * Object validity checker. Returns the data pointer.
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   120
 */
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   121
static jail *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   122
rbjail_check_jail( VALUE self ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   123
	debugMsg(( "Checking a BSD::Jail object (%d).", self ));
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   124
	Check_Type( self, T_DATA );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   125
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   126
    if ( !rb_obj_is_kind_of(self, rbjail_cBSDJail) ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   127
		rb_raise( rb_eTypeError, "wrong argument type %s (expected BSD::Jail)",
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   128
				  rb_class2name(CLASS_OF( self )) );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   129
    }
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   130
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   131
	return DATA_PTR( self );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   132
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   133
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   134
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   135
/*
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   136
 * Fetch the data pointer and check it for sanity.
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   137
 */
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   138
static jail *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   139
rbjail_get_jail( VALUE self ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   140
	jail *ptr = check_sentence( self );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   141
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   142
	debugMsg(( "Fetching a Jail (%p).", ptr ));
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   143
	if ( !ptr )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   144
		rb_raise( rb_eRuntimeError, "uninitialized Sentence" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   145
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   146
	return ptr;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   147
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   148
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   149
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   150
/* --------------------------------------------------------------
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   151
 * Jail utility functions
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   152
 * -------------------------------------------------------------- */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   153
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   154
/*
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   155
 * Try to jail_attach() to the specified +jid+, raising an exception if it fails.
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   156
 */
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   157
static void
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   158
rbjail_do_jail_attach( int jid )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   159
{
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   160
	if ( jail_attach(jid) == -1 )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   161
		rb_sys_fail( "jail_attach" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   162
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   163
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   164
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   165
/* 
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   166
 * Fork + Block function for rbjail_attach(). Mostly stolen from Ruby's process.c.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   167
 */
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   168
static VALUE
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   169
rbjail_attach_block( int jid )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   170
{
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   171
    int pid;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   172
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   173
    rb_secure(2);
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   174
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   175
    fflush( stdout );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   176
    fflush( stderr );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   177
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   178
	switch ( pid = fork() ) {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   179
		case 0:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   180
			rb_thread_atfork();
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   181
			if ( rb_block_given_p() ) {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   182
				int status;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   183
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   184
				rbjail_do_jail_attach( jid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   185
				rb_protect( rb_yield, Qundef, &status );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   186
				ruby_stop( status );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   187
			}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   188
			return Qnil;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   189
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   190
		case -1:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   191
			rb_sys_fail( "fork(2)" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   192
			return Qnil;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   193
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   194
		default:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   195
			return INT2FIX( pid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   196
	}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   197
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   198
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   199
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   200
/* --------------------------------------------------------------
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   201
 * Class methods
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   202
 * -------------------------------------------------------------- */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   203
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   204
/*
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   205
 *  call-seq:
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   206
 *     BSD::Jail.allocate   -> bsdjail
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   207
 *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   208
 *  Allocate a new BSD::Jail object.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   209
 *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   210
 */
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   211
static VALUE
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   212
rbjail_jail_s_allocate( VALUE klass ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   213
	return Data_Wrap_Struct( klass, 0, rbjail_gc_free, 0 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   214
}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   215
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   216
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   217
/*
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   218
 *  call-seq:
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   219
 *     BSD::Jail.list   -> array
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   220
 *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   221
 *  Return an Array of all the running jails on the host.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   222
 *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   223
 */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   224
static VALUE
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   225
rbjail_jail_s_list( VALUE klass ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   226
	VALUE rval = rb_ary_new();
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   227
	struct xprison *xp;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   228
	struct in_addr in;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   229
	size_t i, len;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   230
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   231
	if ( sysctlbyname("jail.list", NULL, &len, NULL, 0) == -1 )
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   232
		rb_sys_fail( "sysctlbyname(): jail.list" );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   233
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   234
	xp = ALLOCA_N( xprison, 1 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   235
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   236
	if ( sysctlbyname("jail.list", xp, &len, NULL, 0) == -1 ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   237
		rb_sys_fail( "sysctlbyname(): jail.list" );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   238
	}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   239
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   240
	if ( len < sizeof(*xp) || len % sizeof(*xp) ||
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   241
		xp->pr_version != KINFO_PRISON_VERSION )
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   242
		rb_fatal( "Kernel and userland out of sync" );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   243
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   244
	len /= sizeof( *xp );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   245
	for ( i = 0; i < len; i++ ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   246
		VALUE jail, args[3];
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   247
		
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   248
		/* Hostname */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   249
		args[0] = rb_str_new2( xp->pr_host );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   250
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   251
		/* IP */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   252
		in.s_addr = ntohl( xp->pr_ip );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   253
		args[1] = rb_str_new2( inet_ntoa(in) );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   254
		
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   255
		/* Path */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   256
		args[2] = rb_str_new2( xp->pr_path );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   257
		
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   258
		jail = rb_class_new_instance( 3, args, klass );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   259
		rb_ary_push( rval, jail );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   260
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   261
		xp++;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   262
	}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   263
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   264
	return rval;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   265
}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   266
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   267
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   268
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   269
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   270
/* --------------------------------------------------------------
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   271
 * Instance methods
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   272
 * -------------------------------------------------------------- */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   273
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   274
/*
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   275
 *  call-seq:
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   276
 *     BSD::Jail.new( hostname, ip, path )           -> new_jail
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   277
 *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   278
 *  Create a new jail for the given +hostname+, +ip+, and +path+.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   279
 */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   280
static VALUE
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   281
rbjail_jail_initialize( VALUE self, VALUE hostname, VALUE ip, VALUE path ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   282
	if ( !rbjail_check_jail(self) ) {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   283
		struct jail *ptr = rbjail_jail_alloc();
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   284
		char *pathstr = NULL, *hostnamestr = NULL;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   285
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   286
		Check_Type( hostname, T_STRING );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   287
		Check_Type( ip, T_STRING );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   288
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   289
		pathstr = ALLOC_N( char, RSTRING(path)->len );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   290
		strncpy( pathstr, RSTRING(path)->ptr, RSTRING(path)->len );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   291
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   292
		hostnamestr = ALLOC_N( char, RSTRING(hostname)->len );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   293
		strncpy( hostnamestr, RSTRING(hostname)->ptr, RSTRING(hostname)->len );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   294
		
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   295
		/*
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   296
		struct jail {
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   297
			u_int32_t       version;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   298
			char            *path;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   299
			char            *hostname;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   300
			u_int32_t       ip_number;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   301
		};
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   302
		*/
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   303
		ptr->version = 0;   /* Per the manpage's recommendation */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   304
		ptr->ip_number = inet_addr( StringValuePtr(ip) );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   305
		ptr->path = path;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   306
		ptr->hostname = hostname;
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   307
	}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   308
}
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   309
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   310
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   311
/*
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   312
 *  call-seq:
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   313
 *     jail.attach             -> true or false
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   314
 *     jail.attach { block }   -> pid
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   315
 *
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   316
 *  Attach to the given jail. In the non-block form, attach the current process to the
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   317
 *  jail and return +true+ if it succeeds. This is a one-way operation, and requires root
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   318
 *  privileges.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   319
 *  
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   320
 *  In the block form, the process will be forked, and the block will be attached to the jail and 
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   321
 *  run by the child. The parent process will receive the process ID of the child.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   322
 */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   323
static VALUE
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   324
rbjail_attach( int argc, VALUE *argv, VALUE self ) {
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   325
	VALUE jidnum, rval;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   326
	int jid;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   327
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   328
	rb_scan_args( argc, argv, "1", &jidnum );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   329
	jid = NUM2INT( jidnum );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   330
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   331
	if ( rb_block_given_p() ) {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   332
		rval = rbjail_attach_block( jid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   333
	}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   334
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   335
	else {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   336
		rbjail_do_jail_attach( jid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   337
		rval = Qtrue;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   338
	}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   339
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   340
	return rval;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   341
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   342
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   343
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   344
/*
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   345
 * I can't remember how the hell you document a class, but that will go here.
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   346
 */
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   347
void
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   348
Init_bsdjail( void ) {
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   349
	rbjail_mBSD = rb_define_module( "BSD" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   350
	rbjail_cBSDJail = rb_define_class_under( rbjail_mBSD, "Jail" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   351
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   352
	/* Class methods */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   353
	rb_define_alloc_function( rbjail_cBSDJail, rbjail_jail_s_allocate );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   354
	rb_define_singleton_method( rbjail_cBSDJail, "jail", rbjail_jail_s_jail, 0 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   355
	rb_define_singleton_method( rbjail_cBSDJail, "list", rbjail_jail_s_list, 0 );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   356
	
2
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   357
	/* Instance methods */
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   358
	rb_define_method( rbjail_cBSDJail, "initialize", rbjail_jail_initialize, 3 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   359
	rb_define_method( rbjail_cBSDJail, "attach", rbjail_jail_attach, -1 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   360
	rb_define_method( rbjail_cBSDJail, "hostname", rbjail_jail_hostname, 0 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   361
	rb_define_method( rbjail_cBSDJail, "path", rbjail_jail_path, 0 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   362
	rb_define_method( rbjail_cBSDJail, "ip", rbjail_jail_ip, 0 );
0c24586f579a Checkpoint commit.
Michael Granger <ged@FaerieMUD.org>
parents: 0
diff changeset
   363
	rb_define_method( rbjail_cBSDJail, "jid", rbjail_jail_jid, 0 );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   364
	
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   365
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   366