4 # InspIRCd -- Internet Relay Chat Daemon
6 # Copyright (C) 2015 Steven Van Acker <steven@singularity.be>
7 # Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
8 # Copyright (C) 2014 Dan Parsons <dparsons@nyip.net>
9 # Copyright (C) 2013-2014, 2016-2019 Sadie Powell <sadie@witchery.services>
10 # Copyright (C) 2012 Robby <robby@chatbelgie.be>
11 # Copyright (C) 2012 Adam <Adam@anope.org>
12 # Copyright (C) 2011 DjSlash <djslash@djslash.org>
13 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
14 # Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
15 # Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
16 # Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
17 # Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
18 # Copyright (C) 2006 John Brooks <special@inspircd.org>
19 # Copyright (C) 2005-2006, 2008-2009 Craig Edwards <brain@inspircd.org>
20 # Copyright (C) 2005 Craig McLure <craig@frostycoolslug.com>
22 # This file is part of InspIRCd. InspIRCd is free software: you can
23 # redistribute it and/or modify it under the terms of the GNU General Public
24 # License as published by the Free Software Foundation, version 2.
26 # This program is distributed in the hope that it will be useful, but WITHOUT
27 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
28 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
31 # You should have received a copy of the GNU General Public License
32 # along with this program. If not, see <http://www.gnu.org/licenses/>.
35 # InspIRCd Start up the InspIRCd Internet Relay Chat Daemon
37 # chkconfig: 2345 55 25
38 # description: InspIRCd -- Internet Relay Chat Daemon
40 # processname: inspircd
46 # From http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
48 STATUS_EXIT_SUCCESS => 0,
49 STATUS_EXIT_DEAD_WITH_PIDFILE => 1,
50 STATUS_EXIT_DEAD_WITH_LOCKFILE => 2,
51 STATUS_EXIT_NOT_RUNNING => 3,
52 STATUS_EXIT_UNKNOWN => 4,
54 GENERIC_EXIT_SUCCESS => 0,
55 GENERIC_EXIT_UNSPECIFIED => 1,
56 GENERIC_EXIT_INVALID_ARGUMENTS => 2,
57 GENERIC_EXIT_UNIMPLEMENTED => 3,
58 GENERIC_EXIT_INSUFFICIENT_PRIVILEGE => 4,
59 GENERIC_EXIT_NOT_INSTALLED => 5,
60 GENERIC_EXIT_NOT_CONFIGURED => 6,
61 GENERIC_EXIT_NOT_RUNNING => 7
64 my $scriptpath = "@SCRIPT_DIR@";
65 my $basepath = "@BASE_DIR@";
66 my $confpath = "@CONFIG_DIR@";
67 my $binpath = "@BINARY_DIR@";
68 my $runtimedir = "@RUNTIME_DIR@";
69 my $valgrindlogpath = "$basepath/valgrindlogs";
70 my $executable = "inspircd";
71 my $version = "@VERSION_FULL@";
75 '--eval-command', 'handle SIGPIPE pass nostop noprint',
76 '--eval-command', 'handle SIGHUP pass nostop noprint',
77 '--eval-command', 'run',
78 '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
81 sub expand_fragment($$) {
82 my ($base, $fragment) = @_;
83 if ($fragment =~ /^\//) {
86 return "$base/$fragment";
90 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
91 if ($uid !~ /^\d+$/) {
92 # Named UID, look it up
96 die "Cannot find a valid UID to change to";
98 # drop root if we were configured with an ircd UID
101 if ($< == 0 || $> == 0) {
102 die "Could not drop root: $!";
107 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
108 my $arg = shift(@ARGV);
112 if ($a =~ m/^--config=(.*)$/)
118 if (!defined $conf) {
119 $conf = expand_fragment $confpath, "inspircd.conf";
120 push @ARGV, '--config='.$conf;
125 # System for naming script command subs:
126 # cmd_<name> - Normal command for use by users.
127 # dev_<name> - Developer commands.
128 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
129 # Ideally command subs shouldn't return.
133 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
136 print STDERR "Invalid command or none given.\n";
138 exit GENERIC_EXIT_UNIMPLEMENTED;
142 exit $sub->(@ARGV); # Error code passed through return value
147 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
148 my @cmds = grep /^cmd_/, @subs;
149 my @devs = grep /^dev_/, @subs;
151 $_ =~ s/^(cmd|dev)_// for (@cmds, @devs);
152 $_ =~ s/_/-/g for (@cmds, @devs);
153 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
154 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
155 exit GENERIC_EXIT_SUCCESS;
160 if (getstatus() == 1) {
161 my $pid = getprocessid();
162 print "InspIRCd is running (PID: $pid)\n";
163 exit STATUS_EXIT_SUCCESS;
165 print "InspIRCd is not running. (Or PID File not found)\n";
166 exit STATUS_EXIT_NOT_RUNNING;
172 if (getstatus() == 1) {
173 my $pid = getprocessid();
175 print "InspIRCd rehashed (pid: $pid).\n";
176 exit GENERIC_EXIT_SUCCESS;
178 print "InspIRCd is not running. (Or PID File not found)\n";
179 exit GENERIC_EXIT_NOT_RUNNING;
185 if (getstatus() == 0) { goto &cmd_start(@_); }
186 exit GENERIC_EXIT_UNSPECIFIED;
191 print "InspIRCd version: $version\n";
192 exit GENERIC_EXIT_SUCCESS;
198 unlink($pidfile) if (-e $pidfile);
202 sub hid_cheese_sandwich()
204 print "Creating Cheese Sandwich..\n";
206 exit GENERIC_EXIT_SUCCESS;
211 # Check to see its not 'running' already.
212 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
214 # If we are still alive here.. Try starting the IRCd..
215 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
216 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
218 exec "$binpath/$executable", @_;
219 die "Failed to start IRCd: $!\n";
224 # Check to see its not 'running' already.
225 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
227 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
228 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
233 # If we are still alive here.. Try starting the IRCd..
234 exec 'gdb', @gdbargs, @_;
235 die "Failed to start GDB: $!\n";
238 sub dev_screendebug(@)
240 # Check to see its not 'running' already.
241 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
243 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
249 # If we are still alive here.. Try starting the IRCd..
250 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
251 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
252 exec qw(screen -m -d gdb), @gdbargs, @_;
253 die "Failed to start screen: $!\n";
258 # Check to see its not 'running' already.
259 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
261 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
262 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
264 # Check we have valgrind and gdb
268 # If we are still alive here.. Try starting the IRCd..
269 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
270 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
271 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
272 die "Failed to start valgrind: $!\n";
275 sub dev_valdebug_unattended(@)
277 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
278 # Check to see its not 'running' already.
279 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
281 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
282 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
284 # Check we have valgrind and gdb
288 # If we are still alive here.. Try starting the IRCd..
290 # 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!
291 # Redirect stdout to /dev/null if you're worried about the security.
296 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
297 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
298 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
299 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
300 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
301 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
302 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
303 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
304 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
305 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
306 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
307 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
308 die "Can't execute valgrind: $!\n";
312 sub dev_screenvaldebug(@)
314 # Check to see its not 'running' already.
315 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
317 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
318 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
325 # If we are still alive here.. Try starting the IRCd..
326 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
327 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
328 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
329 die "Failed to start screen: $!\n";
334 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
335 # Get to here, we have something to kill.
336 my $pid = getprocessid();
337 print "Stopping InspIRCd (pid: $pid)...\n";
338 my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
339 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
342 if (getstatus() == 0) {
343 print "InspIRCd Stopped.\n";
344 return GENERIC_EXIT_SUCCESS;
347 print "InspIRCd not dying quietly -- forcing kill\n";
349 return GENERIC_EXIT_SUCCESS;
353 # Generic Helper Functions.
361 # Before we start, do we have a PID already? (Should never occur)
366 # Expand any relative paths.
367 $file = expand_fragment $confpath, $file;
369 # Have we checked this file before?
370 return if $filesparsed{$file};
371 $filesparsed{$file} = 1;
374 open INFILE, '<', $file or return;
375 # Grab entire file contents..
376 my(@lines) = <INFILE>;
380 # remove trailing spaces
384 $i =~ s/[^=]+=\s(.*)/\1/;
385 # Does this file have a pid?
386 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
388 # Set the PID file and return.
389 $pidfile = expand_fragment $runtimedir, $1;
395 # If we get here, NO PID FILE! -- Check for includes
397 $i =~ s/[^=]+=\s(.*)/\1/;
398 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
400 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
403 if ($pidfile ne "") {
410 # End of includes / No includes found. Using default.
411 $pidfile = $runtimedir . "/inspircd.pid";
415 my $pid = getprocessid();
416 return 0 if $pid == 0;
423 open PIDFILE, '<', $pidfile or return 0;
426 /^(\d+)$/ and $pid = $1;
434 unless(`valgrind --version`)
436 print "Couldn't start valgrind: $!\n";
437 exit GENERIC_EXIT_UNSPECIFIED;
443 unless(`gdb --version`)
445 print "Couldn't start gdb: $!\n";
446 exit GENERIC_EXIT_UNSPECIFIED;
452 unless(`screen --version`)
454 print "Couldn't start screen: $!\n";
455 exit GENERIC_EXIT_UNSPECIFIED;