| 1 | #!/usr/bin/env perl | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | # Receive syslog datagrams over UDP and append them to a CSV file. | ||||||||||||||||||||||||||||
| 4 | # All of the work is in App::Syslogd; this file only turns the command | ||||||||||||||||||||||||||||
| 5 | # line into constructor arguments, so that the logic can be unit tested. | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # syslogd [--port 514] [--address 0.0.0.0] [--file /var/log/syslog/syslog.csv] | ||||||||||||||||||||||||||||
| 8 | # [--no-resolve] [--language en] | ||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||
| 10 | # Send SIGHUP to reopen the log file after rotation, SIGTERM or SIGINT to | ||||||||||||||||||||||||||||
| 11 | # stop. | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 22 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 22 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | 22 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||
| 17 | # Installed as /usr/local/etc/syslogd, so "../lib" is /usr/local/lib: copy | ||||||||||||||||||||||||||||
| 18 | # lib/App there. The same path also finds lib/ when run from a git | ||||||||||||||||||||||||||||
| 19 | # checkout (etc/../lib), and a normal @INC install still works. | ||||||||||||||||||||||||||||
| 20 | # | ||||||||||||||||||||||||||||
| 21 | # Under perl -T, $Bin is tainted (it comes from $0), and a library path | ||||||||||||||||||||||||||||
| 22 | # made from it would let whoever chooses $0 choose the code that is loaded. | ||||||||||||||||||||||||||||
| 23 | # So with -T only an installed App::Syslogd (or one named with -I) is used. | ||||||||||||||||||||||||||||
| 24 | 22 | ▶ | use FindBin qw($Bin); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 22 | ▶ | use if !${^TAINT}, lib => "$Bin/../lib"; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | 22 | ▶ | use Getopt::Long qw(GetOptions); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 28 | 22 | ▶ | use IO::Handle; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 29 | 22 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | 22 | ▶ | use App::Syslogd; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # Exit statuses (documented under EXIT STATUS). Errors are caught and | ||||||||||||||||||||||||||||
| 33 | # turned into these: a bare die would exit with whatever $! happened to | ||||||||||||||||||||||||||||
| 34 | # hold (13 for a refused file, 22 for a bad address, ...). | ||||||||||||||||||||||||||||
| 35 | 22 | Readonly my $EXIT_OK => 0; # stopped cleanly | |||||||||||||||||||||||||||
| 36 | 22 | Readonly my $EXIT_FAILURE => 1; # could not start, or failed while running | |||||||||||||||||||||||||||
| 37 | 22 | Readonly my $EXIT_USAGE => 2; # started wrongly: a bad option, or by a web server | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # Report an error and exit with a fixed status | ||||||||||||||||||||||||||||
| 40 | 20 | ▶ | sub fail { | ||||||||||||||||||||||||||
|
Subroutine
fail: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | 20 | my ($status, $message) = @_; | |||||||||||||||||||||||||||
| 42 | 20 | print STDERR $message; | |||||||||||||||||||||||||||
| 43 | 20 | exit($status); | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | # Status lines must appear when they happen, not when the buffer fills: | ||||||||||||||||||||||||||||
| 47 | # under systemd or a pipe STDOUT is not a terminal, so without this the | ||||||||||||||||||||||||||||
| 48 | # "listening" line would only be seen when the server exits | ||||||||||||||||||||||||||||
| 49 | 22 | STDOUT->autoflush(1); | |||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | # This is a server, never a CGI program. If a web server runs it (it was | ||||||||||||||||||||||||||||
| 52 | # dropped into cgi-bin, say), a query without "=" becomes command-line | ||||||||||||||||||||||||||||
| 53 | # arguments (RFC 3875 4.4, "ISINDEX"), so any visitor could start a | ||||||||||||||||||||||||||||
| 54 | # listening daemon writing to a file of their choice. Refuse before the | ||||||||||||||||||||||||||||
| 55 | # command line is even read. RFC 3875 requires GATEWAY_INTERFACE; | ||||||||||||||||||||||||||||
| 56 | # REQUEST_METHOD covers servers that leave it out. | ||||||||||||||||||||||||||||
| 57 | 22 | ▶ | if(defined($ENV{GATEWAY_INTERFACE}) || defined($ENV{REQUEST_METHOD})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $ENV{"GATEWAY_INTERFACE"} or defined $ENV{"REQUEST_METHOD"}
MC/DC100%2/2
defined $ENV{"GATEWAY_INTERFACE"} or defined $ENV{"REQUEST_METHOD"}
defined $ENV{"GATEWAY_INTERFACE"}
defined $ENV{"REQUEST_METHOD"}
Truth table100%3/3
A or B
A= defined $ENV{"GATEWAY_INTERFACE"}B= defined $ENV{"REQUEST_METHOD"}
| |||||||||||||||||||||||||||||
| 58 | 3 | fail($EXIT_USAGE, App::Syslogd->i18n('not_cgi') . "\n"); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Only options the user actually gave are passed on, so App::Syslogd's | ||||||||||||||||||||||||||||
| 62 | # %DEFAULTS stay the single place where defaults are defined | ||||||||||||||||||||||||||||
| 63 | 19 | my %opts; | |||||||||||||||||||||||||||
| 64 | 19 | ▶ | GetOptions(\%opts, 'port=i', 'address=s', 'file=s', 'resolve!', 'language=s') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | or fail($EXIT_USAGE, App::Syslogd->i18n('usage', { program => $0 }) . "\n"); | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # Bind first so a busy port is reported before anything is written | ||||||||||||||||||||||||||||
| 68 | 10 | ▶ | my $server = eval { App::Syslogd->new(\%opts)->open_socket()->reopen_log() } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 69 | or fail($EXIT_FAILURE, $@); | ||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 2 | print $server->i18n('listening', { address => $server->address(), port => $server->port() }), "\n"; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # Returns only on SIGTERM or SIGINT | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | eval { $server->run(); 1 } or fail($EXIT_FAILURE, $@); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | print $server->i18n('shutdown', { count => $server->count() }), "\n"; | |||||||||||||||||||||||||||
| 77 | 2 | exit($EXIT_OK); | |||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | __END__ | ||||||||||||||||||||||||||||