/usr/share/perl5/Arch/Run.pm is in libarch-perl 0.5.2-2.
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 | # Arch Perl library, Copyright (C) 2004-2005 Mikhael Goikhman, Enno Cramer
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
use 5.006;
use strict;
package Arch::Run;
use IO::Poll qw(POLLIN POLLOUT POLLERR);
use POSIX qw(waitpid WNOHANG setsid);
use constant RAW => 0;
use constant LINES => 1;
use constant ALL => 2;
use vars qw(@ISA @EXPORT_OK @OBSERVERS %SUBS $DETACH_CONSOLE);
use Exporter;
@ISA = qw(Exporter);
@EXPORT_OK = qw(
run_with_pipe run_async poll wait unobserve observe
RAW LINES ALL
);
BEGIN {
$DETACH_CONSOLE = 0;
}
sub set_detach_console ($) {
$DETACH_CONSOLE = shift;
}
sub run_with_pipe (@) {
my $arg0 = shift || die "Missing command to run_with_pipe\n";
my @args = (split(/\s+/, $arg0), @_);
pipe TO_PARENT_RDR, TO_PARENT_WRT;
pipe TO_CHILD_RDR, TO_CHILD_WRT;
my $pid = fork;
die "Can't fork: $!\n" unless defined $pid;
if ($pid) {
close TO_PARENT_WRT;
close TO_CHILD_RDR;
return wantarray
? (\*TO_PARENT_RDR, \*TO_CHILD_WRT, $pid)
: \*TO_PARENT_RDR;
} else {
close TO_PARENT_RDR;
close TO_CHILD_WRT;
close STDIN;
# my perl won't compile this if i use
# open STDIN, "<&", TO_CHILD_RDR
# the same thing for STDOUT is accepted though,
# the "<&" vs ">&" makes the difference
open STDIN, "<&TO_CHILD_RDR";
close TO_CHILD_RDR;
close STDOUT;
open STDOUT, ">&TO_PARENT_WRT";
close TO_PARENT_WRT;
setsid
if $DETACH_CONSOLE;
exec(@args);
}
}
sub run_async (%) {
my %args = @_;
die "Missing command to run_async\n"
unless exists $args{command};
my @args = ref $args{command} ? @{$args{command}} : $args{command};
my ($out, $in, $pid) = run_with_pipe(@args);
_notify('cmd_start', $pid, @args);
$SUBS{$pid} = {
# in => $in, # not for now
out => $out,
mode => $args{mode},
data => $args{datacb},
exit => $args{exitcb},
accum => '',
};
close($in); # no input for now
return $pid;
}
sub get_output_handle ($) {
my $key = shift;
return $SUBS{$key}->{out};
}
sub handle_output ($) {
my $key = shift;
my $rec = $SUBS{$key};
my $buffer;
my $result = sysread $rec->{out}, $buffer, 4096;
_notify('cmd_output_raw', $key, $buffer)
if $result > 0;
# handle output
if ($result) {
# raw mode
if ($rec->{mode} eq RAW) {
$rec->{data}->($buffer);
# line mode
} elsif ($rec->{mode} eq LINES) {
$rec->{accum} .= $buffer;
while ($rec->{accum} =~ s/^.*?(\015\012|\012|\015)//) {
$rec->{data}->($&);
}
# bloody big block mode
} else {
$rec->{accum} .= $buffer;
$rec->{data}->($rec->{accum})
if $result == 0;
}
# error and eof
} else {
$rec->{data}->($rec->{accum})
if length $rec->{accum};
my $pid = waitpid $key, 0;
my $exitcode = $pid == $key ? $? : undef;
_notify('cmd_exit', $exitcode);
$rec->{exit}->($exitcode)
if defined $rec->{exit};
delete $SUBS{$key};
}
}
sub poll (;$) {
my $count = 0;
# check for output
my $poll = IO::Poll->new;
foreach my $key (keys %SUBS) {
$poll->mask($SUBS{$key}->{out}, POLLIN | POLLERR)
unless $SUBS{$key}->{done};
}
my $result = $poll->poll($_[0]);
foreach my $key (keys %SUBS) {
if ($poll->events($SUBS{$key}->{out})) {
handle_output($key);
++$count;
}
}
return $count;
}
sub wait ($) {
my $pid = shift;
my $ret;
# overwrite callback to capture exit code
if (exists $SUBS{$pid}) {
my $old_cb = $SUBS{$pid}->{exit};
$SUBS{$pid}->{exit} = sub {
$ret = shift;
$old_cb->($ret)
if defined $old_cb;
};
# Poll until a) our target has exited or b) there are no more
# file handles to poll for.
while (exists $SUBS{$pid} && poll(undef)) {}
}
# returns undef if childs exit has already been handled
return $ret;
}
sub killall (;$) {
my $signal = shift || 'INT';
kill $signal, keys %SUBS;
while (%SUBS && poll(undef)) {}
}
sub _notify (@) {
die "no touching\n"
if caller ne __PACKAGE__;
my $method = shift;
foreach my $observer (@OBSERVERS) {
$observer->$method(@_) if $observer->can($method);
}
}
sub unobserve ($) {
my $observer = shift;
@OBSERVERS = grep { $_ ne $observer } @OBSERVERS;
}
sub observe ($) {
my $observer = shift;
unobserve($observer);
push @OBSERVERS, $observer;
}
1;
__END__
=head1 NAME
Arch::Run - run subprocesses and capture output
=head1 SYNOPSIS
use Gtk2 -init;
use Arch::Run qw(poll run_async LINES);
my $window = Gtk2::Window->new;
my $label = Gtk2::Label->new;
my $pbar = Gtk2::ProgressBar->new;
my $vbox = Gtk2::VBox->new;
$vbox->add($label); $vbox->add($pbar); $window->add($vbox);
$window->signal_connect(destroy => sub { Gtk2->main_quit; });
$window->set_default_size(200, 48); $window->show_all;
sub set_str { $label->set_text($_[0]); }
my $go = 1; # keep progress bar pulsing
Glib::Timeout->add(100, sub { $pbar->pulse; poll(0); $go; });
run_async(
command => [ 'du', '-hs', glob('/usr/share/*') ],
mode => LINES,
datacb => sub { chomp(my $str = $_[0]); set_str($str); },
exitcb => sub { $go = 0; set_str("exit code: $_[0]"); },
);
Gtk2->main;
=head1 DESCRIPTION
Arch::Run allows the user to run run subprocesses and capture their
output in a single threaded environment without blocking the whole
application.
You can use either B<poll> to wait for and handle process output, or
use B<handle_output> and B<handle_exits> to integrate
B<Arch::Run> with your applications main loop.
=head1 METHODS
The following functions are available:
B<run_with_pipe>,
B<run_async>,
B<get_output_handle>,
B<handle_output>,
B<poll>,
B<wait>,
B<killall>,
B<observe>,
B<unobserve>.
=over 4
=item B<run_with_pipe> I<$command>
=item B<run_with_pipe> I<$executable> I<$argument> ...
Fork and exec a program with STDIN and STDOUT connected to pipes. In
scalar context returns the output handle, STDIN will be connected to
/dev/null. In list context, returns the output and input handle.
The programs standard error handle (STDERR) is left unchanged.
=item B<run_async> I<%args>
Run a command asyncronously in the background. Returns the
subprocesses pid.
Valid keys for I<%args> are:
=over 4
=item B<command> => I<$command>
=item B<command> => [ I<$executable> I<$argument> ... ]
Program and parameters.
=item B<mode> => I<$accum_mode>
Control how output data is accumulated and passed to B<data> and
B<finish> callbacks.
I<$accum_mode> can be one of
=over 4
=item B<RAW>
No accumulation. Pass output to B<data> callback as it is received.
=item B<LINES>
Accumulate output in lines. Pass every line separately to B<data>
callback.
=item B<ALL>
Accumulate all data. Pass complete command output as one block to
B<data> callback.
=back
=item B<datacb> => I<$data_callback>
Codeblock or subroutine to be called when new output is available.
Receives one parameter, the accumulated command output.
=item B<exitcb> => I<$exit_callback>
Codeblock or subroutine to be called when subprocess exits. Receives
a single parameter, the commands exit code. (Or maybe not. We have to
handle SIG{CHLD} then. But maybe we have to do so anyway.)
=back
=item B<get_output_handle> I<$pid>
Returns the STDOUT handle of process $pid. You should never directly
read from the returned handle. Use L<IO::Select> or L<IO::Poll> to
wait for output and call B<handle_output> to process the output.
=item B<handle_output> I<$pid>
Handle available output from process I<$pid>.
B<ATTENTION:> Call this method only if there really is output to be
read. It will block otherwise.
=item B<poll> I<$timeout>
Check running subprocesses for available output and run callbacks as
appropriate. Wait at most I<$timeout> seconds when no output is
available.
Returns the number of processes that had output available.
=item B<wait> I<$pid>
Wait for subprocess I<$pid> to terminate, repeatedly calling B<poll>.
Returns the processes exit status or C<undef> if B<poll> has already been
called after the processes exit.
=item B<killall> [I<$signal>]
Send signal I<$signal> (B<SIGINT> if omitted) to all managed
subprocesses, and wait until every subprocess to terminate.
=item B<observe> I<$observer>
Register an observer object that wishes to be notified of running
subprocesses. I<$observer> should implement one or more of the
following methods, depending on which event it wishes to receive.
=over 4
=item B<-E<gt>cmd_start> I<$pid> I<$executable> I<$argument> ...
Called whenever a new subprocess has been started. Receives the
subprocesses PID and the executed command line.
=item B<-E<gt>cmd_output_raw> I<$pid> I<$data>
Called whenever a subprocess has generated output. Receives the
subprocesses PID and a block of output data.
B<NOTE:> I<$data> is not preprocesses (e.g. split into lines).
B<cmd_output_raw> receives data block as if B<RAW> mode was used.
=item B<-E<gt>cmd_exit> I<$pid> I<$exitcode>
Called whenever a subprocess exits. Receives the subprocesses PID and
exit code.
=back
=item B<unobserve> I<$observer>
Remove I<$observer> from observer list.
=back
=cut
|