ext/bsdjail.c
author Mahlon E. Smith <mahlon@martini.nu>
Thu, 25 Dec 2008 08:35:46 +0000
branchmahlon-misc
changeset 11 e908d309e7ec
parent 9 4c51ebe6e9b6
permissions -rw-r--r--
* It compiles! * Removed additional leftover link parser stuff. * Removed unused memory allocate/free stuff until I discuss with my cohort. It may be back... it may be left over from linkparser too. ;) * list() works, but cores. Not sure why yet. Really, list() should be renamed to something sensible, and return instantiated BSD::Jail objects to attach() to or get additional info on. * Ruby C bindings are way, way fun. Reminds me how little C I can remember with one project a year :)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     1
/*
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
     2
 *
9
4c51ebe6e9b6 * Add a mkrf monkeypatch so BSD build flags are generated correctly.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
     3
 *  bsdjail.c - Ruby jparallel
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     4
 *  $Id$
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
     5
 *
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
     6
 *  vim: set nosta noet ts=4 sw=4:
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     7
 *  
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     8
 *  Authors:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
     9
 *    * Michael Granger <ged@FaerieMUD.org>
9
4c51ebe6e9b6 * Add a mkrf monkeypatch so BSD build flags are generated correctly.
Mahlon E. Smith <mahlon@martini.nu>
parents: 0
diff changeset
    10
 *    * Mahlon E. Smith <mahlon@martini.nu>
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    11
 *  
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    12
 *  Copyright (c) 2006 The FaerieMUD Consortium.
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    13
 *  
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    14
 *  This work is licensed under the Creative Commons Attribution License. To
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    15
 *  view a copy of this license, visit
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    16
 *  http://creativecommons.org/licenses/by/1.0 or send a letter to Creative
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    17
 *  Commons, 559 Nathan Abbott Way, Stanford, California 94305, USA.
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    18
 *  
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    19
 */
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    20
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    21
#include "bsdjail.h"
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    22
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    23
VALUE rbjail_mBSD;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    24
VALUE rbjail_cBSDJail;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    25
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    26
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    27
/*
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    28
struct jail {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    29
	u_int32_t       version;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    30
	char            *path;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    31
	char            *hostname;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    32
	u_int32_t       ip_number;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    33
};
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    34
*/
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    35
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    36
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    37
static void
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    38
rbjail_do_jail_attach( int jid )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    39
{
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    40
	if ( jail_attach(jid) == -1 )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    41
		rb_sys_fail( "jail_attach" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    42
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    43
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    44
/* Mostly ripped off from Ruby's process.c */
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    45
static VALUE
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    46
rbjail_attach_block( int jid )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    47
{
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    48
	int pid;
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    49
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    50
	rb_secure(2);
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    51
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    52
	fflush(stdout);
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    53
	fflush(stderr);
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    54
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    55
	switch ( pid = fork() ) {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    56
		case 0:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    57
			rb_thread_atfork();
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    58
			if ( rb_block_given_p() ) {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    59
				int status;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    60
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    61
				rbjail_do_jail_attach( jid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    62
				rb_protect( rb_yield, Qundef, &status );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    63
				ruby_stop( status );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    64
			}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    65
			return Qnil;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    66
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    67
		case -1:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    68
			rb_sys_fail( "fork(2)" );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    69
			return Qnil;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    70
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    71
		default:
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    72
			return INT2FIX( pid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    73
	}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    74
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    75
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    76
static VALUE
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    77
rbjail_attach( int argc, VALUE *argv, VALUE self )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    78
{
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    79
	VALUE jidnum, rval;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    80
	int jid;
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    81
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    82
	rb_scan_args( argc, argv, "1", &jidnum );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    83
	jid = NUM2INT( jidnum );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    84
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    85
	if ( rb_block_given_p() ) {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    86
		rval = rbjail_attach_block( jid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    87
	}
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    88
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    89
	else {
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    90
		rbjail_do_jail_attach( jid );
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    91
		rval = Qtrue;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    92
	}
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    93
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    94
	return rval;
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
static VALUE
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
    98
rbjail_list( VALUE self )
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
    99
{
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   100
	struct xprison *xp;
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   101
	struct in_addr in;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   102
	size_t i, len;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   103
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   104
	/* Get the size of the xprison and allocate memory to it. */
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   105
	if ( sysctlbyname("security.jail.list", NULL, &len, NULL, 0) == -1 )
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   106
		rb_sys_fail("sysctlbyname(): security.jail.list");
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   107
	xp = ALLOCA_N( struct xprison, 1 );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   108
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   109
	/* Get and sanity check the current prison list */
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   110
	if ( sysctlbyname("security.jail.list", xp, &len, NULL, 0) == -1 ) {
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   111
		rb_sys_fail("sysctlbyname(): security.jail.list");
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   112
	}
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   113
	if ( len < sizeof(*xp) || len % sizeof(*xp) ||
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   114
			xp->pr_version != XPRISON_VERSION )
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   115
		rb_fatal("Kernel and userland out of sync");
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   116
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   117
	len /= sizeof(*xp);
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   118
	printf("   JID  IP Address      Hostname                      Path\n");
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   119
	for ( i = 0; i < len; i++ ) {
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   120
		in.s_addr = ntohl( xp->pr_ip );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   121
		printf("%6d  %-15.15s %-29.29s %.74s\n",
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   122
				xp->pr_id, inet_ntoa(in), xp->pr_host, xp->pr_path);
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   123
		xp++;
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   124
	}
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   125
	return self;
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   126
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   127
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   128
void
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   129
Init_bsdjail( void )
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   130
{
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   131
	rbjail_mBSD = rb_define_module( "BSD" );
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   132
	rbjail_cBSDJail = rb_define_class_under( rbjail_mBSD, "Jail", rb_cObject );
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   133
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   134
	rb_define_singleton_method( rbjail_cBSDJail, "list", rbjail_list, 0 );
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   135
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   136
	/*
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   137
	rb_define_alloc_function( rbjail_cBSDJail, );
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   138
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   139
	rb_define_method( rbjail_cBSDJail, "attach", rbjail_attach, -1 );
11
e908d309e7ec * It compiles!
Mahlon E. Smith <mahlon@martini.nu>
parents: 9
diff changeset
   140
	*/
0
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   141
}
92d00ff32c56 Initial checkin
Michael Granger <ged@FaerieMUD.org>
parents:
diff changeset
   142