/usr/lib/amanda/perl/Amanda/Process.pm is in amanda-common 1:3.3.6-4.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 | # Copyright (c) 2008-2013 Zmanda, Inc. All Rights Reserved.
#
# 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
#
# Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
# Sunnyvale, CA 94085, USA, or: http://www.zmanda.com
package Amanda::Process;
use strict;
use warnings;
use Carp;
use POSIX ();
use Exporter;
use vars qw( @ISA @EXPORT_OK );
use File::Basename;
use Amanda::Constants;
use Amanda::Debug qw( debug );
@ISA = qw( Exporter );
=head1 NAME
Amanda::Process -- interface to process
=head1 SYNOPSIS
use Amanda::Process;
Amanda::Process::load_ps_table();
Amanda::Process::scan_log($logfile);
Amanda::Process::add_child();
Amanda::Process::set_master_process(@pname);
Amanda::Process::set_master($pname, $pid);
Amanda::Process::kill_process($signal);
my $count = Amanda::Process::process_running();
my $count = Amanda::Process::count_process();
my $alive = Amanda::Process::process_alive($pid, $pname);
=head1 INTERFACE
This module provides an object-oriented interface to track process used by
amanda.
my $Amanda_process = Amanda::Process->new($verbose);
=over
=item load_ps_table
$Amanda_process->load_ps_table();
Load a table of all processes in the system.
=item scan_log
$Amanda_process->scan_log($logfile);
Parse all 'pid' and 'pid-done' lines of the logfile.
=item add_child
$Amanda_process->add_child();
Add all children of already known amanda processes.
=item set_master_process
$Amanda_process->set_master_process($arg, @pname);
Search the process table to find a process in @pname and make it the master, $arg must be an argument of the process.
=item set_master
$Amanda_process->set_master($pname, $pid);
Set $Amanda_process->{master_pname} and $Amanda_process->{master_pid}.
=item kill_process
$Amanda_process->kill_process($signal);
Send the $signal to all amanda processes.
=item process_running
my $count = $Amanda_process->process_running();
Return the number of amanda process alive.
=item count_process
my $count = $Amanda_process->count_process();
Return the number of amanda process in the table.
=item process_alive
my $alive = Amanda::Process::process_alive($pid, $pname);
Return 0 if the process is not alive.
Return 1 if the process is still alive.
=back
=cut
sub new {
my $class = shift;
my ($verbose) = shift;
my $self = {
verbose => $verbose,
master_name => "",
master_pid => "",
pids => {},
pstable => {},
ppid => {},
};
bless ($self, $class);
return $self;
}
# Get information about the current set of processes, using ps -e
# and ps -ef.
#
# Side effects:
# - sets %pstable to a map (pid => process name) of all running
# processes
# - sets %ppid to a map (pid -> parent pid) of all running
# processes' parent pids
#
sub load_ps_table() {
my $self = shift;
$self->{pstable} = {};
$self->{ppid} = ();
my $ps_argument = $Amanda::Constants::PS_ARGUMENT;
if ($ps_argument eq "CYGWIN") {
open(PSTABLE, "-|", "ps -ef") || die("ps -ef: $!");
my $psline = <PSTABLE>; #header line
while($psline = <PSTABLE>) {
chomp $psline;
my @psline = split " ", $psline;
my $pid = $psline[1];
my $ppid = $psline[2];
my $stime = $psline[4];
my $pname;
if ($stime =~ /:/) { # 10:32:44
$pname = basename($psline[5])
} else { # May 22
$pname = basename($psline[6])
}
$self->{pstable}->{$pid} = $pname;
$self->{ppid}->{$pid} = $ppid;
}
close(PSTABLE);
} else {
open(PSTABLE, "-|", "$Amanda::Constants::PS $ps_argument")
or die("$Amanda::Constants::PS $ps_argument: $!");
my $psline = <PSTABLE>; #header line
while($psline = <PSTABLE>) {
chomp $psline;
my ($pid, $ppid, $pname, $arg1, $arg2) = split " ", $psline;
$pname = basename($pname);
if ($pname =~ /^perl/ && defined $arg1) {
if ($arg1 !~ /^\-/) {
$pname = $arg1;
} elsif (defined $arg2) {
if ($arg2 !~ /^\-/) {
$pname = $arg2;
}
}
$pname = basename($pname);
}
$self->{pstable}->{$pid} = $pname;
$self->{ppid}->{$pid} = $ppid;
}
close(PSTABLE);
}
}
# Scan a logfile for processes that should still be running: processes
# having an "INFO foo bar pid 1234" line in the log, but no corresponding
# "INFO pid-done 1234", and only if pid 1234 has the correct process
# name.
#
# Prerequisites:
# %pstable must be set up (use load_ps_table)
#
# Side effects:
# - sets %pids to a map (pid => process name) of all still-running
# Amanda processes
# - sets $master_pname to the top-level process for this run (e.g.,
# amdump, amflush)
# - sets $master_pid to the pid of $master_pname
#
# @param $logfile: the logfile to scan
#
sub scan_log($) {
my $self = shift;
my $logfile = shift;
my $first = 1;
my($line);
open(LOGFILE, "<", $logfile) || die("$logfile: $!");
while($line = <LOGFILE>) {
if ($line =~ /^INFO .* (.*) pid (\d*)$/) {
my ($pname, $pid) = ($1, $2);
if ($first == 1) {
$self->{master_pname} = $pname;
$self->{master_pid} = $pid;
$first = 0;
}
if (defined $self->{pstable}->{$pid} && $pname eq $self->{pstable}->{$pid}) {
$self->{pids}->{$pid} = $pname;
} elsif (defined $self->{pstable}->{$pid} && $self->{pstable}->{$pid} =~ /perl/) {
# We can get 'perl' for a perl script.
$self->{pids}->{$pid} = $pname;
} elsif (defined $self->{pstable}->{$pid}) {
debug("pid $pid doesn't match: " . $pname . " != " . $self->{pstable}->{$pid});
print "pid $pid doesn't match: ", $pname, " != ", $self->{pstable}->{$pid}, "\n" if $self->{verbose};
}
} elsif ($line =~ /^INFO .* pid-done (\d*)$/) {
my $pid = $1;
print "pid $pid is done\n" if $self->{verbose};
delete $self->{pids}->{$pid};
}
}
close(LOGFILE);
# log unexpected dead process
if ($self->{verbose}) {
for my $pid (keys %{$self->{pids}}) {
if (!defined $self->{pstable}->{$pid}) {
print "pid $pid is dead\n";
}
}
}
}
# Recursive function to add all child processes of $pid to %amprocess.
#
# Prerequisites:
# - %ppid must be set (load_ps_table)
#
# Side-effects:
# - adds all child processes of $pid to %amprocess
#
# @param $pid: the process to start at
#
sub add_child_pid($);
sub add_child_pid($) {
my $self = shift;
my $pid = shift;
foreach my $cpid (keys %{$self->{ppid}}) {
my $ppid = $self->{ppid}->{$cpid};
if ($pid == $ppid) {
if (!defined $self->{amprocess}->{$cpid}) {
$self->{amprocess}->{$cpid} = $cpid;
$self->add_child_pid($cpid);
}
}
}
}
# Find all children of all amanda processes, as determined by traversing
# the process graph (via %ppid).
#
# Prerequisites:
# - %ppid must be set (load_ps_table)
# - %pids must be set (scan_log)
#
# Side-effects:
# - sets %amprocess to a map (pid => pid) of all amanda processes, including
# children
#
sub add_child() {
my $self = shift;
foreach my $pid (keys %{$self->{pids}}) {
if (defined $pid) {
$self->{amprocess}->{$pid} = $pid;
}
}
foreach my $pid (keys %{$self->{pids}}) {
$self->add_child_pid($pid);
}
}
# Set master_pname and master_pid.
#
# Side-effects:
# - sets $self->{master_pname} and $self->{master_pid}.
#
sub set_master_process {
my $self = shift;
my $arg = shift;
my @pname = @_;
my $ps_argument_args = $Amanda::Constants::PS_ARGUMENT_ARGS;
for my $pname (@pname) {
my $pid;
if ($ps_argument_args eq "CYGWIN") {
$pid = `ps -ef|grep -w ${pname}|grep -w ${arg}| grep -v grep | awk '{print \$2}'`;
} else {
$pid = `$Amanda::Constants::PS $ps_argument_args|grep -w ${pname}|grep -w ${arg}| grep -v grep | awk '{print \$1}'`;
}
chomp $pid;
if ($pid ne "") {
$self->set_master($pname, $pid);
}
}
}
# Set master_pname and master_pid.
#
# Side-effects:
# - sets $self->{master_pname} and $self->{master_pid}.
#
sub set_master($$) {
my $self = shift;
my $pname = shift;
my $pid = shift;
$self->{master_pname} = $pname;
$self->{master_pid} = $pid;
$self->{pids}->{$pid} = $pname;
}
# Send a signal to all amanda process
#
# Side-effects:
# - All amanda process receive the signal.
#
# Prerequisites:
# - %amprocess must be set (add_child)
#
# @param $signal: the signal to send
#
sub kill_process($) {
my $self = shift;
my $signal = shift;
foreach my $pid (keys %{$self->{amprocess}}) {
print "Sendding $signal signal to pid $pid\n" if $self->{verbose};
kill $signal, $pid;
}
}
# Count the number of processes in %amprocess that are still running. This
# re-runs 'ps -e' every time, so calling it repeatedly may result in a
# decreasing count.
#
# Prerequisites:
# - %amprocess must be set (add_child)
#
# @returns: number of pids in %amprocess that are still alive
sub process_running() {
my $self = shift;
$self->load_ps_table();
my $count = 0;
foreach my $pid (keys %{$self->{amprocess}}) {
if (defined $self->{pstable}->{$pid}) {
$count++;
}
}
return $count;
}
# Count the number of processes in %amprocess.
#
# Prerequisites:
# - %amprocess must be set (add_child)
#
# @returns: number of pids in %amprocess.
sub count_process() {
my $self = shift;
return scalar keys( %{$self->{amprocess}} );
}
# return if a process is alive. If $pname is provided,
# only returns 1 if the name matches.
#
# Prerequisites:
# - %pstable must be set (load_ps_table)
#
# @param $pid: the pid of the process
# @param $pname: the name of the process (optional)
#
# @returns: 1 if process is alive
# '' if process is dead
sub process_alive() {
my $self = shift;
my $pid = shift;
my $pname = shift;
if (defined $pname && defined $self->{pstable}->{$pid}) {
return $self->{pstable}->{$pid} eq $pname;
} else {
return defined $self->{pstable}->{$pid};
}
}
1;
|