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..
204 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
205 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
207 # 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!
208 # Redirect stdout to /dev/null if you're worried about the security.
213 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
214 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
215 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
216 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
217 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
218 exec qw(valgrind -v --tool=memcheck --leak-check=yes --num-callers=10 --time-stamp=yes --log-fd=2), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
219 die "Can't execute valgrind: $!\n";
223 sub dev_screenvaldebug(@)
225 # Check to see its not 'running' already.
226 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
228 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
229 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
236 # If we are still alive here.. Try starting the IRCd..
237 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
238 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
239 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
240 die "Failed to start screen: $!\n";
245 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
246 # Get to here, we have something to kill.
247 my $pid = getprocessid();
248 print "Stopping InspIRCd (pid: $pid)...\n";
249 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 30 : 5;
250 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
253 if (getstatus() == 0) {
254 print "InspIRCd Stopped.\n";
258 print "InspIRCd not dying quietly -- forcing kill\n";
264 # Generic Helper Functions.
267 # GetPidfile Version 2 - Now With Include Support..
268 # I beg for months for include support in insp, then..
269 # when it is added, it comes around and BITES ME IN THE ASS,
270 # because i then have to code support into this script.. Evil.
272 # Craig got bitten in the ass again --
273 # in 1.1 beta the include file is manditory, therefore
274 # if we cant find it, default to %conf%/inspircd.pid.
275 # Note, this also contains a fix for when the pid file is
276 # defined, but defined in a comment (line starts with #)
284 # Before we start, do we have a PID already? (Should never occur)
288 # Are We using a relative path?
289 if ($file !~ /^\//) {
290 # Convert it to a full path.
291 $file = $confpath . $file;
294 # Have we checked this file before?
295 return if $filesparsed{$file};
296 $filesparsed{$file} = 1;
299 open INFILE, "< $file" or die "Unable to open file $file included in configuration\n";
300 # Grab entire file contents..
301 my(@lines) = <INFILE>;
305 # remove trailing spaces
309 $i =~ s/[^=]+=\s(.*)/\1/;
310 # Does this file have a pid?
311 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
313 # Set the PID file and return.
319 # If we get here, NO PID FILE! -- Check for includes
321 $i =~ s/[^=]+=\s(.*)/\1/;
322 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
324 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
327 if ($pidfile ne "") {
334 # End of includes / No includes found. Using default.
335 $pidfile = $confpath . "inspircd.pid";
339 my $pid = getprocessid();
340 return 0 if $pid == 0;
347 open PIDFILE, "< $pidfile" or return 0;
358 unless(`valgrind --version`)
360 print "Couldn't start valgrind: $!\n";
367 unless(`gdb --version`)
369 print "Couldn't start gdb: $!\n";
376 unless(`screen --version`)
378 print "Couldn't start screen: $!\n";
385 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
392 print "Config check complete\n";
402 # Are We using a relative path?
403 if ($file !~ /^\//) {
404 # Convert it to a full path..
405 $file = $confpath . $file;
408 # Have we checked this file before?
409 return if $filechecked{$file};
410 $filechecked{$file} = 1;
413 open INFILE, "< $file" or die "Unable to open file $file\n";
414 # Grab entire file contents..
415 my(@lines) = <INFILE>;
419 # remove trailing spaces
426 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
427 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
428 push @newlines, "<config>";
432 # remove trailing newlines
435 # convert tabs to spaces
438 # remove leading spaces
447 # remove trailing comments
450 for (my $j = 0; $j < length($i); $j++)
452 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
453 $line .= substr($i,$j, 1);
457 # remove trailing spaces
460 # setup incf for include check and clean it up, since this breaks parsing use local var
462 $incf =~ s/[^=]+=\s(.*)/\1/;
465 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
471 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
473 push @newlines, $conline;
474 push @newlines, @blanks;
480 $i =~ s/(.*)>$/\1 \/>/;
486 $conline =~ s/(.*)>$/\1 \/>/;
488 push @newlines, $conline;
489 push @newlines, @blanks;
497 elsif ($conline =~ /^<.*/ && $i)
516 push @newlines, $conline;
517 push @newlines, @blanks;
520 push @newlines, "</config>";
526 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
528 for my $n (@newlines)
534 my @result = `xmllint -noout $tmpfile 2>&1`;
545 $n =~ s/$tmpfile\:\d*\: *//g;
546 if ($n =~ /.*config>.*/)
554 if ($n =~ /line \d*/)
557 $lineno =~ s/.*line (\d*).*/\1/;
559 $n =~ s/line (\d*)/line $lineno/;
561 print "$file : $n\n";