2 # +------------------------------------+
3 # | Inspire Internet Relay Chat Daemon |
4 # +------------------------------------+
6 # InspIRCd: (C) 2002-2008 InspIRCd Development Team
7 # See: http://www.inspircd.org/wiki/index.php/Credits
9 # This program is free but copyrighted software; see
10 # the file COPYING for details.
12 # ---------------------------------------------------
16 my $basepath = "@BASE_DIR@";
17 my $confpath = "@CONFIG_DIR@/";
18 my $binpath = "@BINARY_DIR@";
19 my $libpath = "@LIBRARY_DIR@";
20 my $valgrindlogpath = "$basepath/valgrindlogs";
21 my $executable = "@EXECUTABLE@";
22 my $version = "@VERSION@";
24 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
25 my $arg = shift(@ARGV);
26 my $conf = $confpath . "inspircd.conf";
29 if ($a =~ m/^--config=(.*)$/)
37 # System for naming script command subs:
38 # cmd_<name> - Normal command for use by users.
39 # dev_<name> - Developer commands.
40 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
41 # Ideally command subs shouldn't return.
45 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
48 print STDERR "Invalid command or none given.\n";
60 @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
61 my @cmds = grep /^cmd_/, @subs;
62 my @devs = grep /^dev_/, @subs;
64 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
65 $_ =~ s/_/-/g foreach (@cmds, @devs);
66 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
67 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
73 if (getstatus() == 1) {
74 my $pid = getprocessid();
75 print "InspIRCd is running (PID: $pid)\n";
78 print "InspIRCd is not running. (Or PID File not found)\n";
85 if (getstatus() == 1) {
86 my $pid = getprocessid();
87 system("kill -HUP $pid >/dev/null 2>&1");
88 print "InspIRCd rehashed (pid: $pid).\n";
91 print "InspIRCd is not running. (Or PID File not found)\n";
98 if (getstatus() == 0) { goto &cmd_start(); }
104 print "InspIRCd version: $version\n";
111 unlink($pidfile) if (-e $pidfile);
115 sub hid_cheese_sandwich()
117 print "Creating Cheese Sandwich..\n";
124 # Check to see its not 'running' already.
125 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
126 # If we are still alive here.. Try starting the IRCd..
127 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
128 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
130 exec { "$binpath/$executable" } "$binpath/$executable", @_;
131 die "Failed to start IRCd: $!\n";
136 # Check to see its not 'running' already.
137 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
139 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
140 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
145 # If we are still alive here.. Try starting the IRCd..
146 exec {gdb} 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(-nofork -debug), @_;
147 die "Failed to start GDB: $!\n";
150 sub dev_screendebug(@)
152 # Check to see its not 'running' already.
153 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
155 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
161 # If we are still alive here.. Try starting the IRCd..
162 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
163 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
164 exec {screen} qw(screen -m -d gdb), "--comand=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(-nofork -debug -nolog), @_;
165 die "Failed to start screen: $!\n";
170 # Check to see its not 'running' already.
171 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
173 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
174 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
176 # Check we have valgrind and gdb
180 # If we are still alive here.. Try starting the IRCd..
181 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
182 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
183 exec {valgrind} qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
184 die "Failed to start valgrind: $!\n";
187 sub dev_valdebug_unattended(@)
189 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
190 # Check to see its not 'running' already.
191 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
193 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
194 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
196 # Check we have valgrind and gdb
200 # If we are still alive here.. Try starting the IRCd..
201 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
202 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
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.
211 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
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 exec {valgrind} qw(valgrind -v --tool=memcheck --leak-check=yes --num-callers=10 --time-stamp=yes --log-fd=2), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
217 die "Can't execute valgrind: $!\n";
221 sub dev_screenvaldebug(@)
223 # Check to see its not 'running' already.
224 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
226 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
227 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
234 # If we are still alive here.. Try starting the IRCd..
235 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
236 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
237 exec {screen} qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(-nofork -debug -nolog), @_;
238 die "Failed to start screen: $!\n";
243 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
244 # Get to here, we have something to kill.
245 my $pid = getprocessid();
246 print "Stopping InspIRCd (pid: $pid)...\n";
247 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 30 : 5;
248 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
251 if (getstatus() == 1) {
252 print "InspIRCd Stopped.\n";
256 print "InspIRCd not dying quietly -- forcing kill\n";
262 # Generic Helper Functions.
265 # GetPidfile Version 2 - Now With Include Support..
266 # I beg for months for include support in insp, then..
267 # when it is added, it comes around and BITES ME IN THE ASS,
268 # because i then have to code support into this script.. Evil.
270 # Craig got bitten in the ass again --
271 # in 1.1 beta the include file is manditory, therefore
272 # if we cant find it, default to %conf%/inspircd.pid.
273 # Note, this also contains a fix for when the pid file is
274 # defined, but defined in a comment (line starts with #)
280 # Before we start, do we have a PID already? (Should never occur)
284 # Are We using a relative path?
285 if ($file !~ /^\//) {
286 # Convert it to a full path.
287 $file = $confpath . $file;
290 # Have we checked this file before?
291 for (my $i = 0; $i < $filesparsed; $i++) {
292 if ($filesparsed[$i] eq $file) {
293 # Already Parsed, Possible recursive loop..
298 # If we get here, Mark as 'Read'
299 $filesparsed[$filesparsed] = $file;
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
310 foreach $i (@lines) {
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.
322 # If we get here, NO PID FILE! -- Check for includes
323 foreach $i (@lines) {
324 $i =~ s/[^=]+=\s(.*)/\1/;
325 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
327 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
330 if ($pidfile ne "") {
337 # End of includes / No includes found. Using default.
338 $pidfile = $confpath . "inspircd.pid";
342 my $pid = getprocessid();
343 return 0 if $pid == 0;
350 open PIDFILE, "< $pidfile" or return 0;
351 while($i = <PIDFILE>)
361 unless(`valgrind --version`)
363 print "Couldn't start valgrind: $!\n";
370 unless(`gdb --version`)
372 print "Couldn't start gdb: $!\n";
379 unless(`screen --version`)
381 print "Couldn't start screen: $!\n";
388 open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
395 print "Config check complete\n";
403 # Are We using a relative path?
404 if ($file !~ /^\//) {
405 # Convert it to a full path..
406 $file = $confpath . $file;
409 # Have we checked this file before?
410 for (my $i = 0; $i < $filechecked; $i++) {
411 if ($filechecked[$i] eq $file) {
412 # Already Parsed, Possible recursive loop..
417 # If we get here, Mark as 'Read'
418 $filechecked[$filechecked] = $file;
421 open INFILE, "< $file" or die "Unable to open file $file\n";
422 # Grab entire file contents..
423 my(@lines) = <INFILE>;
427 # remove trailing spaces
434 push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
435 # push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
436 push @newlines, "<config>";
440 # remove trailing newlines
443 # convert tabs to spaces
446 # remove leading spaces
455 # remove trailing comments
458 for (my $j = 0; $j < length($i); $j++)
460 if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
461 $line .= substr($i,$j, 1);
465 # remove trailing spaces
468 # setup incf for include check and clean it up, since this breaks parsing use local var
470 $incf =~ s/[^=]+=\s(.*)/\1/;
473 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
479 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
481 push @newlines, $conline;
482 push @newlines, @blanks;
488 $i =~ s/(.*)>$/\1 \/>/;
494 $conline =~ s/(.*)>$/\1 \/>/;
496 push @newlines, $conline;
497 push @newlines, @blanks;
505 elsif ($conline =~ /^<.*/ && $i)
524 push @newlines, $conline;
525 push @newlines, @blanks;
528 push @newlines, "</config>";
534 } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
536 foreach $n (@newlines)
542 my @result = `xmllint -noout $tmpfile 2>&1`;
553 $n =~ s/$tmpfile\:\d*\: *//g;
554 if ($n =~ /.*config>.*/)
562 if ($n =~ /line \d*/)
565 $lineno =~ s/.*line (\d*).*/\1/;
567 $n =~ s/line (\d*)/line $lineno/;
569 print "$file : $n\n";