2 # +------------------------------------+
3 # | Inspire Internet Relay Chat Daemon |
4 # +------------------------------------+
6 # Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
8 # <brain@chatspike.net>
9 # <Craig@chatspike.net>
11 # Written by Craig Edwards, Craig McLure, and others.
12 # This program is free but copyrighted software; see
13 # the file COPYING for details.
15 # I HATE PERL.. kthxbye
16 # ---------------------------------------------------
19 my $basepath = "@BASE_DIR@";
20 my $confpath = "@CONFIG_DIR@/";
21 my $binpath = "@BINARY_DIR@";
22 my $libpath = "@LIBRARY_DIR@";
23 my $executable = "@EXECUTABLE@";
24 my $version = "@VERSION@";
28 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
30 getpidfile($confpath."inspircd.conf");
32 if ($arg eq "start") { start(); exit(); }
33 if ($arg eq "debug") { debug(); exit(); }
34 if ($arg eq "screendebug") { screendebug(); exit() }
35 if ($arg eq "valdebug") { valdebug(); exit(); }
36 if ($arg eq "screenvaldebug") { screenvaldebug(); exit(); }
37 if ($arg eq "stop") { stop(); exit(); }
38 if ($arg eq "status") {
39 if (getstatus() == 1) {
40 my $pid = getprocessid();
41 print "InspIRCd is running (PID: $pid)\n";
44 print "InspIRCd is not running. (Or PID File not found)\n";
48 if ($arg eq "rehash") {
49 if (getstatus() == 1) {
50 my $pid = getprocessid();
51 system("kill -HUP $pid >/dev/null 2>&1");
52 print "InspIRCd rehashed (pid: $pid).\n";
55 print "InspIRCd is not running. (Or PID File not found)\n";
61 if (getstatus() == 0) { start(); }
65 if ($arg eq "version") {
66 print "InspIRCd version: $version\n";
70 if ($arg eq "restart") {
72 unlink($pidfile) if (-e $pidfile);
78 if ($arg eq "checkconf") {
83 if ($arg eq "Cheese-Sandwich") {
84 print "Creating Cheese Sandwich..\n";
90 # If we get here.. bad / no parameters.
92 print "Invalid Argument: $arg\n";
93 print "Usage: inspircd (start|stop|restart|rehash|status|cron|checkconf|version)\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");
106 system("$binpath/$executable");
111 # Check to see its not 'running' already.
112 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
114 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
119 # If we are still alive here.. Try starting the IRCd..
120 system("gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
125 # Check to see its not 'running' already.
126 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
128 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
134 # If we are still alive here.. Try starting the IRCd..
135 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
136 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
137 system("screen -m -d gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
142 # Check to see its not 'running' already.
143 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
145 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
147 # Check we have valgrind and gdb
151 # If we are still alive here.. Try starting the IRCd..
152 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
153 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
154 system("valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
159 # Check to see its not 'running' already.
160 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
162 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
169 # If we are still alive here.. Try starting the IRCd..
170 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
171 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
172 system("screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
176 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
177 # Get to here, we have something to kill.
178 my $pid = getprocessid();
179 print "Stopping InspIRCd (pid: $pid)...\n";
180 system("kill -TERM $pid >/dev/null 2>&1");
181 # Give it a second to exit
183 if (getstatus() == 1)
185 print "InspIRCd not dying quietly -- forcing kill\n";
186 system("kill -9 $pid >/dev/null 2>&1");
188 print "InspIRCd Stopped.\n";
191 # GetPidfile Version 2 - Now With Include Support..
192 # I beg for months for include support in insp, then..
193 # when it is added, it comes around and BITES ME IN THE ASS,
194 # because i then have to code support into this script.. Evil.
196 # Craig got bitten in the ass again --
197 # in 1.1 beta the include file is manditory, therefore
198 # if we cant find it, default to %conf%/inspircd.pid.
199 # Note, this also contains a fix for when the pid file is
200 # defined, but defined in a comment (line starts with #)
205 # Before we start, do we have a PID already? (Should never occur)
209 # Are We using a relative path?
210 if ($file !~ /^\//) {
211 # Convert it to a full path..
212 $file = $confpath . $file;
215 # Have we checked this file before?
216 for (my $i = 0; $i < $filesparsed; $i++) {
217 if ($filesparsed[$i] eq $file) {
218 # Already Parsed, Possible recursive loop..
223 # If we get here, Mark as 'Read'
224 $filesparsed[$filesparsed] = $file;
227 open INFILE, "< $file" or die "Unable to open file $file\n";
228 # Grab entire file contents..
229 my(@lines) = <INFILE>;
233 # remove trailing spaces
235 foreach $i (@lines) {
237 $i =~ s/[^=]+=\s(.*)/\1/;
238 # Does this file have a pid?
239 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
241 # Set the PID file and return.
247 # If we get here, NO PID FILE! -- Check for includes
248 foreach $i (@lines) {
249 $i =~ s/[^=]+=\s(.*)/\1/;
250 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
252 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
255 if ($pidfile ne "") {
262 # End of includes / No includes found. Using default.
263 $pidfile = $confpath . "inspircd.pid";
267 my $pid = getprocessid();
268 if ($pid == 0) { return 0; }
269 $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
270 if ($status == 0) { return 1; }
277 open PIDFILE, "< $pidfile" or return 0;
278 while($i = <PIDFILE>)
288 unless(`valgrind --version`)
290 print "Couldn't start valgrind: $!\n";
297 unless(`gdb --version`)
299 print "Couldn't start gdb: $!\n";
306 unless(`screen --version`)
308 print "Couldn't start screen: $!\n";
315 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
321 validateconf($confpath."inspircd.conf");
322 print "Config check complete\n";
329 # Are We using a relative path?
330 if ($file !~ /^\//) {
331 # Convert it to a full path..
332 $file = $confpath . $file;
335 # Have we checked this file before?
336 for (my $i = 0; $i < $filechecked; $i++) {
337 if ($filechecked[$i] eq $file) {
338 # Already Parsed, Possible recursive loop..
343 # If we get here, Mark as 'Read'
344 $filechecked[$filechecked] = $file;
347 open INFILE, "< $file" or die "Unable to open file $file\n";
348 # Grab entire file contents..
349 my(@lines) = <INFILE>;
353 # remove trailing spaces
360 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
361 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
362 push @newlines, "<config>";
366 # remove trailing newlines
369 # convert tabs to spaces
372 # remove leading spaces
381 # remove trailing comments
384 for (my $j = 0; $j < length($i); $j++)
386 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
387 $line .= substr($i,$j, 1);
391 # remove trailing spaces
394 # setup incf for include check and clean it up, since this breaks parsing use local var
396 $incf =~ s/[^=]+=\s(.*)/\1/;
399 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
405 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
407 push @newlines, $conline;
408 push @newlines, @blanks;
414 $i =~ s/(.*)>$/\1 \/>/;
420 $conline =~ s/(.*)>$/\1 \/>/;
422 push @newlines, $conline;
423 push @newlines, @blanks;
431 elsif ($conline =~ /^<.*/ && $i)
450 push @newlines, $conline;
451 push @newlines, @blanks;
454 push @newlines, "</config>";
460 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
462 foreach $n (@newlines)
468 my @result = `xmllint -noout $tmpfile 2>&1`;
479 $n =~ s/$tmpfile\:\d*\: *//g;
480 if ($n =~ /.*config>.*/)
488 if ($n =~ /line \d*/)
491 $lineno =~ s/.*line (\d*).*/\1/;
493 $n =~ s/line (\d*)/line $lineno/;
495 print "$file : $n\n";