/usr/lib/perl5/Inline/Java/JVM.pm is in libinline-java-perl 0.53-1build1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 | package Inline::Java::JVM ;
use strict ;
use Carp ;
use IO::File ;
use IPC::Open3 ;
use IO::Socket ;
use Text::ParseWords ;
use Inline::Java::Portable ;
$Inline::Java::JVM::VERSION = '0.53' ;
my %SIGS = () ;
my @SIG_LIST = ('HUP', 'INT', 'PIPE', 'TERM') ;
sub new {
my $class = shift ;
my $o = shift ;
my $this = {} ;
bless($this, $class) ;
foreach my $sig (@SIG_LIST){
local $SIG{__WARN__} = sub {} ;
if (exists($SIG{$sig})){
$SIGS{$sig} = $SIG{$sig} ;
}
}
$this->{socket} = undef ;
$this->{JNI} = undef ;
$this->{embedded} = $o->get_java_config('EMBEDDED_JNI') ;
$this->{owner} = 1 ;
$this->{destroyed} = 0 ;
$this->{private} = $o->get_java_config('PRIVATE') ;
$this->{debugger} = $o->get_java_config('DEBUGGER') ;
if ($this->{embedded}){
Inline::Java::debug(1, "using embedded JVM...") ;
}
else{
Inline::Java::debug(1, "starting JVM...") ;
}
my $args = $o->get_java_config('EXTRA_JAVA_ARGS') ;
if ($o->get_java_config('JNI')){
Inline::Java::debug(1, "JNI mode") ;
# Split args and remove quotes
my @args = map {s/(['"])(.*)\1/$2/ ; $_}
parse_line('\s+', 1, $args) ;
my $jni = new Inline::Java::JNI(
$ENV{CLASSPATH} || '',
\@args,
$this->{embedded},
Inline::Java::get_DEBUG(),
$o->get_java_config('NATIVE_DOUBLES'),
) ;
$jni->create_ijs() ;
$this->{JNI} = $jni ;
}
else {
Inline::Java::debug(1, "client/server mode") ;
my $debug = Inline::Java::get_DEBUG() ;
$this->{shared} = $o->get_java_config('SHARED_JVM') ;
$this->{start_jvm} = $o->get_java_config('START_JVM') ;
$this->{port} = $o->get_java_config('PORT') ;
$this->{host} = $o->get_java_config('HOST') ;
# Used to limit the bind of the JVM server
$this->{'bind'} = $o->get_java_config('BIND') ;
# Grab the next free port number and release it.
if ((! $this->{shared})&&($this->{port} < 0)){
if (Inline::Java::Portable::portable("GOT_NEXT_FREE_PORT")){
my $sock = IO::Socket::INET->new(
Listen => 0, Proto => 'tcp',
LocalAddr => 'localhost', LocalPort => 0) ;
if ($sock){
$this->{port} = $sock->sockport() ;
Inline::Java::debug(2, "next available port number is $this->{port}") ;
close($sock) ;
}
else{
# Revert to the default.
$this->{port} = - $this->{port} ;
carp(
"Could not get next available port number, using port " .
"$this->{port} instead. Use the PORT configuration " .
"option to suppress this warning.\n Error: $!\n") ;
}
}
else{
# Revert to the default.
# Try this maybe: 9000 + $$ ?
$this->{port} = - $this->{port} ;
}
}
# Check if JVM is already running
if ($this->{shared}){
eval {
$this->reconnect() ;
} ;
if (! $@){
Inline::Java::debug(1, "connected to already running JVM!") ;
return $this ;
}
if (! $this->{start_jvm}){
croak("Can't find running JVM and START_JVM = 0") ;
}
}
my $java = File::Spec->catfile($o->get_java_config('J2SDK'),
Inline::Java::Portable::portable("J2SDK_BIN"),
($this->{debugger} ? "jdb" : "java") .
Inline::Java::Portable::portable("EXE_EXTENSION")) ;
my $shared = ($this->{shared} ? "true" : "false") ;
my $priv = ($this->{private} ? "true" : "false") ;
my $native_doubles = ($o->get_java_config('NATIVE_DOUBLES') ? "true" : "false") ;
my $cmd = Inline::Java::Portable::portable("SUB_FIX_CMD_QUOTES", "\"$java\" $args org.perl.inline.java.InlineJavaServer $debug $this->{bind} $this->{port} $shared $priv $native_doubles") ;
Inline::Java::debug(2, $cmd) ;
if ($o->get_config('UNTAINT')){
($cmd) = $cmd =~ /(.*)/ ;
}
my $pid = 0 ;
eval {
$pid = $this->launch($o, $cmd) ;
} ;
croak "Can't exec JVM: $@" if $@ ;
if ($this->{shared}){
# As of 0.40, we release by default.
$this->release() ;
}
else{
$this->capture() ;
}
$this->{pid} = $pid ;
$this->{socket} = setup_socket(
$this->{host},
$this->{port},
# Give the user an extra hour's time set breakpoints and the like...
($this->{debugger} ? 3600 : 0) + int($o->get_java_config('STARTUP_DELAY')),
0
) ;
}
return $this ;
}
sub launch {
my $this = shift ;
my $o = shift ;
my $cmd = shift ;
local $SIG{__WARN__} = sub {} ;
my $dn = Inline::Java::Portable::portable("DEV_NULL") ;
my $in = ($this->{debugger} ? ">&STDIN" : new IO::File("<$dn")) ;
if (! defined($in)){
croak "Can't open $dn for reading" ;
}
my $out = ">&STDOUT" ;
if ($this->{shared}){
$out = new IO::File(">$dn") ;
if (! defined($out)){
croak "Can't open $dn for writing" ;
}
}
my $err = ">&STDERR" ;
my $pid = open3($in, $out, $err, $cmd) ;
if (! $this->{debugger}){
close($in) ;
}
if ($this->{shared}){
close($out) ;
}
return $pid ;
}
sub DESTROY {
my $this = shift ;
$this->shutdown() ;
}
sub shutdown {
my $this = shift ;
if ($this->{embedded}){
Inline::Java::debug(1, "embedded JVM, skipping shutdown.") ;
return ;
}
if (! $this->{destroyed}){
if ($this->am_owner()){
Inline::Java::debug(1, "JVM owner exiting...") ;
if ($this->{socket}){
# This asks the Java server to stop and die.
my $sock = $this->{socket} ;
if ($sock->peername()){
Inline::Java::debug(1, "Sending 'die' message to JVM...") ;
print $sock "die\n" ;
}
else{
carp "Lost connection with Java virtual machine" ;
}
close($sock) ;
if ($this->{pid}){
# Here we go ahead and send the signals anyway to be very
# sure it's dead...
# Always be polite first, and then insist.
if (Inline::Java::Portable::portable('GOT_SAFE_SIGNALS')){
Inline::Java::debug(1, "Sending 15 signal to JVM...") ;
kill(15, $this->{pid}) ;
Inline::Java::debug(1, "Sending 9 signal to JVM...") ;
kill(9, $this->{pid}) ;
}
# Reap the child...
waitpid($this->{pid}, 0) ;
}
}
if ($this->{JNI}){
$this->{JNI}->shutdown() ;
}
}
else{
# We are not the JVM owner, so we simply politely disconnect
if ($this->{socket}){
Inline::Java::debug(1, "JVM non-owner exiting...") ;
close($this->{socket}) ;
$this->{socket} = undef ;
}
# This should never happen in JNI mode
}
$this->{destroyed} = 1 ;
}
}
# This cannot be a member function because it can be used
# elsewhere to connect to the JVM.
sub setup_socket {
my $host = shift ;
my $port = shift ;
my $timeout = shift ;
my $one_shot = shift ;
my $socket = undef ;
my $last_words = "timeout\n" ;
my $got_alarm = Inline::Java::Portable::portable("GOT_ALARM") ;
eval {
local $SIG{ALRM} = sub { die($last_words) ; } ;
if ($got_alarm){
alarm($timeout) ;
}
# ignore expected "connection refused" warnings
# Thanks binkley!
local $SIG{__WARN__} = sub {
warn($@) unless ($@ =~ /Connection refused/i) ;
} ;
while (1){
$socket = new IO::Socket::INET(
PeerAddr => $host,
PeerPort => $port,
Proto => 'tcp') ;
if (($socket)||($one_shot)){
last ;
}
select(undef, undef, undef, 0.1) ;
}
if ($got_alarm){
alarm(0) ;
}
} ;
if ($@){
if ($@ eq $last_words){
croak "JVM taking more than $timeout seconds to start, or died before Perl could connect. Increase config STARTUP_DELAY if necessary." ;
}
else{
if ($got_alarm){
alarm(0) ;
}
croak $@ ;
}
}
if (! $socket){
croak "Can't connect to JVM at ($host:$port): $!" ;
}
$socket->autoflush(1) ;
return $socket ;
}
sub reconnect {
my $this = shift ;
if (($this->{JNI})||(! $this->{shared})){
return ;
}
if ($this->{socket}){
# Close the previous socket
close($this->{socket}) ;
$this->{socket} = undef ;
}
my $socket = setup_socket(
$this->{host},
$this->{port},
0,
1
) ;
$this->{socket} = $socket ;
# Now that we have reconnected, we release the JVM
$this->release() ;
}
sub capture {
my $this = shift ;
if (($this->{JNI})||(! $this->{shared})){
return ;
}
foreach my $sig (@SIG_LIST){
if (exists($SIG{$sig})){
$SIG{$sig} = \&Inline::Java::done ;
}
}
$this->{owner} = 1 ;
}
sub am_owner {
my $this = shift ;
return $this->{owner} ;
}
sub release {
my $this = shift ;
if (($this->{JNI})||(! $this->{shared})){
return ;
}
foreach my $sig (@SIG_LIST){
local $SIG{__WARN__} = sub {} ;
if (exists($SIG{$sig})){
$SIG{$sig} = $SIGS{$sig} ;
}
}
$this->{owner} = 0 ;
}
sub process_command {
my $this = shift ;
my $inline = shift ;
my $data = shift ;
my $resp = undef ;
# Patch by Simon Cozens for perl -wle 'use Our::Module; do_stuff()'
local $/ = "\n" ;
local $\ = "" ;
# End Patch
while (1){
Inline::Java::debug(3, "packet sent is $data") ;
if ($this->{socket}){
my $sock = $this->{socket} ;
print $sock $data . "\n" or
croak "Can't send packet to JVM: $!" ;
$resp = <$sock> ;
if (! $resp){
croak "Can't receive packet from JVM: $!" ;
}
# Release the reference since the object has been sent back
# to Java.
$Inline::Java::Callback::OBJECT_HOOK = undef ;
}
if ($this->{JNI}){
$Inline::Java::JNI::INLINE_HOOK = $inline ;
$resp = $this->{JNI}->process_command($data) ;
}
chomp($resp) ;
Inline::Java::debug(3, "packet recv is $resp") ;
# We got an answer from the server. Is it a callback?
if ($resp =~ /^callback/o){
($data, $Inline::Java::Callback::OBJECT_HOOK) = Inline::Java::Callback::InterceptCallback($inline, $resp) ;
next ;
}
else{
last ;
}
}
return $resp ;
}
1 ;
|