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);
28 my $conf = $confpath . "inspircd.conf";
31 if ($a =~ m/^--config=(.*)$/)
39 # System for naming script command subs:
40 # cmd_<name> - Normal command for use by users.
41 # dev_<name> - Developer commands.
42 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
43 # Ideally command subs shouldn't return.
47 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
50 print STDERR "Invalid command or none given.\n";
62 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
63 my @cmds = grep /^cmd_/, @subs;
64 my @devs = grep /^dev_/, @subs;
66 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
67 $_ =~ s/_/-/g foreach (@cmds, @devs);
68 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
69 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
75 if (getstatus() == 1) {
76 my $pid = getprocessid();
77 print "InspIRCd is running (PID: $pid)\n";
80 print "InspIRCd is not running. (Or PID File not found)\n";
87 if (getstatus() == 1) {
88 my $pid = getprocessid();
89 system("kill -HUP $pid >/dev/null 2>&1");
90 print "InspIRCd rehashed (pid: $pid).\n";
93 print "InspIRCd is not running. (Or PID File not found)\n";
100 if (getstatus() == 0) { goto &cmd_start(); }
106 print "InspIRCd version: $version\n";
113 unlink($pidfile) if (-e $pidfile);
117 sub hid_cheese_sandwich()
119 print "Creating Cheese Sandwich..\n";
126 # Check to see its not 'running' already.
127 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
128 # If we are still alive here.. Try starting the IRCd..
129 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
130 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
132 exec { "$binpath/$executable" } "$binpath/$executable", @_;
133 die "Failed to start IRCd: $!\n";
138 # Check to see its not 'running' already.
139 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
141 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
142 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
147 # If we are still alive here.. Try starting the IRCd..
148 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_;
149 die "Failed to start GDB: $!\n";
152 sub dev_screendebug(@)
154 # Check to see its not 'running' already.
155 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
157 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
163 # If we are still alive here.. Try starting the IRCd..
164 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
165 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
166 exec qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_;
167 die "Failed to start screen: $!\n";
172 # Check to see its not 'running' already.
173 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
175 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
176 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
178 # Check we have valgrind and gdb
182 # If we are still alive here.. Try starting the IRCd..
183 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
184 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
185 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
186 die "Failed to start valgrind: $!\n";
189 sub dev_valdebug_unattended(@)
191 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
192 # Check to see its not 'running' already.
193 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
195 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
196 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
198 # Check we have valgrind and gdb
202 # If we are still alive here.. Try starting the IRCd..
204 # 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!
205 # Redirect stdout to /dev/null if you're worried about the security.
210 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
211 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
212 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
213 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
214 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
215 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
216 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
217 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
218 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
219 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
220 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
221 "$binpath/$executable", qw(-nofork -debug -nolog), @_;
222 die "Can't execute valgrind: $!\n";
226 sub dev_screenvaldebug(@)
228 # Check to see its not 'running' already.
229 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
231 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
232 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
239 # If we are still alive here.. Try starting the IRCd..
240 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
241 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
242 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
243 die "Failed to start screen: $!\n";
248 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
249 # Get to here, we have something to kill.
250 my $pid = getprocessid();
251 print "Stopping InspIRCd (pid: $pid)...\n";
252 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 5;
253 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
256 if (getstatus() == 0) {
257 print "InspIRCd Stopped.\n";
261 print "InspIRCd not dying quietly -- forcing kill\n";
267 # Generic Helper Functions.
270 # GetPidfile Version 2 - Now With Include Support..
271 # I beg for months for include support in insp, then..
272 # when it is added, it comes around and BITES ME IN THE ASS,
273 # because i then have to code support into this script.. Evil.
275 # Craig got bitten in the ass again --
276 # in 1.1 beta the include file is manditory, therefore
277 # if we cant find it, default to %conf%/inspircd.pid.
278 # Note, this also contains a fix for when the pid file is
279 # defined, but defined in a comment (line starts with #)
287 # Before we start, do we have a PID already? (Should never occur)
291 # Are We using a relative path?
292 if ($file !~ /^\//) {
293 # Convert it to a full path.
294 $file = $confpath . $file;
297 # Have we checked this file before?
298 return if $filesparsed{$file};
299 $filesparsed{$file} = 1;
302 open INFILE, "< $file" or die "Unable to open file $file included in configuration\n";
303 # Grab entire file contents..
304 my(@lines) = <INFILE>;
308 # remove trailing spaces
312 $i =~ s/[^=]+=\s(.*)/\1/;
313 # Does this file have a pid?
314 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
316 # Set the PID file and return.
324 if (-f $confpath . $pidfile)
326 $pidfile = $confpath . $pidfile;
335 # If we get here, NO PID FILE! -- Check for includes
337 $i =~ s/[^=]+=\s(.*)/\1/;
338 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
340 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
349 if (-f $confpath . $pidfile)
351 $pidfile = $confpath . $pidfile;
355 if ($pidfile ne "") {
362 # End of includes / No includes found. Using default.
363 $pidfile = $confpath . "inspircd.pid";
367 my $pid = getprocessid();
368 return 0 if $pid == 0;
375 open PIDFILE, "< $pidfile" or return 0;
386 unless(`valgrind --version`)
388 print "Couldn't start valgrind: $!\n";
395 unless(`gdb --version`)
397 print "Couldn't start gdb: $!\n";
404 unless(`screen --version`)
406 print "Couldn't start screen: $!\n";
413 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
420 print "Config check complete\n";
430 # Are We using a relative path?
431 if ($file !~ /^\//) {
432 # Convert it to a full path..
433 $file = $confpath . $file;
436 # Have we checked this file before?
437 return if $filechecked{$file};
438 $filechecked{$file} = 1;
441 open INFILE, "< $file" or die "Unable to open file $file\n";
442 # Grab entire file contents..
443 my(@lines) = <INFILE>;
447 # remove trailing spaces
454 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
455 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
456 push @newlines, "<config>";
460 # remove trailing newlines
463 # convert tabs to spaces
466 # remove leading spaces
475 # remove trailing comments
478 for (my $j = 0; $j < length($i); $j++)
480 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
481 $line .= substr($i,$j, 1);
485 # remove trailing spaces
488 # setup incf for include check and clean it up, since this breaks parsing use local var
490 $incf =~ s/[^=]+=\s(.*)/\1/;
493 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
499 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
501 push @newlines, $conline;
502 push @newlines, @blanks;
508 $i =~ s/(.*)>$/\1 \/>/;
514 $conline =~ s/(.*)>$/\1 \/>/;
516 push @newlines, $conline;
517 push @newlines, @blanks;
525 elsif ($conline =~ /^<.*/ && $i)
544 push @newlines, $conline;
545 push @newlines, @blanks;
548 push @newlines, "</config>";
554 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
556 for my $n (@newlines)
562 my @result = `xmllint -noout $tmpfile 2>&1`;
573 $n =~ s/$tmpfile\:\d*\: *//g;
574 if ($n =~ /.*config>.*/)
582 if ($n =~ /line \d*/)
585 $lineno =~ s/.*line (\d*).*/\1/;
587 $n =~ s/line (\d*)/line $lineno/;
589 print "$file : $n\n";