4 # InspIRCd -- Internet Relay Chat Daemon
6 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 # This file is part of InspIRCd. InspIRCd is free software: you can
9 # redistribute it and/or modify it under the terms of the GNU General Public
10 # License as published by the Free Software Foundation, version 2.
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
26 my $basepath = "@BASE_DIR@";
27 my $confpath = "@CONFIG_DIR@/";
28 my $binpath = "@BINARY_DIR@";
29 my $runpath = "@BASE_DIR@";
30 my $valgrindlogpath = "$basepath/valgrindlogs";
31 my $executable = "@EXECUTABLE@";
32 my $version = "@VERSION@";
35 if ($< == 0 || $> == 0) {
36 if ($uid !~ /^\d+$/) {
37 # Named UID, look it up
41 die "Cannot find a valid UID to change to";
43 # drop root if we were configured with an ircd UID
46 if ($< == 0 || $> == 0) {
47 die "Could not drop root: $!";
52 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
53 my $arg = shift(@ARGV);
57 if ($a =~ m/^--config=(.*)$/)
64 $conf = $confpath . "inspircd.conf";
65 push @ARGV, '--config='.$conf;
70 # System for naming script command subs:
71 # cmd_<name> - Normal command for use by users.
72 # dev_<name> - Developer commands.
73 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
74 # Ideally command subs shouldn't return.
78 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
81 print STDERR "Invalid command or none given.\n";
93 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
94 my @cmds = grep /^cmd_/, @subs;
95 my @devs = grep /^dev_/, @subs;
97 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
98 $_ =~ s/_/-/g foreach (@cmds, @devs);
99 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
100 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
106 if (getstatus() == 1) {
107 my $pid = getprocessid();
108 print "InspIRCd is running (PID: $pid)\n";
111 print "InspIRCd is not running. (Or PID File not found)\n";
118 if (getstatus() == 1) {
119 my $pid = getprocessid();
120 system("kill -HUP $pid >/dev/null 2>&1");
121 print "InspIRCd rehashed (pid: $pid).\n";
124 print "InspIRCd is not running. (Or PID File not found)\n";
131 if (getstatus() == 0) { goto &cmd_start(); }
137 print "InspIRCd version: $version\n";
144 unlink($pidfile) if (-e $pidfile);
148 sub hid_cheese_sandwich()
150 print "Creating Cheese Sandwich..\n";
157 # Check to see its not 'running' already.
158 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
159 # If we are still alive here.. Try starting the IRCd..
161 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
162 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
164 exec "$binpath/$executable", @_;
165 die "Failed to start IRCd: $!\n";
170 # Check to see its not 'running' already.
171 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
174 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
175 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
180 # If we are still alive here.. Try starting the IRCd..
181 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
182 die "Failed to start GDB: $!\n";
185 sub dev_screendebug(@)
187 # Check to see its not 'running' already.
188 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
191 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
197 # If we are still alive here.. Try starting the IRCd..
198 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
199 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
200 exec qw(screen -m -d gdb), "--command=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
201 die "Failed to start screen: $!\n";
206 # Check to see its not 'running' already.
207 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
210 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
211 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
213 # Check we have valgrind and gdb
217 # If we are still alive here.. Try starting the IRCd..
218 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
219 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
220 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
221 die "Failed to start valgrind: $!\n";
224 sub dev_valdebug_unattended(@)
226 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
227 # Check to see its not 'running' already.
228 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");
234 # Check we have valgrind and gdb
238 # If we are still alive here.. Try starting the IRCd..
240 # 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!
241 # Redirect stdout to /dev/null if you're worried about the security.
246 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
247 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
248 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
249 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
250 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
251 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
252 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
253 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
254 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
255 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
256 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
257 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
258 die "Can't execute valgrind: $!\n";
262 sub dev_screenvaldebug(@)
264 # Check to see its not 'running' already.
265 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
268 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
269 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
276 # If we are still alive here.. Try starting the IRCd..
277 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
278 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
279 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
280 die "Failed to start screen: $!\n";
285 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
286 # Get to here, we have something to kill.
287 my $pid = getprocessid();
288 print "Stopping InspIRCd (pid: $pid)...\n";
289 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 15;
290 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
293 if (getstatus() == 0) {
294 print "InspIRCd Stopped.\n";
298 print "InspIRCd not dying quietly -- forcing kill\n";
304 # Generic Helper Functions.
307 # GetPidfile Version 2 - Now With Include Support..
308 # I beg for months for include support in insp, then..
309 # when it is added, it comes around and BITES ME IN THE ASS,
310 # because i then have to code support into this script.. Evil.
312 # Craig got bitten in the ass again --
313 # in 1.1 beta the include file is manditory, therefore
314 # if we cant find it, default to %conf%/inspircd.pid.
315 # Note, this also contains a fix for when the pid file is
316 # defined, but defined in a comment (line starts with #)
324 # Before we start, do we have a PID already? (Should never occur)
328 # Are We using a relative path?
329 if ($file !~ /^\//) {
330 # Convert it to a full path.
331 $file = $runpath .'/'. $file;
334 # Have we checked this file before?
335 return if $filesparsed{$file};
336 $filesparsed{$file} = 1;
339 open INFILE, '<', $file or return;
340 # Grab entire file contents..
341 my(@lines) = <INFILE>;
345 # remove trailing spaces
349 $i =~ s/[^=]+=\s(.*)/\1/;
350 # Does this file have a pid?
351 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
353 # Set the PID file and return.
359 elsif (-f "$runpath/$pidfile")
361 $pidfile = "$runpath/$pidfile";
369 # If we get here, NO PID FILE! -- Check for includes
371 $i =~ s/[^=]+=\s(.*)/\1/;
372 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
374 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
377 if ($pidfile ne "") {
384 # End of includes / No includes found. Using default.
385 $pidfile = $runpath . "/data/inspircd.pid";
389 my $pid = getprocessid();
390 return 0 if $pid == 0;
397 open PIDFILE, '<', $pidfile or return 0;
400 /^(\d+)$/ and $pid = $1;
408 unless(`valgrind --version`)
410 print "Couldn't start valgrind: $!\n";
417 unless(`gdb --version`)
419 print "Couldn't start gdb: $!\n";
426 unless(`screen --version`)
428 print "Couldn't start screen: $!\n";