Make the Perl helper script somewhat cleaner.

- Avoid duplicate check* methods.
- Remove some broken valgrind behaviour.
- Call executable instead of hardcoding the version.
- Simplify $binpath/$executable code.
This commit is contained in:
Sadie Powell 2024-08-10 03:01:48 +01:00
parent e28ba1739c
commit 7b6b3b9f35

View File

@ -60,19 +60,17 @@ use constant {
GENERIC_EXIT_NOT_RUNNING => 7 GENERIC_EXIT_NOT_RUNNING => 7
}; };
my $scriptpath = "@SCRIPT_DIR@"; my $configdir = "@CONFIG_DIR@";
my $confpath = "@CONFIG_DIR@"; my @debugargs = qw(--nolog --nopid --protocoldebug);
my $binpath = "@BINARY_DIR@"; my $executable = "@BINARY_DIR@/inspircd";
my $runtimedir = "@RUNTIME_DIR@"; my $runtimedir = "@RUNTIME_DIR@";
my $valgrindlogpath = "@LOG_DIR@/valgrind"; my $valgrindlogdir = "@LOG_DIR@/valgrind";
my $executable = "inspircd";
my $version = "@VERSION_FULL@";
my @gdbargs = ( my @gdbargs = (
'--eval-command', 'set pagination off', '--eval-command', 'set pagination off',
'--eval-command', 'handle all nostop pass print', '--eval-command', 'handle all nostop pass print',
'--eval-command', 'run', '--eval-command', 'run',
'--args', "$binpath/$executable", qw(--nofork --nolog --protocoldebug) '--args', $executable, @debugargs,
); );
sub expand_fragment($$) { sub expand_fragment($$) {
@ -97,7 +95,7 @@ for my $a (@ARGV)
} }
} }
if (!defined $conf) { if (!defined $conf) {
$conf = expand_fragment $confpath, "inspircd.conf"; $conf = expand_fragment $configdir, "inspircd.conf";
push @ARGV, '--config='.$conf; push @ARGV, '--config='.$conf;
} }
@ -169,8 +167,7 @@ sub cmd_cron()
sub cmd_version() sub cmd_version()
{ {
print "InspIRCd version: $version\n"; exec $executable, '--version' or die "Unable to execute $executable: $!"
exit GENERIC_EXIT_SUCCESS;
} }
sub cmd_restart(@) sub cmd_restart(@)
@ -180,23 +177,16 @@ sub cmd_restart(@)
goto &cmd_start(@_); goto &cmd_start(@_);
} }
sub hid_cheese_sandwich()
{
print "Creating Cheese Sandwich..\n";
print "Done.\n";
exit GENERIC_EXIT_SUCCESS;
}
sub cmd_start(@) sub cmd_start(@)
{ {
# Check to see its not 'running' already. # Check to see its not 'running' already.
if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; } if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
# If we are still alive here.. Try starting the IRCd.. # If we are still alive here.. Try starting the IRCd..
print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$executable doesn't exist\n" and return 0 unless(-e $executable);
print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); print "$executable is not executable\n" and return 0 unless(-f $executable && -x $executable);
exec "$binpath/$executable", @_; exec $executable, @_;
die "Failed to start IRCd: $!\n"; die "Failed to start IRCd: $!\n";
} }
@ -205,11 +195,11 @@ sub dev_debug(@)
# Check to see its not 'running' already. # Check to see its not 'running' already.
if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$executable doesn't exist\n" and return 0 unless(-e $executable);
print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); print "$executable is not executable\n" and return 0 unless(-f $executable && -x $executable);
# Check we have gdb # Check we have gdb
checkgdb(); check("gdb");
# If we are still alive here.. Try starting the IRCd.. # If we are still alive here.. Try starting the IRCd..
exec 'gdb', @gdbargs, @_; exec 'gdb', @gdbargs, @_;
@ -221,11 +211,10 @@ sub dev_screendebug(@)
# Check to see its not 'running' already. # Check to see its not 'running' already.
if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$executable doesn't exist\n" and return 0 unless(-e $executable);
#Check we have gdb #Check we have gdb
checkgdb(); check("gdb", "screen");
checkscreen();
# If we are still alive here.. Try starting the IRCd.. # If we are still alive here.. Try starting the IRCd..
print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n"; print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
@ -239,16 +228,16 @@ sub dev_valdebug(@)
# Check to see its not 'running' already. # Check to see its not 'running' already.
if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$executable doesn't exist\n" and return 0 unless(-e $executable);
print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); print "$executable is not executable\n" and return 0 unless(-f $executable && -x $executable);
# Check we have valgrind # Check we have valgrind
checkvalgrind(); check("valgrind");
# If we are still alive here.. Try starting the IRCd.. # If we are still alive here.. Try starting the IRCd..
# May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
# Could be useful when we want to stop it complaining about things we're sure aren't issues. # Could be useful when we want to stop it complaining about things we're sure aren't issues.
exec qw(valgrind -v --tool=memcheck --leak-check=yes --num-callers=30), "$binpath/$executable", qw(--nofork --nolog --protocoldebug), @_; exec qw(valgrind -v --tool=memcheck --leak-check=yes --num-callers=30), $executable, @debugargs, @_;
die "Failed to start valgrind: $!\n"; die "Failed to start valgrind: $!\n";
} }
@ -258,11 +247,11 @@ sub dev_valdebug_unattended(@)
# Check to see its not 'running' already. # Check to see its not 'running' already.
if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$executable doesn't exist\n" and return 0 unless(-e $executable);
print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); print "$executable is not executable\n" and return 0 unless(-f $executable && -x $executable);
# Check we have valgrind # Check we have valgrind
checkvalgrind(); check("valgrind");
# If we are still alive here.. Try starting the IRCd.. # If we are still alive here.. Try starting the IRCd..
# #
@ -272,18 +261,14 @@ sub dev_valdebug_unattended(@)
my $pid = fork; my $pid = fork;
if ($pid == 0) { if ($pid == 0) {
POSIX::setsid(); POSIX::setsid();
-d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n"; -d $valgrindlogdir or mkdir $valgrindlogdir or die "Cannot create $valgrindlogdir: $!\n";
-e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$"; my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n"; open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n"; sysopen STDOUT, "$valgrindlogdir/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogdir/out.$suffix: $!\n";
sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n"; sysopen STDERR, "$valgrindlogdir/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogdir/valdebug.$suffix: $!\n";
# May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
# Could be useful when we want to stop it complaining about things we're sure aren't issues.
exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes), exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
"--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --), qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
"$binpath/$executable", qw(--nofork --nolog --protocoldebug), @_; $executable, @debugargs, @_;
die "Can't execute valgrind: $!\n"; die "Can't execute valgrind: $!\n";
} }
} }
@ -293,18 +278,15 @@ sub dev_screenvaldebug(@)
# Check to see its not 'running' already. # Check to see its not 'running' already.
if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$executable doesn't exist\n" and return 0 unless(-e $executable);
print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); print "$executable is not executable\n" and return 0 unless(-f $executable && -x $executable);
#Check we have gdb check("gdb", "screen", "valgrind");
checkvalgrind();
checkgdb();
checkscreen();
# If we are still alive here.. Try starting the IRCd.. # If we are still alive here.. Try starting the IRCd..
print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n"; print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
print "Once you're inside the screen session press ^C + d to re-detach from the session\n"; print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --num-callers=30), "$binpath/$executable", qw(--nofork --nolog --protocoldebug), @_; exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --num-callers=30), $executable, @debugargs, @_;
die "Failed to start screen: $!\n"; die "Failed to start screen: $!\n";
} }
@ -343,7 +325,7 @@ sub getpidfile
} }
# Expand any relative paths. # Expand any relative paths.
$file = expand_fragment $confpath, $file; $file = expand_fragment $configdir, $file;
# Have we checked this file before? # Have we checked this file before?
return if $filesparsed{$file}; return if $filesparsed{$file};
@ -387,7 +369,7 @@ sub getpidfile
} }
# End of includes / No includes found. Using default. # End of includes / No includes found. Using default.
$pidfile = $runtimedir . "/inspircd.pid"; $pidfile = expand_fragment $runtimedir, "inspircd.pid";
} }
sub getstatus { sub getstatus {
@ -408,29 +390,14 @@ sub getprocessid {
return $pid; return $pid;
} }
sub checkvalgrind sub check(@)
{ {
unless(`valgrind --version`) for my $tool (@_)
{ {
print "Couldn't start valgrind: $!\n"; unless(`$tool --version`)
exit GENERIC_EXIT_UNSPECIFIED; {
} print "Couldn't start $tool: $!\n";
} exit GENERIC_EXIT_UNSPECIFIED;
}
sub checkgdb
{
unless(`gdb --version`)
{
print "Couldn't start gdb: $!\n";
exit GENERIC_EXIT_UNSPECIFIED;
}
}
sub checkscreen
{
unless(`screen --version`)
{
print "Couldn't start screen: $!\n";
exit GENERIC_EXIT_UNSPECIFIED;
} }
} }