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 $basepath = "@BASE_DIR@";
52 my $confpath = "@CONFIG_DIR@/";
53 my $binpath = "@BINARY_DIR@";
54 my $runpath = "@BASE_DIR@";
55 my $datadir = "@DATA_DIR@";
56 my $valgrindlogpath = "$basepath/valgrindlogs";
57 my $executable = "inspircd";
58 my $version = "@VERSION_MAJOR@.@VERSION_MINOR@.@VERSION_PATCH@+@VERSION_LABEL@";
61 if (!("--runasroot" ~~ @ARGV) && ($< == 0 || $> == 0)) {
62 if ($uid !~ /^\d+$/) {
63 # Named UID, look it up
67 die "Cannot find a valid UID to change to";
69 # drop root if we were configured with an ircd UID
72 if ($< == 0 || $> == 0) {
73 die "Could not drop root: $!";
78 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
79 my $arg = shift(@ARGV);
83 if ($a =~ m/^--config=(.*)$/)
90 $conf = $confpath . "inspircd.conf";
91 push @ARGV, '--config='.$conf;
96 # System for naming script command subs:
97 # cmd_<name> - Normal command for use by users.
98 # dev_<name> - Developer commands.
99 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
100 # Ideally command subs shouldn't return.
104 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
107 print STDERR "Invalid command or none given.\n";
109 exit GENERIC_EXIT_UNIMPLEMENTED;
113 exit $sub->(@ARGV); # Error code passed through return value
118 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
119 my @cmds = grep /^cmd_/, @subs;
120 my @devs = grep /^dev_/, @subs;
122 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
123 $_ =~ s/_/-/g foreach (@cmds, @devs);
124 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
125 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
126 exit GENERIC_EXIT_SUCCESS;
131 if (getstatus() == 1) {
132 my $pid = getprocessid();
133 print "InspIRCd is running (PID: $pid)\n";
134 exit STATUS_EXIT_SUCCESS;
136 print "InspIRCd is not running. (Or PID File not found)\n";
137 exit STATUS_EXIT_NOT_RUNNING;
143 if (getstatus() == 1) {
144 my $pid = getprocessid();
145 system("kill -HUP $pid >/dev/null 2>&1");
146 print "InspIRCd rehashed (pid: $pid).\n";
147 exit GENERIC_EXIT_SUCCESS;
149 print "InspIRCd is not running. (Or PID File not found)\n";
150 exit GENERIC_EXIT_NOT_RUNNING;
156 if (getstatus() == 0) { goto &cmd_start(); }
157 exit GENERIC_EXIT_UNSPECIFIED;
162 print "InspIRCd version: $version\n";
163 exit GENERIC_EXIT_SUCCESS;
169 unlink($pidfile) if (-e $pidfile);
173 sub hid_cheese_sandwich()
175 print "Creating Cheese Sandwich..\n";
177 exit GENERIC_EXIT_SUCCESS;
182 # Check to see its not 'running' already.
183 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
184 # If we are still alive here.. Try starting the IRCd..
186 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
187 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
189 exec "$binpath/$executable", @_;
190 die "Failed to start IRCd: $!\n";
195 # Check to see its not 'running' already.
196 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
199 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
200 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
205 # If we are still alive here.. Try starting the IRCd..
206 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
207 die "Failed to start GDB: $!\n";
210 sub dev_screendebug(@)
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");
222 # If we are still alive here.. Try starting the IRCd..
223 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
224 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
225 exec qw(screen -m -d gdb), "--command=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
226 die "Failed to start screen: $!\n";
231 # Check to see its not 'running' already.
232 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
235 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
236 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
238 # Check we have valgrind and gdb
242 # If we are still alive here.. Try starting the IRCd..
243 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
244 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
245 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
246 die "Failed to start valgrind: $!\n";
249 sub dev_valdebug_unattended(@)
251 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
252 # Check to see its not 'running' already.
253 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
256 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
257 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
259 # Check we have valgrind and gdb
263 # If we are still alive here.. Try starting the IRCd..
265 # 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!
266 # Redirect stdout to /dev/null if you're worried about the security.
271 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
272 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
273 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
274 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
275 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
276 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
277 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
278 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
279 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
280 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
281 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
282 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
283 die "Can't execute valgrind: $!\n";
287 sub dev_screenvaldebug(@)
289 # Check to see its not 'running' already.
290 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
293 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
294 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
301 # If we are still alive here.. Try starting the IRCd..
302 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
303 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
304 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
305 die "Failed to start screen: $!\n";
310 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
311 # Get to here, we have something to kill.
312 my $pid = getprocessid();
313 print "Stopping InspIRCd (pid: $pid)...\n";
314 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 15;
315 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
318 if (getstatus() == 0) {
319 print "InspIRCd Stopped.\n";
320 return GENERIC_EXIT_SUCCESS;
323 print "InspIRCd not dying quietly -- forcing kill\n";
325 return GENERIC_EXIT_SUCCESS;
329 # Generic Helper Functions.
332 # GetPidfile Version 2 - Now With Include Support..
333 # I beg for months for include support in insp, then..
334 # when it is added, it comes around and BITES ME IN THE ASS,
335 # because i then have to code support into this script.. Evil.
337 # Craig got bitten in the ass again --
338 # in 1.1 beta the include file is manditory, therefore
339 # if we cant find it, default to %conf%/inspircd.pid.
340 # Note, this also contains a fix for when the pid file is
341 # defined, but defined in a comment (line starts with #)
349 # Before we start, do we have a PID already? (Should never occur)
353 # Are We using a relative path?
354 if ($file !~ /^\//) {
355 # Convert it to a full path.
356 $file = $runpath .'/'. $file;
359 # Have we checked this file before?
360 return if $filesparsed{$file};
361 $filesparsed{$file} = 1;
364 open INFILE, '<', $file or return;
365 # Grab entire file contents..
366 my(@lines) = <INFILE>;
370 # remove trailing spaces
374 $i =~ s/[^=]+=\s(.*)/\1/;
375 # Does this file have a pid?
376 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
378 # Set the PID file and return.
384 elsif (-f "$runpath/$pidfile")
386 $pidfile = "$runpath/$pidfile";
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 = $datadir . "/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;