X-Git-Url: https://git.netwichtig.de/gitweb/?a=blobdiff_plain;f=make%2Ftemplate%2Finspircd;h=c50d28c7fa0c337f3fa81417085f605be2c949a1;hb=ce05e885a9258139eb7b076c29a70b5f8d366209;hp=6a74d1ec1429be9f798659b695a33be44cb02715;hpb=cd712c40e1b352c05e7ae0f72e0a5e84cdf64323;p=user%2Fhenk%2Fcode%2Finspircd.git diff --git a/make/template/inspircd b/make/template/inspircd index 6a74d1ec1..c50d28c7f 100644 --- a/make/template/inspircd +++ b/make/template/inspircd @@ -1,27 +1,107 @@ -#!/usr/bin/perl -# +------------------------------------+ -# | Inspire Internet Relay Chat Daemon | -# +------------------------------------+ +%mode 0750 +#!/usr/bin/env perl # -# InspIRCd: (C) 2002-2010 InspIRCd Development Team -# See: http://wiki.inspircd.org/Credits +# InspIRCd -- Internet Relay Chat Daemon # -# This program is free but copyrighted software; see -# the file COPYING for details. +# Copyright (C) 2015 Steven Van Acker +# Copyright (C) 2015 Attila Molnar +# Copyright (C) 2014 Dan Parsons +# Copyright (C) 2013-2014, 2016-2019 Sadie Powell +# Copyright (C) 2012 Robby +# Copyright (C) 2012 Adam +# Copyright (C) 2011 DjSlash +# Copyright (C) 2009-2010 Daniel De Graaf +# Copyright (C) 2008-2009 Robin Burchell +# Copyright (C) 2008 Thomas Stagner +# Copyright (C) 2007 Dennis Friis +# Copyright (C) 2006 Oliver Lupton +# Copyright (C) 2006 John Brooks +# Copyright (C) 2005-2006, 2008-2009 Craig Edwards +# Copyright (C) 2005 Craig McLure # -# --------------------------------------------------- +# This file is part of InspIRCd. InspIRCd is free software: you can +# redistribute it and/or modify it under the terms of the GNU General Public +# License as published by the Free Software Foundation, version 2. # +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more +# details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# + +# InspIRCd Start up the InspIRCd Internet Relay Chat Daemon +# +# chkconfig: 2345 55 25 +# description: InspIRCd -- Internet Relay Chat Daemon +# +# processname: inspircd + use strict; use POSIX; use Fcntl; +# From http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html +use constant { + STATUS_EXIT_SUCCESS => 0, + STATUS_EXIT_DEAD_WITH_PIDFILE => 1, + STATUS_EXIT_DEAD_WITH_LOCKFILE => 2, + STATUS_EXIT_NOT_RUNNING => 3, + STATUS_EXIT_UNKNOWN => 4, + + GENERIC_EXIT_SUCCESS => 0, + GENERIC_EXIT_UNSPECIFIED => 1, + GENERIC_EXIT_INVALID_ARGUMENTS => 2, + GENERIC_EXIT_UNIMPLEMENTED => 3, + GENERIC_EXIT_INSUFFICIENT_PRIVILEGE => 4, + GENERIC_EXIT_NOT_INSTALLED => 5, + GENERIC_EXIT_NOT_CONFIGURED => 6, + GENERIC_EXIT_NOT_RUNNING => 7 +}; + +my $scriptpath = "@SCRIPT_DIR@"; my $basepath = "@BASE_DIR@"; -my $confpath = "@CONFIG_DIR@/"; +my $confpath = "@CONFIG_DIR@"; my $binpath = "@BINARY_DIR@"; -my $runpath = "@BASE_DIR@"; +my $runtimedir = "@RUNTIME_DIR@"; my $valgrindlogpath = "$basepath/valgrindlogs"; -my $executable = "@EXECUTABLE@"; -my $version = "@VERSION@"; +my $executable = "inspircd"; +my $version = "@VERSION_FULL@"; +my $uid = "@UID@"; + +my @gdbargs = ( + '--eval-command', 'handle SIGPIPE pass nostop noprint', + '--eval-command', 'handle SIGHUP pass nostop noprint', + '--eval-command', 'run', + '--args', "$binpath/$executable", qw(--nofork --nolog --debug) +); + +sub expand_fragment($$) { + my ($base, $fragment) = @_; + if ($fragment =~ /^\//) { + return $fragment; + } else { + return "$base/$fragment"; + } +} + +if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) { + if ($uid !~ /^\d+$/) { + # Named UID, look it up + $uid = getpwnam $uid; + } + if (!$uid) { + die "Cannot find a valid UID to change to"; + } + # drop root if we were configured with an ircd UID + $< = $uid; + $> = $uid; + if ($< == 0 || $> == 0) { + die "Could not drop root: $!"; + } +} our($pid,$pidfile); # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder) @@ -36,7 +116,7 @@ for my $a (@ARGV) } } if (!defined $conf) { - $conf = $confpath . "inspircd.conf"; + $conf = expand_fragment $confpath, "inspircd.conf"; push @ARGV, '--config='.$conf; } @@ -55,12 +135,11 @@ if (!defined($sub)) { print STDERR "Invalid command or none given.\n"; cmd_help(); - exit 1; + exit GENERIC_EXIT_UNIMPLEMENTED; } else { - $sub->(@ARGV); - exit 0; + exit $sub->(@ARGV); # Error code passed through return value } sub cmd_help() @@ -69,11 +148,11 @@ sub cmd_help() my @cmds = grep /^cmd_/, @subs; my @devs = grep /^dev_/, @subs; local $_; - $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs); - $_ =~ s/_/-/g foreach (@cmds, @devs); + $_ =~ s/^(cmd|dev)_// for (@cmds, @devs); + $_ =~ s/_/-/g for (@cmds, @devs); print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n"; print STDERR "Developer arguments: (" . join("|", @devs) . ")\n"; - exit 0; + exit GENERIC_EXIT_SUCCESS; } sub cmd_status() @@ -81,10 +160,10 @@ sub cmd_status() if (getstatus() == 1) { my $pid = getprocessid(); print "InspIRCd is running (PID: $pid)\n"; - exit(); + exit STATUS_EXIT_SUCCESS; } else { print "InspIRCd is not running. (Or PID File not found)\n"; - exit(); + exit STATUS_EXIT_NOT_RUNNING; } } @@ -92,47 +171,47 @@ sub cmd_rehash() { if (getstatus() == 1) { my $pid = getprocessid(); - system("kill -HUP $pid >/dev/null 2>&1"); + kill HUP => $pid; print "InspIRCd rehashed (pid: $pid).\n"; - exit(); + exit GENERIC_EXIT_SUCCESS; } else { print "InspIRCd is not running. (Or PID File not found)\n"; - exit(); + exit GENERIC_EXIT_NOT_RUNNING; } } sub cmd_cron() { - if (getstatus() == 0) { goto &cmd_start(); } - exit(); + if (getstatus() == 0) { goto &cmd_start(@_); } + exit GENERIC_EXIT_UNSPECIFIED; } sub cmd_version() { print "InspIRCd version: $version\n"; - exit(); + exit GENERIC_EXIT_SUCCESS; } sub cmd_restart(@) { cmd_stop(); unlink($pidfile) if (-e $pidfile); - goto &cmd_start; + goto &cmd_start(@_); } sub hid_cheese_sandwich() { print "Creating Cheese Sandwich..\n"; print "Done.\n"; - exit(); + exit GENERIC_EXIT_SUCCESS; } sub cmd_start(@) { # 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"; exit GENERIC_EXIT_SUCCESS; } + # If we are still alive here.. Try starting the IRCd.. - chdir $runpath; print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); @@ -145,7 +224,6 @@ sub dev_debug(@) # Check to see its not 'running' already. if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } - chdir $runpath; print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); @@ -153,7 +231,7 @@ sub dev_debug(@) checkgdb(); # If we are still alive here.. Try starting the IRCd.. - exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_; + exec 'gdb', @gdbargs, @_; die "Failed to start GDB: $!\n"; } @@ -162,7 +240,6 @@ sub dev_screendebug(@) # Check to see its not 'running' already. if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } - chdir $runpath; print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); #Check we have gdb @@ -172,7 +249,7 @@ sub dev_screendebug(@) # 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 "Once you're inside the screen session press ^C + d to re-detach from the session\n"; - exec qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_; + exec qw(screen -m -d gdb), @gdbargs, @_; die "Failed to start screen: $!\n"; } @@ -181,7 +258,6 @@ sub dev_valdebug(@) # Check to see its not 'running' already. if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } - chdir $runpath; print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); @@ -192,7 +268,7 @@ sub dev_valdebug(@) # 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 # 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 --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_; + exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_; die "Failed to start valgrind: $!\n"; } @@ -202,7 +278,6 @@ sub dev_valdebug_unattended(@) # Check to see its not 'running' already. if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } - chdir $runpath; print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); @@ -226,10 +301,10 @@ sub dev_valdebug_unattended(@) sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/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=15 --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 --), - "$binpath/$executable", qw(-nofork -debug -nolog), @_; + "$binpath/$executable", qw(--nofork --debug --nolog), @_; die "Can't execute valgrind: $!\n"; } } @@ -239,7 +314,6 @@ sub dev_screenvaldebug(@) # Check to see its not 'running' already. if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; } - chdir $runpath; print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable"); print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable"); @@ -251,46 +325,34 @@ sub dev_screenvaldebug(@) # 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 "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 --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_; + exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_; die "Failed to start screen: $!\n"; } sub cmd_stop() { - if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; } + if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; } # Get to here, we have something to kill. my $pid = getprocessid(); print "Stopping InspIRCd (pid: $pid)...\n"; - my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 15; + my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15; kill TERM => $pid or die "Cannot terminate IRCd: $!\n"; for (1..$maxwait) { sleep 1; if (getstatus() == 0) { print "InspIRCd Stopped.\n"; - return; + return GENERIC_EXIT_SUCCESS; } } print "InspIRCd not dying quietly -- forcing kill\n"; kill KILL => $pid; - return 0; + return GENERIC_EXIT_SUCCESS; } ### # Generic Helper Functions. ### -# GetPidfile Version 2 - Now With Include Support.. -# I beg for months for include support in insp, then.. -# when it is added, it comes around and BITES ME IN THE ASS, -# because i then have to code support into this script.. Evil. - -# Craig got bitten in the ass again -- -# in 1.1 beta the include file is manditory, therefore -# if we cant find it, default to %conf%/inspircd.pid. -# Note, this also contains a fix for when the pid file is -# defined, but defined in a comment (line starts with #) -# -- Brain - my %filesparsed; sub getpidfile @@ -300,11 +362,9 @@ sub getpidfile if ($pid ne "") { return; } - # Are We using a relative path? - if ($file !~ /^\//) { - # Convert it to a full path. - $file = $runpath .'/'. $file; - } + + # Expand any relative paths. + $file = expand_fragment $confpath, $file; # Have we checked this file before? return if $filesparsed{$file}; @@ -326,16 +386,7 @@ sub getpidfile if (($i =~ //i) && ($i !~ /^#/)) { # Set the PID file and return. - $pidfile = $1; - if (-f $pidfile) - { - return; - } - elsif (-f "$runpath/$pidfile") - { - $pidfile = "$runpath/$pidfile"; - return; - } + $pidfile = expand_fragment $runtimedir, $1; return; } } @@ -357,7 +408,7 @@ sub getpidfile } # End of includes / No includes found. Using default. - $pidfile = $runpath . "/data/inspircd.pid"; + $pidfile = $runtimedir . "/inspircd.pid"; } sub getstatus { @@ -368,11 +419,11 @@ sub getstatus { sub getprocessid { - my $pid; - open PIDFILE, "< $pidfile" or return 0; + my $pid = 0; + open PIDFILE, '<', $pidfile or return 0; while() { - $pid = $_; + /^(\d+)$/ and $pid = $1; } close PIDFILE; return $pid; @@ -383,7 +434,7 @@ sub checkvalgrind unless(`valgrind --version`) { print "Couldn't start valgrind: $!\n"; - exit; + exit GENERIC_EXIT_UNSPECIFIED; } } @@ -392,7 +443,7 @@ sub checkgdb unless(`gdb --version`) { print "Couldn't start gdb: $!\n"; - exit; + exit GENERIC_EXIT_UNSPECIFIED; } } @@ -401,190 +452,6 @@ sub checkscreen unless(`screen --version`) { print "Couldn't start screen: $!\n"; - exit; - } -} - -sub checkxmllint -{ - open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n"; -} - -sub cmd_checkconf() -{ - checkxmllint(); - validateconf($conf); - print "Config check complete\n"; - exit 0; -} - -my %filechecked; - -sub validateconf -{ - my ($file) = @_; - - # Are We using a relative path? - if ($file !~ /^\//) { - # Convert it to a full path.. - $file = $runpath . $file; - } - - # Have we checked this file before? - return if $filechecked{$file}; - $filechecked{$file} = 1; - - # Open the File.. - open INFILE, "< $file" or die "Unable to open file $file\n"; - # Grab entire file contents.. - my(@lines) = ; - # Close the file - close INFILE; - - # remove trailing spaces - chomp(@lines); - - my @newlines = (); - my @blanks = (); - my $conline; - - push @newlines, ""; -# push @newlines, ""; - push @newlines, ""; - - for my $i (@lines) - { - # remove trailing newlines - chomp($i); - - # convert tabs to spaces - $i =~ s/\t/ /g; - - # remove leading spaces - $i =~ s/^ *//; - - # remove comments - $i =~ s/^#.*//; - - # remove trailing #s - $i =~ s/(.*)#$/\1/; - - # remove trailing comments - my $line = ""; - my $quote = 0; - for (my $j = 0; $j < length($i); $j++) - { - if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; } - $line .= substr($i,$j, 1); - } - $i = $line; - - # remove trailing spaces - $i =~ s/ *$//; - - # setup incf for include check and clean it up, since this breaks parsing use local var - my $incf = $i; - $incf =~ s/[^=]+=\s(.*)/\1/; - - # include file? - if (($incf =~ s/\//i) && ($incf !~ /^#/)) - { - # yes, process it - validateconf($1); - } - - if ($i =~ /^<.*/ && $conline =~ /^<.*/) - { - push @newlines, $conline; - push @newlines, @blanks; - $conline = $i; - } - - if ($i =~ /^<.*>$/) - { - $i =~ s/(.*)>$/\1 \/>/; - push @newlines, $i; - } - elsif ($i =~ /.*>$/) - { - $conline .= " $i"; - $conline =~ s/(.*)>$/\1 \/>/; - push @blanks, ""; - push @newlines, $conline; - push @newlines, @blanks; - $conline = ""; - undef @blanks; - } - elsif ($i =~ /^<.*/) - { - $conline = $i; - } - elsif ($conline =~ /^<.*/ && $i) - { - $conline .= " $i"; - push @blanks, ""; - } - else - { - if ($conline) - { - push @blanks, $i; - } - else - { - push @newlines, $i; - } - } - } - if ($conline) - { - push @newlines, $conline; - push @newlines, @blanks; - } - - push @newlines, ""; - - my $tmpfile; - do - { - $tmpfile = tmpnam(); - } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700); - - for my $n (@newlines) - { - print TF "$n\n"; - } - close TF; - - my @result = `xmllint -noout $tmpfile 2>&1`; - chomp(@result); - - my $skip = 0; - for my $n (@result) - { - if ($skip) - { - $skip = 0; - next; - } - $n =~ s/$tmpfile\:\d*\: *//g; - if ($n =~ /.*config>.*/) - { - $n = ""; - $skip = 1; - } - - if ($n && !$skip) - { - if ($n =~ /line \d*/) - { - my $lineno = $n; - $lineno =~ s/.*line (\d*).*/\1/; - $lineno = $lineno-2; - $n =~ s/line (\d*)/line $lineno/; - } - print "$file : $n\n"; - } + exit GENERIC_EXIT_UNSPECIFIED; } - unlink($tmpfile); }