/usr/share/lintian/lib/Lintian/Command/Simple.pm is in lintian 2.5.10.4.
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 | # Copyright (C) 2010 Raphael Geissert <atomo64@gmail.com>
#
# 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, see <http://www.gnu.org/licenses/>.
package Lintian::Command::Simple;
use strict;
use warnings;
use POSIX ":sys_wait_h";
=head1 NAME
Lintian::Command::Simple - Run commands without pipes
=head1 SYNOPSIS
use Lintian::Command::Simple;
Lintian::Command::Simple::run("echo", "hello world");
# Start a command in the background:
Lintian::Command::Simple::background("sleep", 10);
print (Lintian::Command::Simple::wait())? "success" : "failure";
# Using the OO interface
my $cmd = Lintian::Command::Simple->new();
$cmd->run("echo", "hello world");
$cmd->background("sleep", 10);
print ($cmd->wait())? "success" : "failure";
=head1 DESCRIPTION
Lintian::Command::Simple allows running commands with the capability of
running them "in the background" (asynchronously.)
Pipes are not handled at all, except for those handled internally by
the shell. See 'perldoc -f exec's note about shell metacharacters.
If you want to pipe to/from Perl, look at Lintian::Command instead.
A procedural and an Object-Oriented (from now on OO) interfaces are
provided.
It is possible to reuse an object to run multiple commands, but only
after reaping the previous command.
=over 4
=item new()
Creates a new Lintian::Command::Simple object and returns a reference
to it.
=cut
sub new {
my ($class, $pkg) = @_;
my $self = {};
bless($self, $class);
return $self;
}
=item run(command, argument [, ...])
Executes the given C<command> with the given arguments and returns the
status code as one would see it from a shell script.
Being fair, the only advantage of this function (or method) over the
CORE::system() function is the way the return status is reported.
=cut
sub run {
my $self;
if (ref $_[0]) {
$self = shift;
return -1
if defined($self->{'pid'});
}
system(@_);
$self->{'status'} = $?
if defined $self;
return $? >> 8;
}
=item rundir(dir, command, argument [, ...])
Executes the given C<command> with the given arguments and in C<dir>
returns the status code as one would see it from a shell script.
Being fair, the only advantage of this function (or method) over the
CORE::system() function is the way the return status is reported.
=cut
sub rundir {
my $self;
my $pid;
my $res;
if (ref $_[0]) {
$self = shift;
return -1
if defined($self->{'pid'});
}
$pid = fork();
if (not defined($pid)) {
# failed
$res = -1;
} elsif ($pid > 0) {
# parent
if (defined($self)){
$self->{'pid'} = $pid;
$res = $self->wait();
} else {
$res = Lintian::Command::Simple::wait($pid);
}
} else {
# child
my $dir = shift;
close(STDIN);
open(STDIN, '<', '/dev/null');
chdir($dir) or die("Failed to chdir to $dir: $!\n");
CORE::exec @_ or die("Failed to exec '$_[0]': $!\n");
}
return $res;
}
=item background(command, argument [, ...])
Executes the given C<command> with the given arguments asynchronously
and returns the process id of the child process.
A return value of -1 indicates an error. This can either be a problem
when calling CORE::fork() or when trying to run another command before
calling wait() to reap the previous command.
=cut
sub background {
my $self;
if (ref $_[0]) {
$self = shift;
return -1
if (defined($self->{'pid'}));
$self->{'status'} = undef;
}
my $pid = fork();
if (not defined($pid)) {
# failed
return -1;
} elsif ($pid > 0) {
# parent
$self->{'pid'} = $pid
if (defined($self));
return $pid;
} else {
# child
close(STDIN);
open(STDIN, '<', '/dev/null');
CORE::exec @_ or die("Failed to exec '$_[0]': $!\n");
}
}
=item background_dir(dir, command, argument [, ...])
Executes the given C<command> with the given arguments asynchronously
in dir and returns the process id of the child process.
A return value of -1 indicates an error. This can either be a problem
when calling CORE::fork() or when trying to run another command before
calling wait() to reap the previous command.
=cut
sub background_dir {
my $self;
if (ref $_[0]) {
$self = shift;
return -1
if (defined($self->{'pid'}));
$self->{'status'} = undef;
}
my $pid = fork();
if (not defined($pid)) {
# failed
return -1;
} elsif ($pid > 0) {
# parent
$self->{'pid'} = $pid
if (defined($self));
return $pid;
} else {
# child
my $dir = shift;
close(STDIN);
open(STDIN, '<', '/dev/null');
chdir($dir) or die("Failed to chdir to $dir: $!\n");
CORE::exec @_ or die("Failed to exec '$_[0]': $!\n");
}
}
=item wait([pid|hashref])
When called as a function:
If C<pid> is specified, it waits until the given process (which must be
a child of the current process) returns. If C<pid> is not specified, it
waits for any child process to finish and returns.
When called as a method:
It takes no argument. It waits for the previously background()ed process to
return.
The return value is either -1, probably indicating an error, or the
return status of the process as it would be seen from a shell script.
See 'perldoc -f wait' for more details about the possible meanings of
-1.
To reap one from many:
When starting multiple processes asynchronously, it is common to wait
until the first is done. While the CORE::wait() function is usually
used for that very pourpose, it does not provide the desired results
when the processes were started via the OO interface.
To help with this task, wait() can take a hash ref where the value of
each entry is an instance of Lintian::Command::Simple. The key of each
entry is irrelevant and is not used for any pourpose.
Under this mode, wait() waits until any child process is done and if the
deceased process is one of the set passed via the hash ref it marks it
as reaped and stores the return status.
The results and return value are undefined when under this mode wait()
"accidentally" reaps a process not started by one of the objects passed
in the hash ref.
The return value in scalar context is the instance of the object that
started the now deceased process. In list context, the key and value
(i.e. the object instance) are returned.
Whenever CORE::wait() would return -1, wait() returns undef or a null
value so that it is safe to:
while($cmd = Lintian::Command::Simple::wait(\%hash)) { something; }
The same is true whenever the hash reference points to an empty hash.
Passing any other kind of reference or value as arguments has undefined
results.
=cut
sub wait {
my ($self, $pid, $nohang);
if (ref $_[0] eq 'Lintian::Command::Simple') {
($self, $nohang) = @_;
$pid = $self->{'pid'};
} else {
($pid, $nohang) = @_;
}
$nohang = WNOHANG if $nohang;
$nohang //= 0;
if (defined($pid) && !ref $pid) {
$self->{'pid'} = undef
if defined($self);
my $ret = waitpid($pid, $nohang);
my $status = $?;
$self->{'status'} = $?
if defined($self);
return ($ret == -1)? -1 : $status >> 8;
} elsif (defined($pid)) {
# in this case $pid is a ref (must be a hash ref)
# rename it accordingly:
my $jobs = $pid;
$pid = 0;
my ($reaped_pid, $reaped_status);
# count the number of members and reset the internal hash iterator
if (scalar keys %$jobs == 0) {
return;
}
$reaped_pid = waitpid(-1, $nohang);
$reaped_status = $?;
if ($reaped_pid == -1 or ($nohang and $reaped_pid == 0)) {
return;
}
while (my ($k, $cmd) = each %$jobs) {
next unless (defined($cmd->pid()) && $reaped_pid == $cmd->pid());
$cmd->status($reaped_status)
or die("internal error: object of pid $reaped_pid " .
"failed to recognise its termination\n");
if (wantarray) {
return ($k, $cmd);
} else {
return $cmd;
}
}
} elsif (not defined($self)) {
return (CORE::wait() == -1)? -1 : ($? >> 8);
} else {
return -1;
}
}
=item kill([pid|hashref])
When called as a function:
C<pid> must be specified. It sigTERMs the given process.
Under this mode, it acts as a wrapper around CORE::kill().
When called as a method:
It takes no argument. It sigTERMsr the previously background()ed
process and cleans up internal variables.
The return value is that of CORE:kill().
Killing multiple processes:
In a similar way to wait(), it is possible to pass a hash reference to
kill() so that it calls the kill() method of each of the objects and
reaps them afterwards with wait().
Only the processes that were successfully signaled are reaped.
Depending on the effects of the signal, it is possible that the call to
wait() blocks. To reduce the chances of blocking, the processes are
reaped in the same order they were signaled.
The return value is the number of processes that were successfully
signaled (and per the above description, reaped.)
=cut
sub kill {
my ($self, $pid);
if (ref $_[0] eq 'Lintian::Command::Simple') {
$self = shift;
$pid = $self->pid();
} elsif (ref $_[0]) {
my $jobs = shift;
my $count = 0;
my @killed_jobs;
# reset internal iterator
keys %$jobs;
# send signals
while (my ($k, $cmd) = each %$jobs) {
if ($cmd->kill()) {
$count++;
push @killed_jobs, $k;
}
}
# and reap afterwards
while (my $k = shift @killed_jobs) {
$jobs->{$k}->wait();
}
return $count;
} else {
$pid = shift;
}
return CORE::kill('TERM', $pid);
}
=item pid()
Only available under the OO interface, it returns the pid of a
background()ed process.
After calling wait(), this method always returns undef.
=cut
sub pid {
my $self = shift;
return $self->{'pid'};
}
=item status()
Only available under the OO interface, it returns the return status of
the background()ed or run()-ran process.
When used on async processes, it is only defined after calling wait().
B<Note>: it is also the method internally used by wait() to set the return
status in some cases.
=cut
sub status {
my $self = shift;
my $status = shift;
# Externally set the return status.
# It performs a sanity check by making sure the executed command is
# indeed done.
if (defined($status)) {
my $rstatus = $self->wait();
return 0 if ($rstatus != -1);
$self->{'status'} = $status;
return 1;
}
return (defined $self->{'status'})? $self->{'status'} >> 8 : undef;
}
1;
__END__
=back
=head1 TODO
Provide the necessary methods to modify the environment variables of
the to-be-executed commands. This would let us drop C<system_env> (from
Lintian::Util) and make C<run> more useful.
=head1 NOTES
Unless specified by prefixing the package name, every reference to a
function/method in this documentation refers to the functions/methods
provided by this package itself.
=head1 CAVEATS
Combining asynchronous jobs from Lintian::Command and calls to wait()
can lead to unexpected results.
Calling wait() without a pid via the procedural interface can lead to
processes started via the OO interface to be reaped. In this case, the
object that started the reaped process won't be able to determine the
return status, which can affect the rest of the application.
As a general advise, the procedural and OO interfaces should not be
combined when using background(). Unless, of course, you are calling wait()
with a hash ref.
=head1 AUTHOR
Originally written by Raphael Geissert <atomo64@gmail.com> for Lintian.
=cut
# Local Variables:
# indent-tabs-mode: nil
# cperl-indent-level: 4
# End:
# vim: syntax=perl sw=4 sts=4 sr et
|