5 # InspIRCd -- Internet Relay Chat Daemon
7 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
9 # This file is part of InspIRCd. InspIRCd is free software: you can
10 # redistribute it and/or modify it under the terms of the GNU General Public
11 # License as published by the Free Software Foundation, version 2.
13 # This program is distributed in the hope that it will be useful, but WITHOUT
14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
22 # InspIRCd Start up the InspIRCd Internet Relay Chat Daemon
24 # chkconfig: 2345 55 25
25 # description: InspIRCd -- Internet Relay Chat Daemon
27 # processname: inspircd
33 my $basepath = "@BASE_DIR@";
34 my $confpath = "@CONFIG_DIR@/";
35 my $binpath = "@BINARY_DIR@";
36 my $runpath = "@BASE_DIR@";
37 my $datadir = "@DATA_DIR@";
38 my $valgrindlogpath = "$basepath/valgrindlogs";
39 my $executable = "inspircd";
40 my $version = "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@+@VERSION_LABEL@";
43 if (!("--runasroot" ~~ @ARGV) && ($< == 0 || $> == 0)) {
44 if ($uid !~ /^\d+$/) {
45 # Named UID, look it up
49 die "Cannot find a valid UID to change to";
51 # drop root if we were configured with an ircd UID
54 if ($< == 0 || $> == 0) {
55 die "Could not drop root: $!";
60 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
61 my $arg = shift(@ARGV);
65 if ($a =~ m/^--config=(.*)$/)
72 $conf = $confpath . "inspircd.conf";
73 push @ARGV, '--config='.$conf;
78 # System for naming script command subs:
79 # cmd_<name> - Normal command for use by users.
80 # dev_<name> - Developer commands.
81 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
82 # Ideally command subs shouldn't return.
86 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
89 print STDERR "Invalid command or none given.\n";
101 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
102 my @cmds = grep /^cmd_/, @subs;
103 my @devs = grep /^dev_/, @subs;
105 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
106 $_ =~ s/_/-/g foreach (@cmds, @devs);
107 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
108 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
114 if (getstatus() == 1) {
115 my $pid = getprocessid();
116 print "InspIRCd is running (PID: $pid)\n";
119 print "InspIRCd is not running. (Or PID File not found)\n";
126 if (getstatus() == 1) {
127 my $pid = getprocessid();
128 system("kill -HUP $pid >/dev/null 2>&1");
129 print "InspIRCd rehashed (pid: $pid).\n";
132 print "InspIRCd is not running. (Or PID File not found)\n";
139 if (getstatus() == 0) { goto &cmd_start(); }
145 print "InspIRCd version: $version\n";
152 unlink($pidfile) if (-e $pidfile);
156 sub hid_cheese_sandwich()
158 print "Creating Cheese Sandwich..\n";
165 # Check to see its not 'running' already.
166 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
167 # If we are still alive here.. Try starting the IRCd..
169 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
170 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
172 exec "$binpath/$executable", @_;
173 die "Failed to start IRCd: $!\n";
178 # Check to see its not 'running' already.
179 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
182 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
183 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
188 # If we are still alive here.. Try starting the IRCd..
189 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
190 die "Failed to start GDB: $!\n";
193 sub dev_screendebug(@)
195 # Check to see its not 'running' already.
196 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
199 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
205 # If we are still alive here.. Try starting the IRCd..
206 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
207 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
208 exec qw(screen -m -d gdb), "--command=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
209 die "Failed to start screen: $!\n";
214 # Check to see its not 'running' already.
215 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
218 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
219 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
221 # Check we have valgrind and gdb
225 # If we are still alive here.. Try starting the IRCd..
226 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
227 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
228 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
229 die "Failed to start valgrind: $!\n";
232 sub dev_valdebug_unattended(@)
234 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
235 # Check to see its not 'running' already.
236 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
239 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
240 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
242 # Check we have valgrind and gdb
246 # If we are still alive here.. Try starting the IRCd..
248 # 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!
249 # Redirect stdout to /dev/null if you're worried about the security.
254 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
255 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
256 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
257 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
258 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
259 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
260 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
261 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
262 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
263 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
264 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
265 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
266 die "Can't execute valgrind: $!\n";
270 sub dev_screenvaldebug(@)
272 # Check to see its not 'running' already.
273 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
276 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
277 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
284 # If we are still alive here.. Try starting the IRCd..
285 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
286 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
287 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
288 die "Failed to start screen: $!\n";
293 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
294 # Get to here, we have something to kill.
295 my $pid = getprocessid();
296 print "Stopping InspIRCd (pid: $pid)...\n";
297 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 15;
298 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
301 if (getstatus() == 0) {
302 print "InspIRCd Stopped.\n";
306 print "InspIRCd not dying quietly -- forcing kill\n";
312 # Generic Helper Functions.
315 # GetPidfile Version 2 - Now With Include Support..
316 # I beg for months for include support in insp, then..
317 # when it is added, it comes around and BITES ME IN THE ASS,
318 # because i then have to code support into this script.. Evil.
320 # Craig got bitten in the ass again --
321 # in 1.1 beta the include file is manditory, therefore
322 # if we cant find it, default to %conf%/inspircd.pid.
323 # Note, this also contains a fix for when the pid file is
324 # defined, but defined in a comment (line starts with #)
332 # Before we start, do we have a PID already? (Should never occur)
336 # Are We using a relative path?
337 if ($file !~ /^\//) {
338 # Convert it to a full path.
339 $file = $runpath .'/'. $file;
342 # Have we checked this file before?
343 return if $filesparsed{$file};
344 $filesparsed{$file} = 1;
347 open INFILE, '<', $file or return;
348 # Grab entire file contents..
349 my(@lines) = <INFILE>;
353 # remove trailing spaces
357 $i =~ s/[^=]+=\s(.*)/\1/;
358 # Does this file have a pid?
359 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
361 # Set the PID file and return.
367 elsif (-f "$runpath/$pidfile")
369 $pidfile = "$runpath/$pidfile";
377 # If we get here, NO PID FILE! -- Check for includes
379 $i =~ s/[^=]+=\s(.*)/\1/;
380 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
382 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
385 if ($pidfile ne "") {
392 # End of includes / No includes found. Using default.
393 $pidfile = $datadir . "/inspircd.pid";
397 my $pid = getprocessid();
398 return 0 if $pid == 0;
405 open PIDFILE, '<', $pidfile or return 0;
408 /^(\d+)$/ and $pid = $1;
416 unless(`valgrind --version`)
418 print "Couldn't start valgrind: $!\n";
425 unless(`gdb --version`)
427 print "Couldn't start gdb: $!\n";
434 unless(`screen --version`)
436 print "Couldn't start screen: $!\n";