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 $scriptpath = "@SCRIPT_DIR@";
52 my $basepath = "@BASE_DIR@";
53 my $confpath = "@CONFIG_DIR@/";
54 my $binpath = "@BINARY_DIR@";
55 my $runpath = "@BASE_DIR@";
56 my $datadir = "@DATA_DIR@";
57 my $valgrindlogpath = "$basepath/valgrindlogs";
58 my $executable = "inspircd";
59 my $version = "@VERSION_FULL@";
62 if (!("--runasroot" ~~ @ARGV) && ($< == 0 || $> == 0)) {
63 if ($uid !~ /^\d+$/) {
64 # Named UID, look it up
68 die "Cannot find a valid UID to change to";
70 # drop root if we were configured with an ircd UID
73 if ($< == 0 || $> == 0) {
74 die "Could not drop root: $!";
79 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
80 my $arg = shift(@ARGV);
84 if ($a =~ m/^--config=(.*)$/)
91 $conf = $confpath . "inspircd.conf";
92 push @ARGV, '--config='.$conf;
97 # System for naming script command subs:
98 # cmd_<name> - Normal command for use by users.
99 # dev_<name> - Developer commands.
100 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
101 # Ideally command subs shouldn't return.
105 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
108 print STDERR "Invalid command or none given.\n";
110 exit GENERIC_EXIT_UNIMPLEMENTED;
114 exit $sub->(@ARGV); # Error code passed through return value
119 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
120 my @cmds = grep /^cmd_/, @subs;
121 my @devs = grep /^dev_/, @subs;
123 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
124 $_ =~ s/_/-/g foreach (@cmds, @devs);
125 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
126 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
127 exit GENERIC_EXIT_SUCCESS;
132 if (getstatus() == 1) {
133 my $pid = getprocessid();
134 print "InspIRCd is running (PID: $pid)\n";
135 exit STATUS_EXIT_SUCCESS;
137 print "InspIRCd is not running. (Or PID File not found)\n";
138 exit STATUS_EXIT_NOT_RUNNING;
144 if (getstatus() == 1) {
145 my $pid = getprocessid();
146 system("kill -HUP $pid >/dev/null 2>&1");
147 print "InspIRCd rehashed (pid: $pid).\n";
148 exit GENERIC_EXIT_SUCCESS;
150 print "InspIRCd is not running. (Or PID File not found)\n";
151 exit GENERIC_EXIT_NOT_RUNNING;
157 if (getstatus() == 0) { goto &cmd_start(@_); }
158 exit GENERIC_EXIT_UNSPECIFIED;
163 print "InspIRCd version: $version\n";
164 exit GENERIC_EXIT_SUCCESS;
170 unlink($pidfile) if (-e $pidfile);
174 sub hid_cheese_sandwich()
176 print "Creating Cheese Sandwich..\n";
178 exit GENERIC_EXIT_SUCCESS;
183 # Check to see its not 'running' already.
184 if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
185 # If we are still alive here.. Try starting the IRCd..
187 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
188 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
190 exec "$binpath/$executable", @_;
191 die "Failed to start IRCd: $!\n";
196 # Check to see its not 'running' already.
197 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
200 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
201 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
206 # If we are still alive here.. Try starting the IRCd..
207 exec 'gdb', "--command=$scriptpath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
208 die "Failed to start GDB: $!\n";
211 sub dev_screendebug(@)
213 # Check to see its not 'running' already.
214 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
217 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
223 # If we are still alive here.. Try starting the IRCd..
224 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
225 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
226 exec qw(screen -m -d gdb), "--command=$scriptpath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
227 die "Failed to start screen: $!\n";
232 # Check to see its not 'running' already.
233 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
236 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
237 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
239 # Check we have valgrind and gdb
243 # If we are still alive here.. Try starting the IRCd..
244 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
245 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
246 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
247 die "Failed to start valgrind: $!\n";
250 sub dev_valdebug_unattended(@)
252 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
253 # Check to see its not 'running' already.
254 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
257 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
258 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
260 # Check we have valgrind and gdb
264 # If we are still alive here.. Try starting the IRCd..
266 # 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!
267 # Redirect stdout to /dev/null if you're worried about the security.
272 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
273 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
274 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
275 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
276 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
277 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
278 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
279 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
280 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
281 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
282 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
283 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
284 die "Can't execute valgrind: $!\n";
288 sub dev_screenvaldebug(@)
290 # Check to see its not 'running' already.
291 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
294 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
295 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
302 # If we are still alive here.. Try starting the IRCd..
303 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
304 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
305 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
306 die "Failed to start screen: $!\n";
311 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
312 # Get to here, we have something to kill.
313 my $pid = getprocessid();
314 print "Stopping InspIRCd (pid: $pid)...\n";
315 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 15;
316 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
319 if (getstatus() == 0) {
320 print "InspIRCd Stopped.\n";
321 return GENERIC_EXIT_SUCCESS;
324 print "InspIRCd not dying quietly -- forcing kill\n";
326 return GENERIC_EXIT_SUCCESS;
330 # Generic Helper Functions.
333 # GetPidfile Version 2 - Now With Include Support..
334 # I beg for months for include support in insp, then..
335 # when it is added, it comes around and BITES ME IN THE ASS,
336 # because i then have to code support into this script.. Evil.
338 # Craig got bitten in the ass again --
339 # in 1.1 beta the include file is manditory, therefore
340 # if we cant find it, default to %conf%/inspircd.pid.
341 # Note, this also contains a fix for when the pid file is
342 # defined, but defined in a comment (line starts with #)
350 # Before we start, do we have a PID already? (Should never occur)
354 # Are We using a relative path?
355 if ($file !~ /^\//) {
356 # Convert it to a full path.
357 $file = $runpath .'/'. $file;
360 # Have we checked this file before?
361 return if $filesparsed{$file};
362 $filesparsed{$file} = 1;
365 open INFILE, '<', $file or return;
366 # Grab entire file contents..
367 my(@lines) = <INFILE>;
371 # remove trailing spaces
375 $i =~ s/[^=]+=\s(.*)/\1/;
376 # Does this file have a pid?
377 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
379 # Set the PID file and return.
385 elsif (-f "$runpath/$pidfile")
387 $pidfile = "$runpath/$pidfile";
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 = $datadir . "/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;