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 $runpath = "@BASE_DIR@";
69 my $datadir = "@DATA_DIR@";
70 my $valgrindlogpath = "$basepath/valgrindlogs";
71 my $executable = "inspircd";
72 my $version = "@VERSION_FULL@";
76 '--eval-command', 'handle SIGPIPE pass nostop noprint',
77 '--eval-command', 'handle SIGHUP pass nostop noprint',
78 '--eval-command', 'run',
79 '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
82 sub expand_fragment($$) {
83 my ($base, $fragment) = @_;
84 if ($fragment =~ /^\//) {
87 return "$base/$fragment";
91 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
92 if ($uid !~ /^\d+$/) {
93 # Named UID, look it up
97 die "Cannot find a valid UID to change to";
99 # drop root if we were configured with an ircd UID
102 if ($< == 0 || $> == 0) {
103 die "Could not drop root: $!";
108 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
109 my $arg = shift(@ARGV);
113 if ($a =~ m/^--config=(.*)$/)
119 if (!defined $conf) {
120 $conf = expand_fragment $confpath, "inspircd.conf";
121 push @ARGV, '--config='.$conf;
126 # System for naming script command subs:
127 # cmd_<name> - Normal command for use by users.
128 # dev_<name> - Developer commands.
129 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
130 # Ideally command subs shouldn't return.
134 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
137 print STDERR "Invalid command or none given.\n";
139 exit GENERIC_EXIT_UNIMPLEMENTED;
143 exit $sub->(@ARGV); # Error code passed through return value
148 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
149 my @cmds = grep /^cmd_/, @subs;
150 my @devs = grep /^dev_/, @subs;
152 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
153 $_ =~ s/_/-/g foreach (@cmds, @devs);
154 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
155 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
156 exit GENERIC_EXIT_SUCCESS;
161 if (getstatus() == 1) {
162 my $pid = getprocessid();
163 print "InspIRCd is running (PID: $pid)\n";
164 exit STATUS_EXIT_SUCCESS;
166 print "InspIRCd is not running. (Or PID File not found)\n";
167 exit STATUS_EXIT_NOT_RUNNING;
173 if (getstatus() == 1) {
174 my $pid = getprocessid();
175 system("kill -HUP $pid >/dev/null 2>&1");
176 print "InspIRCd rehashed (pid: $pid).\n";
177 exit GENERIC_EXIT_SUCCESS;
179 print "InspIRCd is not running. (Or PID File not found)\n";
180 exit GENERIC_EXIT_NOT_RUNNING;
186 if (getstatus() == 0) { goto &cmd_start(@_); }
187 exit GENERIC_EXIT_UNSPECIFIED;
192 print "InspIRCd version: $version\n";
193 exit GENERIC_EXIT_SUCCESS;
199 unlink($pidfile) if (-e $pidfile);
203 sub hid_cheese_sandwich()
205 print "Creating Cheese Sandwich..\n";
207 exit GENERIC_EXIT_SUCCESS;
212 # Check to see its not 'running' already.
213 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
214 # If we are still alive here.. Try starting the IRCd..
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");
219 exec "$binpath/$executable", @_;
220 die "Failed to start IRCd: $!\n";
225 # Check to see its not 'running' already.
226 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
229 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
230 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
235 # If we are still alive here.. Try starting the IRCd..
236 exec 'gdb', @gdbargs, @_;
237 die "Failed to start GDB: $!\n";
240 sub dev_screendebug(@)
242 # Check to see its not 'running' already.
243 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
246 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
252 # If we are still alive here.. Try starting the IRCd..
253 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
254 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
255 exec qw(screen -m -d gdb), @gdbargs, @_;
256 die "Failed to start screen: $!\n";
261 # Check to see its not 'running' already.
262 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
265 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
266 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
268 # Check we have valgrind and gdb
272 # If we are still alive here.. Try starting the IRCd..
273 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
274 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
275 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
276 die "Failed to start valgrind: $!\n";
279 sub dev_valdebug_unattended(@)
281 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
282 # Check to see its not 'running' already.
283 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
286 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
287 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
289 # Check we have valgrind and gdb
293 # If we are still alive here.. Try starting the IRCd..
295 # 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!
296 # Redirect stdout to /dev/null if you're worried about the security.
301 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
302 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
303 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
304 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
305 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
306 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
307 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
308 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
309 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
310 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
311 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
312 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
313 die "Can't execute valgrind: $!\n";
317 sub dev_screenvaldebug(@)
319 # Check to see its not 'running' already.
320 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
323 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
324 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
331 # If we are still alive here.. Try starting the IRCd..
332 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
333 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
334 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
335 die "Failed to start screen: $!\n";
340 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
341 # Get to here, we have something to kill.
342 my $pid = getprocessid();
343 print "Stopping InspIRCd (pid: $pid)...\n";
344 my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
345 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
348 if (getstatus() == 0) {
349 print "InspIRCd Stopped.\n";
350 return GENERIC_EXIT_SUCCESS;
353 print "InspIRCd not dying quietly -- forcing kill\n";
355 return GENERIC_EXIT_SUCCESS;
359 # Generic Helper Functions.
362 # GetPidfile Version 2 - Now With Include Support..
363 # I beg for months for include support in insp, then..
364 # when it is added, it comes around and BITES ME IN THE ASS,
365 # because i then have to code support into this script.. Evil.
367 # Craig got bitten in the ass again --
368 # in 1.1 beta the include file is manditory, therefore
369 # if we cant find it, default to %conf%/inspircd.pid.
370 # Note, this also contains a fix for when the pid file is
371 # defined, but defined in a comment (line starts with #)
379 # Before we start, do we have a PID already? (Should never occur)
384 # Expand any relative paths.
385 $file = expand_fragment $confpath, $file;
387 # Have we checked this file before?
388 return if $filesparsed{$file};
389 $filesparsed{$file} = 1;
392 open INFILE, '<', $file or return;
393 # Grab entire file contents..
394 my(@lines) = <INFILE>;
398 # remove trailing spaces
402 $i =~ s/[^=]+=\s(.*)/\1/;
403 # Does this file have a pid?
404 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
406 # Set the PID file and return.
407 $pidfile = expand_fragment $datadir, $1;
413 # If we get here, NO PID FILE! -- Check for includes
415 $i =~ s/[^=]+=\s(.*)/\1/;
416 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
418 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
421 if ($pidfile ne "") {
428 # End of includes / No includes found. Using default.
429 $pidfile = $datadir . "/inspircd.pid";
433 my $pid = getprocessid();
434 return 0 if $pid == 0;
441 open PIDFILE, '<', $pidfile or return 0;
444 /^(\d+)$/ and $pid = $1;
452 unless(`valgrind --version`)
454 print "Couldn't start valgrind: $!\n";
455 exit GENERIC_EXIT_UNSPECIFIED;
461 unless(`gdb --version`)
463 print "Couldn't start gdb: $!\n";
464 exit GENERIC_EXIT_UNSPECIFIED;
470 unless(`screen --version`)
472 print "Couldn't start screen: $!\n";
473 exit GENERIC_EXIT_UNSPECIFIED;