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@";
63 '--eval-command', 'handle SIGPIPE pass nostop noprint',
64 '--eval-command', 'handle SIGHUP pass nostop noprint',
65 '--eval-command', 'run',
66 '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
69 sub expand_fragment($$) {
70 my ($base, $fragment) = @_;
71 if ($fragment =~ /^\//) {
74 return "$base/$fragment";
78 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
79 if ($uid !~ /^\d+$/) {
80 # Named UID, look it up
84 die "Cannot find a valid UID to change to";
86 # drop root if we were configured with an ircd UID
89 if ($< == 0 || $> == 0) {
90 die "Could not drop root: $!";
95 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
96 my $arg = shift(@ARGV);
100 if ($a =~ m/^--config=(.*)$/)
106 if (!defined $conf) {
107 $conf = expand_fragment $confpath, "inspircd.conf";
108 push @ARGV, '--config='.$conf;
113 # System for naming script command subs:
114 # cmd_<name> - Normal command for use by users.
115 # dev_<name> - Developer commands.
116 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
117 # Ideally command subs shouldn't return.
121 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
124 print STDERR "Invalid command or none given.\n";
126 exit GENERIC_EXIT_UNIMPLEMENTED;
130 exit $sub->(@ARGV); # Error code passed through return value
135 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
136 my @cmds = grep /^cmd_/, @subs;
137 my @devs = grep /^dev_/, @subs;
139 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
140 $_ =~ s/_/-/g foreach (@cmds, @devs);
141 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
142 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
143 exit GENERIC_EXIT_SUCCESS;
148 if (getstatus() == 1) {
149 my $pid = getprocessid();
150 print "InspIRCd is running (PID: $pid)\n";
151 exit STATUS_EXIT_SUCCESS;
153 print "InspIRCd is not running. (Or PID File not found)\n";
154 exit STATUS_EXIT_NOT_RUNNING;
160 if (getstatus() == 1) {
161 my $pid = getprocessid();
162 system("kill -HUP $pid >/dev/null 2>&1");
163 print "InspIRCd rehashed (pid: $pid).\n";
164 exit GENERIC_EXIT_SUCCESS;
166 print "InspIRCd is not running. (Or PID File not found)\n";
167 exit GENERIC_EXIT_NOT_RUNNING;
173 if (getstatus() == 0) { goto &cmd_start(@_); }
174 exit GENERIC_EXIT_UNSPECIFIED;
179 print "InspIRCd version: $version\n";
180 exit GENERIC_EXIT_SUCCESS;
186 unlink($pidfile) if (-e $pidfile);
190 sub hid_cheese_sandwich()
192 print "Creating Cheese Sandwich..\n";
194 exit GENERIC_EXIT_SUCCESS;
199 # Check to see its not 'running' already.
200 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
201 # If we are still alive here.. Try starting the IRCd..
203 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
204 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
206 exec "$binpath/$executable", @_;
207 die "Failed to start IRCd: $!\n";
212 # Check to see its not 'running' already.
213 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
216 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
217 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
222 # If we are still alive here.. Try starting the IRCd..
223 exec 'gdb', @gdbargs, @_;
224 die "Failed to start GDB: $!\n";
227 sub dev_screendebug(@)
229 # Check to see its not 'running' already.
230 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
233 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
239 # If we are still alive here.. Try starting the IRCd..
240 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
241 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
242 exec qw(screen -m -d gdb), @gdbargs, @_;
243 die "Failed to start screen: $!\n";
248 # Check to see its not 'running' already.
249 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
252 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
253 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
255 # Check we have valgrind and gdb
259 # If we are still alive here.. Try starting the IRCd..
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=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
263 die "Failed to start valgrind: $!\n";
266 sub dev_valdebug_unattended(@)
268 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
269 # Check to see its not 'running' already.
270 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
273 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
274 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
276 # Check we have valgrind and gdb
280 # If we are still alive here.. Try starting the IRCd..
282 # 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!
283 # Redirect stdout to /dev/null if you're worried about the security.
288 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
289 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
290 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
291 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
292 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
293 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
294 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
295 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
296 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
297 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
298 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
299 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
300 die "Can't execute valgrind: $!\n";
304 sub dev_screenvaldebug(@)
306 # Check to see its not 'running' already.
307 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
310 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
311 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
318 # If we are still alive here.. Try starting the IRCd..
319 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
320 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
321 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
322 die "Failed to start screen: $!\n";
327 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
328 # Get to here, we have something to kill.
329 my $pid = getprocessid();
330 print "Stopping InspIRCd (pid: $pid)...\n";
331 my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
332 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
335 if (getstatus() == 0) {
336 print "InspIRCd Stopped.\n";
337 return GENERIC_EXIT_SUCCESS;
340 print "InspIRCd not dying quietly -- forcing kill\n";
342 return GENERIC_EXIT_SUCCESS;
346 # Generic Helper Functions.
349 # GetPidfile Version 2 - Now With Include Support..
350 # I beg for months for include support in insp, then..
351 # when it is added, it comes around and BITES ME IN THE ASS,
352 # because i then have to code support into this script.. Evil.
354 # Craig got bitten in the ass again --
355 # in 1.1 beta the include file is manditory, therefore
356 # if we cant find it, default to %conf%/inspircd.pid.
357 # Note, this also contains a fix for when the pid file is
358 # defined, but defined in a comment (line starts with #)
366 # Before we start, do we have a PID already? (Should never occur)
371 # Expand any relative paths.
372 $file = expand_fragment $confpath, $file;
374 # Have we checked this file before?
375 return if $filesparsed{$file};
376 $filesparsed{$file} = 1;
379 open INFILE, '<', $file or return;
380 # Grab entire file contents..
381 my(@lines) = <INFILE>;
385 # remove trailing spaces
389 $i =~ s/[^=]+=\s(.*)/\1/;
390 # Does this file have a pid?
391 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
393 # Set the PID file and return.
394 $pidfile = expand_fragment $datadir, $1;
400 # If we get here, NO PID FILE! -- Check for includes
402 $i =~ s/[^=]+=\s(.*)/\1/;
403 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
405 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
408 if ($pidfile ne "") {
415 # End of includes / No includes found. Using default.
416 $pidfile = $datadir . "/inspircd.pid";
420 my $pid = getprocessid();
421 return 0 if $pid == 0;
428 open PIDFILE, '<', $pidfile or return 0;
431 /^(\d+)$/ and $pid = $1;
439 unless(`valgrind --version`)
441 print "Couldn't start valgrind: $!\n";
442 exit GENERIC_EXIT_UNSPECIFIED;
448 unless(`gdb --version`)
450 print "Couldn't start gdb: $!\n";
451 exit GENERIC_EXIT_UNSPECIFIED;
457 unless(`screen --version`)
459 print "Couldn't start screen: $!\n";
460 exit GENERIC_EXIT_UNSPECIFIED;