2 # +------------------------------------+
3 # | Inspire Internet Relay Chat Daemon |
4 # +------------------------------------+
6 # InspIRCd: (C) 2002-2009 InspIRCd Development Team
7 # See: http://wiki.inspircd.org/Credits
9 # This program is free but copyrighted software; see
10 # the file COPYING for details.
12 # ---------------------------------------------------
18 my $basepath = "@BASE_DIR@";
19 my $confpath = "@CONFIG_DIR@/";
20 my $binpath = "@BINARY_DIR@";
21 my $runpath = "@BASE_DIR@";
22 my $valgrindlogpath = "$basepath/valgrindlogs";
23 my $executable = "@EXECUTABLE@";
24 my $version = "@VERSION@";
27 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
28 my $arg = shift(@ARGV);
32 if ($a =~ m/^--config=(.*)$/)
39 $conf = $confpath . "inspircd.conf";
40 push @ARGV, '--config='.$conf;
45 # System for naming script command subs:
46 # cmd_<name> - Normal command for use by users.
47 # dev_<name> - Developer commands.
48 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
49 # Ideally command subs shouldn't return.
53 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
56 print STDERR "Invalid command or none given.\n";
68 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
69 my @cmds = grep /^cmd_/, @subs;
70 my @devs = grep /^dev_/, @subs;
72 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
73 $_ =~ s/_/-/g foreach (@cmds, @devs);
74 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
75 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
81 if (getstatus() == 1) {
82 my $pid = getprocessid();
83 print "InspIRCd is running (PID: $pid)\n";
86 print "InspIRCd is not running. (Or PID File not found)\n";
93 if (getstatus() == 1) {
94 my $pid = getprocessid();
95 system("kill -HUP $pid >/dev/null 2>&1");
96 print "InspIRCd rehashed (pid: $pid).\n";
99 print "InspIRCd is not running. (Or PID File not found)\n";
106 if (getstatus() == 0) { goto &cmd_start(); }
112 print "InspIRCd version: $version\n";
119 unlink($pidfile) if (-e $pidfile);
123 sub hid_cheese_sandwich()
125 print "Creating Cheese Sandwich..\n";
132 # Check to see its not 'running' already.
133 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
134 # If we are still alive here.. Try starting the IRCd..
136 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
137 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
139 exec "$binpath/$executable", @_;
140 die "Failed to start IRCd: $!\n";
145 # Check to see its not 'running' already.
146 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
149 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
150 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
155 # If we are still alive here.. Try starting the IRCd..
156 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_;
157 die "Failed to start GDB: $!\n";
160 sub dev_screendebug(@)
162 # Check to see its not 'running' already.
163 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");
172 # If we are still alive here.. Try starting the IRCd..
173 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
174 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
175 exec qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_;
176 die "Failed to start screen: $!\n";
181 # Check to see its not 'running' already.
182 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
185 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
186 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
188 # Check we have valgrind and gdb
192 # If we are still alive here.. Try starting the IRCd..
193 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
194 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
195 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
196 die "Failed to start valgrind: $!\n";
199 sub dev_valdebug_unattended(@)
201 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
202 # Check to see its not 'running' already.
203 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
206 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
207 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
209 # Check we have valgrind and gdb
213 # If we are still alive here.. Try starting the IRCd..
215 # 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!
216 # Redirect stdout to /dev/null if you're worried about the security.
221 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
222 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
223 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
224 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
225 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
226 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
227 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
228 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
229 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
230 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
231 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
232 "$binpath/$executable", qw(-nofork -debug -nolog), @_;
233 die "Can't execute valgrind: $!\n";
237 sub dev_screenvaldebug(@)
239 # Check to see its not 'running' already.
240 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
243 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
244 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
251 # If we are still alive here.. Try starting the IRCd..
252 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
253 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
254 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
255 die "Failed to start screen: $!\n";
260 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
261 # Get to here, we have something to kill.
262 my $pid = getprocessid();
263 print "Stopping InspIRCd (pid: $pid)...\n";
264 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 5;
265 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
268 if (getstatus() == 0) {
269 print "InspIRCd Stopped.\n";
273 print "InspIRCd not dying quietly -- forcing kill\n";
279 # Generic Helper Functions.
282 # GetPidfile Version 2 - Now With Include Support..
283 # I beg for months for include support in insp, then..
284 # when it is added, it comes around and BITES ME IN THE ASS,
285 # because i then have to code support into this script.. Evil.
287 # Craig got bitten in the ass again --
288 # in 1.1 beta the include file is manditory, therefore
289 # if we cant find it, default to %conf%/inspircd.pid.
290 # Note, this also contains a fix for when the pid file is
291 # defined, but defined in a comment (line starts with #)
299 # Before we start, do we have a PID already? (Should never occur)
303 # Are We using a relative path?
304 if ($file !~ /^\//) {
305 # Convert it to a full path.
306 $file = $runpath .'/'. $file;
309 # Have we checked this file before?
310 return if $filesparsed{$file};
311 $filesparsed{$file} = 1;
314 open INFILE, '<', $file or return;
315 # Grab entire file contents..
316 my(@lines) = <INFILE>;
320 # remove trailing spaces
324 $i =~ s/[^=]+=\s(.*)/\1/;
325 # Does this file have a pid?
326 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
328 # Set the PID file and return.
334 elsif (-f "$runpath/$pidfile")
336 $pidfile = "$runpath/$pidfile";
344 # If we get here, NO PID FILE! -- Check for includes
346 $i =~ s/[^=]+=\s(.*)/\1/;
347 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
349 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
352 if ($pidfile ne "") {
359 # End of includes / No includes found. Using default.
360 $pidfile = $runpath . "/data/inspircd.pid";
364 my $pid = getprocessid();
365 return 0 if $pid == 0;
372 open PIDFILE, "< $pidfile" or return 0;
383 unless(`valgrind --version`)
385 print "Couldn't start valgrind: $!\n";
392 unless(`gdb --version`)
394 print "Couldn't start gdb: $!\n";
401 unless(`screen --version`)
403 print "Couldn't start screen: $!\n";
410 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
417 print "Config check complete\n";
427 # Are We using a relative path?
428 if ($file !~ /^\//) {
429 # Convert it to a full path..
430 $file = $runpath . $file;
433 # Have we checked this file before?
434 return if $filechecked{$file};
435 $filechecked{$file} = 1;
438 open INFILE, "< $file" or die "Unable to open file $file\n";
439 # Grab entire file contents..
440 my(@lines) = <INFILE>;
444 # remove trailing spaces
451 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
452 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
453 push @newlines, "<config>";
457 # remove trailing newlines
460 # convert tabs to spaces
463 # remove leading spaces
472 # remove trailing comments
475 for (my $j = 0; $j < length($i); $j++)
477 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
478 $line .= substr($i,$j, 1);
482 # remove trailing spaces
485 # setup incf for include check and clean it up, since this breaks parsing use local var
487 $incf =~ s/[^=]+=\s(.*)/\1/;
490 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
496 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
498 push @newlines, $conline;
499 push @newlines, @blanks;
505 $i =~ s/(.*)>$/\1 \/>/;
511 $conline =~ s/(.*)>$/\1 \/>/;
513 push @newlines, $conline;
514 push @newlines, @blanks;
522 elsif ($conline =~ /^<.*/ && $i)
541 push @newlines, $conline;
542 push @newlines, @blanks;
545 push @newlines, "</config>";
551 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
553 for my $n (@newlines)
559 my @result = `xmllint -noout $tmpfile 2>&1`;
570 $n =~ s/$tmpfile\:\d*\: *//g;
571 if ($n =~ /.*config>.*/)
579 if ($n =~ /line \d*/)
582 $lineno =~ s/.*line (\d*).*/\1/;
584 $n =~ s/line (\d*)/line $lineno/;
586 print "$file : $n\n";