/usr/sbin/amavis-mc is in amavisd-new 1:2.11.0-1ubuntu1.
This file is owned by root:root, with mode 0o755.
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 | #!/usr/bin/perl -T
#------------------------------------------------------------------------------
# This is amavis-mc, a master (of ceremonies) processes to supervise
# supporting service processes (such as amavis-services) used by amavisd-new.
#
# Author: Mark Martinec <Mark.Martinec@ijs.si>
#
# Copyright (c) 2012-2014, Mark Martinec
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# The views and conclusions contained in the software and documentation are
# those of the authors and should not be interpreted as representing official
# policies, either expressed or implied, of the Jozef Stefan Institute.
# (the above license is the 2-clause BSD license, also known as
# a "Simplified BSD License", and pertains to this program only)
#
# Patches and problem reports are welcome.
# The latest version of this program is available at:
# http://www.ijs.si/software/amavisd/
#------------------------------------------------------------------------------
use strict;
use re 'taint';
use warnings;
use warnings FATAL => qw(utf8 void);
no warnings 'uninitialized';
use vars qw($VERSION); $VERSION = 2.008002;
use vars qw($myproduct_name $myversion_id $myversion_date $myversion);
BEGIN {
$myproduct_name = 'amavis-mc';
$myversion_id = '2.9.0'; $myversion_date = '20140506';
$myversion = "$myproduct_name-$myversion_id ($myversion_date)";
}
use Errno qw(ESRCH ENOENT);
use POSIX qw(:sys_wait_h
WIFEXITED WIFSIGNALED WIFSTOPPED WEXITSTATUS WTERMSIG WSTOPSIG);
use Time::HiRes qw(time);
use IO::File qw(O_RDONLY O_WRONLY O_RDWR O_APPEND O_CREAT O_EXCL);
use Unix::Syslog qw(:macros :subs);
use vars qw(@path @services $daemon_user $daemon_group $pid_file $log_level
$syslog_ident $syslog_facility);
### USER CONFIGURABLE:
$daemon_user = 'amavis';
$daemon_group = 'amavis';
$pid_file = '/var/run/amavis/amavis-mc.pid';
$log_level = 0;
$syslog_ident = 'amavis-mc';
$syslog_facility = LOG_MAIL;
@path = qw(/usr/local/sbin /usr/local/bin /usr/sbin /sbin /usr/bin /bin);
@services = (
{ cmd => 'amavis-services msg-forwarder' },
{ cmd => 'amavis-services childproc-minder' },
{ cmd => 'amavis-services snmp-responder' },
);
### END OF USER CONFIGURABLE
my($interrupted, $syslog_open, $pid_file_created, @pids_exited, %pid2service);
# Return untainted copy of a string (argument can be a string or a string ref)
#
sub untaint($) {
return undef if !defined $_[0]; # must return undef even in a list context!
no re 'taint';
local $1; # avoid Perl taint bug: tainted global $1 propagates taintedness
(ref($_[0]) ? ${$_[0]} : $_[0]) =~ /^(.*)\z/s;
$1;
}
# is message log level below the current log level (i.e. eligible for logging)?
#
sub ll($) {
my($level) = @_;
$level <= $log_level;
}
sub do_log($$;@) {
# my($level,$errmsg,@args) = @_;
my $level = shift;
if ($level <= $log_level) {
my $errmsg = shift;
# treat $errmsg as sprintf format string if additional arguments provided
$errmsg = sprintf($errmsg,@_) if @_;
if (!$syslog_open) {
$errmsg .= "\n";
print STDERR $errmsg; # print ignoring I/O status, except SIGPIPE
} else {
my $prio = $level >= 3 ? LOG_DEBUG # most frequent first
: $level >= 1 ? LOG_INFO
: $level >= 0 ? LOG_NOTICE
: $level >= -1 ? LOG_WARNING
: LOG_ERR;
syslog($prio, "%s", $errmsg);
}
}
}
sub find_program_path($$) {
my($fv_list, $path_list_ref) = @_;
$fv_list = [$fv_list] if !ref $fv_list;
my $found;
for my $fv (@$fv_list) { # search through alternatives
my(@fv_cmd) = split(' ',$fv);
my $cmd = $fv_cmd[0];
if (!@fv_cmd) {
# empty, not available
} elsif ($cmd =~ m{^/}s) { # absolute path
my $errn = stat($cmd) ? 0 : 0+$!;
if ($errn == ENOENT) {
# file does not exist
} elsif ($errn) {
do_log(-1, "find_program_path: %s inaccessible: %s", $cmd,$!);
} elsif (-d _) {
do_log(0, "find_program_path: %s is a directory", $cmd);
} elsif (!-x _) {
do_log(0, "find_program_path: %s is not executable", $cmd);
} else {
$found = join(' ', @fv_cmd);
}
} elsif ($cmd =~ m{/}s) { # relative path
die "find_program_path: relative paths not implemented: @fv_cmd\n";
} else { # walk through the specified PATH
for my $p (@$path_list_ref) {
my $errn = stat("$p/$cmd") ? 0 : 0+$!;
if ($errn == ENOENT) {
# file does not exist
} elsif ($errn) {
do_log(-1, "find_program_path: %s/%s inaccessible: %s", $p,$cmd,$!);
} elsif (-d _) {
do_log(0, "find_program_path: %s/%s is a directory", $p,$cmd);
} elsif (!-x _) {
do_log(0, "find_program_path: %s/%s is not executable", $p,$cmd);
} else {
$found = $p . '/' . join(' ', @fv_cmd);
last;
}
}
}
last if defined $found;
}
$found;
}
# drop privileges
#
sub drop_priv($$) {
my($desired_user,$desired_group) = @_;
local($1);
my($username,$passwd,$uid,$gid) =
$desired_user=~/^(\d+)$/ ? (undef,undef,$1,undef) :getpwnam($desired_user);
defined $uid or die "drop_priv: No such username: $desired_user\n";
if (!defined($desired_group) || $desired_group eq '') {
$desired_group = $gid; # for logging purposes
}
else { $gid = $desired_group=~/^(\d+)$/ ? $1 : getgrnam($desired_group) }
defined $gid or die "drop_priv: No such group: $desired_group\n";
$( = $gid; $) = "$gid $gid"; # real and effective GID
POSIX::setgid($gid) or die "drop_priv: Can't setgid to $gid: $!";
POSIX::setuid($uid) or die "drop_priv: Can't setuid to $uid: $!";
$> = $uid; $< = $uid; # just in case
# print STDERR "desired user=$desired_user ($uid), current: EUID: $> ($<)\n";
# print STDERR "desired group=$desired_group ($gid), current: EGID: $) ($()\n";
$> != 0 or die "drop_priv: Still running as root, aborting\n";
$< != 0 or die "Effective UID changed, but Real UID is 0, aborting\n";
}
sub daemonize() {
closelog() if $syslog_open;
$syslog_open = 0;
STDOUT->autoflush(1);
STDERR->autoflush(1);
close(STDIN) or die "Can't close STDIN: $!";
my $pid;
# the first fork allows the shell to return and allows doing a setsid
eval { $pid = fork(); 1 }
or do {
my($eval_stat) = $@ ne '' ? $@ : "errno=$!"; chomp $eval_stat;
die "Error forking #1: $eval_stat";
};
defined $pid or die "Can't fork #1: $!";
if ($pid) { # parent process terminates here
POSIX::_exit(0); # avoid END and destructor processing
}
# disassociate from a controlling terminal
my $pgid = POSIX::setsid();
defined $pgid && $pgid >= 0 or die "Can't start a new session: $!";
# We are now a session leader. As a session leader, opening a file
# descriptor that is a terminal will make it our controlling terminal.
# The second fork makes us NOT a session leader. Only session leaders
# can acquire a controlling terminal, so we may now open up any file
# we wish without worrying that it will become a controlling terminal.
# second fork prevents from accidentally reacquiring a controlling terminal
eval { $pid = fork(); 1 }
or do {
my($eval_stat) = $@ ne '' ? $@ : "errno=$!"; chomp $eval_stat;
die "Error forking #2: $eval_stat";
};
defined $pid or die "Can't fork #2: $!";
if ($pid) { # parent process terminates here
POSIX::_exit(0); # avoid END and destructor processing
}
chdir('/') or die "Can't chdir to '/': $!";
# a daemonized child process, live long and prosper...
do_log(2, "Daemonized as process [%s]", $$);
openlog($syslog_ident, LOG_PID | LOG_NDELAY, $syslog_facility);
$syslog_open = 1;
{ # suppress unnecessary warning:
# "Filehandle STDIN reopened as STDOUT only for output"
# See https://rt.perl.org/rt3/Public/Bug/Display.html?id=23838
no warnings 'io';
close(STDOUT) or die "Can't close STDOUT: $!";
open(STDOUT, '>/dev/null') or die "Can't open /dev/null: $!";
close(STDERR) or die "Can't close STDERR: $!";
open(STDERR, '>&STDOUT') or die "Can't dup STDOUT: $!";
}
}
# Run specified command as a subprocess.
# Return a process id of a child process.
#
sub spawn_command($@) {
my($cmd, @args) = @_;
my $cmd_text = join(' ', $cmd, @args);
my $pid;
eval {
# Note that fork(2) returns ENOMEM on lack of swap space, and EAGAIN when
# process limit is reached; we want it to fail in both cases and not obey
# the EAGAIN and keep retrying, as perl open() does.
$pid = fork(); 1;
} or do {
my $eval_stat = $@ ne '' ? $@ : "errno=$!"; chomp $eval_stat;
die "spawn_command (forking): $eval_stat";
};
defined($pid) or die "spawn_command: can't fork: $!";
if (!$pid) { # child
alarm(0); my $interrupt = '';
my $h1 = sub { $interrupt = $_[0] };
my $h2 = sub { die "Received signal ".$_[0] };
@SIG{qw(INT HUP TERM TSTP QUIT USR1 USR2)} = ($h1) x 7;
my $err;
eval { # die must be caught, otherwise we end up with two running daemons
local(@SIG{qw(INT HUP TERM TSTP QUIT USR1 USR2)}) = ($h2) x 7;
if ($interrupt ne '') { my $i = $interrupt; $interrupt = ''; die $i }
close STDIN; # ignoring errors
close STDOUT; # ignoring errors
# BEWARE of Perl older that 5.6.0: sockets and pipes were not FD_CLOEXEC
exec {$cmd} ($cmd,@args);
die "spawn_command: failed to exec $cmd_text: $!";
0; # paranoia
} or do {
$err = $@ ne '' ? $@ : "errno=$!"; chomp $err;
};
eval {
local(@SIG{qw(INT HUP TERM TSTP QUIT USR1 USR2)}) = ($h2) x 7;
if ($interrupt ne '') { my $i = $interrupt; $interrupt = ''; die $i }
# we're in trouble if stderr was attached to a terminal, but no longer is
eval { do_log(-1,"spawn_command: child process [%s]: %s", $$,$err) };
} or 1; # ignore failures, make perlcritic happy
{ # no warnings;
POSIX::_exit(6); # avoid END and destructor processing
kill('KILL',$$); exit 1; # still kicking? die!
}
}
# parent
do_log(5,"spawn_command: [%s] %s", $pid, $cmd_text);
$pid; # return the PID of a subprocess
}
sub usage() {
my $me = $0; local $1; $me =~ s{([^/]*)\z}{$1}s;
"Usage: $me (-h | -V | [-f] [-P pid_file] [-d log_level])";
}
# map process termination status number to an informative string, and
# append optional message (dual-valued errno or a string or a number),
# returning the resulting string
#
sub exit_status_str($;$) {
my($stat,$errno) = @_; my $str;
if (!defined($stat)) {
$str = '(no status)';
} elsif (WIFEXITED($stat)) {
$str = sprintf('exit %d', WEXITSTATUS($stat));
} elsif (WIFSTOPPED($stat)) {
$str = sprintf('stopped, signal %d', WSTOPSIG($stat));
} else {
my $sig = WTERMSIG($stat);
$str = sprintf('%s, signal %d (%04x)',
$sig == 1 ? 'HANGUP' : $sig == 2 ? 'INTERRUPTED' :
$sig == 6 ? 'ABORTED' : $sig == 9 ? 'KILLED' :
$sig == 15 ? 'TERMINATED' : 'DIED',
$sig, $stat);
}
if (defined $errno) { # deal with dual-valued and plain variables
$str .= ', '.$errno if (0+$errno) != 0 || ($errno ne '' && $errno ne '0');
}
$str;
}
# check errno to be 0 and a process exit status to be in the list of success
# status codes, returning true if both are ok, and false otherwise
#
sub proc_status_ok($;$@) {
my($exit_status,$errno,@success) = @_;
my $ok = 0;
if ((!defined $errno || $errno == 0) && WIFEXITED($exit_status)) {
my $j = WEXITSTATUS($exit_status);
if (!@success) { $ok = $j==0 } # empty list implies only status 0 is good
elsif (grep($_==$j, @success)) { $ok = 1 }
}
$ok;
}
sub report_terminations($) {
my($pids_exited_list) = @_;
# note: child_handler may be growing the list at its tail during the loop
while (@$pids_exited_list) {
my $pid_stat = shift(@$pids_exited_list);
next if !$pid_stat; # just in case
my($pid,$status,$timestamp) = @$pid_stat;
my $serv = delete $pid2service{$pid};
if (!$serv) {
do_log(-1,'Unknown process [%d] exited: %s',
$pid, exit_status_str($status,0));
} else {
$serv->{status} = $status;
$serv->{terminated_at} = $timestamp;
my $ll = proc_status_ok($status,0) ? 0 : -1;
do_log($ll, 'Process [%d] exited (%s) after %.1f s: %s',
$pid, $serv->{cmd},
$serv->{terminated_at} - $serv->{started_at},
exit_status_str($status,0));
}
}
}
sub child_handler {
my $signal = $_[0];
for (;;) {
my $child_pid = waitpid(-1,WNOHANG);
# PID may be negative on Windows
last if !$child_pid || $child_pid == -1;
push(@pids_exited, [$child_pid, $?, time]);
}
$SIG{CHLD} = \&child_handler;
};
# main program starts here
delete @ENV{'PATH', 'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$ENV{PATH} = join(':',@path) if @path;
my $foreground = 0;
my(@argv) = @ARGV; # preserve @ARGV, may modify @argv
while (@argv >= 2 && $argv[0] =~ /^-[dP]\z/ ||
@argv >= 1 && $argv[0] =~ /^-/) {
my($opt,$val);
$opt = shift @argv;
$val = shift @argv if $opt !~ /^-[hVf-]\z/; # these take no arguments
if ($opt eq '--') {
last;
} elsif ($opt eq '-h') { # -h (help)
die "$myversion\n\n" . usage() . "\n";
} elsif ($opt eq '-V') { # -V (version)
die "$myversion\n";
} elsif ($opt eq '-f') {
$foreground = 1;
} elsif ($opt eq '-d') { # -d log_level
$val =~ /^\d+\z/ or die "Bad value for option -d: $val\n";
$log_level = untaint($val);
} elsif ($opt eq '-P') { # -P pid_file
$pid_file = untaint($val);
} else {
die "Error in command line options: $opt\n\n" . usage() . "\n";
}
}
!@argv or die sprintf("Error parsing a command line %s\n\n%s\n",
join(' ',@ARGV), usage());
$SIG{'__DIE__' } =
sub { if (!$^S) { my($m) = @_; chomp($m); do_log(-1,"_DIE: %s", $m) } };
$SIG{'__WARN__'} =
sub { my($m) = @_; chomp($m); do_log(0,"_WARN: %s",$m) };
if ($foreground) {
do_log(0,"amavis master process starting in foreground, perl %s", $] );
} else { # daemonize
openlog($syslog_ident, LOG_PID | LOG_NDELAY, $syslog_facility);
$syslog_open = 1;
do_log(2,"to be daemonized");
daemonize();
srand();
do_log(0,'amavis master process starting. '.
'daemonized as PID [%s], perl %s', $$, $] );
}
if (defined $daemon_user) {
drop_priv($daemon_user,$daemon_group);
}
if (defined $pid_file && $pid_file ne '') {
my $pid_file_fh = IO::File->new;
$pid_file_fh->open($pid_file, O_CREAT|O_WRONLY, 0640)
or die "Can't create PID file $pid_file: $!";
$pid_file_fh->print($$."\n")
or die "Can't write to PID file $pid_file: $!";
$pid_file_fh->close
or die "Can't close PID file $pid_file: $!";
$pid_file_created = 1;
}
# initialize
for my $serv (@services) {
$serv->{started_cnt} = 0;
$serv->{pid} = $serv->{status} = undef;
$serv->{started_at} = $serv->{terminated_at} = undef;
my $found = find_program_path($serv->{cmd}, \@path);
defined $found
or die sprintf("Can't find program %s in path %s\n",
$serv->{cmd}, join(':',@path));
$serv->{cmd} = $found;
}
$SIG{CHLD} = \&child_handler;
eval { # catch TERM and INT signals for a controlled shutdown
my $h = sub { $interrupted = $_[0]; die "\n" };
local $SIG{INT} = $h;
local $SIG{TERM} = $h;
for (;;) {
last if defined $interrupted;
for my $serv (@services) {
next if $serv->{disabled};
report_terminations(\@pids_exited) if @pids_exited;
if (defined $serv->{status}) {
# process has terminated, clean up
$serv->{pid} = undef;
$serv->{started_at} = undef;
}
last if defined $interrupted;
if (!defined $serv->{pid}) {
# service not running
if ($serv->{started_cnt} >= 5) {
do_log(-1,'Exceeded restart count, giving up on (%s)', $serv->{cmd});
$serv->{disabled} = 1;
} elsif (defined $serv->{terminated_at} &&
time - $serv->{terminated_at} < 1) {
# postpone a restart for at least a second
do_log(5, 'Postponing a restart (%s)', $serv->{cmd});
} else {
my($cmd,@args) = split(' ',$serv->{cmd});
$serv->{started_cnt}++;
$serv->{status} = $serv->{terminated_at} = undef;
$serv->{started_at} = time;
my $pid = $serv->{pid} = spawn_command($cmd,@args);
do_log(0, 'Process [%d] started: %s', $pid, $serv->{cmd});
# to avoid race the signal handler must not be updating %pid2service
$pid2service{$pid} = $serv;
}
}
}
sleep 5; # sleep may be aborted prematurely by a signal
} # until interrupted
};
do_log(0, 'Master process shutting down');
for my $sig ('TERM', 'KILL') {
# terminate or kill child processes
for my $serv (@services) {
my $pid = $serv->{pid};
next if !$pid;
my $n = kill($sig,$pid);
if ($n == 0 && $! == ESRCH) {
# process already gone
} elsif ($n == 0) {
do_log(-1, "Can't send SIG%s to process [%s]: %s", $sig, $pid, $!);
} else {
do_log(0, "%s process [%s] (%s)",
$sig eq 'TERM' ? 'Terminating' : 'Killing',
$pid, $serv->{cmd});
}
}
my $deadline = time + 10; # 10 second grace period
while (time < $deadline) {
report_terminations(\@pids_exited);
sleep 1; # sleep may be aborted prematurely by a signal
# stop waiting if all gone
last if !grep { $_->{pid} && kill(0, $_->{pid}) } @services;
}
report_terminations(\@pids_exited);
}
END {
do_log(0,'Master process exiting: %s', $interrupted) if defined $interrupted;
if ($pid_file_created) {
unlink($pid_file)
or do_log(-1, "Can't delete a PID file %s: %s", $pid_file, $!);
}
if ($syslog_open) { closelog(); $syslog_open = 0 }
}
|