use POSIX;
use File::Copy;
+my $MainVersion = "2.0";
+
+my $Revis = <<'ENDE';
+ $Revision$
+ENDE
+$Revis =~ s/^.*:\s*(\S+)\s*\$.*/$1/s;
+our $VERSION = $MainVersion . "." . $Revis;
+
+$Data::Dumper::Indent = 1;
+$Data::Dumper::Sortkeys = 1;
+
use LogRotate::Conf;
+use LogRotate::StateFile;
-use constant default_firstline_statusfile => "Logrotate State -- Version 2";
use constant default_buffer_size => 4096;
use constant max_rotate => 100000;
verbose => 0,
test => 0,
force => 0,
- statusfile => '/var/lib/logrotate.pl.status',
+ statusfile => '/var/lib/logrotate.status',
scripts => {},
logfiles => {},
rotatet_files => {},
#------------------------------------------------------------------------------------------
+=head2 check_state()
+
+
+
+=cut
+
+sub check_state($) {
+
+ my $self = shift;
+ my $p = $self->verbose() ? __PACKAGE__ . "::check_state: " : "";
+
+ my $state_file = new LogRotate::StateFile(
+ 'verbose' => $self->{'verbose'},
+ 'test' => $self->{'test'},
+ );
+ $state_file->file($self->{'statusfile'});
+
+ my $states = $state_file->check();
+
+ if ( $states ) {
+ $self->{'state_file'} = $state_file;
+ $self->{'states'} = $states;
+ return 1;
+ }
+
+ warn $p . "Statusdatei '" . $self->{'statusfile'} . " ist nicht verwendungsfaehig.\n";
+ return undef;
+
+}
+
+#------------------------------------------------------------------------------------------
+
=head2 verbose()
Setzt bzw. gibt den Verbose-Level dieses Moduls zurueck.
use LogRotate;
-our $VERSION = "2.0";
+my $MainVersion = "2.0";
+my $Revis = <<'ENDE';
+ $Revision$
+ENDE
+$Revis =~ s/^.*:\s*(\S+)\s*\$.*/$1/s;
+our $VERSION = $MainVersion . "." . $Revis;
+
+$Data::Dumper::Indent = 1;
+$Data::Dumper::Sortkeys = 1;
+
+sub write_pidfile($$);
+sub check_pidfile($);
my $DefConfigFile = "/etc/logrotate.conf";
my $ConfigFile = $DefConfigFile;
$| = 1;
my ( @ConfigFiles, @InvalidConfigFiles );
-my ( $res );
+my ( $res, $delete_pidfile, $pidfile );
Getopt::Long::Configure("bundling");
Getopt::Long::Configure("no_ignore_case");
$test = 1 if $config_check;
-$test = 1;
-
unless ( $test ) {
print "\n" . ( "#" x 80 ) . "\n" if $verbose;
print "$0 beginnt mit Logrotation um: " . localtime() . "\n\n";
} else {
$lr->{'statusfile'} = $lr->{'c'}{'statusfile'} if $lr->{'c'}{'statusfile'};
}
+$pidfile = $lr->{'c'}{'pidfile'} || "/var/run/logrotate.pid";
+$delete_pidfile = $test ? 0 : 1;
unless ( $lr->{'statusfile'} ) {
warn "Keine Statusdatei gegeben.\n";
}
print "Statusdatei ist: '" . $lr->{'statusfile'} . "'.\n" if $verbose;
+unless ( $test ) {
+
+ if ( check_pidfile $pidfile ) {
+ print $p . "Pidfile okay.\n" if $verbose > 1;
+ } else {
+ $delete_pidfile = undef;
+ exit 9;
+ }
+
+ unless ( write_pidfile $pidfile, $$ ) {
+ $delete_pidfile = undef;
+ exit 10;
+ }
+
+}
+
+exit 11 unless $lr->check_state();
+
print "\n" . $p . "Gelesene Konfiguration: " . Dumper( $lr ) if $verbose > 3;
+
+$lr = undef;
+
+print "\n$0 beendet Logrotation um: " . localtime() . "\n" unless $test;
exit 0;
+#----------------------------------------------------------------------
+
+END {
+
+ my $p = $verbose ? __PACKAGE__ . "::END(): " : "";
+ if ( $delete_pidfile and -f $pidfile ) {
+ print $p . "Loesche PID-File '" . $pidfile . "' ...\n" if $verbose > 1;
+ unless ( unlink $pidfile ) {
+ warn $p . "Konnte PID-File '" . $pidfile . "' nicht loeschen: $!\n";
+ }
+ }
+
+}
+
+#----------------------------------------------------------------------
+
+=pod
+
$lr->read_state_file();
print Dumper( $lr ) if $verbose > 2;
$lr->compress_files();
-print "\n$0 ends with logrotation at: " . localtime() . "\n" if $verbose and not $test;
exit 0;
+=cut
+
#------------------------------------------------------------------------------------------
}
+#------------------------------------------------------------------------------------------
+
+=head2 B<check_pidfile($pidfile)>
+
+Inspiziert das übergebene PID-File.
+
+=cut
+
+sub check_pidfile($) {
+
+ my $file = shift;
+ my ( $pid );
+ my $p = $verbose ? __PACKAGE__ . "::check_pidfile(): " : "";
+
+ if ( -f $file ) {
+ unless ( open PID, "<", $file ) {
+ warn $p . "Konnte PID file '" . $file. "' nicht lesend oeffnen: $!\n";
+ return undef;
+ }
+ $pid = <PID>;
+ close PID;
+ if ( $pid =~ /^\d+$/ ) {
+ if ( kill( 0, $pid ) ) {
+ warn $p . "Es laeuft schon ein '$0'-Script mit pid $pid!\n";
+ return undef;
+ }
+ warn $p . "Altes Pidfile gefunden, Prozess aber unbekannt verstorben...\n";
+ } else {
+ warn $p . "Nichts Verwendungsfaehiges im Pidfile gefunden, exit ...\n";
+ return undef;
+ }
+
+ }
+
+ return 1;
+
+}
+
+#------------------------------------------------------------------------------------------
+
+=head2 B<write_pidfile($pidfile, $pid)>
+
+Schreib die uebergebene PID in das uebergebene PID-File.
+
+=cut
+
+sub write_pidfile($$) {
+
+ my ( $pidfile, $pid ) = @_;
+ my $p = $verbose ? __PACKAGE__ . "::write_pidfile(): " : "";
+
+ print $p . "Schreibe PID-File '$pidfile' ...\n";
+ unless ( open PID, ">", $pidfile ) {
+ warn $p . "Konnte PID-file '" . $pidfile . "' nicht schreiben: $!\n";
+ return undef;
+ }
+ print PID "$pid\n";
+ close PID;
+
+ return 1;
+
+}
+
+
#------------------------------------------------------------------------------------
__END__