lilalo
annotate l3-agent @ 119:71bd999bcb04
Исправлено несколько багов:
* выполняется корректная привязка diff'ов
* правильно запоминается raw_start и проч raw_*
* временно отключен вывод признака нажатия ctrl-c (он ставился неверно)
* в приглашение добавлен случайный nonce (для правильной отработки tab)
* выполняется корректная привязка diff'ов
* правильно запоминается raw_start и проч raw_*
* временно отключен вывод признака нажатия ctrl-c (он ставился неверно)
* в приглашение добавлен случайный nonce (для правильной отработки tab)
author | igor |
---|---|
date | Thu Mar 13 12:19:42 2008 +0200 (2008-03-13) |
parents | 9e6359b7ad55 |
children | 42d9af3c851c |
rev | line source |
---|---|
devi@52 | 1 #!/usr/bin/perl -w |
devi@23 | 2 |
devi@23 | 3 # |
igor@119 | 4 # (c) Igor Chubin, igor@chub.in, 2004-2008 |
devi@23 | 5 # |
devi@23 | 6 |
devi@23 | 7 use strict; |
devi@25 | 8 use POSIX; |
devi@23 | 9 use Term::VT102; |
devi@23 | 10 use Text::Iconv; |
devi@23 | 11 use Time::Local 'timelocal_nocheck'; |
devi@27 | 12 use IO::Socket; |
devi@23 | 13 |
igor@115 | 14 use lib "/etc/lilalo"; |
devi@23 | 15 use l3config; |
devi@23 | 16 |
devi@23 | 17 our @Command_Lines; |
devi@23 | 18 our @Command_Lines_Index; |
devi@28 | 19 our %Diffs; |
devi@27 | 20 our %Sessions; |
devi@23 | 21 |
devi@62 | 22 our %Script_Files; # Информация о позициях в скрипт-файлах, |
devi@62 | 23 # до которых уже выполнен разбор |
devi@62 | 24 # и информация о времени модификации файла |
devi@62 | 25 # $Script_Files{$file}->{size} |
devi@62 | 26 # $Script_Files{$file}->{tell} |
devi@23 | 27 |
devi@62 | 28 our $Killed =0; # В режиме демона -- процесс получил сигнал о завершении |
devi@23 | 29 |
devi@23 | 30 sub init_variables; |
devi@23 | 31 sub main; |
devi@23 | 32 |
devi@23 | 33 sub load_diff_files; |
devi@23 | 34 sub bind_diff; |
devi@62 | 35 sub extract_commands_from_cline; |
devi@23 | 36 sub load_command_lines; |
devi@23 | 37 sub sort_command_lines; |
devi@23 | 38 sub print_command_lines; |
devi@23 | 39 sub printq; |
devi@23 | 40 |
devi@25 | 41 sub save_cache_stat; |
devi@25 | 42 sub load_cache_stat; |
devi@27 | 43 sub print_session; |
devi@25 | 44 |
devi@23 | 45 sub load_diff_files |
devi@23 | 46 { |
devi@62 | 47 my @pathes = @_; |
devi@62 | 48 |
devi@62 | 49 for my $path (@pathes) { |
devi@62 | 50 my $template = "*.diff"; |
devi@62 | 51 my @files = <$path/$template>; |
devi@62 | 52 my $i=0; |
devi@62 | 53 for my $file (@files) { |
devi@28 | 54 |
devi@62 | 55 next if defined($Diffs{$file}); |
devi@62 | 56 my %diff; |
devi@23 | 57 |
devi@80 | 58 # Старый формат имени diff-файла |
devi@80 | 59 # DEPRECATED |
devi@80 | 60 if ($file=~m@/(D?[0-9][0-9]?[0-9]?)[^/]*?([0-9]*):([0-9]*):?([0-9]*)@) { |
devi@80 | 61 $diff{"day"}=$1 || ""; |
devi@80 | 62 $diff{"hour"}=$2; |
devi@80 | 63 $diff{"min"}=$3; |
devi@80 | 64 $diff{"sec"}=$4 || 0; |
devi@80 | 65 |
devi@80 | 66 $diff{"uid"} = 0 if $path =~ m@/root/@; |
devi@23 | 67 |
devi@62 | 68 print "diff loaded: $diff{day} $diff{hour}:$diff{min}:$diff{sec}\n"; |
devi@80 | 69 |
devi@80 | 70 } |
devi@80 | 71 # Новый формат имени diff-файла |
devi@80 | 72 elsif ($file =~ m@.*/([^_]*)_([0-9]+)(.*)@) { |
devi@80 | 73 $diff{"local_session_id"} = $1; |
devi@80 | 74 $diff{"time"} = $2; |
devi@80 | 75 $diff{"filename"} = $3; |
devi@80 | 76 $diff{"filename"} =~ s@_@/@g; |
devi@80 | 77 $diff{"filename"} =~ s@//@_@g; |
devi@80 | 78 |
devi@80 | 79 print "diff loaded: $diff{filename} (time=$diff{time},session=$diff{local_session_id})\n"; |
devi@80 | 80 } |
devi@80 | 81 else { |
devi@80 | 82 next; |
devi@80 | 83 } |
devi@80 | 84 |
devi@80 | 85 # Чтение и изменение кодировки содержимого diff-файла |
devi@62 | 86 local $/; |
devi@62 | 87 open (F, "$file") |
devi@62 | 88 or return "Can't open file $file ($_[0]) for reading"; |
devi@62 | 89 my $text = <F>; |
devi@62 | 90 if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i) { |
devi@62 | 91 my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8"); |
devi@62 | 92 $text = $converter->convert($text); |
devi@62 | 93 } |
devi@62 | 94 close(F); |
devi@62 | 95 $diff{"text"}=$text; |
devi@23 | 96 |
devi@80 | 97 $diff{"path"}=$path; |
devi@80 | 98 $diff{"bind_to"}=""; |
devi@80 | 99 $diff{"time_range"}=-1; |
devi@80 | 100 $diff{"index"}=$i; |
devi@80 | 101 |
devi@62 | 102 $Diffs{$file} = \%diff; |
devi@62 | 103 $i++; |
devi@62 | 104 } |
devi@62 | 105 } |
devi@23 | 106 } |
devi@23 | 107 |
devi@23 | 108 |
devi@23 | 109 sub bind_diff |
devi@23 | 110 { |
devi@62 | 111 print "Trying to bind diff...\n"; |
devi@23 | 112 |
devi@62 | 113 my $cl = shift; |
devi@62 | 114 my $hour = $cl->{"hour"}; |
devi@62 | 115 my $min = $cl->{"min"}; |
devi@62 | 116 my $sec = $cl->{"sec"}; |
devi@23 | 117 |
devi@62 | 118 my $min_dt = 10000; |
devi@23 | 119 |
igor@119 | 120 if (defined($cl->{"diff"})) { |
igor@119 | 121 print STDERR "Command ".$cl->{time}." is already bound"; |
igor@119 | 122 return; |
igor@119 | 123 } |
igor@119 | 124 |
igor@119 | 125 # Загружаем новые diff-файлы |
igor@119 | 126 # Это нужно делать непосредственно перед привязкой, поскольку diff'ы могли образоваться только что |
igor@119 | 127 for my $lab_log (split (/\s+/, $Config{"diffs"} || $Config{"input"})) { |
igor@119 | 128 load_diff_files($lab_log); |
igor@119 | 129 } |
igor@119 | 130 |
igor@119 | 131 my $diff_to_bind; |
devi@62 | 132 for my $diff_key (keys %Diffs) { |
igor@119 | 133 my $diff = $Diffs{$diff_key}; |
igor@119 | 134 next if ($diff->{"local_session_id"} |
igor@119 | 135 && $cl->{"local_session_id"} |
igor@119 | 136 && ($cl->{"local_session_id"} ne $diff->{"local_session_id"})); |
devi@80 | 137 |
igor@119 | 138 next if ($diff->{"day"} && $cl->{"day"} && ($cl->{"day"} ne $diff->{"day"})); |
devi@80 | 139 |
igor@119 | 140 my $dt; |
igor@119 | 141 if (not $diff->{"time"}) { |
igor@119 | 142 print STDERR "diff time is 0"; |
igor@119 | 143 print STDERR join(" ", keys(%$diff)); |
igor@119 | 144 print STDERR $diff->{text}; |
igor@119 | 145 } |
igor@119 | 146 if (not $cl->{"time"}) { |
igor@119 | 147 print STDERR "cl time is 0"; |
igor@119 | 148 } |
igor@119 | 149 if ($diff->{"time"} && $cl->{"time"}) { |
igor@119 | 150 $dt = $diff->{"time"} - $cl->{"time"} |
igor@119 | 151 } |
igor@119 | 152 else { |
igor@119 | 153 $dt=($diff->{"hour"}-$hour)*3600 +($diff->{"min"}-$min)*60 + ($diff->{"sec"}-$sec); |
igor@119 | 154 } |
igor@119 | 155 if ($dt >=0 && $dt < $min_dt && !$diff->{"bind_to"}) { |
igor@119 | 156 $min_dt = $dt; |
igor@119 | 157 $diff_to_bind = $diff_key; |
igor@119 | 158 } |
igor@119 | 159 } |
igor@119 | 160 if ($diff_to_bind) { |
igor@119 | 161 print "Approppriate diff found: dt=$min_dt\n"; |
igor@119 | 162 $Diffs{$diff_to_bind}->{"bind_to"}=$cl; |
igor@119 | 163 $cl->{"diff"} = $diff_to_bind; |
igor@119 | 164 } |
igor@119 | 165 else { |
igor@119 | 166 print STDERR "Diff not found\n"; |
igor@119 | 167 print STDERR "cl{time}",$cl->{time},"\n"; |
devi@62 | 168 } |
devi@23 | 169 } |
devi@23 | 170 |
devi@23 | 171 |
devi@62 | 172 sub extract_commands_from_cline |
devi@23 | 173 # Разобрать командную строку $_[1] и возвратить хэш, содержащий |
devi@23 | 174 # номер первого появление команды в строке: |
devi@62 | 175 # команда => первая позиция |
devi@23 | 176 { |
devi@62 | 177 my $cline = $_[0]; |
devi@62 | 178 my @lists = split /\;/, $cline; |
devi@62 | 179 |
devi@62 | 180 |
devi@62 | 181 my @commands = (); |
devi@62 | 182 for my $list (@lists) { |
devi@62 | 183 push @commands, split /\|/, $list; |
devi@62 | 184 } |
devi@23 | 185 |
devi@62 | 186 my %commands; |
devi@62 | 187 my %files; |
devi@62 | 188 my $i=0; |
devi@62 | 189 for my $command (@commands) { |
devi@62 | 190 $command =~ /\s*(\S+)\s*(.*)/; |
devi@62 | 191 if ($1 && $1 eq "sudo" ) { |
devi@62 | 192 $commands{"$1"}=$i++; |
devi@62 | 193 $command =~ s/\s*sudo\s+//; |
devi@62 | 194 } |
devi@62 | 195 $command =~ /\s*(\S+)\s*(.*)/; |
devi@62 | 196 if ($1 && !defined $commands{"$1"}) { |
devi@62 | 197 $commands{"$1"}=$i++; |
devi@62 | 198 }; |
devi@62 | 199 } |
devi@62 | 200 return %commands; |
devi@23 | 201 } |
devi@23 | 202 |
devi@23 | 203 sub load_command_lines |
devi@23 | 204 { |
devi@62 | 205 my $lab_scripts_path = $_[0]; |
devi@62 | 206 my $lab_scripts_mask = $_[1]; |
devi@23 | 207 |
devi@62 | 208 my $cline_re_base = qq' |
devi@62 | 209 ( |
devi@62 | 210 (?:\\^?([0-9]*C?)) # exitcode |
devi@62 | 211 (?:_([0-9]+)_)? # uid |
devi@62 | 212 (?:_([0-9]+)_) # pid |
devi@62 | 213 (...?) # day |
devi@62 | 214 (.?.?) # lab |
devi@62 | 215 \\s # space separator |
devi@62 | 216 ([0-9][0-9]):([0-9][0-9]):([0-9][0-9]) # time |
devi@62 | 217 .\\[50D.\\[K # killing symbols |
devi@62 | 218 (.*?([\$\#]\\s?)) # prompt |
devi@62 | 219 (.*) # command line |
devi@62 | 220 ) |
devi@62 | 221 '; |
devi@62 | 222 my $cline_re = qr/$cline_re_base/sx; |
devi@62 | 223 my $cline_re2 = qr/$cline_re_base$/sx; |
devi@23 | 224 |
devi@74 | 225 my $cline_re_v2_base = qq' |
devi@74 | 226 ( |
devi@74 | 227 v2[\#] # version |
devi@74 | 228 ([0-9]+)[\#] # history line number |
devi@74 | 229 ([0-9]+)[\#] # exitcode |
devi@74 | 230 ([0-9]+)[\#] # uid |
devi@74 | 231 ([0-9]+)[\#] # pid |
devi@74 | 232 ([0-9]+)[\#] # time |
igor@114 | 233 (.*?)[\#] # pwd |
devi@74 | 234 .\\[1024D.\\[K # killing symbols |
devi@74 | 235 (.*?([\$\#]\\s?)) # prompt |
devi@74 | 236 (.*) # command line |
devi@74 | 237 ) |
devi@74 | 238 '; |
devi@74 | 239 |
devi@74 | 240 my $cline_re_v2 = qr/$cline_re_v2_base/sx; |
devi@74 | 241 my $cline_re2_v2 = qr/$cline_re_v2_base$/sx; |
devi@74 | 242 |
igor@114 | 243 my $cline_re_v3_base = qq' |
igor@114 | 244 ( |
igor@114 | 245 v3[\#] # version |
igor@114 | 246 .* |
igor@114 | 247 ) |
igor@114 | 248 '; |
igor@114 | 249 my $cline_re_v3 = qr/$cline_re_v3_base/sx; |
igor@114 | 250 |
igor@114 | 251 my $cline_re2_v3_base = qq' |
igor@114 | 252 ( |
igor@114 | 253 v3[\#] # version |
igor@114 | 254 ([0-9]+)[\#] # history line number |
igor@114 | 255 ([0-9]+)[\#] # exitcode |
igor@114 | 256 ([0-9]+)[\#] # uid |
igor@114 | 257 ([0-9]+)[\#] # pid |
igor@114 | 258 ([0-9]+)[\#] # time |
igor@114 | 259 (.*?)[\#] # pwd |
igor@119 | 260 (.*?)[\#] # nonce |
igor@114 | 261 (.*?([\$\#]\\s?)) # prompt |
igor@114 | 262 (.*) # command line |
igor@114 | 263 ) |
igor@114 | 264 '; |
igor@114 | 265 my $cline_re2_v3 = qr/$cline_re2_v3_base$/sx; |
igor@114 | 266 |
igor@114 | 267 |
igor@115 | 268 my %vt; # Хэш виртуальных терминалов. По одному на каждый сеанс |
devi@81 | 269 my $cline_vt = Term::VT102->new ( |
devi@81 | 270 'cols' => $Config{"terminal_width"}, |
devi@81 | 271 'rows' => $Config{"terminal_height"}); |
devi@23 | 272 |
devi@62 | 273 my $converter = Text::Iconv->new($Config{"encoding"}, "utf-8") |
devi@62 | 274 if ($Config{"encoding"} && $Config{"encoding"} !~ /^utf-8$/i); |
devi@62 | 275 |
devi@74 | 276 print "Parsing lab scripts...\n" if $Config{"verbose"} =~ /y/; |
devi@23 | 277 |
devi@62 | 278 my $file; |
devi@62 | 279 my $skip_info; |
devi@23 | 280 |
devi@62 | 281 my $commandlines_loaded =0; |
devi@62 | 282 my $commandlines_processed =0; |
devi@23 | 283 |
devi@62 | 284 my @lab_scripts = <$lab_scripts_path/$lab_scripts_mask>; |
devi@62 | 285 for $file (@lab_scripts){ |
devi@23 | 286 |
devi@62 | 287 # Пропускаем файл, если он не изменялся со времени нашего предудущего прохода |
devi@62 | 288 my $size = (stat($file))[7]; |
devi@62 | 289 next if ($Script_Files{$file} && $Script_Files{$file}->{size} && $Script_Files{$file}->{size} >= $size); |
devi@27 | 290 |
devi@27 | 291 |
devi@62 | 292 my $local_session_id; |
devi@62 | 293 # Начальное значение идентификатора текущего сеанса определяем из имени скрипта |
devi@62 | 294 # Впоследствии оно может быть уточнено |
devi@62 | 295 $file =~ m@.*/([^/]*)\.script$@; |
devi@62 | 296 $local_session_id = $1; |
devi@27 | 297 |
igor@115 | 298 if (not defined($vt{$local_session_id})) { |
igor@115 | 299 $vt{$local_session_id} = Term::VT102->new ( |
igor@115 | 300 'cols' => $Config{"terminal_width"}, |
igor@115 | 301 'rows' => $Config{"terminal_height"}); |
igor@115 | 302 } |
igor@115 | 303 |
devi@62 | 304 #Если файл только что появился, |
devi@62 | 305 #пытаемся найти и загрузить информацию о соответствующей ему сессии |
devi@62 | 306 if (!$Script_Files{$file}) { |
devi@62 | 307 my $session_file = $file; |
devi@62 | 308 $session_file =~ s/\.script/.info/; |
devi@62 | 309 if (open(SESSION, $session_file)) { |
devi@62 | 310 local $/; |
devi@62 | 311 my $data = <SESSION>; |
devi@62 | 312 close(SESSION); |
devi@27 | 313 |
devi@62 | 314 for my $session_data ($data =~ m@<session>(.*?)</session>@sg) { |
devi@62 | 315 my %session; |
devi@62 | 316 while ($session_data =~ m@<([^>]*?)>(.*?)</\1>@sg) { |
devi@62 | 317 $session{$1} = $2; |
devi@62 | 318 } |
devi@62 | 319 $local_session_id = $session{"local_session_id"} if $session{"local_session_id"}; |
devi@62 | 320 $Sessions{$local_session_id}=\%session; |
devi@62 | 321 } |
devi@25 | 322 |
devi@62 | 323 #Загруженную информацию сразу же отправляем в поток |
devi@62 | 324 print_session($Config{cache}, $local_session_id); |
devi@62 | 325 } |
devi@84 | 326 else { |
devi@84 | 327 die "can't open session file"; |
devi@84 | 328 } |
devi@62 | 329 } |
devi@25 | 330 |
devi@62 | 331 open (FILE, "$file"); |
devi@62 | 332 binmode FILE; |
devi@23 | 333 |
devi@62 | 334 # Переходим к тому месту, где мы окончили разбор |
devi@62 | 335 seek (FILE, $Script_Files{$file}->{tell}, 0) if $Script_Files{$file}->{tell}; |
devi@62 | 336 $Script_Files{$file}->{size} = $size; |
devi@62 | 337 $Script_Files{$file}->{tell} = 0 unless $Script_Files{$file}->{tell}; |
devi@32 | 338 |
devi@62 | 339 $file =~ m@.*/(.*?)-.*@; |
devi@23 | 340 |
devi@83 | 341 print "\n+- processing file $file\n| " |
devi@83 | 342 if $Config{"verbose"} =~/y/; |
devi@74 | 343 |
devi@62 | 344 my $tty = $1; |
devi@62 | 345 my $first_pass = 1; |
devi@62 | 346 my %cl; |
devi@62 | 347 my $last_output_length=0; |
devi@62 | 348 while (<FILE>) { |
devi@62 | 349 $commandlines_processed++; |
devi@23 | 350 |
devi@62 | 351 next if s/^Script started on.*?\n//s; |
devi@23 | 352 |
devi@62 | 353 if (/[0-9][0-9]:[0-9][0-9]:[0-9][0-9].\[[0-9][0-9]D.\[K/ && m/$cline_re/) { |
devi@62 | 354 s/.*\x0d(?!\x0a)//; |
devi@62 | 355 m/$cline_re2/gs; |
devi@23 | 356 |
devi@62 | 357 $commandlines_loaded++; |
devi@62 | 358 $last_output_length=0; |
devi@23 | 359 |
devi@62 | 360 # Previous command |
devi@62 | 361 my %last_cl = %cl; |
igor@119 | 362 my $this_line = $1; |
devi@62 | 363 my $err = $2 || ""; |
devi@23 | 364 |
devi@62 | 365 $cl{"local_session_id"} = $local_session_id; |
devi@62 | 366 # Parse new command |
devi@62 | 367 $cl{"uid"} = $3; |
devi@74 | 368 #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0 |
devi@62 | 369 $cl{"pid"} = $4; |
devi@62 | 370 $cl{"day"} = $5; |
devi@62 | 371 $cl{"lab"} = $6; |
devi@62 | 372 $cl{"hour"} = $7; |
devi@62 | 373 $cl{"min"} = $8; |
devi@62 | 374 $cl{"sec"} = $9; |
devi@72 | 375 #$cl{"fullprompt"} = $10; |
devi@62 | 376 $cl{"prompt"} = $11; |
devi@62 | 377 $cl{"raw_cline"} = $12; |
devi@23 | 378 |
devi@62 | 379 { |
devi@62 | 380 use bytes; |
igor@119 | 381 $cl{"raw_start"} = tell (FILE) - length($this_line); |
devi@62 | 382 $cl{"raw_output_start"} = tell FILE; |
devi@62 | 383 } |
devi@62 | 384 $cl{"raw_file"} = $file; |
devi@23 | 385 |
devi@62 | 386 $cl{"err"} = 0; |
devi@62 | 387 $cl{"output"} = ""; |
devi@62 | 388 $cl{"tty"} = $tty; |
devi@23 | 389 |
devi@62 | 390 $cline_vt->process($cl{"raw_cline"}."\n"); |
devi@62 | 391 $cl{"cline"} = $cline_vt->row_plaintext (1); |
devi@62 | 392 $cl{"cline"} =~ s/\s*$//; |
igor@119 | 393 $cl{"cline"} =~ s/.*?[\#\$]\s*//; |
devi@62 | 394 $cline_vt->reset(); |
devi@23 | 395 |
devi@62 | 396 my %commands = extract_commands_from_cline($cl{"cline"}); |
devi@74 | 397 #$cl{"euid"}=0 if defined $commands{"sudo"}; |
devi@62 | 398 my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; |
devi@62 | 399 $cl{"last_command"} = $comms[$#comms] || ""; |
devi@23 | 400 |
devi@62 | 401 if ( |
devi@62 | 402 $Config{"suppress_editors"} =~ /^y/i |
devi@62 | 403 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) |
devi@62 | 404 || $Config{"suppress_pagers"} =~ /^y/i |
devi@62 | 405 && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) |
devi@62 | 406 || $Config{"suppress_terminal"}=~ /^y/i |
devi@62 | 407 && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}}) |
devi@62 | 408 ) { |
devi@62 | 409 $cl{"suppress_output"} = "1"; |
devi@62 | 410 } |
devi@62 | 411 else { |
devi@62 | 412 $cl{"suppress_output"} = "0"; |
devi@62 | 413 } |
devi@62 | 414 $skip_info = 0; |
devi@23 | 415 |
devi@23 | 416 |
devi@62 | 417 print " ",$cl{"last_command"}; |
devi@23 | 418 |
igor@119 | 419 if (grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { |
igor@119 | 420 bind_diff(\%last_cl); |
igor@119 | 421 } |
igor@119 | 422 |
devi@62 | 423 # Processing previous command line |
igor@119 | 424 #if ($first_pass) { |
igor@119 | 425 # $first_pass = 0; |
igor@119 | 426 # next; |
igor@119 | 427 #} |
devi@23 | 428 |
devi@62 | 429 # Error code |
devi@62 | 430 $last_cl{"raw_end"} = $cl{"raw_start"}; |
devi@62 | 431 $last_cl{"err"}=$err; |
devi@62 | 432 $last_cl{"err"}=130 if $err eq "^C"; |
devi@23 | 433 |
devi@23 | 434 |
devi@62 | 435 # Output |
devi@62 | 436 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { |
devi@62 | 437 for (my $i=0; $i<$Config{"terminal_height"}; $i++) { |
igor@115 | 438 my $line= $vt{$local_session_id}->row_plaintext($i); |
devi@62 | 439 next if !defined ($line) ; #|| $line =~ /^\s*$/; |
devi@62 | 440 $line =~ s/\s*$//; |
devi@62 | 441 $line .= "\n" unless $line =~ /^\s*$/; |
devi@62 | 442 $last_cl{"output"} .= $line; |
devi@62 | 443 } |
devi@62 | 444 } |
devi@62 | 445 else { |
devi@62 | 446 $last_cl{"output"}= ""; |
devi@62 | 447 } |
devi@23 | 448 |
igor@115 | 449 $vt{$local_session_id}->reset(); |
devi@23 | 450 |
devi@23 | 451 |
devi@62 | 452 # Save |
devi@62 | 453 if (!$Config{"lab"} || $cl{"lab"} eq $Config{"lab"}) { |
devi@62 | 454 # Changing encoding |
devi@62 | 455 for (keys %last_cl) { |
devi@62 | 456 next if /raw/; |
devi@62 | 457 $last_cl{$_} = $converter->convert($last_cl{$_}) |
devi@62 | 458 if ($Config{"encoding"} && |
devi@62 | 459 $Config{"encoding"} !~ /^utf-8$/i); |
devi@62 | 460 } |
devi@62 | 461 push @Command_Lines, \%last_cl; |
devi@23 | 462 |
devi@62 | 463 # Сохранение позиции в файле, до которой выполнен |
devi@62 | 464 # успешный разбор |
devi@62 | 465 $Script_Files{$file}->{tell} = $last_cl{raw_end}; |
devi@62 | 466 } |
devi@62 | 467 next; |
devi@62 | 468 } |
devi@74 | 469 |
igor@114 | 470 elsif (m/$cline_re_v2/ || m/$cline_re_v3/) { |
devi@74 | 471 # Разбираем командную строку версии 2 |
igor@119 | 472 my $before=$_; |
igor@119 | 473 s/.*\x0d(?!\x0a)//; |
devi@74 | 474 |
igor@114 | 475 my $re; |
igor@114 | 476 if (m/$cline_re_v2/) { |
igor@114 | 477 $re=$cline_re2_v2; |
igor@114 | 478 } |
igor@114 | 479 else { |
igor@114 | 480 s/.\[1K.\[10D//gs; |
igor@114 | 481 $re=$cline_re2_v3; |
igor@119 | 482 print STDERR "... $_ ...\n"; |
igor@114 | 483 } |
devi@74 | 484 |
devi@74 | 485 $commandlines_loaded++; |
devi@74 | 486 $last_output_length=0; |
devi@74 | 487 |
devi@74 | 488 # Previous command |
devi@74 | 489 my %last_cl = %cl; |
devi@74 | 490 |
devi@74 | 491 $cl{"local_session_id"} = $local_session_id; |
devi@74 | 492 # Parse new command |
igor@119 | 493 my $this_line = $1; |
devi@74 | 494 $cl{"history"} = $2; |
devi@74 | 495 my $err = $3; |
devi@74 | 496 $cl{"uid"} = $4; |
devi@74 | 497 #$cl{"euid"} = $cl{"uid"}; # Если в команде обнаружится sudo, euid поменяем на 0 |
devi@74 | 498 $cl{"pid"} = $5; |
devi@74 | 499 $cl{"time"} = $6; |
devi@74 | 500 $cl{"pwd"} = $7; |
igor@119 | 501 $cl{"nonce"} = $8; |
devi@74 | 502 #$cl{"fullprompt"} = $8; |
igor@119 | 503 $cl{"prompt"} = $10; |
igor@119 | 504 #$cl{"raw_cline"}= $10; |
igor@119 | 505 $cl{"raw_cline"}= $before; |
devi@74 | 506 |
devi@74 | 507 { |
devi@74 | 508 use bytes; |
igor@119 | 509 $cl{"raw_start"} = tell (FILE) - length($before); |
devi@74 | 510 $cl{"raw_output_start"} = tell FILE; |
devi@74 | 511 } |
devi@74 | 512 $cl{"raw_file"} = $file; |
devi@74 | 513 |
devi@74 | 514 $cl{"err"} = 0; |
devi@74 | 515 $cl{"output"} = ""; |
devi@74 | 516 #$cl{"tty"} = $tty; |
devi@74 | 517 |
devi@74 | 518 $cline_vt->process($cl{"raw_cline"}."\n"); |
devi@74 | 519 $cl{"cline"} = $cline_vt->row_plaintext (1); |
devi@74 | 520 $cl{"cline"} =~ s/\s*$//; |
igor@119 | 521 $cl{"cline"} =~ s/.*?[\#\$]\s*//; |
devi@74 | 522 $cline_vt->reset(); |
igor@119 | 523 print STDERR "cline=".$cl{"cline"}."<<\n"; |
devi@74 | 524 |
devi@74 | 525 my %commands = extract_commands_from_cline($cl{"cline"}); |
devi@74 | 526 #$cl{"euid"} = 0 if defined $commands{"sudo"}; |
devi@74 | 527 my @comms = sort { $commands{$a} cmp $commands{$b} } keys %commands; |
devi@74 | 528 $cl{"last_command"} |
devi@74 | 529 = $comms[$#comms] || ""; |
devi@74 | 530 |
igor@119 | 531 print STDERR "last_command=".$cl{"last_command"}."<<\n"; |
igor@119 | 532 |
devi@74 | 533 if ( |
devi@74 | 534 $Config{"suppress_editors"} =~ /^y/i |
devi@74 | 535 && grep ($_ eq $cl{"last_command"}, @{$Config{"editors"}}) |
devi@74 | 536 || $Config{"suppress_pagers"} =~ /^y/i |
devi@74 | 537 && grep ($_ eq $cl{"last_command"}, @{$Config{"pagers"}}) |
devi@74 | 538 || $Config{"suppress_terminal"}=~ /^y/i |
devi@74 | 539 && grep ($_ eq $cl{"last_command"}, @{$Config{"terminal"}}) |
devi@74 | 540 ) { |
devi@74 | 541 $cl{"suppress_output"} = "1"; |
devi@74 | 542 } |
devi@74 | 543 else { |
devi@74 | 544 $cl{"suppress_output"} = "0"; |
devi@74 | 545 } |
devi@74 | 546 $skip_info = 0; |
devi@74 | 547 |
devi@74 | 548 if ($Config{verbose} =~ /y/i) { |
devi@83 | 549 print "\n| " if $commandlines_loaded % 5 == 1; |
devi@74 | 550 print " ",$cl{"last_command"}; |
devi@74 | 551 } |
devi@74 | 552 |
igor@119 | 553 if (defined($last_cl{time}) |
igor@119 | 554 && grep ($_ eq $last_cl{"last_command"}, @{$Config{"editors"}})) { |
igor@119 | 555 bind_diff(\%last_cl); |
devi@74 | 556 } |
devi@74 | 557 |
devi@74 | 558 # Error code |
devi@74 | 559 $last_cl{"err"}=$err; |
devi@74 | 560 $last_cl{"raw_end"} = $cl{"raw_start"}; |
devi@74 | 561 |
devi@74 | 562 # Output |
devi@74 | 563 if (!$last_cl{"suppress_output"} || $last_cl{"err"}) { |
devi@74 | 564 for (my $i=0; $i<$Config{"terminal_height"}; $i++) { |
igor@115 | 565 my $line= $vt{$local_session_id}->row_plaintext($i); |
devi@74 | 566 next if !defined ($line) ; #|| $line =~ /^\s*$/; |
devi@74 | 567 $line =~ s/\s*$//; |
devi@74 | 568 $line .= "\n" unless $line =~ /^\s*$/; |
devi@74 | 569 $last_cl{"output"} .= $line; |
devi@74 | 570 } |
devi@74 | 571 } |
devi@74 | 572 else { |
devi@74 | 573 $last_cl{"output"}= ""; |
devi@74 | 574 } |
devi@74 | 575 |
igor@115 | 576 $vt{$local_session_id}->reset(); |
devi@74 | 577 |
devi@74 | 578 |
devi@74 | 579 # Changing encoding |
devi@74 | 580 for (keys %last_cl) { |
devi@74 | 581 next if /raw/; |
devi@74 | 582 if ($Config{"encoding"} && |
devi@74 | 583 $Config{"encoding"} !~ /^utf-8$/i) { |
devi@74 | 584 $last_cl{$_} = $converter->convert($last_cl{$_}) |
devi@74 | 585 } |
devi@74 | 586 } |
igor@119 | 587 if (defined($last_cl{time})) { |
igor@119 | 588 print STDERR "push id=".$last_cl{time}."\n"; |
igor@119 | 589 push @Command_Lines, \%last_cl; |
igor@119 | 590 # Сохранение позиции в файле, до которой выполнен |
igor@119 | 591 # успешный разбор |
igor@119 | 592 $Script_Files{$file}->{tell} = $last_cl{raw_end}; |
igor@119 | 593 } |
devi@74 | 594 next; |
devi@74 | 595 } |
devi@74 | 596 |
devi@74 | 597 # Иначе, это строка вывода |
devi@74 | 598 |
devi@62 | 599 $last_output_length+=length($_); |
devi@62 | 600 #if (!$cl{"suppress_output"} || $last_output_length < 5000) { |
devi@62 | 601 if ($last_output_length < 50000) { |
igor@115 | 602 $vt{$local_session_id}->process("$_"."\n") |
devi@62 | 603 } |
devi@62 | 604 else |
devi@62 | 605 { |
igor@119 | 606 if (!$skip_info && defined($cl{last_command})) { |
devi@62 | 607 print "($cl{last_command})"; |
devi@62 | 608 $skip_info = 1; |
devi@62 | 609 } |
devi@62 | 610 } |
devi@62 | 611 } |
devi@62 | 612 close(FILE); |
devi@23 | 613 |
devi@62 | 614 } |
devi@62 | 615 if ($Config{"verbose"} =~ /y/) { |
devi@74 | 616 print "\n`- finished.\n" ; |
devi@62 | 617 print "Lines loaded: $commandlines_processed\n"; |
devi@62 | 618 print "Command lines: $commandlines_loaded\n"; |
devi@62 | 619 } |
devi@23 | 620 } |
devi@23 | 621 |
devi@23 | 622 |
devi@23 | 623 |
devi@62 | 624 |
devi@62 | 625 sub sort_command_lines |
devi@62 | 626 { |
devi@74 | 627 print "Sorting command lines..." if $Config{"verbose"} =~ /y/; |
devi@62 | 628 |
devi@62 | 629 # Sort Command_Lines |
devi@62 | 630 # Write Command_Lines to Command_Lines_Index |
devi@62 | 631 |
devi@62 | 632 my @index; |
devi@62 | 633 for (my $i=0;$i<=$#Command_Lines;$i++) { |
devi@62 | 634 $index[$i]=$i; |
devi@62 | 635 } |
devi@62 | 636 |
devi@62 | 637 @Command_Lines_Index = sort { |
devi@86 | 638 defined($Command_Lines[$index[$a]]->{"time"}) |
devi@86 | 639 && defined($Command_Lines[$index[$b]]->{"time"}) |
devi@81 | 640 ? $Command_Lines[$index[$a]]->{"time"} <=> $Command_Lines[$index[$b]]->{"time"} |
devi@84 | 641 : defined($Command_Lines[$index[$a]]->{"day"}) |
devi@86 | 642 && defined($Command_Lines[$index[$b]]->{"day"}) |
devi@86 | 643 && defined($Command_Lines[$index[$a]]->{"hour"}) |
devi@86 | 644 && defined($Command_Lines[$index[$b]]->{"hour"}) |
devi@86 | 645 && defined($Command_Lines[$index[$a]]->{"min"}) |
devi@86 | 646 && defined($Command_Lines[$index[$b]]->{"min"}) |
devi@86 | 647 && defined($Command_Lines[$index[$a]]->{"sec"}) |
devi@86 | 648 && defined($Command_Lines[$index[$b]]->{"sec"}) |
devi@86 | 649 ? $Command_Lines[$index[$a]]->{"day"} cmp $Command_Lines[$index[$b]]->{"day"} |
devi@86 | 650 || $Command_Lines[$index[$a]]->{"hour"} <=> $Command_Lines[$index[$b]]->{"hour"} |
devi@86 | 651 || $Command_Lines[$index[$a]]->{"min"} <=> $Command_Lines[$index[$b]]->{"min"} |
devi@86 | 652 || $Command_Lines[$index[$a]]->{"sec"} <=> $Command_Lines[$index[$b]]->{"sec"} |
devi@86 | 653 : 0 |
devi@62 | 654 } @index; |
devi@62 | 655 |
devi@74 | 656 print "finished\n" if $Config{"verbose"} =~ /y/; |
devi@62 | 657 |
devi@62 | 658 } |
devi@62 | 659 |
devi@23 | 660 sub printq |
devi@23 | 661 { |
devi@62 | 662 my $TO = shift; |
devi@62 | 663 my $text = join "", @_; |
devi@62 | 664 $text =~ s/&/&/g; |
devi@62 | 665 $text =~ s/</</g; |
devi@62 | 666 $text =~ s/>/>/g; |
devi@62 | 667 print $TO $text; |
devi@23 | 668 } |
devi@23 | 669 |
devi@23 | 670 |
devi@23 | 671 =cut |
devi@23 | 672 Вывести результат обработки журнала. |
devi@23 | 673 =cut |
devi@23 | 674 |
devi@23 | 675 sub print_command_lines |
devi@23 | 676 { |
devi@62 | 677 my $output_filename=$_[0]; |
devi@85 | 678 open(OUT, ">>", $output_filename) |
devi@62 | 679 or die "Can't open $output_filename for writing\n"; |
devi@23 | 680 |
devi@23 | 681 |
devi@62 | 682 my $cl; |
devi@62 | 683 my $in_range=0; |
devi@62 | 684 for my $i (@Command_Lines_Index) { |
devi@62 | 685 $cl = $Command_Lines[$i]; |
devi@23 | 686 |
devi@62 | 687 if ($Config{"from"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"from"}/) { |
devi@62 | 688 $in_range=1; |
devi@62 | 689 next; |
devi@62 | 690 } |
devi@62 | 691 if ($Config{"to"} && $cl->{"cline"} =~ /$Config{"signature"}\s*$Config{"to"}/) { |
devi@62 | 692 $in_range=0; |
devi@62 | 693 next; |
devi@62 | 694 } |
devi@62 | 695 next if ($Config{"from"} && $Config{"to"} && !$in_range) |
devi@62 | 696 || |
devi@62 | 697 ($Config{"skip_empty"} =~ /^y/i && $cl->{"cline"} =~ /^\s*$/ ) |
devi@62 | 698 || |
devi@62 | 699 ($Config{"skip_wrong"} =~ /^y/i && $cl->{"err"} != 0) |
devi@62 | 700 || |
devi@62 | 701 ($Config{"skip_interrupted"} =~ /^y/i && $cl->{"err"} == 130); |
devi@62 | 702 |
devi@62 | 703 # Вырезаем из вывода только нужное количество строк |
devi@23 | 704 |
devi@62 | 705 my $output=""; |
devi@69 | 706 |
devi@73 | 707 if (!grep ($_ eq $cl->{"last_command"}, @{$Config{"full_output_commands"}}) |
devi@69 | 708 && ($Config{"head_lines"} |
devi@73 | 709 || $Config{"tail_lines"})) { |
devi@62 | 710 # Partialy output |
devi@62 | 711 my @lines = split '\n', $cl->{"output"}; |
devi@62 | 712 # head |
devi@62 | 713 my $mark=1; |
devi@62 | 714 for (my $i=0; $i<= $#lines && $i < $Config{"cache_head_lines"}; $i++) { |
devi@62 | 715 $output .= $lines[$i]."\n"; |
devi@62 | 716 } |
devi@62 | 717 # tail |
devi@62 | 718 my $start=$#lines-$Config{"cache_tail_lines"}+1; |
devi@62 | 719 if ($start < 0) { |
devi@62 | 720 $start=0; |
devi@62 | 721 $mark=0; |
devi@62 | 722 } |
devi@62 | 723 if ($start < $Config{"cache_head_lines"}) { |
devi@62 | 724 $start=$Config{"cache_head_lines"}; |
devi@62 | 725 $mark=0; |
devi@62 | 726 } |
devi@62 | 727 $output .= $Config{"skip_text"}."\n" if $mark; |
devi@73 | 728 for ($i=$start; $i<= $#lines; $i++) { |
devi@62 | 729 $output .= $lines[$i]."\n"; |
devi@62 | 730 } |
devi@62 | 731 } |
devi@62 | 732 else { |
devi@62 | 733 # Full output |
devi@62 | 734 $output .= $cl->{"output"}; |
devi@62 | 735 } |
devi@23 | 736 |
devi@62 | 737 # Совместимость с labmaker |
devi@23 | 738 |
devi@62 | 739 # Переводим в секунды Эпохи |
devi@62 | 740 # В labmaker'е данные хранились в неудобной форме: hour, min, sec, day of year |
devi@62 | 741 # Информация о годе отсутствовала |
devi@62 | 742 # Её можно внести: |
devi@62 | 743 # Декабрь 2004 год; остальные -- 2005 год. |
devi@23 | 744 |
devi@62 | 745 my $year = 2005; |
devi@62 | 746 #$year = 2004 if ( $cl->{day} > 330 ); |
devi@62 | 747 $year = $Config{year} if $Config{year}; |
devi@62 | 748 # timelocal( $sec, $min, $hour, $mday,$mon,$year); |
devi@74 | 749 $cl->{time} ||= timelocal_nocheck($cl->{sec},$cl->{min},$cl->{hour},$cl->{day},0,$year); |
devi@23 | 750 |
devi@23 | 751 |
devi@62 | 752 # Начинаем вывод команды |
devi@62 | 753 print OUT "<command>\n"; |
devi@98 | 754 print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"}; |
devi@62 | 755 for my $element (qw( |
devi@62 | 756 local_session_id |
devi@74 | 757 history |
devi@74 | 758 uid |
devi@74 | 759 pid |
devi@62 | 760 time |
devi@74 | 761 pwd |
devi@62 | 762 raw_start |
devi@62 | 763 raw_output_start |
devi@62 | 764 raw_end |
devi@62 | 765 raw_file |
devi@62 | 766 tty |
devi@62 | 767 err |
devi@62 | 768 last_command |
devi@74 | 769 history |
igor@119 | 770 nonce |
devi@62 | 771 )) { |
devi@65 | 772 next unless defined($cl->{"$element"}); |
devi@62 | 773 print OUT "<$element>".$cl->{$element}."</$element>\n"; |
devi@62 | 774 } |
devi@62 | 775 for my $element (qw( |
devi@62 | 776 prompt |
devi@62 | 777 cline |
devi@62 | 778 )) { |
devi@65 | 779 next unless defined($cl->{"$element"}); |
devi@62 | 780 print OUT "<$element>"; |
devi@62 | 781 printq(\*OUT,$cl->{"$element"}); |
devi@62 | 782 print OUT "</$element>\n"; |
devi@62 | 783 } |
devi@72 | 784 #note |
devi@72 | 785 #note_title |
devi@62 | 786 print OUT "<output>"; |
devi@62 | 787 printq(\*OUT,$output); |
devi@62 | 788 print OUT "</output>\n"; |
devi@62 | 789 if ($cl->{"diff"}) { |
devi@62 | 790 print OUT "<diff>"; |
devi@62 | 791 printq(\*OUT,${$Diffs{$cl->{"diff"}}}{"text"}); |
devi@62 | 792 print OUT "</diff>\n"; |
devi@62 | 793 } |
devi@62 | 794 print OUT "</command>\n"; |
devi@23 | 795 |
devi@62 | 796 } |
devi@23 | 797 |
devi@62 | 798 close(OUT); |
devi@27 | 799 } |
devi@27 | 800 |
devi@27 | 801 sub print_session |
devi@27 | 802 { |
devi@62 | 803 my $output_filename = $_[0]; |
devi@62 | 804 my $local_session_id = $_[1]; |
devi@62 | 805 return if not defined($Sessions{$local_session_id}); |
devi@27 | 806 |
devi@84 | 807 print "printing session info. session id = ".$local_session_id."\n" |
devi@84 | 808 if $Config{verbose} =~ /y/; |
devi@84 | 809 |
devi@62 | 810 open(OUT, ">>", $output_filename) |
devi@62 | 811 or die "Can't open $output_filename for writing\n"; |
devi@62 | 812 print OUT "<session>\n"; |
devi@98 | 813 print OUT "<l3cd>$Config{l3cd}</l3cd>\n" if $Config{"l3cd"}; |
devi@62 | 814 my %session = %{$Sessions{$local_session_id}}; |
devi@62 | 815 for my $key (keys %session) { |
devi@84 | 816 print OUT "<$key>".$session{$key}."</$key>\n"; |
devi@84 | 817 print " ".$key,"\n"; |
devi@62 | 818 } |
devi@62 | 819 print OUT "</session>\n"; |
devi@62 | 820 close(OUT); |
devi@27 | 821 } |
devi@27 | 822 |
devi@27 | 823 sub send_cache |
devi@27 | 824 { |
devi@62 | 825 # Если в кэше что-то накопилось, |
devi@62 | 826 # попытаемся отправить это на сервер |
devi@62 | 827 # |
devi@62 | 828 my $cache_was_sent=0; |
devi@62 | 829 |
devi@62 | 830 if (open(CACHE, $Config{cache})) { |
devi@62 | 831 local $/; |
devi@62 | 832 my $cache = <CACHE>; |
devi@62 | 833 close(CACHE); |
devi@27 | 834 |
devi@62 | 835 my $socket = IO::Socket::INET->new( |
devi@62 | 836 PeerAddr => $Config{backend_address}, |
devi@62 | 837 PeerPort => $Config{backend_port}, |
devi@62 | 838 proto => "tcp", |
devi@62 | 839 Type => SOCK_STREAM |
devi@62 | 840 ); |
devi@27 | 841 |
devi@62 | 842 if ($socket) { |
devi@62 | 843 print $socket $cache; |
devi@62 | 844 close($socket); |
devi@62 | 845 $cache_was_sent = 1; |
devi@62 | 846 } |
devi@62 | 847 } |
devi@62 | 848 return $cache_was_sent; |
devi@23 | 849 } |
devi@23 | 850 |
devi@25 | 851 sub save_cache_stat |
devi@25 | 852 { |
devi@62 | 853 open (CACHE, ">$Config{cache_stat}"); |
devi@62 | 854 for my $f (keys %Script_Files) { |
devi@62 | 855 print CACHE "$f\t",$Script_Files{$f}->{size},"\t",$Script_Files{$f}->{tell},"\n"; |
devi@62 | 856 } |
devi@62 | 857 close(CACHE); |
devi@25 | 858 } |
devi@25 | 859 |
devi@25 | 860 sub load_cache_stat |
devi@25 | 861 { |
devi@62 | 862 if (open (CACHE, "$Config{cache_stat}")) { |
devi@62 | 863 while(<CACHE>) { |
devi@62 | 864 chomp; |
devi@62 | 865 my ($f, $size, $tell) = split /\t/; |
devi@62 | 866 $Script_Files{$f}->{size} = $size; |
devi@62 | 867 $Script_Files{$f}->{tell} = $tell; |
devi@62 | 868 } |
devi@62 | 869 close(CACHE); |
devi@62 | 870 }; |
devi@25 | 871 } |
devi@23 | 872 |
devi@23 | 873 |
devi@23 | 874 main(); |
devi@23 | 875 |
devi@25 | 876 sub process_was_killed |
devi@25 | 877 { |
devi@62 | 878 $Killed = 1; |
devi@25 | 879 } |
devi@25 | 880 |
devi@98 | 881 sub reload |
devi@98 | 882 { |
devi@98 | 883 init_config; |
devi@98 | 884 } |
devi@98 | 885 |
devi@23 | 886 sub main |
devi@23 | 887 { |
devi@23 | 888 |
devi@62 | 889 $| = 1; |
devi@23 | 890 |
devi@62 | 891 init_variables(); |
devi@62 | 892 init_config(); |
devi@23 | 893 |
devi@27 | 894 |
devi@62 | 895 if ($Config{"mode"} ne "daemon") { |
devi@27 | 896 |
devi@74 | 897 # В нормальном режиме работы нужно |
devi@74 | 898 # считать скрипты, обработать их и записать |
devi@74 | 899 # результат выполнения в результирующий файл. |
devi@74 | 900 # После этого завершить работу. |
devi@74 | 901 |
devi@85 | 902 # Очистим кэш-файл, если он существовал |
devi@85 | 903 if (open (CACHE, ">", $Config{"cache"})) { |
devi@85 | 904 close(CACHE); |
devi@85 | 905 }; |
devi@62 | 906 load_command_lines($Config{"input"}, $Config{"input_mask"}); |
devi@67 | 907 sort_command_lines; |
devi@62 | 908 #process_command_lines; |
devi@62 | 909 print_command_lines($Config{"cache"}); |
devi@62 | 910 } |
devi@62 | 911 else { |
devi@62 | 912 if (open(PIDFILE, $Config{agent_pidfile})) { |
devi@62 | 913 my $pid = <PIDFILE>; |
devi@62 | 914 close(PIDFILE); |
devi@62 | 915 if ($^O eq 'linux' && $pid &&(! -e "/proc/$pid" || !`grep $Config{"l3-agent"} /proc/$pid/cmdline && grep "uid:.*\b$<\b" /proc/$pid/status`)) { |
devi@62 | 916 print "Removing stale pidfile\n"; |
devi@62 | 917 unlink $Config{agent_pidfile} |
devi@62 | 918 or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!"; |
devi@62 | 919 } |
devi@108 | 920 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`) { |
devi@62 | 921 print "Removing stale pidfile\n"; |
devi@62 | 922 unlink $Config{agent_pidfile} |
devi@62 | 923 or die "Can't remove stale pidfile ". $Config{agent_pidfile}. " : $!"; |
devi@62 | 924 } |
devi@62 | 925 elsif ($^O eq 'linux' || $^O eq 'freebsd' ) { |
devi@62 | 926 print "l3-agent is already running: pid=$pid; pidfile=$Config{agent_pidfile}\n"; |
devi@62 | 927 exit(0); |
devi@62 | 928 } |
devi@62 | 929 else { |
devi@62 | 930 print "Unknown operating system"; |
devi@62 | 931 exit(0); |
devi@62 | 932 } |
devi@62 | 933 } |
devi@62 | 934 if ($Config{detach} =~ /^y/i) { |
devi@62 | 935 #$Config{verbose} = "no"; |
devi@62 | 936 my $pid = fork; |
devi@62 | 937 exit if $pid; |
devi@62 | 938 die "Couldn't fork: $!" unless defined ($pid); |
devi@27 | 939 |
devi@62 | 940 open(PIDFILE, ">", $Config{agent_pidfile}) |
devi@62 | 941 or die "Can't open pidfile ". $Config{agent_pidfile}. " for wrting: $!"; |
devi@62 | 942 print PIDFILE $$; |
devi@62 | 943 close(PIDFILE); |
devi@27 | 944 |
devi@62 | 945 for my $handle (*STDIN, *STDOUT, *STDERR) { |
devi@62 | 946 open ($handle, "+<", "/dev/null") |
devi@62 | 947 or die "can't reopen $handle to /dev/null: $!" |
devi@62 | 948 } |
devi@27 | 949 |
devi@62 | 950 POSIX::setsid() |
devi@62 | 951 or die "Can't start a new session: $!"; |
devi@27 | 952 |
devi@62 | 953 $0 = $Config{"l3-agent"}; |
devi@62 | 954 |
devi@98 | 955 $SIG{INT} = $SIG{TERM} = \&process_was_killed; |
devi@98 | 956 $SIG{HUP} = \&reload; |
devi@98 | 957 |
devi@62 | 958 } |
devi@62 | 959 while (not $Killed) { |
devi@62 | 960 @Command_Lines = (); |
devi@62 | 961 @Command_Lines_Index = (); |
devi@62 | 962 load_cache_stat(); |
devi@62 | 963 load_command_lines($Config{"input"}, $Config{"input_mask"}); |
devi@62 | 964 if (@Command_Lines) { |
devi@74 | 965 sort_command_lines; |
devi@65 | 966 #process_command_lines; |
devi@62 | 967 print_command_lines($Config{"cache"}); |
devi@62 | 968 } |
devi@62 | 969 save_cache_stat(); |
devi@62 | 970 if (-e $Config{cache} && (stat($Config{cache}))[7]) { |
devi@62 | 971 send_cache() && unlink($Config{cache}); |
devi@62 | 972 } |
devi@62 | 973 sleep($Config{"daemon_sleep_interval"} || 1); |
devi@62 | 974 } |
devi@62 | 975 |
devi@62 | 976 unlink $Config{agent_pidfile}; |
devi@62 | 977 } |
devi@23 | 978 |
devi@23 | 979 } |
devi@23 | 980 |
devi@23 | 981 sub init_variables |
devi@23 | 982 { |
devi@23 | 983 } |