[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

[SCM] Debian package checker branch, master, updated. 2.3.4-50-g7610c46



The following commit has been merged in the master branch:
commit 594f079d777806ba723c45d31b9903b2aca910f4
Author: Raphael Geissert <atomo64@gmail.com>
Date:   Sat Mar 20 20:23:25 2010 -0600

    Add Lintian::Command::Simple::kill to kill processes

diff --git a/lib/Lintian/Command/Simple.pm b/lib/Lintian/Command/Simple.pm
index 2514d79..284e31b 100644
--- a/lib/Lintian/Command/Simple.pm
+++ b/lib/Lintian/Command/Simple.pm
@@ -260,6 +260,68 @@ sub wait {
     }
 }
 
+=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
diff --git a/t/scripts/Lintian/Command/Simple/09-kill.t b/t/scripts/Lintian/Command/Simple/09-kill.t
new file mode 100644
index 0000000..dc1654c
--- /dev/null
+++ b/t/scripts/Lintian/Command/Simple/09-kill.t
@@ -0,0 +1,15 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+use Lintian::Command::Simple;
+
+my $pid;
+
+$pid = Lintian::Command::Simple::background("sleep", 10);
+
+is(Lintian::Command::Simple::kill($pid), 1, "One job was killed");
+
+is(Lintian::Command::Simple::wait($pid), 0, "The job was reaped");
diff --git a/t/scripts/Lintian/Command/Simple/10-OO-kill.t b/t/scripts/Lintian/Command/Simple/10-OO-kill.t
new file mode 100644
index 0000000..9a814f9
--- /dev/null
+++ b/t/scripts/Lintian/Command/Simple/10-OO-kill.t
@@ -0,0 +1,16 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+use Lintian::Command::Simple;
+
+my ($cmd, $pid);
+
+$cmd = Lintian::Command::Simple->new();
+
+$cmd->background("sleep", 10);
+
+is($cmd->kill(), 1, "One process was killed");
+is($cmd->wait(), 0, "One process was reaped");
diff --git a/t/scripts/Lintian/Command/Simple/11-kill-multiple-jobs.t b/t/scripts/Lintian/Command/Simple/11-kill-multiple-jobs.t
new file mode 100644
index 0000000..d11b66b
--- /dev/null
+++ b/t/scripts/Lintian/Command/Simple/11-kill-multiple-jobs.t
@@ -0,0 +1,23 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More tests => 2;
+
+use Lintian::Command::Simple;
+
+my $cmd;
+my $c = 4;
+my %jobs;
+
+while ($c) {
+    $cmd = Lintian::Command::Simple->new();
+    $cmd->background("sleep", 10);
+    $jobs{$c} = $cmd;
+    $c--;
+}
+
+is(Lintian::Command::Simple::kill(\%jobs), 4, "4 jobs were killed");
+
+is(Lintian::Command::Simple::wait(\%jobs), undef,
+	"kill(hashref) kills and reaps");

-- 
Debian package checker


Reply to: