/usr/share/perl5/TakTuk/Pilot.pm is in libtaktuk-perl 3.7.6-1.
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 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 | package TakTuk::Pilot;
use strict; use bytes;
use Carp;
use Exporter;
our $VERSION = "1.1";
our $RELEASE = sprintf "%d", q$Rev: 1 $ =~ /(\d+)/g;
our @ISA = qw(Exporter);
use constant EARGCM => 1;
use constant ETMOUT => 2;
use constant EARGCB => 3;
use constant ETKTRN => 4;
use constant ETKTNR => 5;
use constant ESPIPE => 6;
use constant ESFORK => 7;
use constant ESCLOS => 8;
use constant ESELEC => 9;
use constant ETPBUG => 10;
use constant EARGFH => 11;
use constant EARGTP => 12;
our @messages = (
"Argument 'command' missing",
"Timeouted",
"Argument 'callback' missing",
"TakTuk is already running",
"TakTuk is not running",
"pipe system call failed",
"fork system call failed",
"close system call failed",
"select error",
"Internal bug in TakTuk Pilot module",
"Argument 'filehandle' missing",
"Argument 'type' missing"
);
use TakTuk::Select;
use Data::Dumper;
our @taktuk_streams =
('taktuk', 'info', 'status', 'connector', 'default', 'state');
our $redirect_options = "-R taktuk='>&=1'";
our $has_setsid = eval("use POSIX 'setsid';1")?1:0;
our $read_granularity = 4096;
our $write_granularity = 4096;
# Internal functions
sub dummy_callback(%);
sub no_flush($);
sub taktuk_read_callback(%);
sub taktuk_write_callback(%);
sub set_callback($$$$);
# User interface
sub new();
sub error_msg($);
sub run(%);
sub continue();
sub create_session();
sub pid();
sub quiet();
sub verbose();
sub add_callback(%);
sub send_command($);
sub send_termination();
sub add_descriptor(%);
sub remove_descriptor(%);
sub create_session() {
my $self = shift;
if ($self->{running} and not $self->{setsid}) {
return ETKTRN;
} else {
$self->{setsid} = 1;
return 0;
}
}
sub pid() {
my $self = shift;
if (!$self->{running}) {
return ETKTNR;
} else {
return $self->{pid};
}
}
sub quiet() {
my $self = shift;
if ($self->{running} and not $self->{quiet}) {
return ETKTRN;
} else {
$self->{quiet} = 1;
return 0;
}
}
sub verbose() {
my $self = shift;
if ($self->{running} and $self->{quiet}) {
return ETKTRN;
} else {
$self->{quiet} = 0;
return 0;
}
}
sub dummy_callback(%) {
my %args = @_;
print STDERR "WARNING: Unhandled TakTuk data on stream $args{stream}\n"
unless $args{taktuk}->{quiet};
}
sub new() {
my $self = shift;
my $data = {};
$data->{quit} = 0;
$data->{option} = {};
$data->{quiet} = 1;
$data->{setsid} = 0;
$data->{stream} = {};
$data->{callback} = {};
$data->{argument} = {};
$data->{read} = {callback => {}, argument => {}};
$data->{write} = {callback => {}, argument => {}};
$data->{exception} = {callback => {}, argument => {}};
$data->{running} = 0;
$data->{read_buffer} = "";
$data->{write_buffer} = "";
$data->{read}->{select} = TakTuk::Select->new or die "Select creation\n";
$data->{write}->{select} = TakTuk::Select->new or die "Select creation\n";
$data->{exception}->{select} =
TakTuk::Select->new or die "Select creation\n";
return bless $data, $self;
}
sub error_msg($) {
my $number = shift;
if (($number < 1) or ($number > scalar(@messages))) {
return "Unknown error";
} else {
return $messages[$number-1];
}
}
sub run(%) {
my $self = shift;
my %arguments = @_;
$self->{command} = $arguments{command};
$self->{timeout} = $arguments{timeout};
return EARGCM unless defined($self->{command});
return ETKTRN if $self->{running};
my ($command, $remaining) = split / /,$self->{command},2;
foreach my $stream (@taktuk_streams) {
if (not exists($self->{option}->{$stream})) {
if ($self->{quiet}) {
$self->{option}->{$stream} = "-o $stream";
} else {
$self->add_callback(callback=>\&dummy_callback,
stream=>$stream);
}
}
}
$command .= " ".$redirect_options;
foreach my $stream (keys(%{$self->{option}})) {
$command .= " ".$self->{option}->{$stream};
}
my ($taktuk_read, $taktuk_write);
pipe($self->{read_channel}, $taktuk_write) or return ESPIPE;
pipe($taktuk_read, $self->{write_channel}) or return ESPIPE;
$self->{pid} = fork();
return ESFORK unless defined($self->{pid});
if ($self->{pid}) {
close($taktuk_read) or return ESCLOS;
close($taktuk_write) or return ESCLOS;
binmode($self->{read_channel});
no_flush($self->{write_channel});
} else {
setsid() if $self->{setsid} and $has_setsid;
close($self->{read_channel}) or die "$!";
close($self->{write_channel}) or die "$!";
open(STDIN,"<&",$taktuk_read) or die "$!";
open(STDOUT,">&",$taktuk_write) or die "$!";
no_flush(\*STDOUT);
close($taktuk_read) or die "$!";
close($taktuk_write) or die "$!";
exec("$command $remaining") or die "$!";
}
$self->add_descriptor(type=>'read', filehandle=>$self->{read_channel},
callback=>\&taktuk_read_callback);
if (length($self->{write_buffer})) {
$self->add_descriptor(type=>'write', filehandle=>$self->{write_channel},
callback=>\&taktuk_write_callback);
} else {
if ($self->{quit}) {
$self->remove_descriptor(type=>'write',
filehandle=>$self->{write_channel});
close($self->{write_channel}) or return ESCLOS;
delete $self->{write_channel};
}
}
$self->{running} = 1;
return $self->continue();
}
sub continue() {
my $self = shift;
return ETKTNR unless $self->{running};
# Main processing loop
while (scalar($self->{read}->{select}->handles())) {
if (exists($self->{write_channel}) and $self->{quit}
and not length($self->{write_buffer})) {
$self->remove_descriptor(type=>'write',
filehandle=>$self->{write_channel});
close($self->{write_channel}) or return ESCLOS;
delete($self->{write_channel});
}
my @select_result = TakTuk::Select::select($self->{read}->{select},
$self->{write}->{select}, $self->{exception}->{select},
$self->{timeout});
if (scalar(@select_result)) {
my @types = ('read', 'write', 'exception');
my $rank = 0;
$rank++ while $rank < 3 and not scalar(@{$select_result[$rank]});
return ETMOUT if $rank == 3;
while ($rank < 3) {
my $type = $types[$rank];
my $handle = shift @{$select_result[$rank]};
my $callback = $self->{$type}->{callback}->{$handle};
my $argument = $self->{$type}->{argument}->{$handle};
return ETPBUG unless defined $callback;
my %call_args = (taktuk=>$self,
type=>$type,
filehandle=>$handle,
argument=>$argument);
&$callback(%call_args);
$rank++
while $rank < 3 and not scalar(@{$select_result[$rank]});
}
} else {
return ESELEC;
}
}
$self->{running} = 0;
delete $self->{pid};
$self->{write_buffer} = "";
$self->{read_buffer} = "";
close($self->{read_channel}) or return ESCLOS;
if (exists($self->{write_channel})) {
close($self->{write_channel}) or return ESCLOS;
}
return 0;
}
sub no_flush($) {
my $new_fd = shift;
my $old_fd=select($new_fd);
$|=1;
select($old_fd);
}
sub taktuk_read_callback(%) {
my %args = @_;
my $self = $args{taktuk};
my $descriptor = $args{filehandle};
my $done = 0;
my $result = sysread($descriptor, $self->{read_buffer}, $read_granularity,
length($self->{read_buffer}));
carp "$!" unless defined $result;
if (not $result) {
$self->remove_descriptor(type=>'read', filehandle=>$descriptor);
$done = 1;
}
while (not $done) {
if (length($self->{read_buffer}) >= 4) {
my ($size) = unpack("N", $self->{read_buffer});
if (length($self->{read_buffer}) >= $size + 4) {
my $buffer = substr $self->{read_buffer}, 4, $size;
my ($stream, $data) = split / /, $buffer, 2;
$self->{read_buffer} = substr $self->{read_buffer}, $size + 4;
my $result = {};
foreach my $field (@{$self->{stream}->{$stream}}) {
my $field_size = unpack("N", $data);
$result->{$field} = substr($data, 4, $field_size);
$data = substr $data, $field_size+4;
}
my %call_args = (taktuk=>$self,
argument=>$self->{argument}->{$stream},
stream=>$stream,
fields=>$result);
if (exists($self->{callback}->{$stream})) {
&{$self->{callback}->{$stream}}(%call_args);
} elsif (exists($self->{callback}->{default})) {
&{$self->{callback}->{default}}(%call_args);
} else {
carp "Internal bug in TakTuk::Pilot\n";
}
} else {
$done = 1;
}
} else {
$done = 1;
}
}
}
sub taktuk_write_callback(%) {
my %args = @_;
my $self = $args{taktuk};
my $descriptor = $args{filehandle};
my $size = length($self->{write_buffer});
my $write_size = ($size > $write_granularity) ? $write_granularity : $size;
my $result = syswrite($descriptor, $self->{write_buffer}, $write_size);
carp "$!" unless defined($result);
if ($result < $size) {
$self->{write_buffer} = substr $self->{write_buffer}, $result;
} else {
$self->{write_buffer} = "";
$self->remove_descriptor(type=>'write', filehandle=>$descriptor);
}
}
sub set_callback($$$$) {
my $self = shift;
my $callback = shift;
my $argument = shift;
my $stream = shift;
my $fields = shift;
my $option = '-o '.$stream.'=\'$user_scalar="'.$stream.' "';
foreach my $field (@$fields) {
$option .= '.pack("N",length($'.$field.')).$'.$field;
}
$option .= ',pack("N",length($user_scalar)).$user_scalar\'';
$self->{option}->{$stream} = $option;
$self->{stream}->{$stream} = $fields;
$self->{callback}->{$stream} = $callback;
$self->{argument}->{$stream} = $argument;
}
sub add_callback(%) {
my $self = shift;
my %arguments = @_;
my $callback = $arguments{callback};
my $argument = $arguments{argument};
my $stream = $arguments{stream};
my $fields = $arguments{fields};
return EARGCB unless defined($callback);
$stream = 'default' unless defined($stream);
$fields = [] unless defined($fields);
if ($stream eq 'default') {
foreach my $taktuk_stream (@taktuk_streams) {
$self->set_callback($callback, $argument, $taktuk_stream, $fields)
unless exists($self->{option}->{$taktuk_stream});
}
} else {
$self->set_callback($callback, $argument, $stream, $fields);
}
return 0;
}
sub send_command($) {
my $self = shift;
my $command = shift;
if (length($command)) {
if (not length($self->{write_buffer}) and $self->{running}) {
$self->add_descriptor(type=>'write',
filehandle=>$self->{write_channel},
callback=>\&taktuk_write_callback);
}
$self->{write_buffer} .= "$command\n";
}
}
sub send_termination() {
my $self = shift;
$self->{quit} = 1;
}
sub add_descriptor(%) {
my $self = shift;
my %arguments = @_;
my $type = $arguments{type};
my $descriptor = $arguments{filehandle};
my $callback = $arguments{callback};
my $argument = $arguments{argument};
return EARGCB unless defined $callback;
return EARGFH unless defined $descriptor;
return EARGTP unless defined $type;
$self->{$type}->{callback}->{$descriptor} = $callback;
$self->{$type}->{argument}->{$descriptor} = $argument;
my $error = $self->{$type}->{select}->add($descriptor);
return ESELEC if $error;
return 0;
}
sub remove_descriptor(%) {
my $self = shift;
my %arguments = @_;
my $type = $arguments{type};
my $descriptor = $arguments{filehandle};
return EARGFH unless defined $descriptor;
return EARGTP unless defined $type;
delete($self->{$type}->{callback}->{$descriptor});
delete($self->{$type}->{argument}->{$descriptor});
my $error = $self->{$type}->{select}->remove($descriptor);
return ESELEC if $error;
return 0;
}
1;
__END__
=pod TakTuk pilot Perl module
=begin html
<center><h1>USER MANUAL</h1></center>
=end html
=head1 NAME
TakTuk::Pilot - Perl module that ease C<taktuk(1)> execution and related I/O
management
=head1 SYNOPSIS
use TakTuk::Pilot;
our @line_counter;
sub output_callback(%) {
my %parameters = @_;
my $field = $parameters{fields};
my $rank = $field->{rank};
my $argument = $parameters{argument};
$argument->[$rank] = 1 unless defined($argument->[$rank]);
print "$field->{host}-$rank : ".
"$argument->[$rank] > $field->{line}\n";
$argument->[$rank]++;
}
sub user_input_callback(%) {
my %parameters = @_;
my $taktuk = $parameters{taktuk};
my $descriptor = $parameters{filehandle};
my $buffer;
my $result = sysread($descriptor, $buffer, 1024);
warn "Read error $!" if not defined($result);
# basic parsing, we assume input is buffered on a line basis
chomp($buffer);
if (length($buffer)) {
print "Executing $buffer\n";
$taktuk->send_command("broadcast exec [ $buffer ]");
}
if (not $result) {
print "Terminating\n";
$taktuk->remove_descriptor(type=>'read',
filehandle=>$descriptor);
$taktuk->send_termination();
}
}
die "This script requieres as arguments hostnames to contact\n"
unless scalar(@ARGV);
my $taktuk = TakTuk::Pilot->new();
$taktuk->add_callback(callback=>\&output_callback, stream=>'output',
argument=>\@line_counter,
fields=>['host', 'rank', 'line']);
$taktuk->add_descriptor(type=>'read', filehandle=>\*STDIN,
callback=>\&user_input_callback);
$taktuk->run(command=>"taktuk -s -m ".join(" -m ", @ARGV));
=head1 DESCRIPTION
The TakTuk::Pilot Perl module ease the use of B<TakTuk> from within a Perl
program (see C<taktuk(1)> for a detailed description of B<TakTuk>). It
transparently manages I/O exchanges as well as B<TakTuk> data demultiplexing
and decoding.
=head1 CONSTRUCTOR
=over
=item B<new>B<()>
Creates a new B<TakTuk> object on which the following method can be called.
=back
=head1 METHODS
=over
=item B<add_callback(%)>
Adds a callback function associated to some B<TakTuk> output stream to the
calling B<TakTuk> object. This callback function will be called by
B<TakTuk::Pilot> for each batch of output data incoming from the related
stream. The hash passed as argument to this function call may contain the
following fields:
callback => reference to the callback fonction (mandatory)
stream => stream related to this callback, might be
'default' (mandatory)
fields => reference to an array of fields names relevant
to the user
argument => scalar that should be passed to each callback
function call
The callback function should accept a hash as argument. This hash will be
populated with the following fields :
taktuk => reference to the taktuk object calling this
callback
argument => scalar given at callback addition or undef
stream => stream on which output data came
fields => reference to a hash containing a
fieldname/value pair for each field requested
upon callback addition
=item B<send_command($)>
Sends to the calling B<TakTuk> object the command passed as argument. Note that
if the B<TakTuk> object is not running, this command will be buffered and
executed upon run.
=item B<send_termination>B<()>
Sends to the calling B<TakTuk> object a termination command. As for
C<send_command>, if the B<TakTuk> object is not running, this command will be
issued upon run.
=item B<run(%)>
Runs B<TakTuk>, executing pending commands and waiting for B<TakTuk> output.
Note that this function is blocking: it waits for B<TakTuk> outputs, possibly
calls related callback functions and returns when B<TakTuk> terminates. Thus,
all B<TakTuk> commands should be given either before calling C<run> or within a
callback function.
This commands takes a hash as argument that may contain the following fields:
command => TakTuk command line to be executed
timeout => optional timeout on the wait for TakTuk output
Upon occurence of the timeout (if one has been given), C<run> will returns an
C<ETMOUT> error code. Note the in this case B<TakTuk> execution will not be
terminated and should be resumed at some point by calling C<continue>.
=item B<continue>B<()>
Resumes a B<TakTuk> execution interrupted by timeout occurence.
=item B<add_descriptor(%)>
Because the call to C<run> is blocking, waiting for B<TakTuk> output, it might
be interesting to let the C<TakTuk::Pilot> module monitor I/O occurence related
to other file descriptors.
This is the intent of C<add_descriptor>. This function takes a hash as
parameter in which the following fields might be defined:
type => 'read', 'write' or 'exception', this is the
type of I/O possiblities that should be
monitored on the descriptor, as in select
system call (mandatory).
filehandle => file descriptor to monitor (mandatory).
callback => reference to the callback function that
should be called when I/O is possible on the
file descriptor.
argument => optional scalar value that will be passed
with each call to the callback function
The callback function should also accept a hash as an argument in which the
following fields will be defined:
taktuk => reference to the TakTuk object from which
the function was called.
type => type of I/O occuring (as in add_callback)
filehandle => the related file descriptor. Notice that the
user is in charge of performing the I/O
operation itslef (sysread or syswrite).
Notice also that, because of the use of a
select in TakTuk::Pilot, the use of buffered
I/O on this descriptor is strongly discouraged
argument => the argument that was given to add_descriptor
=item B<remove_descriptor(%)>
Function that should be called to remove from the B<TakTuk> object a descriptor
previously added with C<add_descriptor>. It takes a hash as argument in which
the following fields may be defined:
type => type of I/O (see add_descriptor)
filehandle => file descriptor to remove
=item B<quiet>B<() / verbose>B<()>
Change verbosity of C<TakTuk::Pilot> on STDOUT (default is quiet). Should not
be called when B<TakTuk> is running.
=item B<create_session>B<()>
Call C<setsid> in the B<TakTuk> process created by C<run>. The main purpose of
this call is to prevent B<TakTuk> from receiving signals sent to the process
group to which the pilot belong. Should not be called when B<TakTuk> is running.
=item B<pid>B<()>
Returns the pid of the process spawned to run B<TakTuk>.
Should only be called when B<TakTuk> is running.
=item B<error_msg($)>
Static function. Returns a character string that corresponds to the error code
given as argument. The error code should be one of the values returned by other
C<TakTuk::Pilot> functions (C<add_callback>, C<send_command>,
C<send_termination>, ...).
=back
=head1 ERRORS
When an error occur in one of these functions, it returns a non nul numeric
error code. This code can take one of the following values:
=over
=item B<TakTuk::Pilot::EARGCM>
Field 'command' is missing in a call to C<run>.
=item B<TakTuk::Pilot::EARGCB>
Field 'callback' is missing in a call to C<add_callback> or C<add_descriptor>.
=item B<TakTuk::Pilot::EARGFH>
Field 'filehandle' is missing in a call to C<add_descriptor> or
C<remove_descriptor>.
=item B<TakTuk::Pilot::EARGTP>
Field 'type' is missing in a call to C<add_descriptor> or
C<remove_descriptor>.
=item B<TakTuk::Pilot::ETMOUT>
A timeout occured in a call to C<run>.
=item B<TakTuk::Pilot::ETKTRN>
B<TakTuk> is alredy running but C<run>, C<verbose> or C<quiet> has been called.
=item B<TakTuk::Pilot::ETKTNR>
B<TakTuk> is not running but C<continue> has been called.
=item B<TakTuk::Pilot::ESPIPE>
A call to C<pipe> failed in C<TakTuk::Pilot> (the error should be in $!).
=item B<TakTuk::Pilot::ESFORK>
A call to C<fork> failed in C<TakTuk::Pilot> (the error should be in $!).
=item B<TakTuk::Pilot::ESCLOS>
A call to C<close> failed in C<TakTuk::Pilot> (the error should be in $!).
=item B<TakTuk::Pilot::ESELEC>
A call to C<select> failed in C<TakTuk::Pilot> (the error should be in $!).
=item B<TakTuk::Pilot::ETPBUG>
Internal bug detected in C<TakTuk::Pilot>.
=back
=head1 SEE ALSO
C<tatkuk(1)>, C<taktukcomm(3)>, C<TakTuk(3)>
=head1 AUTHOR
The original concept of B<TakTuk> has been proposed by Cyrille Martin in his
PhD thesis. People involved in this work include Jacques Briat, Olivier
Richard, Thierry Gautier and Guillaume Huard.
The author of the version 3 (perl version) and current maintainer of the
package is Guillaume Huard.
=head1 COPYRIGHT
The C<TakTuk> communication interface library is provided under the terms
of the GNU General Public License version 2 or later.
=cut
|