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 # From http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
35 STATUS_EXIT_SUCCESS => 0,
36 STATUS_EXIT_DEAD_WITH_PIDFILE => 1,
37 STATUS_EXIT_DEAD_WITH_LOCKFILE => 2,
38 STATUS_EXIT_NOT_RUNNING => 3,
39 STATUS_EXIT_UNKNOWN => 4,
41 GENERIC_EXIT_SUCCESS => 0,
42 GENERIC_EXIT_UNSPECIFIED => 1,
43 GENERIC_EXIT_INVALID_ARGUMENTS => 2,
44 GENERIC_EXIT_UNIMPLEMENTED => 3,
45 GENERIC_EXIT_INSUFFICIENT_PRIVILEGE => 4,
46 GENERIC_EXIT_NOT_INSTALLED => 5,
47 GENERIC_EXIT_NOT_CONFIGURED => 6,
48 GENERIC_EXIT_NOT_RUNNING => 7
51 my $scriptpath = "@SCRIPT_DIR@";
52 my $basepath = "@BASE_DIR@";
53 my $confpath = "@CONFIG_DIR@";
54 my $binpath = "@BINARY_DIR@";
55 my $runpath = "@BASE_DIR@";
56 my $datadir = "@DATA_DIR@";
57 my $valgrindlogpath = "$basepath/valgrindlogs";
58 my $executable = "inspircd";
59 my $version = "@VERSION_FULL@";
62 sub expand_fragment($$) {
63 my ($base, $fragment) = @_;
64 if ($fragment =~ /^\//) {
67 return "$base/$fragment";
71 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
72 if ($uid !~ /^\d+$/) {
73 # Named UID, look it up
77 die "Cannot find a valid UID to change to";
79 # drop root if we were configured with an ircd UID
82 if ($< == 0 || $> == 0) {
83 die "Could not drop root: $!";
88 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
89 my $arg = shift(@ARGV);
93 if ($a =~ m/^--config=(.*)$/)
100 $conf = expand_fragment $confpath, "inspircd.conf";
101 push @ARGV, '--config='.$conf;
106 # System for naming script command subs:
107 # cmd_<name> - Normal command for use by users.
108 # dev_<name> - Developer commands.
109 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
110 # Ideally command subs shouldn't return.
114 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
117 print STDERR "Invalid command or none given.\n";
119 exit GENERIC_EXIT_UNIMPLEMENTED;
123 exit $sub->(@ARGV); # Error code passed through return value
128 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
129 my @cmds = grep /^cmd_/, @subs;
130 my @devs = grep /^dev_/, @subs;
132 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
133 $_ =~ s/_/-/g foreach (@cmds, @devs);
134 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
135 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
136 exit GENERIC_EXIT_SUCCESS;
141 if (getstatus() == 1) {
142 my $pid = getprocessid();
143 print "InspIRCd is running (PID: $pid)\n";
144 exit STATUS_EXIT_SUCCESS;
146 print "InspIRCd is not running. (Or PID File not found)\n";
147 exit STATUS_EXIT_NOT_RUNNING;
153 if (getstatus() == 1) {
154 my $pid = getprocessid();
155 system("kill -HUP $pid >/dev/null 2>&1");
156 print "InspIRCd rehashed (pid: $pid).\n";
157 exit GENERIC_EXIT_SUCCESS;
159 print "InspIRCd is not running. (Or PID File not found)\n";
160 exit GENERIC_EXIT_NOT_RUNNING;
166 if (getstatus() == 0) { goto &cmd_start(@_); }
167 exit GENERIC_EXIT_UNSPECIFIED;
172 print "InspIRCd version: $version\n";
173 exit GENERIC_EXIT_SUCCESS;
179 unlink($pidfile) if (-e $pidfile);
183 sub hid_cheese_sandwich()
185 print "Creating Cheese Sandwich..\n";
187 exit GENERIC_EXIT_SUCCESS;
192 # Check to see its not 'running' already.
193 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
194 # If we are still alive here.. Try starting the IRCd..
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 exec "$binpath/$executable", @_;
200 die "Failed to start IRCd: $!\n";
205 # Check to see its not 'running' already.
206 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
209 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
210 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
215 # If we are still alive here.. Try starting the IRCd..
216 exec 'gdb', "--command=$scriptpath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
217 die "Failed to start GDB: $!\n";
220 sub dev_screendebug(@)
222 # Check to see its not 'running' already.
223 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");
232 # If we are still alive here.. Try starting the IRCd..
233 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
234 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
235 exec qw(screen -m -d gdb), "--command=$scriptpath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
236 die "Failed to start screen: $!\n";
241 # Check to see its not 'running' already.
242 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
245 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
246 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
248 # Check we have valgrind and gdb
252 # If we are still alive here.. Try starting the IRCd..
253 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
254 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
255 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
256 die "Failed to start valgrind: $!\n";
259 sub dev_valdebug_unattended(@)
261 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
262 # Check to see its not 'running' already.
263 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
266 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
267 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
269 # Check we have valgrind and gdb
273 # If we are still alive here.. Try starting the IRCd..
275 # 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!
276 # Redirect stdout to /dev/null if you're worried about the security.
281 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
282 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
283 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
284 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
285 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
286 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
287 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
288 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
289 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
290 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
291 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
292 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
293 die "Can't execute valgrind: $!\n";
297 sub dev_screenvaldebug(@)
299 # Check to see its not 'running' already.
300 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
303 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
304 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
311 # If we are still alive here.. Try starting the IRCd..
312 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
313 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
314 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
315 die "Failed to start screen: $!\n";
320 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
321 # Get to here, we have something to kill.
322 my $pid = getprocessid();
323 print "Stopping InspIRCd (pid: $pid)...\n";
324 my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
325 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
328 if (getstatus() == 0) {
329 print "InspIRCd Stopped.\n";
330 return GENERIC_EXIT_SUCCESS;
333 print "InspIRCd not dying quietly -- forcing kill\n";
335 return GENERIC_EXIT_SUCCESS;
339 # Generic Helper Functions.
342 # GetPidfile Version 2 - Now With Include Support..
343 # I beg for months for include support in insp, then..
344 # when it is added, it comes around and BITES ME IN THE ASS,
345 # because i then have to code support into this script.. Evil.
347 # Craig got bitten in the ass again --
348 # in 1.1 beta the include file is manditory, therefore
349 # if we cant find it, default to %conf%/inspircd.pid.
350 # Note, this also contains a fix for when the pid file is
351 # defined, but defined in a comment (line starts with #)
359 # Before we start, do we have a PID already? (Should never occur)
364 # Expand any relative paths.
365 $file = expand_fragment $confpath, $file;
367 # Have we checked this file before?
368 return if $filesparsed{$file};
369 $filesparsed{$file} = 1;
372 open INFILE, '<', $file or return;
373 # Grab entire file contents..
374 my(@lines) = <INFILE>;
378 # remove trailing spaces
382 $i =~ s/[^=]+=\s(.*)/\1/;
383 # Does this file have a pid?
384 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
386 # Set the PID file and return.
387 $pidfile = expand_fragment $datadir, $1;
393 # If we get here, NO PID FILE! -- Check for includes
395 $i =~ s/[^=]+=\s(.*)/\1/;
396 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
398 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
401 if ($pidfile ne "") {
408 # End of includes / No includes found. Using default.
409 $pidfile = $datadir . "/inspircd.pid";
413 my $pid = getprocessid();
414 return 0 if $pid == 0;
421 open PIDFILE, '<', $pidfile or return 0;
424 /^(\d+)$/ and $pid = $1;
432 unless(`valgrind --version`)
434 print "Couldn't start valgrind: $!\n";
435 exit GENERIC_EXIT_UNSPECIFIED;
441 unless(`gdb --version`)
443 print "Couldn't start gdb: $!\n";
444 exit GENERIC_EXIT_UNSPECIFIED;
450 unless(`screen --version`)
452 print "Couldn't start screen: $!\n";
453 exit GENERIC_EXIT_UNSPECIFIED;