4 # InspIRCd -- Internet Relay Chat Daemon
6 # Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 # This file is part of InspIRCd. InspIRCd is free software: you can
9 # redistribute it and/or modify it under the terms of the GNU General Public
10 # License as published by the Free Software Foundation, version 2.
12 # This program is distributed in the hope that it will be useful, but WITHOUT
13 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # InspIRCd Start up the InspIRCd Internet Relay Chat Daemon
23 # chkconfig: 2345 55 25
24 # description: InspIRCd -- Internet Relay Chat Daemon
26 # processname: inspircd
32 my $basepath = "@BASE_DIR@";
33 my $confpath = "@CONFIG_DIR@/";
34 my $binpath = "@BINARY_DIR@";
35 my $runpath = "@BASE_DIR@";
36 my $datadir = "@DATA_DIR@";
37 my $valgrindlogpath = "$basepath/valgrindlogs";
38 my $executable = "@EXECUTABLE@";
39 my $version = "@VERSION@";
42 if ($< == 0 || $> == 0) {
43 if ($uid !~ /^\d+$/) {
44 # Named UID, look it up
48 die "Cannot find a valid UID to change to";
50 # drop root if we were configured with an ircd UID
53 if ($< == 0 || $> == 0) {
54 die "Could not drop root: $!";
59 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
60 my $arg = shift(@ARGV);
64 if ($a =~ m/^--config=(.*)$/)
71 $conf = $confpath . "inspircd.conf";
72 push @ARGV, '--config='.$conf;
77 # System for naming script command subs:
78 # cmd_<name> - Normal command for use by users.
79 # dev_<name> - Developer commands.
80 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
81 # Ideally command subs shouldn't return.
85 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
88 print STDERR "Invalid command or none given.\n";
100 my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
101 my @cmds = grep /^cmd_/, @subs;
102 my @devs = grep /^dev_/, @subs;
104 $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
105 $_ =~ s/_/-/g foreach (@cmds, @devs);
106 print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
107 print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
113 if (getstatus() == 1) {
114 my $pid = getprocessid();
115 print "InspIRCd is running (PID: $pid)\n";
118 print "InspIRCd is not running. (Or PID File not found)\n";
125 if (getstatus() == 1) {
126 my $pid = getprocessid();
127 system("kill -HUP $pid >/dev/null 2>&1");
128 print "InspIRCd rehashed (pid: $pid).\n";
131 print "InspIRCd is not running. (Or PID File not found)\n";
138 if (getstatus() == 0) { goto &cmd_start(); }
144 print "InspIRCd version: $version\n";
151 unlink($pidfile) if (-e $pidfile);
155 sub hid_cheese_sandwich()
157 print "Creating Cheese Sandwich..\n";
164 # Check to see its not 'running' already.
165 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
166 # If we are still alive here.. Try starting the IRCd..
168 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
169 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
171 exec "$binpath/$executable", @_;
172 die "Failed to start IRCd: $!\n";
177 # Check to see its not 'running' already.
178 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
181 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
182 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
187 # If we are still alive here.. Try starting the IRCd..
188 exec 'gdb', "--command=$basepath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
189 die "Failed to start GDB: $!\n";
192 sub dev_screendebug(@)
194 # Check to see its not 'running' already.
195 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
198 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
204 # If we are still alive here.. Try starting the IRCd..
205 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
206 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
207 exec qw(screen -m -d gdb), "--command=$basepath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
208 die "Failed to start screen: $!\n";
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");
218 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
220 # Check we have valgrind and gdb
224 # If we are still alive here.. Try starting the IRCd..
225 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
226 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
227 exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
228 die "Failed to start valgrind: $!\n";
231 sub dev_valdebug_unattended(@)
233 # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
234 # Check to see its not 'running' already.
235 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
238 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
239 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
241 # Check we have valgrind and gdb
245 # If we are still alive here.. Try starting the IRCd..
247 # 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!
248 # Redirect stdout to /dev/null if you're worried about the security.
253 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
254 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
255 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
256 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
257 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
258 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
259 # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
260 # Could be useful when we want to stop it complaining about things we're sure aren't issues.
261 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=15 --track-fds=yes),
262 "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
263 qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
264 "$binpath/$executable", qw(--nofork --debug --nolog), @_;
265 die "Can't execute valgrind: $!\n";
269 sub dev_screenvaldebug(@)
271 # Check to see its not 'running' already.
272 if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
275 print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
276 print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
283 # If we are still alive here.. Try starting the IRCd..
284 print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
285 print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
286 exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
287 die "Failed to start screen: $!\n";
292 if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
293 # Get to here, we have something to kill.
294 my $pid = getprocessid();
295 print "Stopping InspIRCd (pid: $pid)...\n";
296 my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 90 : 15;
297 kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
300 if (getstatus() == 0) {
301 print "InspIRCd Stopped.\n";
305 print "InspIRCd not dying quietly -- forcing kill\n";
311 # Generic Helper Functions.
314 # GetPidfile Version 2 - Now With Include Support..
315 # I beg for months for include support in insp, then..
316 # when it is added, it comes around and BITES ME IN THE ASS,
317 # because i then have to code support into this script.. Evil.
319 # Craig got bitten in the ass again --
320 # in 1.1 beta the include file is manditory, therefore
321 # if we cant find it, default to %conf%/inspircd.pid.
322 # Note, this also contains a fix for when the pid file is
323 # defined, but defined in a comment (line starts with #)
331 # Before we start, do we have a PID already? (Should never occur)
335 # Are We using a relative path?
336 if ($file !~ /^\//) {
337 # Convert it to a full path.
338 $file = $runpath .'/'. $file;
341 # Have we checked this file before?
342 return if $filesparsed{$file};
343 $filesparsed{$file} = 1;
346 open INFILE, '<', $file or return;
347 # Grab entire file contents..
348 my(@lines) = <INFILE>;
352 # remove trailing spaces
356 $i =~ s/[^=]+=\s(.*)/\1/;
357 # Does this file have a pid?
358 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
360 # Set the PID file and return.
366 elsif (-f "$runpath/$pidfile")
368 $pidfile = "$runpath/$pidfile";
376 # If we get here, NO PID FILE! -- Check for includes
378 $i =~ s/[^=]+=\s(.*)/\1/;
379 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
381 # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
384 if ($pidfile ne "") {
391 # End of includes / No includes found. Using default.
392 $pidfile = $datadir . "/inspircd.pid";
396 my $pid = getprocessid();
397 return 0 if $pid == 0;
404 open PIDFILE, '<', $pidfile or return 0;
407 /^(\d+)$/ and $pid = $1;
415 unless(`valgrind --version`)
417 print "Couldn't start valgrind: $!\n";
424 unless(`gdb --version`)
426 print "Couldn't start gdb: $!\n";
433 unless(`screen --version`)
435 print "Couldn't start screen: $!\n";