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-2021 Sadie Powell <sadie@witchery.services>
10 # Copyright (C) 2012 Robby <robby@chatbelgie.be>
11 # Copyright (C) 2011 DjSlash <djslash@djslash.org>
12 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
13 # Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
14 # Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
15 # Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
16 # Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
17 # Copyright (C) 2006 John Brooks <special@inspircd.org>
18 # Copyright (C) 2005-2006, 2008-2009 Craig Edwards <brain@inspircd.org>
19 # Copyright (C) 2005 Craig McLure <craig@frostycoolslug.com>
21 # This file is part of InspIRCd. InspIRCd is free software: you can
22 # redistribute it and/or modify it under the terms of the GNU General Public
23 # License as published by the Free Software Foundation, version 2.
25 # This program is distributed in the hope that it will be useful, but WITHOUT
26 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
27 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
30 # You should have received a copy of the GNU General Public License
31 # along with this program. If not, see <http://www.gnu.org/licenses/>.
34 # InspIRCd Start up the InspIRCd Internet Relay Chat Daemon
36 # chkconfig: 2345 55 25
37 # description: InspIRCd -- Internet Relay Chat Daemon
39 # processname: inspircd
45 # From http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
47 STATUS_EXIT_SUCCESS => 0,
48 STATUS_EXIT_DEAD_WITH_PIDFILE => 1,
49 STATUS_EXIT_DEAD_WITH_LOCKFILE => 2,
50 STATUS_EXIT_NOT_RUNNING => 3,
51 STATUS_EXIT_UNKNOWN => 4,
53 GENERIC_EXIT_SUCCESS => 0,
54 GENERIC_EXIT_UNSPECIFIED => 1,
55 GENERIC_EXIT_INVALID_ARGUMENTS => 2,
56 GENERIC_EXIT_UNIMPLEMENTED => 3,
57 GENERIC_EXIT_INSUFFICIENT_PRIVILEGE => 4,
58 GENERIC_EXIT_NOT_INSTALLED => 5,
59 GENERIC_EXIT_NOT_CONFIGURED => 6,
60 GENERIC_EXIT_NOT_RUNNING => 7
63 my $scriptpath = "@SCRIPT_DIR@";
64 my $confpath = "@CONFIG_DIR@";
65 my $binpath = "@BINARY_DIR@";
66 my $runtimedir = "@RUNTIME_DIR@";
67 my $valgrindlogpath = "@LOG_DIR@/valgrind";
68 my $executable = "inspircd";
69 my $version = "@VERSION_FULL@";
73 '--eval-command', 'handle SIGPIPE pass nostop noprint',
74 '--eval-command', 'handle SIGHUP pass nostop noprint',
75 '--eval-command', 'run',
76 '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
79 sub expand_fragment($$) {
80 my ($base, $fragment) = @_;
81 if ($fragment =~ /^\//) {
84 return "$base/$fragment";
88 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
89 if ($uid !~ /^\d+$/) {
90 # Named UID, look it up
94 die "Cannot find a valid UID to change to";
96 # drop root if we were configured with an ircd UID
99 if ($< == 0 || $> == 0) {
100 die "Could not drop root: $!";
105 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
106 my $arg = shift(@ARGV);
110 if ($a =~ m/^--config=(.*)$/)
116 if (!defined $conf) {
117 $conf = expand_fragment $confpath, "inspircd.conf";
118 push @ARGV, '--config='.$conf;
123 # System for naming script command subs:
124 # cmd_<name> - Normal command for use by users.
125 # dev_<name> - Developer commands.
126 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
127 # Ideally command subs shouldn't return.
131 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
134 print STDERR "Invalid command or none given.\n";
136 exit GENERIC_EXIT_UNIMPLEMENTED;
140 exit $sub->(@ARGV); # Error code passed through return value
145 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
146 my @cmds = grep /^cmd_/, @subs;
147 my @devs = grep /^dev_/, @subs;
149 $_ =~ s/^(cmd|dev)_// for (@cmds, @devs);
150 $_ =~ s/_/-/g for (@cmds, @devs);
151 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
152 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
153 exit GENERIC_EXIT_SUCCESS;
158 if (getstatus() == 1) {
159 my $pid = getprocessid();
160 print "InspIRCd is running (PID: $pid)\n";
161 exit STATUS_EXIT_SUCCESS;
163 print "InspIRCd is not running. (Or PID File not found)\n";
164 exit STATUS_EXIT_NOT_RUNNING;
170 if (getstatus() == 1) {
171 my $pid = getprocessid();
173 print "InspIRCd rehashed (pid: $pid).\n";
174 exit GENERIC_EXIT_SUCCESS;
176 print "InspIRCd is not running. (Or PID File not found)\n";
177 exit GENERIC_EXIT_NOT_RUNNING;
183 if (getstatus() == 0) { goto &cmd_start(@_); }
184 exit GENERIC_EXIT_UNSPECIFIED;
189 print "InspIRCd version: $version\n";
190 exit GENERIC_EXIT_SUCCESS;
196 unlink($pidfile) if (-e $pidfile);
200 sub hid_cheese_sandwich()
202 print "Creating Cheese Sandwich..\n";
204 exit GENERIC_EXIT_SUCCESS;
209 # Check to see its not 'running' already.
210 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
212 # If we are still alive here.. Try starting the IRCd..
213 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
214 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
216 exec "$binpath/$executable", @_;
217 die "Failed to start IRCd: $!\n";
222 # Check to see its not 'running' already.
223 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
225 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
226 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
231 # If we are still alive here.. Try starting the IRCd..
232 exec 'gdb', @gdbargs, @_;
233 die "Failed to start GDB: $!\n";
236 sub dev_screendebug(@)
238 # Check to see its not 'running' already.
239 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
241 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
247 # If we are still alive here.. Try starting the IRCd..
248 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
249 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
250 exec qw(screen -m -d gdb), @gdbargs, @_;
251 die "Failed to start screen: $!\n";
256 # Check to see its not 'running' already.
257 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
259 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
260 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
262 # Check we have valgrind and gdb
266 # If we are still alive here.. Try starting the IRCd..
267 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
268 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
269 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
270 die "Failed to start valgrind: $!\n";
273 sub dev_valdebug_unattended(@)
275 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
276 # Check to see its not 'running' already.
277 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
279 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
280 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
282 # Check we have valgrind and gdb
286 # If we are still alive here.. Try starting the IRCd..
288 # 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!
289 # Redirect stdout to /dev/null if you're worried about the security.
294 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
295 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
296 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
297 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
298 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
299 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
300 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
301 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
302 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
303 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
304 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
305 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
306 die "Can't execute valgrind: $!\n";
310 sub dev_screenvaldebug(@)
312 # Check to see its not 'running' already.
313 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
315 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
316 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
323 # If we are still alive here.. Try starting the IRCd..
324 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
325 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
326 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
327 die "Failed to start screen: $!\n";
332 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
333 # Get to here, we have something to kill.
334 my $pid = getprocessid();
335 print "Stopping InspIRCd (pid: $pid)...\n";
336 my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
337 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
340 if (getstatus() == 0) {
341 print "InspIRCd Stopped.\n";
342 return GENERIC_EXIT_SUCCESS;
345 print "InspIRCd not dying quietly -- forcing kill\n";
347 return GENERIC_EXIT_SUCCESS;
351 # Generic Helper Functions.
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 $runtimedir, $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 = $runtimedir . "/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;