#!/usr/bin/perl -w # # (c) Igor Chubin, igor@chub.in, 2004-2008 # use strict; use POSIX; use Term::VT102; use Text::Iconv; use Time::Local 'timelocal_nocheck'; use IO::Socket; use lib "/etc/lilalo"; use l3config; our @Command_Lines; our @Command_Lines_Index; our %Diffs; our %Sessions; our %Script_Files; # Информация о позициях в скрипт-файлах, # до которых уже выполнен разбор # и информация о времени модификации файла # $Script_Files{$file}->{size} # $Script_Files{$file}->{tell} our $Killed =0; # В режиме демона -- процесс получил сигнал о завершении sub init_variables; sub main; sub load_diff_files; sub bind_diff; sub extract_commands_from_cline; sub load_command_lines; sub sort_command_lines; sub print_command_lines; sub printq; sub save_cache_stat; sub load_cache_stat; sub print_session; sub load_diff_files { my @pathes = @_; for my $path (@pathes) { my $template = "*.diff"; my @files = <$path/$template>; my $i=0; for my $file (@files) { next if defined($Diffs{$file}); my %diff; # Старый формат имени diff-файла # DEPRECATED if ($file=~m@/(D?[0-9][0-9]?[0-9]?)[^/]*?([0-9]*):([0-9]*):?([0-9]*)@) { $diff{"day"}=$1 || ""; $diff{"hour"}=$2; $diff{"min"}=$3; $diff{"sec"}=$4 || 0; $diff{"uid"} = 0 if $path =~ m@/root/@; print "diff loaded: $diff{day} $diff{hour}:$diff{min}:$diff{sec}\n"; } # Новый формат имени diff-файла elsif ($file =~ m@.*/([^_]*)_([0-9]+)(.*)@) { $diff{"local_session_id"} = $1; $diff{"time"} = $2; $diff{"filename"} = $3; $diff{"filename"} =~ s@_@/@g; $diff{"filename"} =~ s@//@_@g; print "diff loaded: $diff{filename} (time=$diff{time},session=$diff{local_session_id})\n"; } else { next; } # Чтение и изменение кодировки содержимого diff-файла local $/; open (F, "$file") or return "Can't open file $file ($_[0]) for reading"; my $text = ; if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i) { my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8"); $text = $converter->convert($text); } close(F); $diff{"text"}=$text; $diff{"path"}=$path; $diff{"bind_to"}=""; $diff{"time_range"}=-1; $diff{"index"}=$i; $Diffs{$file} = \%diff; $i++; } } } sub bind_diff { print "Trying to bind diff...\n"; my $cl = shift; my $hour = $cl->{"hour"}; my $min = $cl->{"min"}; my $sec = $cl->{"sec"}; my $min_dt = 10000; if (defined($cl->{"diff"})) { print STDERR "Command ".$cl->{time}." is already bound"; return; } # Загружаем новые diff-файлы # Это нужно делать непосредственно перед привязкой, поскольку diff'ы могли образоваться только что for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) { load_diff_files($lab_log); } my $diff_to_bind; for my $diff_key (keys %Diffs) { my $diff = $Diffs{$diff_key}; next if ($diff->{"local_session_id"} && $cl->{"local_session_id"} && ($cl->{"local_session_id"} ne $diff->{"local_session_id"})); next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"})); my $dt; if (not $diff->{"time"}) { print STDERR "diff time is 0"; print STDERR join(" ", keys(%$diff)); print STDERR $diff->{text}; } if (not $cl->{"time"}) { print STDERR "cl time is 0"; } if ($diff->{"time"} && $cl->{"time"}) { $dt = $diff->{"time"} - $cl->{"time"} } else { $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec); } if ($dt >=0 && $dt < $min_dt && !$diff->{"bind_to"}) { $min_dt = $dt; $diff_to_bind = $diff_key; } } if ($diff_to_bind) { print "Approppriate diff found: dt=$min_dt\n"; $Diffs{$diff_to_bind}->{"bind_to"}=$cl; $cl->{"diff"} = $diff_to_bind; } else { print STDERR "Diff not found\n"; print STDERR "cl{time}",$cl->{time},"\n"; } } sub extract_commands_from_cline # Разобрать командную строку $_[1] и возвратить хэш, содержащий # номер первого появление команды в строке: # команда => первая позиция { my $cline = $_[0]; my @lists = split /\;/, $cline; my @commands = (); for my $list (@lists) { push @commands, split /\|/, $list; } my %commands; my %files; my $i=0; for my $command (@commands) { $command =~ /\s*(\S+)\s*(.*)/; if ($1 && $1 eq "sudo" ) { $commands{"$1"}=$i++; $command =~ s/\s*sudo\s+//; } $command =~ /\s*(\S+)\s*(.*)/; if ($1 && !defined $commands{"$1"}) { $commands{"$1"}=$i++; }; } return %commands; } sub load_command_lines { my $lab_scripts_path = $_[0]; my $lab_scripts_mask = $_[1]; my $cline_re_base = qq' ( (?:\\^?([0-9]*C?)) # exitcode (?:_([0-9]+)_)? # uid (?:_([0-9]+)_) # pid (...?) # day (.?.?) # lab \\s # space separator ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) # time .\\[50D.\\[K # killing symbols (.*?([\$\#]\\s?)) # prompt (.*) # command line ) '; my $cline_re = qr/$cline_re_base/sx; my $cline_re2 = qr/$cline_re_base$/sx; my $cline_re_v2_base = qq' ( v2[\#] # version ([0-9]+)[\#] # history line number ([0-9]+)[\#] # exitcode ([0-9]+)[\#] # uid ([0-9]+)[\#] # pid ([0-9]+)[\#] # time (.*?)[\#] # pwd .\\[1024D.\\[K # killing symbols (.*?([\$\#]\\s?)) # prompt (.*) # command line ) '; my $cline_re_v2 = qr/$cline_re_v2_base/sx; my $cline_re2_v2 = qr/$cline_re_v2_base$/sx; my $cline_re_v3_base = qq' ( v3[\#] # version .* ) '; my $cline_re_v3 = qr/$cline_re_v3_base/sx; my $cline_re2_v3_base = qq' ( v3[\#] # version ([0-9]+)[\#] # history line number ([0-9]+)[\#] # exitcode ([0-9]+)[\#] # uid ([0-9]+)[\#] # pid ([0-9]+)[\#] # time (.*?)[\#] # pwd (.*?)[\#] # nonce (.*?([\$\#]\\s?)) # prompt (.*) # command line ) '; my $cline_re2_v3 = qr/$cline_re2_v3_base$/sx; my %vt; # Хэш виртуальных терминалов. По одному на каждый сеанс my $cline_vt = Term::VT102->new ( 'cols' => $Config{"terminal_width"}, 'rows' => $Config{"terminal_height"}); my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8") if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i); print "Parsing lab scripts...\n" if $Config{"verbose"} =~ /y/; my $file; my $skip_info; my $commandlines_loaded =0; my $commandlines_processed =0; my @lab_scripts = <$lab_scripts_path/$lab_scripts_mask>; for $file (@lab_scripts){ # Пропускаем файл, если он не изменялся со времени нашего предудущего прохода my $size = (stat($file))[7]; next if ($Script_Files{$file} && $Script_Files{$file}->{size} && $Script_Files{$file}->{size} >= $size); my $local_session_id; # Начальное значение идентификатора текущего сеанса определяем из имени скрипта # Впоследствии оно может быть уточнено $file =~ m@.*/([^/]*)\.script$@; $local_session_id = $1; if (not defined($vt{$local_session_id})) { $vt{$local_session_id} = Term::VT102->new ( 'cols' => $Config{"terminal_width"}, 'rows' => $Config{"terminal_height"}); } #Если файл только что появился, #пытаемся найти и загрузить информацию о соответствующей ему сессии if (!$Script_Files{$file}) { my $session_file = $file; $session_file =~ s/\.script/.info/; if (open(SESSION, $session_file)) { local $/; my $data = ; close(SESSION); for my $session_data ($data =~ m@(.*?)@sg) { my %session; while ($session_data =~ m@<([^>]*?)>(.*?)@sg) { $session{$1} = $2; } $local_session_id = $session{"local_session_id"} if $session{"local_session_id"}; $Sessions{$local_session_id}=\%session; } #Загруженную информацию сразу же отправляем в поток print_session($Config{cache}, $local_session_id); } else { die "can't open session file"; } } open (FILE, "$file"); binmode FILE; # Переходим к тому месту, где мы окончили разбор seek (FILE, $Script_Files{$file}->{tell}, 0) if $Script_Files{$file}->{tell}; $Script_Files{$file}->{size} = $size; $Script_Files{$file}->{tell} = 0 unless $Script_Files{$file}->{tell}; $file =~ m@.*/(.*?)-.*@; print "\n+- processing file $file\n| " if $Config{"verbose"} =~/y/; my $tty = $1; my $first_pass = 1; my %cl; my $last_output_length=0; while () { $commandlines_processed++; next if s/^Script started on.*?\n//s; if (/[0-9][0-9]:[0-9][0-9]:[0-9][0-9].\[[0-9][0-9]D.\[K/ && m/$cline_re/) { s/.*\x0d(?!\x0a)//; m/$cline_re2/gs; $commandlines_loaded++; $last_output_length=0; # Previous command my %last_cl = %cl; my $this_line = $1; my $err = $2 || ""; $cl{"local_session_id"} = $local_session_id; # Parse new command $cl{"uid"} = $3; #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0 $cl{"pid"} = $4; $cl{"day"} = $5; $cl{"lab"} = $6; $cl{"hour"} = $7; $cl{"min"} = $8; $cl{"sec"} = $9; #$cl{"fullprompt"} = $10; $cl{"prompt"} = $11; $cl{"raw_cline"} = $12; { use bytes; $cl{"raw_start"} = tell (FILE) - length($this_line); $cl{"raw_output_start"} = tell FILE; } $cl{"raw_file"} = $file; $cl{"err"} = 0; $cl{"output"} = ""; $cl{"tty"} = $tty; $cline_vt->process($cl{"raw_cline"}."\n"); $cl{"cline"} = $cline_vt->row_plaintext (1); $cl{"cline"} =~ s/\s*$//; $cl{"cline"} =~ s/.*?[\#\$]\s*//; $cline_vt->reset(); my %commands = extract_commands_from_cline($cl{"cline"}); #$cl{"euid"}=0 if defined $commands{"sudo"}; my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; $cl{"last_command"} = $comms[$#comms] || ""; if ( $Config{"suppress_editors"} =~ /^y/i && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) || $Config{"suppress_pagers"} =~ /^y/i && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) || $Config{"suppress_terminal"}=~ /^y/i && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}}) ) { $cl{"suppress_output"} = "1"; } else { $cl{"suppress_output"} = "0"; } $skip_info = 0; print " ",$cl{"last_command"}; if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { bind_diff(\%last_cl); } # Processing previous command line #if ($first_pass) { # $first_pass = 0; # next; #} # Error code $last_cl{"raw_end"} = $cl{"raw_start"}; $last_cl{"err"}=$err; $last_cl{"err"}=130 if $err eq "^C"; # Output if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { for (my $i=0; $i<$Config{"terminal_height"}; $i++) { my $line= $vt{$local_session_id}->row_plaintext($i); next if !defined ($line) ; #|| $line =~ /^\s*$/; $line =~ s/\s*$//; $line .= "\n" unless $line =~ /^\s*$/; $last_cl{"output"} .= $line; } } else { $last_cl{"output"}= ""; } $vt{$local_session_id}->reset(); # Save if (!$Config{"lab"} || $cl{"lab"} eq $Config{"lab"}) { # Changing encoding for (keys %last_cl) { next if /raw/; $last_cl{$_} = $converter->convert($last_cl{$_}) if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i); } push @Command_Lines, \%last_cl; # Сохранение позиции в файле, до которой выполнен # успешный разбор $Script_Files{$file}->{tell} = $last_cl{raw_end}; } next; } elsif (m/$cline_re_v2/ || m/$cline_re_v3/) { # Разбираем командную строку версии 2 my $before=$_; s/.*\x0d(?!\x0a)//; my $re; if (m/$cline_re_v2/) { $re=$cline_re2_v2; } else { s/.\[1K.\[10D//gs; $re=$cline_re2_v3; print STDERR "... $_ ...\n"; } $commandlines_loaded++; $last_output_length=0; # Previous command my %last_cl = %cl; $cl{"local_session_id"} = $local_session_id; # Parse new command my $this_line = $1; $cl{"history"} = $2; my $err = $3; $cl{"uid"} = $4; #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0 $cl{"pid"} = $5; $cl{"time"} = $6; $cl{"pwd"} = $7; $cl{"nonce"} = $8; #$cl{"fullprompt"} = $8; $cl{"prompt"} = $10; #$cl{"raw_cline"}= $10; $cl{"raw_cline"}= $before; { use bytes; $cl{"raw_start"} = tell (FILE) - length($before); $cl{"raw_output_start"} = tell FILE; } $cl{"raw_file"} = $file; $cl{"err"} = 0; $cl{"output"} = ""; #$cl{"tty"} = $tty; $cline_vt->process($cl{"raw_cline"}."\n"); $cl{"cline"} = $cline_vt->row_plaintext (1); $cl{"cline"} =~ s/\s*$//; $cl{"cline"} =~ s/.*?[\#\$]\s*//; $cline_vt->reset(); print STDERR "cline=".$cl{"cline"}."<<\n"; my %commands = extract_commands_from_cline($cl{"cline"}); #$cl{"euid"} = 0 if defined $commands{"sudo"}; my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; $cl{"last_command"} = $comms[$#comms] || ""; print STDERR "last_command=".$cl{"last_command"}."<<\n"; if ( $Config{"suppress_editors"} =~ /^y/i && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) || $Config{"suppress_pagers"} =~ /^y/i && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) || $Config{"suppress_terminal"}=~ /^y/i && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}}) ) { $cl{"suppress_output"} = "1"; } else { $cl{"suppress_output"} = "0"; } $skip_info = 0; if ($Config{verbose} =~ /y/i) { print "\n| " if $commandlines_loaded % 5 == 1; print " ",$cl{"last_command"}; } if (defined($last_cl{time}) && grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { bind_diff(\%last_cl); } # Error code $last_cl{"err"}=$err; $last_cl{"raw_end"} = $cl{"raw_start"}; # Output if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { for (my $i=0; $i<$Config{"terminal_height"}; $i++) { my $line= $vt{$local_session_id}->row_plaintext($i); next if !defined ($line) ; #|| $line =~ /^\s*$/; $line =~ s/\s*$//; $line .= "\n" unless $line =~ /^\s*$/; $last_cl{"output"} .= $line; } } else { $last_cl{"output"}= ""; } $vt{$local_session_id}->reset(); # Changing encoding for (keys %last_cl) { next if /raw/; if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i) { $last_cl{$_} = $converter->convert($last_cl{$_}) } } if (defined($last_cl{time})) { print STDERR "push id=".$last_cl{time}."\n"; push @Command_Lines, \%last_cl; # Сохранение позиции в файле, до которой выполнен # успешный разбор $Script_Files{$file}->{tell} = $last_cl{raw_end}; } next; } # Иначе, это строка вывода $last_output_length+=length($_); #if (!$cl{"suppress_output"} || $last_output_length < 5000) { if ($last_output_length < 50000) { $vt{$local_session_id}->process("$_"."\n") } else { if (!$skip_info && defined($cl{last_command})) { print "($cl{last_command})"; $skip_info = 1; } } } close(FILE); } if ($Config{"verbose"} =~ /y/) { print "\n`- finished.\n" ; print "Lines loaded: $commandlines_processed\n"; print "Command lines: $commandlines_loaded\n"; } } sub sort_command_lines { print "Sorting command lines..." if $Config{"verbose"} =~ /y/; # Sort Command_Lines # Write Command_Lines to Command_Lines_Index my @index; for (my $i=0;$i<=$#Command_Lines;$i++) { $index[$i]=$i; } @Command_Lines_Index = sort { defined($Command_Lines[$index[$a]]->{"time"}) && defined($Command_Lines[$index[$b]]->{"time"}) ? $Command_Lines[$index[$a]]->{"time"} <=> $Command_Lines[$index[$b]]->{"time"} : defined($Command_Lines[$index[$a]]->{"day"}) && defined($Command_Lines[$index[$b]]->{"day"}) && defined($Command_Lines[$index[$a]]->{"hour"}) && defined($Command_Lines[$index[$b]]->{"hour"}) && defined($Command_Lines[$index[$a]]->{"min"}) && defined($Command_Lines[$index[$b]]->{"min"}) && defined($Command_Lines[$index[$a]]->{"sec"}) && defined($Command_Lines[$index[$b]]->{"sec"}) ? $Command_Lines[$index[$a]]->{"day"} cmp $Command_Lines[$index[$b]]->{"day"} || $Command_Lines[$index[$a]]->{"hour"} <=> $Command_Lines[$index[$b]]->{"hour"} || $Command_Lines[$index[$a]]->{"min"} <=> $Command_Lines[$index[$b]]->{"min"} || $Command_Lines[$index[$a]]->{"sec"} <=> $Command_Lines[$index[$b]]->{"sec"} : 0 } @index; print "finished\n" if $Config{"verbose"} =~ /y/; } sub printq { my $TO = shift; my $text = join "", @_; $text =~ s/&/&/g; $text =~ s//>/g; print $TO $text; } =cut Вывести результат обработки журнала. =cut sub print_command_lines { my $output_filename=$_[0]; open(OUT, ">>", $output_filename) or die "Can't open $output_filename for writing\n"; my $cl; my $in_range=0; for my $i (@Command_Lines_Index) { $cl = $Command_Lines[$i]; if ($Config{"from"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"from"}/) { $in_range=1; next; } if ($Config{"to"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"to"}/) { $in_range=0; next; } next if ($Config{"from"} && $Config{"to"} && !$in_range) || ($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ ) || ($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0) || ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130); # Вырезаем из вывода только нужное количество строк my $output=""; if (!grep ($_ eq $cl->{"last_command"}, @{$Config{"full_output_commands"}}) && ($Config{"head_lines"} || $Config{"tail_lines"})) { # Partialy output my @lines = split '\n', $cl->{"output"}; # head my $mark=1; for (my $i=0; $i<= $#lines && $i < $Config{"cache_head_lines"}; $i++) { $output .= $lines[$i]."\n"; } # tail my $start=$#lines-$Config{"cache_tail_lines"}+1; if ($start < 0) { $start=0; $mark=0; } if ($start < $Config{"cache_head_lines"}) { $start=$Config{"cache_head_lines"}; $mark=0; } $output .= $Config{"skip_text"}."\n" if $mark; for ($i=$start; $i<= $#lines; $i++) { $output .= $lines[$i]."\n"; } } else { # Full output $output .= $cl->{"output"}; } # Совместимость с labmaker # Переводим в секунды Эпохи # В labmaker'е данные хранились в неудобной форме: hour, min, sec, day of year # Информация о годе отсутствовала # Её можно внести: # Декабрь 2004 год; остальные -- 2005 год. my $year = 2005; #$year = 2004 if ( $cl->{day} > 330 ); $year = $Config{year} if $Config{year}; # timelocal( $sec, $min, $hour, $mday,$mon,$year); $cl->{time} ||= timelocal_nocheck($cl->{sec},$cl->{min},$cl->{hour},$cl->{day},0,$year); # Начинаем вывод команды print OUT "\n"; print OUT "$Config{l3cd}\n" if $Config{"l3cd"}; for my $element (qw( local_session_id history uid pid time pwd raw_start raw_output_start raw_end raw_file tty err last_command history nonce )) { next unless defined($cl->{"$element"}); print OUT "<$element>".$cl->{$element}."\n"; } for my $element (qw( prompt cline )) { next unless defined($cl->{"$element"}); print OUT "<$element>"; printq(\*OUT,$cl->{"$element"}); print OUT "\n"; } #note #note_title print OUT ""; printq(\*OUT,$output); print OUT "\n"; if ($cl->{"diff"}) { print OUT ""; printq(\*OUT,${$Diffs{$cl->{"diff"}}}{"text"}); print OUT "\n"; } print OUT "\n"; } close(OUT); } sub print_session { my $output_filename = $_[0]; my $local_session_id = $_[1]; return if not defined($Sessions{$local_session_id}); print "printing session info. session id = ".$local_session_id."\n" if $Config{verbose} =~ /y/; open(OUT, ">>", $output_filename) or die "Can't open $output_filename for writing\n"; print OUT "\n"; print OUT "$Config{l3cd}\n" if $Config{"l3cd"}; my %session = %{$Sessions{$local_session_id}}; for my $key (keys %session) { print OUT "<$key>".$session{$key}."\n"; print " ".$key,"\n"; } print OUT "\n"; close(OUT); } sub send_cache { # Если в кэше что-то накопилось, # попытаемся отправить это на сервер # my $cache_was_sent=0; if (open(CACHE, $Config{cache})) { local $/; my $cache = ; close(CACHE); my $socket = IO::Socket::INET->new( PeerAddr => $Config{backend_address}, PeerPort => $Config{backend_port}, proto => "tcp", Type => SOCK_STREAM ); if ($socket) { print $socket $cache; close($socket); $cache_was_sent = 1; } } return $cache_was_sent; } sub save_cache_stat { open (CACHE, ">$Config{cache_stat}"); for my $f (keys %Script_Files) { print CACHE "$f\t",$Script_Files{$f}->{size},"\t",$Script_Files{$f}->{tell},"\n"; } close(CACHE); } sub load_cache_stat { if (open (CACHE, "$Config{cache_stat}")) { while() { chomp; my ($f, $size, $tell) = split /\t/; $Script_Files{$f}->{size} = $size; $Script_Files{$f}->{tell} = $tell; } close(CACHE); }; } main(); sub process_was_killed { $Killed = 1; } sub reload { init_config; } sub main { $| = 1; init_variables(); init_config(); if ($Config{"mode"} ne "daemon") { # В нормальном режиме работы нужно # считать скрипты, обработать их и записать # результат выполнения в результирующий файл. # После этого завершить работу. # Очистим кэш-файл, если он существовал if (open (CACHE, ">", $Config{"cache"})) { close(CACHE); }; load_command_lines($Config{"input"}, $Config{"input_mask"}); sort_command_lines; #process_command_lines; print_command_lines($Config{"cache"}); } else { if (open(PIDFILE, $Config{agent_pidfile})) { my $pid = ; close(PIDFILE); if ($^O eq 'linux' && $pid &&(! -e "/proc/$pid" || !`grep $Config{"l3-agent"} /proc/$pid/cmdline && grep "uid:.*\b$<\b" /proc/$pid/status`)) { print "Removing stale pidfile\n"; unlink $Config{agent_pidfile} or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!"; } elsif ($^O eq 'freebsd' && defined($pid) && $pid ne "" && not `ps axo uid,pid,command | grep '$< $pid $Config{"l3-agent"}' | grep -v grep 2> /dev/null`) { print "Removing stale pidfile\n"; unlink $Config{agent_pidfile} or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!"; } elsif ($^O eq 'linux' || $^O eq 'freebsd' ) { print "l3-agent is already running: pid=$pid; pidfile=$Config{agent_pidfile}\n"; exit(0); } else { print "Unknown operating system"; exit(0); } } if ($Config{detach} =~ /^y/i) { #$Config{verbose} = "no"; my $pid = fork; exit if $pid; die "Couldn't fork: $!" unless defined ($pid); open(PIDFILE, ">", $Config{agent_pidfile}) or die "Can't open pidfile ". $Config{agent_pidfile}. " for wrting: $!"; print PIDFILE $$; close(PIDFILE); for my $handle (*STDIN, *STDOUT, *STDERR) { open ($handle, "+<", "/dev/null") or die "can't reopen $handle to /dev/null: $!" } POSIX::setsid() or die "Can't start a new session: $!"; $0 = $Config{"l3-agent"}; $SIG{INT} = $SIG{TERM} = \&process_was_killed; $SIG{HUP} = \&reload; } while (not $Killed) { @Command_Lines = (); @Command_Lines_Index = (); load_cache_stat(); load_command_lines($Config{"input"}, $Config{"input_mask"}); if (@Command_Lines) { sort_command_lines; #process_command_lines; print_command_lines($Config{"cache"}); } save_cache_stat(); if (-e $Config{cache} && (stat($Config{cache}))[7]) { send_cache() && unlink($Config{cache}); } sleep($Config{"daemon_sleep_interval"} || 1); } unlink $Config{agent_pidfile}; } } sub init_variables { }