| 1 | package Yandex::Tools; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use vars qw($VERSION @ISA @EXPORT @EXPORT_OK $AUTOLOAD); | ||
| 7 | |||
| 8 | require Exporter; | ||
| 9 | |||
| 10 | $VERSION = '0.16'; | ||
| 11 | @ISA = qw(Exporter); | ||
| 12 | @EXPORT_OK = qw ( | ||
| 13 | can_log | ||
| 14 | do_log | ||
| 15 | get_log_filename | ||
| 16 | get_log_options | ||
| 17 | set_log_filename | ||
| 18 | set_log_options | ||
| 19 | |||
| 20 | can_write | ||
| 21 | fileinfo_struct | ||
| 22 | read_dir | ||
| 23 | read_file_array | ||
| 24 | read_file_option | ||
| 25 | read_file_scalar | ||
| 26 | write_file_option | ||
| 27 | write_file_scalar | ||
| 28 | |||
| 29 | array_clear_dupes | ||
| 30 | canonize_delimiters | ||
| 31 | is_ascii | ||
| 32 | is_digital | ||
| 33 | safe_string | ||
| 34 | |||
| 35 | matches_with_one_of_regexps | ||
| 36 | |||
| 37 | get_callstack | ||
| 38 | |||
| 39 | daemonize | ||
| 40 | run_forked | ||
| 41 | |||
| 42 | send_mail | ||
| 43 | ); | ||
| 44 | |||
| 45 | use POSIX; | ||
| 46 | use utf8; | ||
| 47 | use Data::Dumper; | ||
| 48 | use File::Path; | ||
| 49 | use IPC::Open3; | ||
| 50 | use IO::Select; | ||
| 51 | use IO::Handle; # autoflush | ||
| 52 | use FileHandle; | ||
| 53 | use Time::HiRes qw/usleep clock_gettime CLOCK_MONOTONIC/; | ||
| 54 | use Socket; # socketpair | ||
| 55 | use File::Basename; # basename | ||
| 56 | use File::Path; # mkpath | ||
| 57 | use File::Copy; | ||
| 58 | use Carp; | ||
| 59 | use Storable; | ||
| 60 | use IPC::Cmd; | ||
| 61 | |||
| 62 | my $have_mime_lite; | ||
| 63 | eval { | ||
| 64 | require MIME::Lite; | ||
| 65 | MIME::Lite->import(); | ||
| 66 | require MIME::Base64; | ||
| 67 | MIME::Base64->import(); | ||
| 68 | require MIME::QuotedPrint; | ||
| 69 | MIME::QuotedPrint->import(); | ||
| 70 | require File::MMagic; | ||
| 71 | File::MMagic->import(); | ||
| 72 | }; | ||
| 73 | $have_mime_lite = 1 unless $@; | ||
| 74 | |||
| 75 | # variables available for programs importing this library, | ||
| 76 | # should be removed (!) | ||
| 77 | our $debug; | ||
| 78 | our $debug2; | ||
| 79 | our $cmd; | ||
| 80 | our $cmd_opts; | ||
| 81 | our $new_cmd_opts; | ||
| 82 | our $command = ""; | ||
| 83 | |||
| 84 | our $HOOKS = {}; | ||
| 85 | |||
| 86 | # read file, removing carriage returns | ||
| 87 | sub read_file_option { | ||
| 88 | my ($filename, $opts) = @_; | ||
| 89 | $opts = {} unless $opts; | ||
| 90 | |||
| 91 | my $t = undef; | ||
| 92 | |||
| 93 | if (-e $filename || $opts->{'mandatory'}) { | ||
| 94 | $t = read_file_scalar($filename); | ||
| 95 | $t =~ s/[\r\n]//sgo; | ||
| 96 | } | ||
| 97 | |||
| 98 | return $t; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub write_file_option { | ||
| 102 | my ($filename, $value, $opts) = @_; | ||
| 103 | |||
| 104 | $opts = {} unless $opts; | ||
| 105 | my $fh = Yandex::Tools::safe_open($filename, "overwrite", {'timeout' => $opts->{'timeout'} || 2}); | ||
| 106 | return 0 unless $fh; | ||
| 107 | |||
| 108 | $value = "" unless defined($value); | ||
| 109 | |||
| 110 | print $fh $value . "\n"; | ||
| 111 | Yandex::Tools::safe_close($fh); | ||
| 112 | } | ||
| 113 | |||
| 114 | sub write_file_option_safe { | ||
| 115 | my ($filename, $value, $opts) = @_; | ||
| 116 | |||
| 117 | $opts = {} unless $opts; | ||
| 118 | |||
| 119 | my $dirname = $filename; | ||
| 120 | $dirname =~ s/\/[^\/]*$//goi; | ||
| 121 | |||
| 122 | my $filename_tmp = $filename; | ||
| 123 | $filename_tmp =~ s/.+\///goi; | ||
| 124 | |||
| 125 | $filename_tmp = $dirname . "/.safe." . $filename_tmp; | ||
| 126 | |||
| 127 | my $fh = Yandex::Tools::safe_open($filename_tmp, "overwrite", {'timeout' => $opts->{'timeout'} || 2}); | ||
| 128 | return 0 unless $fh; | ||
| 129 | |||
| 130 | $value = "" unless defined($value); | ||
| 131 | |||
| 132 | print $fh $value . "\n"; | ||
| 133 | Yandex::Tools::safe_close($fh); | ||
| 134 | |||
| 135 | my $res = rename ($filename_tmp, $filename); | ||
| 136 | return $res; | ||
| 137 | } | ||
| 138 | |||
| 139 | sub write_file_scalar { | ||
| 140 | my ($filename, $value, $opts) = @_; | ||
| 141 | |||
| 142 | $opts = {} unless $opts; | ||
| 143 | my $fh = Yandex::Tools::safe_open($filename, "overwrite", {'timeout' => $opts->{'timeout'} || 2}); | ||
| 144 | return 0 unless $fh; | ||
| 145 | |||
| 146 | $value = "" unless defined($value); | ||
| 147 | |||
| 148 | print $fh $value; | ||
| 149 | Yandex::Tools::safe_close($fh); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub read_file_scalar { | ||
| 153 | my ($filename) = @_; | ||
| 154 | |||
| 155 | my $filecontent; | ||
| 156 | unless (open F, $filename) { | ||
| 157 | Yandex::Tools::die("Couldn't open $filename for reading: $!"); | ||
| 158 | } | ||
| 159 | { local $/ = undef; $filecontent = <F>; } | ||
| 160 | close F; | ||
| 161 | |||
| 162 | return $filecontent; | ||
| 163 | } | ||
| 164 | |||
| 165 | # read file, each line is an array element | ||
| 166 | # | ||
| 167 | sub read_file_array { | ||
| 168 | my ($filename, $opts) = @_; | ||
| 169 | $opts = {} unless $opts; | ||
| 170 | |||
| 171 | my $arr = []; | ||
| 172 | my $t = undef; | ||
| 173 | |||
| 174 | if (-e $filename || $opts->{'mandatory'}) { | ||
| 175 | $t = read_file_scalar($filename); | ||
| 176 | @{$arr} = split(/\n/so, $t); | ||
| 177 | } | ||
| 178 | |||
| 179 | return $arr; | ||
| 180 | } | ||
| 181 | |||
| 182 | # generate fileinfo structure | ||
| 183 | # | ||
| 184 | sub fileinfo_struct { | ||
| 185 | my ($opts) = @_; | ||
| 186 | |||
| 187 | $opts = {} unless $opts; | ||
| 188 | |||
| 189 | Yandex::Tools::die("fileinfo_struct: absolute_name must be specified") | ||
| 190 | unless $opts->{'absolute_name'}; | ||
| 191 | |||
| 192 | my $entry = {}; | ||
| 193 | |||
| 194 | # return empty struct if file does not exist | ||
| 195 | if (! -e $opts->{'absolute_name'} && ! -l $opts->{'absolute_name'}) { | ||
| 196 | return $entry; | ||
| 197 | } | ||
| 198 | |||
| 199 | foreach my $k(qw/absolute_name relative_name short_name/) { | ||
| 200 | $entry->{$k} = $opts->{$k} if defined($opts->{$k}); | ||
| 201 | } | ||
| 202 | |||
| 203 | if (! $entry->{'relative_name'}) { | ||
| 204 | $entry->{'relative_name'} = $entry->{'absolute_name'}; | ||
| 205 | $entry->{'relative_name'} =~ s/^\///o; | ||
| 206 | } | ||
| 207 | if (! $entry->{'short_name'}) { | ||
| 208 | $entry->{'short_name'} = File::Basename::basename($entry->{'absolute_name'}); | ||
| 209 | } | ||
| 210 | |||
| 211 | if (-l $entry->{'absolute_name'}) { | ||
| 212 | $entry->{'type'} = "symlink"; | ||
| 213 | $entry->{'symlink_target'} = readlink($entry->{'absolute_name'}); | ||
| 214 | |||
| 215 | # calculate absolute path for relative symlinks | ||
| 216 | # | ||
| 217 | if ($entry->{'symlink_target'} !~ /^\//o) { | ||
| 218 | |||
| 219 | my $abs_dir = dirname($entry->{'absolute_name'}); | ||
| 220 | $abs_dir = $abs_dir . ($abs_dir eq "/" ? "" : "/") . $entry->{'symlink_target'}; | ||
| 221 | |||
| 222 | $entry->{'symlink_target_abs'} = $abs_dir; | ||
| 223 | } | ||
| 224 | else { | ||
| 225 | $entry->{'symlink_target_abs'} = $entry->{'symlink_target'}; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | elsif (-d $entry->{'absolute_name'}) { | ||
| 229 | $entry->{'type'} = "dir"; | ||
| 230 | } | ||
| 231 | else { | ||
| 232 | $entry->{'type'} = "file"; | ||
| 233 | } | ||
| 234 | |||
| 235 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | ||
| 236 | $atime,$mtime,$ctime,$blksize,$blocks) = lstat($entry->{'absolute_name'}); | ||
| 237 | |||
| 238 | # extract MASK as specified in http://perldoc.perl.org/functions/stat.html | ||
| 239 | $mode = $mode & 07777; | ||
| 240 | |||
| 241 | # saving entry attributes | ||
| 242 | $entry->{'mode'} = $mode; | ||
| 243 | $entry->{'uid'} = $uid; | ||
| 244 | $entry->{'gid'} = $gid; | ||
| 245 | # $entry->{'atime'} = $atime; | ||
| 246 | $entry->{'mtime'} = $mtime; | ||
| 247 | $entry->{'size'} = $size; | ||
| 248 | # $entry->{'mode_printable'} = sprintf("%3o", $mode); | ||
| 249 | |||
| 250 | return $entry; | ||
| 251 | } | ||
| 252 | |||
| 253 | sub read_dir { | ||
| 254 | my ($dirname, $opts) = @_; | ||
| 255 | |||
| 256 | $opts = {} unless $opts; | ||
| 257 | $opts->{'output_type'} = 'arrayref' | ||
| 258 | unless $opts->{'output_type'}; | ||
| 259 | |||
| 260 | my $dummy; | ||
| 261 | if (!opendir($dummy, $dirname)) { | ||
| 262 | if ($opts->{'non_fatal'}) { | ||
| 263 | return 0; | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | Yandex::Tools::die("ERROR: unable to open directory [$dirname]"); | ||
| 267 | } | ||
| 268 | } | ||
| 269 | |||
| 270 | my @all_entries = readdir($dummy); | ||
| 271 | close($dummy); | ||
| 272 | |||
| 273 | my $entries; | ||
| 274 | if ($opts->{'output_type'} eq 'arrayref') { | ||
| 275 | $entries = []; | ||
| 276 | } | ||
| 277 | elsif ($opts->{'output_type'} eq 'hashref') { | ||
| 278 | $entries = {}; | ||
| 279 | } | ||
| 280 | |||
| 281 | foreach my $e (sort @all_entries) { | ||
| 282 | next if $e eq '.' || $e eq '..'; | ||
| 283 | |||
| 284 | my $absolute_name = $dirname . "/" . $e; | ||
| 285 | |||
| 286 | if ($opts->{'output_type'} eq 'arrayref') { | ||
| 287 | # skipping non-directories if requested | ||
| 288 | # effectively means "get only files"; | ||
| 289 | if ($opts->{'only-directories'}) { | ||
| 290 | next if -l $absolute_name || ! -d $absolute_name; | ||
| 291 | } | ||
| 292 | |||
| 293 | # symlinks are also files | ||
| 294 | if ($opts->{'only-files'}) { | ||
| 295 | next if -d $absolute_name && ! -l $absolute_name; | ||
| 296 | } | ||
| 297 | |||
| 298 | # simple output, feasible only | ||
| 299 | # for non-recursive directory reads | ||
| 300 | push(@{$entries}, $e); | ||
| 301 | } | ||
| 302 | elsif ($opts->{'output_type'} eq 'hashref') { | ||
| 303 | # used to generate following type of struct | ||
| 304 | # | ||
| 305 | # "etc" => { | ||
| 306 | # "short_name" => "etc", | ||
| 307 | # "relative_name" => "etc", | ||
| 308 | # "type" => "dir", | ||
| 309 | # } | ||
| 310 | # "etc/passwd" => { | ||
| 311 | # "short_name" => "passwd", | ||
| 312 | # "relative_name" => "etc/passwd", | ||
| 313 | # "type" => "file", | ||
| 314 | # } | ||
| 315 | |||
| 316 | my $relative_entry_name = ""; | ||
| 317 | |||
| 318 | # append entry relative path (if we're recursing) | ||
| 319 | if (defined($opts->{'recursed_dir'})) { | ||
| 320 | $relative_entry_name = $opts->{'recursed_dir'} . "/"; | ||
| 321 | } | ||
| 322 | |||
| 323 | # append entry short name | ||
| 324 | $relative_entry_name .= $e; | ||
| 325 | |||
| 326 | # populate entry struct | ||
| 327 | my $entry = Yandex::Tools::fileinfo_struct({ | ||
| 328 | 'absolute_name' => $absolute_name, | ||
| 329 | 'relative_name' => $relative_entry_name, | ||
| 330 | 'short_name' => $e, | ||
| 331 | 'user_map' => $opts->{'user_map'}, | ||
| 332 | 'group_map' => $opts->{'group_map'}, | ||
| 333 | }); | ||
| 334 | |||
| 335 | # append entry | ||
| 336 | $entries->{$relative_entry_name} = $entry; | ||
| 337 | |||
| 338 | # do recursion into directories | ||
| 339 | # (but not into symlinked directories!) | ||
| 340 | if (-d $absolute_name && ! -l $absolute_name) { | ||
| 341 | my $opts_r = Storable::dclone($opts); | ||
| 342 | $opts_r->{'recursed_dir'} = $relative_entry_name; | ||
| 343 | |||
| 344 | my $e = read_dir($absolute_name, $opts_r); | ||
| 345 | foreach my $ee (keys %{$e}) { | ||
| 346 | $entries->{$ee} = $e->{$ee}; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | } | ||
| 351 | |||
| 352 | return $entries; | ||
| 353 | } | ||
| 354 | |||
| 355 | sub disable_all_signals { | ||
| 356 | foreach my $s (keys %SIG) { | ||
| 357 | $SIG{$s} = 'IGNORE'; | ||
| 358 | } | ||
| 359 | } | ||
| 360 | |||
| 361 | sub install_layered_signal { return IPC::Cmd::install_layered_signal(@_); } | ||
| 362 | sub kill_gently { return IPC::Cmd::kill_gently(@_); } | ||
| 363 | sub open3_run { return IPC::Cmd::open3_run(@_); } | ||
| 364 | sub run_forked { return IPC::Cmd::run_forked(@_); } | ||
| 365 | |||
| 366 | sub debug2 { | ||
| 367 | my ($text, $opts) = @_; | ||
| 368 | |||
| 369 | if ($Yandex::Tools::debug2) { | ||
| 370 | debug($text, $opts); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | sub debug { | ||
| 375 | my ($text, $opts) = @_; | ||
| 376 | |||
| 377 | $opts = {} unless $opts; | ||
| 378 | |||
| 379 | if ($Yandex::Tools::debug) { | ||
| 380 | my $stamp = localtime() . ": "; | ||
| 381 | |||
| 382 | utf8::encode($text) if utf8::is_utf8($text); | ||
| 383 | |||
| 384 | if ($opts->{'stderr'}) { | ||
| 385 | if (ref($text)) { | ||
| 386 | print STDERR join("", map { "$stamp$_\n" } Dumper($text)); | ||
| 387 | } | ||
| 388 | else { | ||
| 389 | print STDERR $stamp . ($text ? $text : "") . | ||
| 390 | ($text =~ /[\r\n]$/so ? "" : "\n"); | ||
| 391 | } | ||
| 392 | } | ||
| 393 | else { | ||
| 394 | if (ref($text)) { | ||
| 395 | print join("", map { "$stamp$_\n" } Dumper($text)); | ||
| 396 | } | ||
| 397 | else { | ||
| 398 | print $stamp . ($text ? $text : "") . | ||
| 399 | ($text =~ /[\r\n]$/so ? "" : "\n"); | ||
| 400 | } | ||
| 401 | } | ||
| 402 | } | ||
| 403 | } | ||
| 404 | |||
| 405 | # safe_open and safe_close are copied | ||
| 406 | # from ps_farm.pm (should be one library actually) | ||
| 407 | sub safe_open { | ||
| 408 | my ($filename, $mode, $opts) = @_; | ||
| 409 | |||
| 410 | $opts = {} unless $opts; | ||
| 411 | $opts->{'timeout'} = 30 unless defined($opts->{'timeout'}); | ||
| 412 | |||
| 413 | $mode = "open" unless $mode; | ||
| 414 | |||
| 415 | if ($mode eq "overwrite" || $mode eq ">") { | ||
| 416 | $mode = ">"; | ||
| 417 | } | ||
| 418 | elsif ($mode eq "append" || $mode eq ">>") { | ||
| 419 | $mode = ">>"; | ||
| 420 | } | ||
| 421 | else { | ||
| 422 | $mode = ""; | ||
| 423 | } | ||
| 424 | |||
| 425 | my $fh; | ||
| 426 | my $i=0; | ||
| 427 | while (! open($fh, "${mode}${filename}")) { | ||
| 428 | $i = $i + 1; | ||
| 429 | if ($i > $opts->{'timeout'}) { | ||
| 430 | print STDERR "Unable to open $filename\n" if ! $opts->{'silent'}; | ||
| 431 | return 0; | ||
| 432 | } | ||
| 433 | |||
| 434 | print STDERR "still trying to open $filename\n" if ! $opts->{'silent'}; | ||
| 435 | sleep 1; | ||
| 436 | } | ||
| 437 | |||
| 438 | # http://perldoc.perl.org/functions/flock.html | ||
| 439 | # | ||
| 440 | # LOCK_SH, LOCK_EX, LOCK_UN, LOCK_NB <=> 1, 2, 8, 4 | ||
| 441 | # | ||
| 442 | # If LOCK_NB is bitwise-or'ed with LOCK_SH or LOCK_EX | ||
| 443 | # then flock will return immediately | ||
| 444 | |||
| 445 | while (! flock($fh, 2 | 4)) { | ||
| 446 | $i = $i + 1; | ||
| 447 | if ($i > $opts->{'timeout'}) { | ||
| 448 | print STDERR "Unable to lock $filename\n" if ! $opts->{'silent'}; | ||
| 449 | return 0; | ||
| 450 | } | ||
| 451 | |||
| 452 | print STDERR "still trying to lock $filename\n" if ! $opts->{'silent'}; | ||
| 453 | sleep 1; | ||
| 454 | } | ||
| 455 | |||
| 456 | my $fh1; | ||
| 457 | if (!open($fh1, "${mode}${filename}")) { | ||
| 458 | $i = $i + 1; | ||
| 459 | if ($i > $opts->{'timeout'}) { | ||
| 460 | print STDERR "Unable to open and lock $filename\n" if ! $opts->{'silent'}; | ||
| 461 | return 0; | ||
| 462 | } | ||
| 463 | |||
| 464 | print STDERR "Locked $filename, but it's gone. Retrying...\n" if ! $opts->{'silent'}; | ||
| 465 | $opts->{'timeout'} = $opts->{'timeout'} - 1; | ||
| 466 | return safe_open($filename, $mode, $opts); | ||
| 467 | } | ||
| 468 | else { | ||
| 469 | close($fh1); | ||
| 470 | return $fh; | ||
| 471 | } | ||
| 472 | } | ||
| 473 | |||
| 474 | sub safe_close { | ||
| 475 | my ($fh) = @_; | ||
| 476 | return flock($fh, 8) && close($fh); | ||
| 477 | } | ||
| 478 | |||
| 479 | sub set_log_options { | ||
| 480 | my ($opts) = @_; | ||
| 481 | |||
| 482 | $Yandex::Tools::LOG = {} | ||
| 483 | unless $Yandex::Tools::LOG; | ||
| 484 | |||
| 485 | foreach my $k (keys %{$opts}) { | ||
| 486 | $Yandex::Tools::LOG->{$k} = $opts->{$k}; | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | sub get_log_options { | ||
| 491 | if ($Yandex::Tools::LOG) { | ||
| 492 | return Storable::dclone($Yandex::Tools::LOG); | ||
| 493 | } | ||
| 494 | return undef; | ||
| 495 | } | ||
| 496 | |||
| 497 | sub set_log_filename { | ||
| 498 | my ($filename, $opts) = @_; | ||
| 499 | |||
| 500 | if ($Yandex::Tools::LOG) { | ||
| 501 | if (ref($Yandex::Tools::LOG)) { | ||
| 502 | $Yandex::Tools::LOG->{'filename'} = $filename; | ||
| 503 | } | ||
| 504 | else { | ||
| 505 | $Yandex::Tools::LOG = $filename | ||
| 506 | } | ||
| 507 | } | ||
| 508 | else { | ||
| 509 | $Yandex::Tools::LOG = { | ||
| 510 | 'filename' => $filename, | ||
| 511 | }; | ||
| 512 | } | ||
| 513 | } | ||
| 514 | |||
| 515 | sub get_log_filename { | ||
| 516 | if (ref($Yandex::Tools::LOG)) { | ||
| 517 | return $Yandex::Tools::LOG->{'filename'}; | ||
| 518 | } | ||
| 519 | else { | ||
| 520 | return $Yandex::Tools::LOG; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | |||
| 524 | sub do_log { | ||
| 525 | my ($message, $opts) = @_; | ||
| 526 | |||
| 527 | my $module; | ||
| 528 | my $stderr = 0; | ||
| 529 | |||
| 530 | # old style $module population | ||
| 531 | # | ||
| 532 | if (ref($opts) eq 'HASH') { | ||
| 533 | if ($opts->{'module'}) { | ||
| 534 | $module = $opts->{'module'}; | ||
| 535 | } | ||
| 536 | if ($opts->{'stderr'}) { | ||
| 537 | $stderr = $opts->{'stderr'}; | ||
| 538 | } | ||
| 539 | } | ||
| 540 | else { | ||
| 541 | $module = $opts; | ||
| 542 | } | ||
| 543 | |||
| 544 | # current program name (if all previous methods | ||
| 545 | # did not return meaningful module identification) | ||
| 546 | # | ||
| 547 | $module = $0 unless $module; | ||
| 548 | |||
| 549 | # module name formatting | ||
| 550 | # | ||
| 551 | $module = "[" . $module . "] " if $module; | ||
| 552 | |||
| 553 | |||
| 554 | $message = "" unless $message; | ||
| 555 | utf8::encode($message) if utf8::is_utf8($message); | ||
| 556 | $message =~ s/[\r\n]/ /sgo; | ||
| 557 | |||
| 558 | my $message_eol = chop($message); | ||
| 559 | my $message_formatted = | ||
| 560 | localtime() . " " . $module . | ||
| 561 | $message . | ||
| 562 | $message_eol . | ||
| 563 | ($message_eol eq "\n" ? "" : "\n"); | ||
| 564 | |||
| 565 | if ($stderr) { | ||
| 566 | print STDERR $message_formatted; | ||
| 567 | } | ||
| 568 | elsif ($Yandex::Tools::debug) { | ||
| 569 | print $message_formatted; | ||
| 570 | } | ||
| 571 | |||
| 572 | my $log_filename; | ||
| 573 | my $log_dirname; | ||
| 574 | |||
| 575 | if (!defined($Yandex::Tools::LOG)) { | ||
| 576 | print STDERR "Yandex::Tools::LOG not configured: $message_formatted\n"; | ||
| 577 | return; | ||
| 578 | } | ||
| 579 | if (ref($Yandex::Tools::LOG)) { | ||
| 580 | if ($Yandex::Tools::LOG->{'turned_off'}) { | ||
| 581 | return; | ||
| 582 | } | ||
| 583 | |||
| 584 | $log_filename = $Yandex::Tools::LOG->{'filename'}; | ||
| 585 | if (!$log_filename) { | ||
| 586 | print STDERR "Yandex::Tools::LOG->{'filename'} not configured: $message_formatted\n"; | ||
| 587 | return; | ||
| 588 | } | ||
| 589 | } | ||
| 590 | else { | ||
| 591 | $log_filename = $Yandex::Tools::LOG; | ||
| 592 | } | ||
| 593 | |||
| 594 | if (!can_write($log_filename)) { | ||
| 595 | print STDERR "$log_filename is not writeable: $message_formatted\n"; | ||
| 596 | return; | ||
| 597 | } | ||
| 598 | |||
| 599 | $log_dirname = dirname($log_filename); | ||
| 600 | if (! -d $log_dirname) { | ||
| 601 | File::Path::mkpath($log_dirname) || | ||
| 602 | return Yandex::Tools::warn("[$log_filename] does not exist and unable to create [$log_dirname]"); | ||
| 603 | } | ||
| 604 | |||
| 605 | if (ref($Yandex::Tools::LOG) && $Yandex::Tools::LOG->{'rotate_size'}) { | ||
| 606 | # quick check that we might need to do rotation | ||
| 607 | my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | ||
| 608 | $atime,$mtime,$ctime,$blksize,$blocks) = stat($log_filename); | ||
| 609 | |||
| 610 | if ($size && $size > $Yandex::Tools::LOG->{'rotate_size'}) { | ||
| 611 | |||
| 612 | my $rotated = 0; | ||
| 613 | |||
| 614 | # if it seems that rotation is needed -- lock file | ||
| 615 | # and retry (someone might already done that) | ||
| 616 | my $rfh = safe_open($log_filename, ""); | ||
| 617 | Yandex::Tools::die ("Unable to open $log_filename; log message: $message_formatted", {'no_log' => 1}) | ||
| 618 | unless $rfh; | ||
| 619 | |||
| 620 | ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size, | ||
| 621 | $atime,$mtime,$ctime,$blksize,$blocks) = stat($log_filename); | ||
| 622 | if ($size && $size > $Yandex::Tools::LOG->{'rotate_size'}) { | ||
| 623 | link($log_filename, $log_filename . "." . time()) && unlink($log_filename) && ($rotated = 1); | ||
| 624 | } | ||
| 625 | else { | ||
| 626 | # print "already rotated!\n"; | ||
| 627 | } | ||
| 628 | safe_close($rfh); | ||
| 629 | |||
| 630 | if ($rotated && $Yandex::Tools::LOG->{'rotate_keep_copies'}) { | ||
| 631 | my $dummy; | ||
| 632 | if (!opendir($dummy, $log_dirname)) { | ||
| 633 | Yandex::Tools::warn("unable to open log directory [$log_dirname] during log rotation"); | ||
| 634 | } | ||
| 635 | else { | ||
| 636 | my @all_entries = readdir($dummy); | ||
| 637 | close($dummy); | ||
| 638 | |||
| 639 | my $log_archive_count = 0; | ||
| 640 | foreach my $e (sort {$b cmp $a} @all_entries) { | ||
| 641 | next if "$log_dirname/$e" !~ /$log_filename\.[0-9]+$/; | ||
| 642 | next if ! -e "$log_dirname/$e"; | ||
| 643 | |||
| 644 | $log_archive_count = $log_archive_count + 1; | ||
| 645 | if ($log_archive_count > $Yandex::Tools::LOG->{'rotate_keep_copies'}) { | ||
| 646 | unlink("$log_dirname/$e") || Yandex::Tools::warn("unable to delete expired log archive [$log_dirname/$e]"); | ||
| 647 | } | ||
| 648 | } | ||
| 649 | } | ||
| 650 | } | ||
| 651 | } | ||
| 652 | } | ||
| 653 | |||
| 654 | my $fh = safe_open($log_filename, ">>"); | ||
| 655 | Yandex::Tools::die ("Unable to open $log_filename; log message: $message_formatted", {'no_log' => 1}) | ||
| 656 | unless $fh; | ||
| 657 | |||
| 658 | # binmode($fh, ":utf8"); | ||
| 659 | print $fh $message_formatted; | ||
| 660 | safe_close($fh); | ||
| 661 | } | ||
| 662 | |||
| 663 | # are we able to write to file | ||
| 664 | sub can_write { | ||
| 665 | my ($filename) = @_; | ||
| 666 | |||
| 667 | if (! -d dirname($filename)) { | ||
| 668 | if (!File::Path::mkpath(dirname($filename))) { | ||
| 669 | return 0; | ||
| 670 | } | ||
| 671 | } | ||
| 672 | |||
| 673 | if (-e $filename) { | ||
| 674 | if (-w $filename) { | ||
| 675 | return 1; | ||
| 676 | } | ||
| 677 | else { | ||
| 678 | return 0; | ||
| 679 | } | ||
| 680 | } | ||
| 681 | else { | ||
| 682 | my $fh = safe_open($filename, ">", {"timeout" => 0, "silent" => 1}); | ||
| 683 | if ($fh) { | ||
| 684 | safe_close($fh); | ||
| 685 | return 1; | ||
| 686 | } | ||
| 687 | else { | ||
| 688 | return 0; | ||
| 689 | } | ||
| 690 | } | ||
| 691 | } | ||
| 692 | |||
| 693 | # are we able to log | ||
| 694 | sub can_log { | ||
| 695 | if (defined($Yandex::Tools::LOG)) { | ||
| 696 | my $log_filename; | ||
| 697 | if (ref($Yandex::Tools::LOG)) { | ||
| 698 | $log_filename = $Yandex::Tools::LOG->{'filename'}; | ||
| 699 | if (!$log_filename) { | ||
| 700 | $Yandex::Tools::LOG = undef; | ||
| 701 | return 0; | ||
| 702 | } | ||
| 703 | } | ||
| 704 | else { | ||
| 705 | $log_filename = $Yandex::Tools::LOG; | ||
| 706 | } | ||
| 707 | |||
| 708 | if (!can_write($log_filename)) { | ||
| 709 | $Yandex::Tools::LOG = undef; | ||
| 710 | return 0; | ||
| 711 | } | ||
| 712 | } | ||
| 713 | |||
| 714 | return 1; | ||
| 715 | } | ||
| 716 | |||
| 717 | sub add_hook { | ||
| 718 | my ($opts) = @_; | ||
| 719 | |||
| 720 | Yandex::Tools::die("add_hook: invalid input") | ||
| 721 | unless $opts->{'type'} && | ||
| 722 | ref($opts->{'func'}) eq 'CODE' && | ||
| 723 | ref($opts->{'args'}) eq 'ARRAY'; | ||
| 724 | |||
| 725 | push @{$Yandex::Tools::HOOKS->{$opts->{'type'}}}, { | ||
| 726 | 'func' => $opts->{'func'}, | ||
| 727 | 'args' => $opts->{'args'}, | ||
| 728 | }; | ||
| 729 | } | ||
| 730 | |||
| 731 | sub run_hook { | ||
| 732 | my ($type) = @_; | ||
| 733 | |||
| 734 | if (scalar(@{$Yandex::Tools::HOOKS->{$type}}) gt 0) { | ||
| 735 | my $h = pop(@{$Yandex::Tools::HOOKS->{$type}}); | ||
| 736 | $h->{'func'}->(@{$h->{'args'}}); | ||
| 737 | } | ||
| 738 | } | ||
| 739 | |||
| 740 | sub run_hooks { | ||
| 741 | my ($type) = @_; | ||
| 742 | |||
| 743 | while (scalar(@{$Yandex::Tools::HOOKS->{$type}}) gt 0) { | ||
| 744 | Yandex::Tools::run_hook($type); | ||
| 745 | } | ||
| 746 | } | ||
| 747 | |||
| 748 | sub warn { | ||
| 749 | my ($text) = @_; | ||
| 750 | print STDERR "WARNING: $text\n"; | ||
| 751 | Yandex::Tools::do_log("[WARN] " . $text); | ||
| 752 | } | ||
| 753 | |||
| 754 | sub die { | ||
| 755 | my ($message, $opts) = @_; | ||
| 756 | |||
| 757 | $opts = {} unless $opts; | ||
| 758 | |||
| 759 | # backward compatibility (second parameter used to be scalar) | ||
| 760 | if (!ref($opts)) { | ||
| 761 | $opts->{'show_callstack'} = 1; | ||
| 762 | } | ||
| 763 | |||
| 764 | if ($Yandex::Tools::HOOKS->{'before_die'} && scalar(@{$Yandex::Tools::HOOKS->{'before_die'}})) { | ||
| 765 | Yandex::Tools::run_hooks("before_die"); | ||
| 766 | } | ||
| 767 | |||
| 768 | print STDERR ($message ? $message : ""); | ||
| 769 | |||
| 770 | if ($opts->{'show_callstack'}) { | ||
| 771 | print STDERR get_callstack(); | ||
| 772 | } | ||
| 773 | else { | ||
| 774 | print "\n"; | ||
| 775 | } | ||
| 776 | |||
| 777 | if (!$opts->{'no_log'}) { | ||
| 778 | Yandex::Tools::do_log("[ERR] " . $message); | ||
| 779 | } | ||
| 780 | else { | ||
| 781 | # try logger here? | ||
| 782 | } | ||
| 783 | |||
| 784 | exit 1; | ||
| 785 | } | ||
| 786 | |||
| 787 | sub get_callstack { | ||
| 788 | my $cstack = ""; | ||
| 789 | my $i = 0; | ||
| 790 | while ( 1 ) { | ||
| 791 | my $tfunc = (caller($i))[3]; | ||
| 792 | if ($tfunc && $tfunc ne "") { | ||
| 793 | if ($tfunc !~ /\_\_ANON\_\_/ && | ||
| 794 | $tfunc !~ /.*::get_callstack/) { | ||
| 795 | $cstack .= "\t" . $tfunc . "\n"; | ||
| 796 | } | ||
| 797 | $i = $i + 1; | ||
| 798 | } | ||
| 799 | else { | ||
| 800 | last; | ||
| 801 | } | ||
| 802 | } | ||
| 803 | return "\nCallstack:\n" . $cstack . "\n"; | ||
| 804 | } | ||
| 805 | |||
| 806 | sub read_cmdline { | ||
| 807 | my $i = 0; | ||
| 808 | |||
| 809 | $new_cmd_opts = {}; | ||
| 810 | |||
| 811 | my $v; | ||
| 812 | my $first_parameter; | ||
| 813 | while ($ARGV[$i]) { | ||
| 814 | if ($ARGV[$i] =~ /--(.+)\b/) { | ||
| 815 | $v = $1; | ||
| 816 | if ($v eq "debug") { | ||
| 817 | $debug = 1; | ||
| 818 | } | ||
| 819 | elsif ($v eq "debug2") { | ||
| 820 | $debug = 1; | ||
| 821 | $debug2 = 1; | ||
| 822 | } | ||
| 823 | else { | ||
| 824 | $command = $v unless $command; | ||
| 825 | |||
| 826 | $cmd_opts->{$v} = ""; | ||
| 827 | $first_parameter = 1; | ||
| 828 | } | ||
| 829 | |||
| 830 | $new_cmd_opts->{$v} = { | ||
| 831 | 'defined' => 1, | ||
| 832 | 'value' => '', | ||
| 833 | }; | ||
| 834 | } | ||
| 835 | else { | ||
| 836 | if ($v) { | ||
| 837 | if ($first_parameter) { | ||
| 838 | $cmd_opts->{$v} = ""; | ||
| 839 | $first_parameter = 0; | ||
| 840 | } | ||
| 841 | |||
| 842 | $cmd_opts->{$v} .= ($cmd_opts->{$v} ? " " : "") . $ARGV[$i]; | ||
| 843 | |||
| 844 | $new_cmd_opts->{$v}->{'value'} .= | ||
| 845 | ($new_cmd_opts->{$v}->{'value'} ? " " : "") . $ARGV[$i]; | ||
| 846 | } | ||
| 847 | else { | ||
| 848 | $cmd_opts->{$ARGV[$i]} = 1; | ||
| 849 | |||
| 850 | $new_cmd_opts->{$ARGV[$i]} = { | ||
| 851 | 'defined' => 1, | ||
| 852 | 'value' => '', | ||
| 853 | }; | ||
| 854 | } | ||
| 855 | } | ||
| 856 | $i++; | ||
| 857 | } | ||
| 858 | } | ||
| 859 | |||
| 860 | sub defined_cmdline_param { | ||
| 861 | my ($pname) = @_; | ||
| 862 | |||
| 863 | if (!ref($new_cmd_opts)) { | ||
| 864 | croak("Programmer error: get_cmdline_param called without read_cmdline"); | ||
| 865 | } | ||
| 866 | |||
| 867 | return defined($new_cmd_opts->{$pname}); | ||
| 868 | } | ||
| 869 | |||
| 870 | sub get_cmdline_param { | ||
| 871 | my ($pname) = @_; | ||
| 872 | |||
| 873 | if (!ref($new_cmd_opts)) { | ||
| 874 | croak("Programmer error: get_cmdline_param called without read_cmdline"); | ||
| 875 | } | ||
| 876 | |||
| 877 | if ($pname) { | ||
| 878 | if ($new_cmd_opts->{$pname}) { | ||
| 879 | return $new_cmd_opts->{$pname}->{'value'}; | ||
| 880 | } | ||
| 881 | else { | ||
| 882 | return undef; | ||
| 883 | } | ||
| 884 | } | ||
| 885 | else { | ||
| 886 | return $new_cmd_opts; | ||
| 887 | } | ||
| 888 | } | ||
| 889 | |||
| 890 | sub num_cmdline_param { | ||
| 891 | return scalar(keys %{$new_cmd_opts}); | ||
| 892 | } | ||
| 893 | |||
| 894 | sub canonize_delimiters { | ||
| 895 | my ($str, $except) = @_; | ||
| 896 | |||
| 897 | my $delimiters = "ts,;&"; | ||
| 898 | |||
| 899 | my $delimiters_prepared; | ||
| 900 | foreach my $d (split(//, $delimiters)) { | ||
| 901 | next if $except && $except =~ /$d/; | ||
| 902 | $delimiters_prepared = $delimiters_prepared . "\\$d" | ||
| 903 | } | ||
| 904 | |||
| 905 | my $delim_re = qr/[$delimiters_prepared]+/; | ||
| 906 | $$str =~ s/$delim_re/\ /g; | ||
| 907 | } | ||
| 908 | |||
| 909 | # - removes duplicates from array | ||
| 910 | # - leaves first occurence of each duplicate | ||
| 911 | # - preserves order | ||
| 912 | # - valid only for arrays of scalars and arrayrefs | ||
| 913 | # | ||
| 914 | sub array_clear_dupes { | ||
| 915 | my @array_in = @_; | ||
| 916 | |||
| 917 | my @array_to_process; | ||
| 918 | foreach my $e (@array_in) { | ||
| 919 | if (ref($e) eq 'ARRAY') { | ||
| 920 | push (@array_to_process, @{$e}); | ||
| 921 | } | ||
| 922 | else { | ||
| 923 | push (@array_to_process, $e); | ||
| 924 | } | ||
| 925 | } | ||
| 926 | |||
| 927 | my @array_out; | ||
| 928 | my $c = {}; | ||
| 929 | foreach my $e (@array_to_process) { | ||
| 930 | unless ($c->{$e}) { | ||
| 931 | $c->{$e} = 1; | ||
| 932 | push (@array_out, $e); | ||
| 933 | } | ||
| 934 | } | ||
| 935 | |||
| 936 | return @array_out; | ||
| 937 | } | ||
| 938 | |||
| 939 | # <LJFUNC> | ||
| 940 | # name: LJ::is_ascii | ||
| 941 | # des: checks if text is pure ASCII. | ||
| 942 | # args: text | ||
| 943 | # des-text: text to check for being pure 7-bit ASCII text. | ||
| 944 | # returns: 1 if text is indeed pure 7-bit, 0 otherwise. | ||
| 945 | # </LJFUNC> | ||
| 946 | sub is_ascii { | ||
| 947 | my $text = shift; | ||
| 948 | return ($text !~ m/[^\x01-\x7f]/o); | ||
| 949 | } | ||
| 950 | |||
| 951 | sub is_digital { | ||
| 952 | my $text = shift; | ||
| 953 | return ( $text =~ /^\d+$/o ); | ||
| 954 | } | ||
| 955 | |||
| 956 | sub QP { | ||
| 957 | my ($text, $encoding) = @_; | ||
| 958 | |||
| 959 | $text = "" unless $text; | ||
| 960 | |||
| 961 | $encoding = "UTF-8" unless $encoding; | ||
| 962 | |||
| 963 | my $encoded_text = $text; | ||
| 964 | |||
| 965 | if ($encoding eq "UTF-8") { | ||
| 966 | $encoded_text = encode("utf8", $encoded_text); | ||
| 967 | } | ||
| 968 | |||
| 969 | $encoded_text = encode_base64($encoded_text, ""); | ||
| 970 | $encoded_text = "=?${encoding}?B?$encoded_text?="; | ||
| 971 | |||
| 972 | # $encoded_text = encode_qp($text); | ||
| 973 | # $encoded_text = "=?${encoding}?Q?$encoded_text?="; | ||
| 974 | |||
| 975 | return $encoded_text; | ||
| 976 | } | ||
| 977 | |||
| 978 | # should be used when you need to concatenate string | ||
| 979 | # which might be undefined and you want empty string ("") | ||
| 980 | # instead of perl warnings about uninitialized values | ||
| 981 | # | ||
| 982 | sub safe_string { | ||
| 983 | my ($str) = @_; | ||
| 984 | |||
| 985 | if (defined($str)) { | ||
| 986 | return $str; | ||
| 987 | } | ||
| 988 | else { | ||
| 989 | return ""; | ||
| 990 | } | ||
| 991 | } | ||
| 992 | |||
| 993 | # | ||
| 994 | # generic mail sending function | ||
| 995 | # | ||
| 996 | # expects one input parameter, hashref, with following keys: | ||
| 997 | # | ||
| 998 | # to [mandatory] -- comma-separated list or arrayref of recipients | ||
| 999 | # from [optional] -- string | ||
| 1000 | # cc [optional] -- comma-separated list or arrayref | ||
| 1001 | # bcc [optional] -- comma-separated list or arrayref | ||
| 1002 | # subject [optional] -- string | ||
| 1003 | # data [optional] -- string, message body | ||
| 1004 | # charset [optional] -- string, utf-8 by default | ||
| 1005 | # attach [optional] -- scalar (might be binary) | ||
| 1006 | # attach_name [optional] -- string, attachment name | ||
| 1007 | # headers [optional] -- hashref { header_name => header_value } | ||
| 1008 | # | ||
| 1009 | sub send_mail { | ||
| 1010 | my ($opts) = @_; | ||
| 1011 | |||
| 1012 | Yandex::Tools::die("Programmer error: send_mail expects hashref with at least 'to' set") | ||
| 1013 | unless $opts && $opts->{'to'}; | ||
| 1014 | |||
| 1015 | foreach my $att (qw /subject body cc bcc/) { | ||
| 1016 | $opts->{$att} = "" unless defined($opts->{$att}); | ||
| 1017 | } | ||
| 1018 | $opts->{'charset'} = "utf-8" unless $opts->{'charset'}; | ||
| 1019 | |||
| 1020 | my $process_emails_to_array = sub { | ||
| 1021 | my ($a) = @_; | ||
| 1022 | |||
| 1023 | my @b; | ||
| 1024 | if (ref($a) eq 'ARRAY') { | ||
| 1025 | @b = @{$a}; | ||
| 1026 | } | ||
| 1027 | else { | ||
| 1028 | Yandex::Tools::canonize_delimiters(\$a); | ||
| 1029 | @b = split(/\ /, $a); | ||
| 1030 | } | ||
| 1031 | |||
| 1032 | return \@b; | ||
| 1033 | }; | ||
| 1034 | |||
| 1035 | my $process_emails_to_string = sub { | ||
| 1036 | my ($a) = @_; | ||
| 1037 | |||
| 1038 | if (ref($a) eq 'ARRAY') { | ||
| 1039 | return join(",", @{$a}); | ||
| 1040 | } | ||
| 1041 | else { | ||
| 1042 | return $a; | ||
| 1043 | } | ||
| 1044 | }; | ||
| 1045 | |||
| 1046 | foreach my $f (qw/to cc bcc/) { | ||
| 1047 | $opts->{$f} = $process_emails_to_array->($opts->{$f}); | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | push (@{$opts->{'cc'}}, $Yandex::Tools::CC_ALL) if $Yandex::Tools::CC_ALL && !$opts->{'no_cc_all'}; | ||
| 1051 | push (@{$opts->{'bcc'}}, $Yandex::Tools::BCC_ALL) if $Yandex::Tools::BCC_ALL && !$opts->{'no_bcc_all'}; | ||
| 1052 | |||
| 1053 | foreach my $f (qw/to cc bcc/) { | ||
| 1054 | my @a = Yandex::Tools::array_clear_dupes($opts->{$f}); | ||
| 1055 | $opts->{$f} = $process_emails_to_string->(\@a); | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | $opts->{'from'} = $Yandex::Tools::MAIL_FROM | ||
| 1059 | unless $opts->{'from'}; | ||
| 1060 | |||
| 1061 | my @log_to; | ||
| 1062 | my $clean_emails = sub { | ||
| 1063 | my ($in) = @_; | ||
| 1064 | my @out; | ||
| 1065 | |||
| 1066 | foreach my $e (split /,/, $in) { | ||
| 1067 | $e =~ s/^.*<//o; | ||
| 1068 | $e =~ s/>.*$//o; | ||
| 1069 | push(@out, $e); | ||
| 1070 | } | ||
| 1071 | |||
| 1072 | return join(",", @out); | ||
| 1073 | }; | ||
| 1074 | foreach my $f (qw/from to cc bcc/) { | ||
| 1075 | push @log_to, "$f: " . $clean_emails->($opts->{$f}) if $opts->{$f}; | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | if ($have_mime_lite) { | ||
| 1079 | my $msg = new MIME::Lite ( | ||
| 1080 | 'From' => $opts->{'from'}, | ||
| 1081 | 'To' => $opts->{'to'}, | ||
| 1082 | 'Cc' => $opts->{'cc'}, | ||
| 1083 | 'Bcc' => $opts->{'bcc'}, | ||
| 1084 | 'Subject' => QP($opts->{'subject'}, $opts->{'charset'}), | ||
| 1085 | 'Data' => $opts->{'body'}, | ||
| 1086 | ); | ||
| 1087 | |||
| 1088 | # $opts->{'headers'}->{'Reply-To'} = $Golem::SUPPORT_EMAIL | ||
| 1089 | # if $Golem::SUPPORT_EMAIL; | ||
| 1090 | |||
| 1091 | $msg->attr("content-type.charset" => $opts->{'charset'}) | ||
| 1092 | if $opts->{'charset'} && | ||
| 1093 | ! (Yandex::Tools::is_ascii($opts->{'body'}) && | ||
| 1094 | Yandex::Tools::is_ascii($opts->{'subject'})); | ||
| 1095 | |||
| 1096 | if ($opts->{'attach'}) { | ||
| 1097 | my $mm = new File::MMagic; | ||
| 1098 | my $attach_type = $mm->checktype_contents($opts->{'attach'}); | ||
| 1099 | |||
| 1100 | my $part = MIME::Lite->new( | ||
| 1101 | Type => $attach_type, | ||
| 1102 | Data => $opts->{'attach'}, | ||
| 1103 | Filename => $opts->{'attach_name'} ? $opts->{'attach_name'} : undef, | ||
| 1104 | ); | ||
| 1105 | $msg->attach($part); | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | if ($opts->{'headers'}) { | ||
| 1109 | while (my ($k, $v) = each %{$opts->{'headers'}}) { | ||
| 1110 | $msg->add($k => $v); | ||
| 1111 | } | ||
| 1112 | } | ||
| 1113 | |||
| 1114 | unless ($msg->send()) { | ||
| 1115 | Yandex::Tools::do_log("Unable to send mail through MIME::Lite; recipients: [" . join (" ", @log_to) . "]", {"stderr" => 1}); | ||
| 1116 | return 0; | ||
| 1117 | } | ||
| 1118 | } | ||
| 1119 | else { | ||
| 1120 | my $to_scalar; | ||
| 1121 | my $subj = $opts->{'subject'} || ""; | ||
| 1122 | |||
| 1123 | $subj =~ s/"/\\"/go; | ||
| 1124 | |||
| 1125 | foreach my $f (qw/to cc/) { | ||
| 1126 | if ($opts->{$f}) { | ||
| 1127 | $to_scalar = ($to_scalar ? $to_scalar . "," : "") . $clean_emails->($opts->{$f}); | ||
| 1128 | } | ||
| 1129 | } | ||
| 1130 | |||
| 1131 | if ($opts->{'from'}) { | ||
| 1132 | # for Heirloom mailx 12.4 used on forward*.mail.yandex.net | ||
| 1133 | $ENV{'from'} = $opts->{'from'}; | ||
| 1134 | } | ||
| 1135 | |||
| 1136 | my $r = Yandex::Tools::run_forked("mail -s \"$subj\" $to_scalar", {'child_stdin' => $opts->{'body'}}); | ||
| 1137 | if ($r->{'exit_code'} ne 0) { | ||
| 1138 | Yandex::Tools::do_log("Unable to send mail through system mailer, exit_code [$r->{'exit_code'}], stderr [$r->{'stderr'}], " . | ||
| 1139 | "to [$to_scalar], message [" . substr($opts->{'body'}, 0, 200) . "] ", {"stderr" => 1}); | ||
| 1140 | return 0; | ||
| 1141 | } | ||
| 1142 | } | ||
| 1143 | |||
| 1144 | if ($Yandex::Tools::LOG) { | ||
| 1145 | Yandex::Tools::do_log("sent mail " . join (" ", @log_to)); | ||
| 1146 | } | ||
| 1147 | |||
| 1148 | return 1; | ||
| 1149 | } | ||
| 1150 | |||
| 1151 | # checks if $str matches one of $rfx (arrayref) | ||
| 1152 | # | ||
| 1153 | sub matches_with_one_of_regexps { | ||
| 1154 | my ($str, $rxs) = @_; | ||
| 1155 | |||
| 1156 | $rxs = [] if !$rxs || ref($rxs) ne 'ARRAY'; | ||
| 1157 | |||
| 1158 | foreach my $rx (@{$rxs}) { | ||
| 1159 | next if !defined($rx); | ||
| 1160 | |||
| 1161 | if ($str =~ /$rx/) { | ||
| 1162 | return 1; | ||
| 1163 | } | ||
| 1164 | } | ||
| 1165 | |||
| 1166 | return 0; | ||
| 1167 | } | ||
| 1168 | |||
| 1169 | sub daemonize { | ||
| 1170 | chdir('/') | ||
| 1171 | or CORE::die "Can not chdir to '/': $!\n"; | ||
| 1172 | |||
| 1173 | open(STDIN, '/dev/null') | ||
| 1174 | or CORE::die "Can not read /dev/null: $!\n"; | ||
| 1175 | |||
| 1176 | open(STDOUT, '/dev/null') | ||
| 1177 | or CORE::die "Can not write to /dev/null: $!\n"; | ||
| 1178 | |||
| 1179 | defined(my $pid = fork) | ||
| 1180 | or CORE::die "Can not fork: $!\n"; | ||
| 1181 | |||
| 1182 | exit if $pid; | ||
| 1183 | |||
| 1184 | POSIX::setsid() | ||
| 1185 | or CORE::die("Error running setsid: " . $!); | ||
| 1186 | |||
| 1187 | open(STDERR, "/dev/null") | ||
| 1188 | or CORE::die("unable to bind stderr to /dev/null: $!"); | ||
| 1189 | } | ||
| 1190 | |||
| 1191 | # fork && exec, replace myself with new myself, | ||
| 1192 | # possibly reading in my new version | ||
| 1193 | sub exec { | ||
| 1194 | my ($cmd) = @_; | ||
| 1195 | |||
| 1196 | if (my $pid = fork) { | ||
| 1197 | if (!$pid) { | ||
| 1198 | Yandex::Tools::die("unable to fork: " . $!); | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | # parent | ||
| 1202 | exit 0; | ||
| 1203 | } | ||
| 1204 | else { | ||
| 1205 | POSIX::setsid() || Yandex::Tools::die("Error running setsid: " . $!); | ||
| 1206 | |||
| 1207 | # let parent exit and clean up from /proc (or whatever) | ||
| 1208 | sleep 1; | ||
| 1209 | |||
| 1210 | POSIX::setsid() || Yandex::Tools::do_log("Error running setsid: " . $!, {'stderr' => 1}) && CORE::die(); | ||
| 1211 | |||
| 1212 | # next life | ||
| 1213 | exec($cmd) || Yandex::Tools::do_log("[$$] unable to exec $cmd", {'stderr' => 1}) && CORE::die(); | ||
| 1214 | } | ||
| 1215 | exit(255); | ||
| 1216 | } | ||
| 1217 | |||
| 1218 | |||
| 1219 | sub lock { | ||
| 1220 | my ($file) = @_; | ||
| 1221 | |||
| 1222 | Yandex::Tools::die("lock: lock name expected") unless $file; | ||
| 1223 | |||
| 1224 | $Yandex::Tools::locks = {} unless $Yandex::Tools::locks; | ||
| 1225 | |||
| 1226 | $Yandex::Tools::locks->{$file} = Yandex::Tools::safe_open($file, "overwrite", {'silent' => 1, 'timeout' => 5}); | ||
| 1227 | return undef unless $Yandex::Tools::locks->{$file}; | ||
| 1228 | |||
| 1229 | my $lock_fh = $Yandex::Tools::locks->{$file}; | ||
| 1230 | autoflush $lock_fh; | ||
| 1231 | print $lock_fh $$; | ||
| 1232 | |||
| 1233 | Yandex::Tools::add_hook({ | ||
| 1234 | 'type' => 'before_die', | ||
| 1235 | 'func' => sub {Yandex::Tools::unlock($file);}, | ||
| 1236 | 'args' => [], | ||
| 1237 | }); | ||
| 1238 | |||
| 1239 | return 1; | ||
| 1240 | } | ||
| 1241 | |||
| 1242 | sub unlock { | ||
| 1243 | my ($file) = @_; | ||
| 1244 | |||
| 1245 | Yandex::Tools::die("unlock: lock name expected") unless $file; | ||
| 1246 | |||
| 1247 | if ($Yandex::Tools::locks->{$file}) { | ||
| 1248 | Yandex::Tools::safe_close($Yandex::Tools::locks->{$file}); | ||
| 1249 | unlink($file); | ||
| 1250 | $Yandex::Tools::locks->{$file} = undef; | ||
| 1251 | return 1; | ||
| 1252 | } | ||
| 1253 | |||
| 1254 | return undef; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | sub is_locked { | ||
| 1258 | my ($file) = @_; | ||
| 1259 | |||
| 1260 | Yandex::Tools::die("is_locked: lock name expected") unless $file; | ||
| 1261 | |||
| 1262 | if (! -e $file) { | ||
| 1263 | return 0; | ||
| 1264 | } | ||
| 1265 | |||
| 1266 | $Yandex::Tools::locks->{$file} = Yandex::Tools::safe_open($file, "", | ||
| 1267 | { | ||
| 1268 | 'silent' => 1, | ||
| 1269 | 'timeout' => 0, | ||
| 1270 | }); | ||
| 1271 | |||
| 1272 | if ($Yandex::Tools::locks->{$file}) { | ||
| 1273 | Yandex::Tools::safe_close($Yandex::Tools::locks->{$file}); | ||
| 1274 | $Yandex::Tools::locks->{$file} = undef; | ||
| 1275 | return 0; | ||
| 1276 | } | ||
| 1277 | else { | ||
| 1278 | return Yandex::Tools::read_file_scalar($file); | ||
| 1279 | } | ||
| 1280 | } | ||
| 1281 | |||
| 1282 | |||
| 1283 | |||
| 1284 | 1; | ||
| 1285 | |||
| 1286 | __END__ |