2 # +------------------------------------+
3 # | Inspire Internet Relay Chat Daemon |
4 # +------------------------------------+
6 # (C) 2002-2008 InspIRCd Development Team
7 # http://www.inspircd.org/wiki/index.php/Credits
9 # Written by Craig Edwards, Craig McLure, and others.
10 # This program is free but copyrighted software; see
11 # the file COPYING for details.
13 # ---------------------------------------------------
16 my $basepath = "@BASE_DIR@";
17 my $confpath = "@CONFIG_DIR@/";
18 my $binpath = "@BINARY_DIR@";
19 my $libpath = "@LIBRARY_DIR@";
20 my $valgrindlogpath = "$basepath/valgrindlogs";
21 my $executable = "@EXECUTABLE@";
22 my $version = "@VERSION@";
26 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
28 getpidfile($confpath."inspircd.conf");
30 if ($arg eq "start") { start(); exit(); }
31 if ($arg eq "debug") { debug(); exit(); }
32 if ($arg eq "screendebug") { screendebug(); exit() }
33 if ($arg eq "valdebug") { valdebug(); exit(); }
34 if ($arg eq "valdebug-unattended") { valdebugunattended(); exit(); }
35 if ($arg eq "screenvaldebug") { screenvaldebug(); exit(); }
36 if ($arg eq "stop") { stop(); exit(); }
37 if ($arg eq "status") {
38 if (getstatus() == 1) {
39 my $pid = getprocessid();
40 print "InspIRCd is running (PID: $pid)\n";
43 print "InspIRCd is not running. (Or PID File not found)\n";
47 if ($arg eq "rehash") {
48 if (getstatus() == 1) {
49 my $pid = getprocessid();
50 system("kill -HUP $pid >/dev/null 2>&1");
51 print "InspIRCd rehashed (pid: $pid).\n";
54 print "InspIRCd is not running. (Or PID File not found)\n";
60 if (getstatus() == 0) { start(); }
64 if ($arg eq "version") {
65 print "InspIRCd version: $version\n";
69 if ($arg eq "restart") {
71 unlink($pidfile) if (-e $pidfile);
77 if ($arg eq "checkconf") {
82 if ($arg eq "Cheese-Sandwich") {
83 print "Creating Cheese Sandwich..\n";
89 # If we get here.. bad / no parameters.
91 print "Invalid Argument: $arg\n";
92 print "Usage: inspircd (start|stop|restart|rehash|status|cron|checkconf|version)\n";
93 print "Developer arguments: (debug|screendebug|valdebug|valdebug-unattended|screenvaldebug)\n";
97 # Generic Helper Functions.
101 # Check to see its not 'running' already.
102 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
103 # If we are still alive here.. Try starting the IRCd..
104 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
105 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
107 system("$binpath/$executable");
112 # Check to see its not 'running' already.
113 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
115 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
116 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
121 # If we are still alive here.. Try starting the IRCd..
122 system("gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug");
127 # Check to see its not 'running' already.
128 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
130 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
136 # If we are still alive here.. Try starting the IRCd..
137 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
138 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
139 system("screen -m -d gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
144 # Check to see its not 'running' already.
145 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
147 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
148 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
150 # Check we have valgrind and gdb
154 # If we are still alive here.. Try starting the IRCd..
155 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
156 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
157 system("valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
160 sub valdebugunattended
162 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
163 # Check to see its not 'running' already.
164 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
166 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
167 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
169 # Check we have valgrind and gdb
173 # If we are still alive here.. Try starting the IRCd..
174 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
175 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
177 # NOTE: Saving the debug log (redirected stdout), while useful, is a potential security risk AND one hell of a spacehog. DO NOT SAVE THIS WHERE EVERYONE HAS ACCESS!
178 # Redirect stdout to /dev/null if you're worried about the security.
180 # Note the & at the end. We *must* detach otherwise the script hangs.
181 system("valgrind -v --tool=memcheck --leak-check=yes --num-callers=10 --time-stamp=yes --log-file=$valgrindlogpath/valdebug.%p $binpath/$executable -nofork -debug -nolog >> $basepath/valdebug.insp.out &");
182 # Sleep 5 seconds to give valgrind time to initialize before chmodding the logdir for apache access.
184 system("chmod 644 $valgrindlogpath/*");
189 # Check to see its not 'running' already.
190 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
192 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
193 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
200 # If we are still alive here.. Try starting the IRCd..
201 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
202 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
203 system("screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
207 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
208 # Get to here, we have something to kill.
209 my $pid = getprocessid();
210 print "Stopping InspIRCd (pid: $pid)...\n";
211 system("kill -TERM $pid >/dev/null 2>&1");
212 # Give it twenty seconds to exit, otherwise valgrind can't write out all output before being killed -9
213 # TODO make this a variable number based on if it's valgrind or normal start.
215 if (getstatus() == 1)
217 print "InspIRCd not dying quietly -- forcing kill\n";
218 system("kill -9 $pid >/dev/null 2>&1");
220 print "InspIRCd Stopped.\n";
223 # GetPidfile Version 2 - Now With Include Support..
224 # I beg for months for include support in insp, then..
225 # when it is added, it comes around and BITES ME IN THE ASS,
226 # because i then have to code support into this script.. Evil.
228 # Craig got bitten in the ass again --
229 # in 1.1 beta the include file is manditory, therefore
230 # if we cant find it, default to %conf%/inspircd.pid.
231 # Note, this also contains a fix for when the pid file is
232 # defined, but defined in a comment (line starts with #)
237 # Before we start, do we have a PID already? (Should never occur)
241 # Are We using a relative path?
242 if ($file !~ /^\//) {
243 # Convert it to a full path..
244 $file = $confpath . $file;
247 # Have we checked this file before?
248 for (my $i = 0; $i < $filesparsed; $i++) {
249 if ($filesparsed[$i] eq $file) {
250 # Already Parsed, Possible recursive loop..
255 # If we get here, Mark as 'Read'
256 $filesparsed[$filesparsed] = $file;
259 open INFILE, "< $file" or die "Unable to open file $file\n";
260 # Grab entire file contents..
261 my(@lines) = <INFILE>;
265 # remove trailing spaces
267 foreach $i (@lines) {
269 $i =~ s/[^=]+=\s(.*)/\1/;
270 # Does this file have a pid?
271 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
273 # Set the PID file and return.
279 # If we get here, NO PID FILE! -- Check for includes
280 foreach $i (@lines) {
281 $i =~ s/[^=]+=\s(.*)/\1/;
282 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
284 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
287 if ($pidfile ne "") {
294 # End of includes / No includes found. Using default.
295 $pidfile = $confpath . "inspircd.pid";
299 my $pid = getprocessid();
300 if ($pid == 0) { return 0; }
301 $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
302 if ($status == 0) { return 1; }
309 open PIDFILE, "< $pidfile" or return 0;
310 while($i = <PIDFILE>)
320 unless(`valgrind --version`)
322 print "Couldn't start valgrind: $!\n";
329 unless(`gdb --version`)
331 print "Couldn't start gdb: $!\n";
338 unless(`screen --version`)
340 print "Couldn't start screen: $!\n";
347 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
353 validateconf($confpath."inspircd.conf");
354 print "Config check complete\n";
361 # Are We using a relative path?
362 if ($file !~ /^\//) {
363 # Convert it to a full path..
364 $file = $confpath . $file;
367 # Have we checked this file before?
368 for (my $i = 0; $i < $filechecked; $i++) {
369 if ($filechecked[$i] eq $file) {
370 # Already Parsed, Possible recursive loop..
375 # If we get here, Mark as 'Read'
376 $filechecked[$filechecked] = $file;
379 open INFILE, "< $file" or die "Unable to open file $file\n";
380 # Grab entire file contents..
381 my(@lines) = <INFILE>;
385 # remove trailing spaces
392 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
393 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
394 push @newlines, "<config>";
398 # remove trailing newlines
401 # convert tabs to spaces
404 # remove leading spaces
413 # remove trailing comments
416 for (my $j = 0; $j < length($i); $j++)
418 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
419 $line .= substr($i,$j, 1);
423 # remove trailing spaces
426 # setup incf for include check and clean it up, since this breaks parsing use local var
428 $incf =~ s/[^=]+=\s(.*)/\1/;
431 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
437 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
439 push @newlines, $conline;
440 push @newlines, @blanks;
446 $i =~ s/(.*)>$/\1 \/>/;
452 $conline =~ s/(.*)>$/\1 \/>/;
454 push @newlines, $conline;
455 push @newlines, @blanks;
463 elsif ($conline =~ /^<.*/ && $i)
482 push @newlines, $conline;
483 push @newlines, @blanks;
486 push @newlines, "</config>";
492 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
494 foreach $n (@newlines)
500 my @result = `xmllint -noout $tmpfile 2>&1`;
511 $n =~ s/$tmpfile\:\d*\: *//g;
512 if ($n =~ /.*config>.*/)
520 if ($n =~ /line \d*/)
523 $lineno =~ s/.*line (\d*).*/\1/;
525 $n =~ s/line (\d*)/line $lineno/;
527 print "$file : $n\n";