From ceeab16c8e13ff0f96b4142d791b80e8657ace69 Mon Sep 17 00:00:00 2001 From: Jacob Bachmeyer Date: Wed, 5 Oct 2022 23:18:48 -0500 Subject: [PATCH] Move syslog initialization and final configuration check to preliminaries This also required moving the message reporting subs farther up, since this code uses ftp_syslog and ftp_die. --- gatekeeper.pl | 99 ++++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 49 deletions(-) diff --git a/gatekeeper.pl b/gatekeeper.pl index 666efee..c32dabc 100755 --- a/gatekeeper.pl +++ b/gatekeeper.pl @@ -97,6 +97,43 @@ # Imported into Git by Jacob Bachmeyer (jcb@gnu.org), July 2020 # Further changes are tracked in Git. + +# +# - Message reporting +# + +sub ftp_syslog { + my ($priority,$message) = @_; + + # Remove a trailing newline + $message =~ s/[\r\n]+$//; + # Collapse the message to a single line for syslog + $message =~ s/[\r\n]+/ \/ /g; + + # The syslog function is pretty picky, and (sometimes) dies silently + # when using non-valid syslog priorities. + # That's why we run it inside an eval, and print out any errors to STDERR. + eval { + syslog($priority, $message); + }; + if ($@) { + print STDERR "$@\n"; + } +} + +sub ftp_warn($) { + ftp_syslog('warning', "($log_style) " . $_[0]); + warn $_[0]; +} + +sub ftp_die($;$) { + my $msg = shift; + my $exitcode = shift; + $exitcode ||= 1; + ftp_syslog('err', "($log_style) " . $msg); + exit $exitcode; +} + # # - Initialization and preliminaries @@ -329,41 +366,21 @@ if (IN_TEST_MODE) { # override the above for testing my %info; # package being processed; a global so fatal and mail can use it - -# -# - Message reporting and email -# - -sub ftp_syslog { - my ($priority,$message) = @_; - - # Remove a trailing newline - $message =~ s/[\r\n]+$//; - # Collapse the message to a single line for syslog - $message =~ s/[\r\n]+/ \/ /g; - - # The syslog function is pretty picky, and (sometimes) dies silently - # when using non-valid syslog priorities. - # That's why we run it inside an eval, and print out any errors to STDERR. - eval { - syslog($priority, $message); - }; - if ($@) { - print STDERR "$@\n"; - } -} - -sub ftp_warn($) { - ftp_syslog('warning', "($log_style) " . $_[0]); - warn $_[0]; +# Initialize our syslogging +if (IN_TEST_MODE) { + $ENV{TEST_SYSLOG_SOCKET} =~ m/^([[:alnum:]\/]+)$/ + or die "strange test syslog socket"; + -S $1 or die "test syslog socket is not a socket"; + setlogsock(unix => $1); } +openlog("ftp-upload", 'pid', $facility); +ftp_syslog('info', "($log_style) Beginning upload processing run."); -sub ftp_die($;$) { - my $msg = shift; - my $exitcode = shift; - $exitcode ||= 1; - ftp_syslog('err', "($log_style) " . $msg); - exit $exitcode; +# make sure our directories all exist, or it's hopeless. +# Use die instead of fatal - this error should "never" happen. +for my $dir ($package_config_base, $incoming_dir, $incoming_tmp, + $destfinal, $desttmp) { + -d $dir || ftp_die("FATAL: configuration problem, $dir is not a directory"); } @@ -1450,22 +1467,6 @@ sub cleanup { # - Main execution path # -# Initialize our syslogging -if (IN_TEST_MODE) { - $ENV{TEST_SYSLOG_SOCKET} =~ m/^([[:alnum:]\/]+)$/ - or die "strange test syslog socket"; - -S $1 or die "test syslog socket is not a socket"; - setlogsock(unix => $1); -} -openlog("ftp-upload", 'pid', $facility); -ftp_syslog('info', "($log_style) Beginning upload processing run."); - -# make sure our directories all exist, or it's hopeless. -# Use die instead of fatal - this error should "never" happen. -for my $dir ($package_config_base, $incoming_dir, $incoming_tmp, - $destfinal, $desttmp) { - -d $dir || ftp_die("FATAL: configuration problem, $dir is not a directory"); -} # the chdir simplifies our filename parsing, so the base names don't # have any directory. chdir ($incoming_dir) || ftp_die("FATAL: chdir($incoming_dir) failed: $!"); -- 2.25.1