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 $valgrindlogpath = "$basepath/valgrindlogs";
22 my $executable = "@EXECUTABLE@";
23 my $version = "@VERSION@";
26 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
27 my $arg = shift(@ARGV);
31 if ($a =~ m/^--config=(.*)$/)
38 $conf = $confpath . "inspircd.conf";
39 push @ARGV, '--config='.$conf;
44 # System for naming script command subs:
45 # cmd_<name> - Normal command for use by users.
46 # dev_<name> - Developer commands.
47 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
48 # Ideally command subs shouldn't return.
52 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
55 print STDERR "Invalid command or none given.\n";
67 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
68 my @cmds = grep /^cmd_/, @subs;
69 my @devs = grep /^dev_/, @subs;
71 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
72 $_ =~ s/_/-/g foreach (@cmds, @devs);
73 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
74 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
80 if (getstatus() == 1) {
81 my $pid = getprocessid();
82 print "InspIRCd is running (PID: $pid)\n";
85 print "InspIRCd is not running. (Or PID File not found)\n";
92 if (getstatus() == 1) {
93 my $pid = getprocessid();
94 system("kill -HUP $pid >/dev/null 2>&1");
95 print "InspIRCd rehashed (pid: $pid).\n";
98 print "InspIRCd is not running. (Or PID File not found)\n";
105 if (getstatus() == 0) { goto &cmd_start(); }
111 print "InspIRCd version: $version\n";
118 unlink($pidfile) if (-e $pidfile);
122 sub hid_cheese_sandwich()
124 print "Creating Cheese Sandwich..\n";
131 # Check to see its not 'running' already.
132 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
133 # If we are still alive here.. Try starting the IRCd..
134 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
135 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
137 exec { "$binpath/$executable" } "$binpath/$executable", @_;
138 die "Failed to start IRCd: $!\n";
143 # Check to see its not 'running' already.
144 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
146 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
147 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
152 # If we are still alive here.. Try starting the IRCd..
153 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_;
154 die "Failed to start GDB: $!\n";
157 sub dev_screendebug(@)
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");
168 # If we are still alive here.. Try starting the IRCd..
169 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
170 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
171 exec qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_;
172 die "Failed to start screen: $!\n";
177 # Check to see its not 'running' already.
178 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
180 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
181 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
183 # Check we have valgrind and gdb
187 # If we are still alive here.. Try starting the IRCd..
188 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
189 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
190 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
191 die "Failed to start valgrind: $!\n";
194 sub dev_valdebug_unattended(@)
196 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
197 # Check to see its not 'running' already.
198 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
200 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
201 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
203 # Check we have valgrind and gdb
207 # If we are still alive here.. Try starting the IRCd..
209 # 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!
210 # Redirect stdout to /dev/null if you're worried about the security.
215 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
216 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
217 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
218 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
219 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
220 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
221 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
222 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
223 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
224 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
225 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
226 "$binpath/$executable", qw(-nofork -debug -nolog), @_;
227 die "Can't execute valgrind: $!\n";
231 sub dev_screenvaldebug(@)
233 # Check to see its not 'running' already.
234 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
236 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
237 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
244 # If we are still alive here.. Try starting the IRCd..
245 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
246 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
247 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
248 die "Failed to start screen: $!\n";
253 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
254 # Get to here, we have something to kill.
255 my $pid = getprocessid();
256 print "Stopping InspIRCd (pid: $pid)...\n";
257 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 5;
258 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
261 if (getstatus() == 0) {
262 print "InspIRCd Stopped.\n";
266 print "InspIRCd not dying quietly -- forcing kill\n";
272 # Generic Helper Functions.
275 # GetPidfile Version 2 - Now With Include Support..
276 # I beg for months for include support in insp, then..
277 # when it is added, it comes around and BITES ME IN THE ASS,
278 # because i then have to code support into this script.. Evil.
280 # Craig got bitten in the ass again --
281 # in 1.1 beta the include file is manditory, therefore
282 # if we cant find it, default to %conf%/inspircd.pid.
283 # Note, this also contains a fix for when the pid file is
284 # defined, but defined in a comment (line starts with #)
292 # Before we start, do we have a PID already? (Should never occur)
296 # Are We using a relative path?
297 if ($file !~ /^\//) {
298 # Convert it to a full path.
299 $file = $confpath . $file;
302 # Have we checked this file before?
303 return if $filesparsed{$file};
304 $filesparsed{$file} = 1;
307 open INFILE, "< $file" or die "Unable to open file $file included in configuration\n";
308 # Grab entire file contents..
309 my(@lines) = <INFILE>;
313 # remove trailing spaces
317 $i =~ s/[^=]+=\s(.*)/\1/;
318 # Does this file have a pid?
319 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
321 # Set the PID file and return.
329 if (-f $confpath . $pidfile)
331 $pidfile = $confpath . $pidfile;
340 # If we get here, NO PID FILE! -- Check for includes
342 $i =~ s/[^=]+=\s(.*)/\1/;
343 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
345 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
354 if (-f $confpath . $pidfile)
356 $pidfile = $confpath . $pidfile;
360 if ($pidfile ne "") {
367 # End of includes / No includes found. Using default.
368 $pidfile = $confpath . "inspircd.pid";
372 my $pid = getprocessid();
373 return 0 if $pid == 0;
380 open PIDFILE, "< $pidfile" or return 0;
391 unless(`valgrind --version`)
393 print "Couldn't start valgrind: $!\n";
400 unless(`gdb --version`)
402 print "Couldn't start gdb: $!\n";
409 unless(`screen --version`)
411 print "Couldn't start screen: $!\n";
418 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
425 print "Config check complete\n";
435 # Are We using a relative path?
436 if ($file !~ /^\//) {
437 # Convert it to a full path..
438 $file = $confpath . $file;
441 # Have we checked this file before?
442 return if $filechecked{$file};
443 $filechecked{$file} = 1;
446 open INFILE, "< $file" or die "Unable to open file $file\n";
447 # Grab entire file contents..
448 my(@lines) = <INFILE>;
452 # remove trailing spaces
459 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
460 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
461 push @newlines, "<config>";
465 # remove trailing newlines
468 # convert tabs to spaces
471 # remove leading spaces
480 # remove trailing comments
483 for (my $j = 0; $j < length($i); $j++)
485 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
486 $line .= substr($i,$j, 1);
490 # remove trailing spaces
493 # setup incf for include check and clean it up, since this breaks parsing use local var
495 $incf =~ s/[^=]+=\s(.*)/\1/;
498 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
504 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
506 push @newlines, $conline;
507 push @newlines, @blanks;
513 $i =~ s/(.*)>$/\1 \/>/;
519 $conline =~ s/(.*)>$/\1 \/>/;
521 push @newlines, $conline;
522 push @newlines, @blanks;
530 elsif ($conline =~ /^<.*/ && $i)
549 push @newlines, $conline;
550 push @newlines, @blanks;
553 push @newlines, "</config>";
559 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
561 for my $n (@newlines)
567 my @result = `xmllint -noout $tmpfile 2>&1`;
578 $n =~ s/$tmpfile\:\d*\: *//g;
579 if ($n =~ /.*config>.*/)
587 if ($n =~ /line \d*/)
590 $lineno =~ s/.*line (\d*).*/\1/;
592 $n =~ s/line (\d*)/line $lineno/;
594 print "$file : $n\n";