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 $libpath = "@LIBRARY_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);
29 my $conf = $confpath . "inspircd.conf";
32 if ($a =~ m/^--config=(.*)$/)
40 # System for naming script command subs:
41 # cmd_<name> - Normal command for use by users.
42 # dev_<name> - Developer commands.
43 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
44 # Ideally command subs shouldn't return.
48 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
51 print STDERR "Invalid command or none given.\n";
63 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
64 my @cmds = grep /^cmd_/, @subs;
65 my @devs = grep /^dev_/, @subs;
67 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
68 $_ =~ s/_/-/g foreach (@cmds, @devs);
69 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
70 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
76 if (getstatus() == 1) {
77 my $pid = getprocessid();
78 print "InspIRCd is running (PID: $pid)\n";
81 print "InspIRCd is not running. (Or PID File not found)\n";
88 if (getstatus() == 1) {
89 my $pid = getprocessid();
90 system("kill -HUP $pid >/dev/null 2>&1");
91 print "InspIRCd rehashed (pid: $pid).\n";
94 print "InspIRCd is not running. (Or PID File not found)\n";
101 if (getstatus() == 0) { goto &cmd_start(); }
107 print "InspIRCd version: $version\n";
114 unlink($pidfile) if (-e $pidfile);
118 sub hid_cheese_sandwich()
120 print "Creating Cheese Sandwich..\n";
127 # Check to see its not 'running' already.
128 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
129 # If we are still alive here.. Try starting the IRCd..
130 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
131 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
133 exec { "$binpath/$executable" } "$binpath/$executable", @_;
134 die "Failed to start IRCd: $!\n";
139 # Check to see its not 'running' already.
140 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
142 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
143 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
148 # If we are still alive here.. Try starting the IRCd..
149 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_;
150 die "Failed to start GDB: $!\n";
153 sub dev_screendebug(@)
155 # Check to see its not 'running' already.
156 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
158 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
164 # If we are still alive here.. Try starting the IRCd..
165 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
166 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
167 exec qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_;
168 die "Failed to start screen: $!\n";
173 # Check to see its not 'running' already.
174 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
176 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
177 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
179 # Check we have valgrind and gdb
183 # If we are still alive here.. Try starting the IRCd..
184 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
185 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
186 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
187 die "Failed to start valgrind: $!\n";
190 sub dev_valdebug_unattended(@)
192 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
193 # Check to see its not 'running' already.
194 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
196 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
197 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
199 # Check we have valgrind and gdb
203 # If we are still alive here.. Try starting the IRCd..
205 # 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!
206 # Redirect stdout to /dev/null if you're worried about the security.
211 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
212 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
213 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
214 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
215 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
216 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
217 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
218 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
219 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
220 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
221 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
222 "$binpath/$executable", qw(-nofork -debug -nolog), @_;
223 die "Can't execute valgrind: $!\n";
227 sub dev_screenvaldebug(@)
229 # Check to see its not 'running' already.
230 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
232 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
233 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
240 # If we are still alive here.. Try starting the IRCd..
241 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
242 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
243 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
244 die "Failed to start screen: $!\n";
249 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
250 # Get to here, we have something to kill.
251 my $pid = getprocessid();
252 print "Stopping InspIRCd (pid: $pid)...\n";
253 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 5;
254 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
257 if (getstatus() == 0) {
258 print "InspIRCd Stopped.\n";
262 print "InspIRCd not dying quietly -- forcing kill\n";
268 # Generic Helper Functions.
271 # GetPidfile Version 2 - Now With Include Support..
272 # I beg for months for include support in insp, then..
273 # when it is added, it comes around and BITES ME IN THE ASS,
274 # because i then have to code support into this script.. Evil.
276 # Craig got bitten in the ass again --
277 # in 1.1 beta the include file is manditory, therefore
278 # if we cant find it, default to %conf%/inspircd.pid.
279 # Note, this also contains a fix for when the pid file is
280 # defined, but defined in a comment (line starts with #)
288 # Before we start, do we have a PID already? (Should never occur)
292 # Are We using a relative path?
293 if ($file !~ /^\//) {
294 # Convert it to a full path.
295 $file = $confpath . $file;
298 # Have we checked this file before?
299 return if $filesparsed{$file};
300 $filesparsed{$file} = 1;
303 open INFILE, "< $file" or die "Unable to open file $file included in configuration\n";
304 # Grab entire file contents..
305 my(@lines) = <INFILE>;
309 # remove trailing spaces
313 $i =~ s/[^=]+=\s(.*)/\1/;
314 # Does this file have a pid?
315 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
317 # Set the PID file and return.
325 if (-f $confpath . $pidfile)
327 $pidfile = $confpath . $pidfile;
336 # If we get here, NO PID FILE! -- Check for includes
338 $i =~ s/[^=]+=\s(.*)/\1/;
339 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
341 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
350 if (-f $confpath . $pidfile)
352 $pidfile = $confpath . $pidfile;
356 if ($pidfile ne "") {
363 # End of includes / No includes found. Using default.
364 $pidfile = $confpath . "inspircd.pid";
368 my $pid = getprocessid();
369 return 0 if $pid == 0;
376 open PIDFILE, "< $pidfile" or return 0;
387 unless(`valgrind --version`)
389 print "Couldn't start valgrind: $!\n";
396 unless(`gdb --version`)
398 print "Couldn't start gdb: $!\n";
405 unless(`screen --version`)
407 print "Couldn't start screen: $!\n";
414 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
421 print "Config check complete\n";
431 # Are We using a relative path?
432 if ($file !~ /^\//) {
433 # Convert it to a full path..
434 $file = $confpath . $file;
437 # Have we checked this file before?
438 return if $filechecked{$file};
439 $filechecked{$file} = 1;
442 open INFILE, "< $file" or die "Unable to open file $file\n";
443 # Grab entire file contents..
444 my(@lines) = <INFILE>;
448 # remove trailing spaces
455 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
456 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
457 push @newlines, "<config>";
461 # remove trailing newlines
464 # convert tabs to spaces
467 # remove leading spaces
476 # remove trailing comments
479 for (my $j = 0; $j < length($i); $j++)
481 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
482 $line .= substr($i,$j, 1);
486 # remove trailing spaces
489 # setup incf for include check and clean it up, since this breaks parsing use local var
491 $incf =~ s/[^=]+=\s(.*)/\1/;
494 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
500 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
502 push @newlines, $conline;
503 push @newlines, @blanks;
509 $i =~ s/(.*)>$/\1 \/>/;
515 $conline =~ s/(.*)>$/\1 \/>/;
517 push @newlines, $conline;
518 push @newlines, @blanks;
526 elsif ($conline =~ /^<.*/ && $i)
545 push @newlines, $conline;
546 push @newlines, @blanks;
549 push @newlines, "</config>";
555 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
557 for my $n (@newlines)
563 my @result = `xmllint -noout $tmpfile 2>&1`;
574 $n =~ s/$tmpfile\:\d*\: *//g;
575 if ($n =~ /.*config>.*/)
583 if ($n =~ /line \d*/)
586 $lineno =~ s/.*line (\d*).*/\1/;
588 $n =~ s/line (\d*)/line $lineno/;
590 print "$file : $n\n";