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