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 $confpath = "@CONFIG_DIR@";
66 my $binpath = "@BINARY_DIR@";
67 my $runtimedir = "@RUNTIME_DIR@";
68 my $valgrindlogpath = "@LOG_DIR@/valgrind";
69 my $executable = "inspircd";
70 my $version = "@VERSION_FULL@";
74 '--eval-command', 'handle SIGPIPE pass nostop noprint',
75 '--eval-command', 'handle SIGHUP pass nostop noprint',
76 '--eval-command', 'run',
77 '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
80 sub expand_fragment($$) {
81 my ($base, $fragment) = @_;
82 if ($fragment =~ /^\//) {
85 return "$base/$fragment";
89 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
90 if ($uid !~ /^\d+$/) {
91 # Named UID, look it up
95 die "Cannot find a valid UID to change to";
97 # drop root if we were configured with an ircd UID
100 if ($< == 0 || $> == 0) {
101 die "Could not drop root: $!";
106 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
107 my $arg = shift(@ARGV);
111 if ($a =~ m/^--config=(.*)$/)
117 if (!defined $conf) {
118 $conf = expand_fragment $confpath, "inspircd.conf";
119 push @ARGV, '--config='.$conf;
124 # System for naming script command subs:
125 # cmd_<name> - Normal command for use by users.
126 # dev_<name> - Developer commands.
127 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
128 # Ideally command subs shouldn't return.
132 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
135 print STDERR "Invalid command or none given.\n";
137 exit GENERIC_EXIT_UNIMPLEMENTED;
141 exit $sub->(@ARGV); # Error code passed through return value
146 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
147 my @cmds = grep /^cmd_/, @subs;
148 my @devs = grep /^dev_/, @subs;
150 $_ =~ s/^(cmd|dev)_// for (@cmds, @devs);
151 $_ =~ s/_/-/g for (@cmds, @devs);
152 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
153 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
154 exit GENERIC_EXIT_SUCCESS;
159 if (getstatus() == 1) {
160 my $pid = getprocessid();
161 print "InspIRCd is running (PID: $pid)\n";
162 exit STATUS_EXIT_SUCCESS;
164 print "InspIRCd is not running. (Or PID File not found)\n";
165 exit STATUS_EXIT_NOT_RUNNING;
171 if (getstatus() == 1) {
172 my $pid = getprocessid();
174 print "InspIRCd rehashed (pid: $pid).\n";
175 exit GENERIC_EXIT_SUCCESS;
177 print "InspIRCd is not running. (Or PID File not found)\n";
178 exit GENERIC_EXIT_NOT_RUNNING;
184 if (getstatus() == 0) { goto &cmd_start(@_); }
185 exit GENERIC_EXIT_UNSPECIFIED;
190 print "InspIRCd version: $version\n";
191 exit GENERIC_EXIT_SUCCESS;
197 unlink($pidfile) if (-e $pidfile);
201 sub hid_cheese_sandwich()
203 print "Creating Cheese Sandwich..\n";
205 exit GENERIC_EXIT_SUCCESS;
210 # Check to see its not 'running' already.
211 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
213 # If we are still alive here.. Try starting the IRCd..
214 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
215 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
217 exec "$binpath/$executable", @_;
218 die "Failed to start IRCd: $!\n";
223 # Check to see its not 'running' already.
224 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");
227 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
232 # If we are still alive here.. Try starting the IRCd..
233 exec 'gdb', @gdbargs, @_;
234 die "Failed to start GDB: $!\n";
237 sub dev_screendebug(@)
239 # Check to see its not 'running' already.
240 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
242 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
248 # If we are still alive here.. Try starting the IRCd..
249 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
250 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
251 exec qw(screen -m -d gdb), @gdbargs, @_;
252 die "Failed to start screen: $!\n";
257 # Check to see its not 'running' already.
258 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
260 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
261 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
263 # Check we have valgrind and gdb
267 # If we are still alive here.. Try starting the IRCd..
268 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
269 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
270 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
271 die "Failed to start valgrind: $!\n";
274 sub dev_valdebug_unattended(@)
276 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
277 # Check to see its not 'running' already.
278 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
280 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
281 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
283 # Check we have valgrind and gdb
287 # If we are still alive here.. Try starting the IRCd..
289 # 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!
290 # Redirect stdout to /dev/null if you're worried about the security.
295 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
296 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
297 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
298 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
299 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
300 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
301 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
302 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
303 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
304 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
305 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
306 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
307 die "Can't execute valgrind: $!\n";
311 sub dev_screenvaldebug(@)
313 # Check to see its not 'running' already.
314 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
316 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
317 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
324 # If we are still alive here.. Try starting the IRCd..
325 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
326 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
327 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
328 die "Failed to start screen: $!\n";
333 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
334 # Get to here, we have something to kill.
335 my $pid = getprocessid();
336 print "Stopping InspIRCd (pid: $pid)...\n";
337 my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
338 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
341 if (getstatus() == 0) {
342 print "InspIRCd Stopped.\n";
343 return GENERIC_EXIT_SUCCESS;
346 print "InspIRCd not dying quietly -- forcing kill\n";
348 return GENERIC_EXIT_SUCCESS;
352 # Generic Helper Functions.
360 # Before we start, do we have a PID already? (Should never occur)
365 # Expand any relative paths.
366 $file = expand_fragment $confpath, $file;
368 # Have we checked this file before?
369 return if $filesparsed{$file};
370 $filesparsed{$file} = 1;
373 open INFILE, '<', $file or return;
374 # Grab entire file contents..
375 my(@lines) = <INFILE>;
379 # remove trailing spaces
383 $i =~ s/[^=]+=\s(.*)/\1/;
384 # Does this file have a pid?
385 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
387 # Set the PID file and return.
388 $pidfile = expand_fragment $runtimedir, $1;
394 # If we get here, NO PID FILE! -- Check for includes
396 $i =~ s/[^=]+=\s(.*)/\1/;
397 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
399 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
402 if ($pidfile ne "") {
409 # End of includes / No includes found. Using default.
410 $pidfile = $runtimedir . "/inspircd.pid";
414 my $pid = getprocessid();
415 return 0 if $pid == 0;
422 open PIDFILE, '<', $pidfile or return 0;
425 /^(\d+)$/ and $pid = $1;
433 unless(`valgrind --version`)
435 print "Couldn't start valgrind: $!\n";
436 exit GENERIC_EXIT_UNSPECIFIED;
442 unless(`gdb --version`)
444 print "Couldn't start gdb: $!\n";
445 exit GENERIC_EXIT_UNSPECIFIED;
451 unless(`screen --version`)
453 print "Couldn't start screen: $!\n";
454 exit GENERIC_EXIT_UNSPECIFIED;