]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/template/inspircd
Allow customising the example config directory at build time.
[user/henk/code/inspircd.git] / make / template / inspircd
1 %mode 0750
2 #!/usr/bin/env perl
3
4 #
5 # InspIRCd -- Internet Relay Chat Daemon
6 #
7 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
8 #
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.
12 #
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
16 # details.
17 #
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/>.
20 #
21
22 # InspIRCd          Start up the InspIRCd Internet Relay Chat Daemon
23 #
24 # chkconfig: 2345 55 25
25 # description: InspIRCd -- Internet Relay Chat Daemon
26 #
27 # processname: inspircd
28
29 use strict;
30 use POSIX;
31 use Fcntl;
32
33 # From http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
34 use constant {
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,
40
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
49 };
50
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@";
60 my $uid = "@UID@";
61
62 sub expand_fragment($$) {
63         my ($base, $fragment) = @_;
64         if ($fragment =~ /^\//) {
65                 return $fragment;
66         } else {
67                 return "$base/$fragment";
68         }
69 }
70
71 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
72         if ($uid !~ /^\d+$/) {
73                 # Named UID, look it up
74                 $uid = getpwnam $uid;
75         }
76         if (!$uid) {
77                 die "Cannot find a valid UID to change to";
78         }
79         # drop root if we were configured with an ircd UID
80         $< = $uid;
81         $> = $uid;
82         if ($< == 0 || $> == 0) {
83                 die "Could not drop root: $!";
84         }
85 }
86
87 our($pid,$pidfile);
88 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
89 my $arg = shift(@ARGV);
90 my $conf;
91 for my $a (@ARGV)
92 {
93         if ($a =~ m/^--config=(.*)$/)
94         {
95                 $conf = $1;
96                 last;
97         }
98 }
99 if (!defined $conf) {
100         $conf = expand_fragment $confpath, "inspircd.conf";
101         push @ARGV, '--config='.$conf;
102 }
103
104 getpidfile($conf);
105
106 # System for naming script command subs:
107 # cmd_<name> - Normal command for use by users.
108 # dev_<name> - Developer commands.
109 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
110 # Ideally command subs shouldn't return.
111
112 my $subname = $arg;
113 $subname =~ s/-/_/g;
114 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
115 if (!defined($sub))
116 {
117         print STDERR "Invalid command or none given.\n";
118         cmd_help();
119         exit GENERIC_EXIT_UNIMPLEMENTED;
120 }
121 else
122 {
123         exit $sub->(@ARGV); # Error code passed through return value
124 }
125
126 sub cmd_help()
127 {
128         my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
129         my @cmds = grep /^cmd_/, @subs;
130         my @devs = grep /^dev_/, @subs;
131         local $_;
132         $_ =~ s/^(cmd|dev)_// foreach (@cmds, @devs);
133         $_ =~ s/_/-/g foreach (@cmds, @devs);
134         print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
135         print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
136         exit GENERIC_EXIT_SUCCESS;
137 }
138
139 sub cmd_status()
140 {
141         if (getstatus() == 1) {
142                 my $pid = getprocessid();
143                 print "InspIRCd is running (PID: $pid)\n";
144                 exit STATUS_EXIT_SUCCESS;
145         } else {
146                 print "InspIRCd is not running. (Or PID File not found)\n";
147                 exit STATUS_EXIT_NOT_RUNNING;
148         }
149 }
150
151 sub cmd_rehash()
152 {
153         if (getstatus() == 1) {
154                 my $pid = getprocessid();
155                 system("kill -HUP $pid >/dev/null 2>&1");
156                 print "InspIRCd rehashed (pid: $pid).\n";
157                 exit GENERIC_EXIT_SUCCESS;
158         } else {
159                 print "InspIRCd is not running. (Or PID File not found)\n";
160                 exit GENERIC_EXIT_NOT_RUNNING;
161         }
162 }
163
164 sub cmd_cron()
165 {
166         if (getstatus() == 0) { goto &cmd_start(@_); }
167         exit GENERIC_EXIT_UNSPECIFIED;
168 }
169
170 sub cmd_version()
171 {
172         print "InspIRCd version: $version\n";
173         exit GENERIC_EXIT_SUCCESS;
174 }
175
176 sub cmd_restart(@)
177 {
178         cmd_stop();
179         unlink($pidfile) if (-e $pidfile);
180         goto &cmd_start(@_);
181 }
182
183 sub hid_cheese_sandwich()
184 {
185         print "Creating Cheese Sandwich..\n";
186         print "Done.\n";
187         exit GENERIC_EXIT_SUCCESS;
188 }
189
190 sub cmd_start(@)
191 {
192         # Check to see its not 'running' already.
193         if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
194         # If we are still alive here.. Try starting the IRCd..
195         chdir $runpath;
196         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
197         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
198
199         exec "$binpath/$executable", @_;
200         die "Failed to start IRCd: $!\n";
201 }
202
203 sub dev_debug(@)
204 {
205         # Check to see its not 'running' already.
206         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
207
208         chdir $runpath;
209         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
210         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
211
212         # Check we have gdb
213         checkgdb();
214
215         # If we are still alive here.. Try starting the IRCd..
216         exec 'gdb', "--command=$scriptpath/.gdbargs", '--args', "$binpath/$executable", qw(--nofork --debug), @_;
217         die "Failed to start GDB: $!\n";
218 }
219
220 sub dev_screendebug(@)
221 {
222         # Check to see its not 'running' already.
223         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
224
225         chdir $runpath;
226         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
227
228         #Check we have gdb
229         checkgdb();
230         checkscreen();
231
232         # If we are still alive here.. Try starting the IRCd..
233         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
234         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
235         exec qw(screen -m -d gdb), "--command=$scriptpath/.gdbargs", '-args', "$binpath/$executable", qw(--nofork --debug --nolog), @_;
236         die "Failed to start screen: $!\n";
237 }
238
239 sub dev_valdebug(@)
240 {
241         # Check to see its not 'running' already.
242         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
243
244         chdir $runpath;
245         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
246         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
247
248         # Check we have valgrind and gdb
249         checkvalgrind();
250         checkgdb();
251
252         # If we are still alive here.. Try starting the IRCd..
253         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
254         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
255         exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
256         die "Failed to start valgrind: $!\n";
257 }
258
259 sub dev_valdebug_unattended(@)
260 {
261         # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
262         # Check to see its not 'running' already.
263         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
264
265         chdir $runpath;
266         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
267         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
268
269         # Check we have valgrind and gdb
270         checkvalgrind();
271         checkgdb();
272
273         # If we are still alive here.. Try starting the IRCd..
274         #
275         # 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!
276         # Redirect stdout to /dev/null if you're worried about the security.
277         #
278         my $pid = fork;
279         if ($pid == 0) {
280                 POSIX::setsid();
281                 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
282                 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
283                 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
284                 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
285                 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
286                 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
287         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
288         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
289                 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
290                         "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
291                         qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
292                         "$binpath/$executable", qw(--nofork --debug --nolog), @_;
293                 die "Can't execute valgrind: $!\n";
294         }
295 }
296
297 sub dev_screenvaldebug(@)
298 {
299         # Check to see its not 'running' already.
300         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
301
302         chdir $runpath;
303         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
304         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
305
306         #Check we have gdb
307         checkvalgrind();
308         checkgdb();
309         checkscreen();
310
311         # If we are still alive here.. Try starting the IRCd..
312         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
313         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
314         exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
315         die "Failed to start screen: $!\n";
316 }
317
318 sub cmd_stop()
319 {
320         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
321         # Get to here, we have something to kill.
322         my $pid = getprocessid();
323         print "Stopping InspIRCd (pid: $pid)...\n";
324         my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
325         kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
326         for (1..$maxwait) {
327                 sleep 1;
328                 if (getstatus() == 0) {
329                         print "InspIRCd Stopped.\n";
330                         return GENERIC_EXIT_SUCCESS;
331                 }
332         }
333         print "InspIRCd not dying quietly -- forcing kill\n";
334         kill KILL => $pid;
335         return GENERIC_EXIT_SUCCESS;
336 }
337
338 ###
339 # Generic Helper Functions.
340 ###
341
342 # GetPidfile Version 2 - Now With Include Support..
343 # I beg for months for include support in insp, then..
344 # when it is added, it comes around and BITES ME IN THE ASS,
345 # because i then have to code support into this script.. Evil.
346
347 # Craig got bitten in the ass again --
348 # in 1.1 beta the include file is manditory, therefore
349 # if we cant find it, default to %conf%/inspircd.pid.
350 # Note, this also contains a fix for when the pid file is
351 # defined, but defined in a comment (line starts with #)
352 # -- Brain
353
354 my %filesparsed;
355
356 sub getpidfile
357 {
358         my ($file) = @_;
359         # Before we start, do we have a PID already? (Should never occur)
360         if ($pid ne "") {
361                 return;
362         }
363
364         # Expand any relative paths.
365         $file = expand_fragment $confpath, $file;
366
367         # Have we checked this file before?
368         return if $filesparsed{$file};
369         $filesparsed{$file} = 1;
370
371         # Open the File..
372         open INFILE, '<', $file or return;
373         # Grab entire file contents..
374         my(@lines) = <INFILE>;
375         # Close the file
376         close INFILE;
377
378         # remove trailing spaces
379         chomp(@lines);
380         for my $i (@lines) {
381                 # clean it up
382                 $i =~ s/[^=]+=\s(.*)/\1/;
383                 # Does this file have a pid?
384                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
385                 {
386                         # Set the PID file and return.
387                         $pidfile = expand_fragment $datadir, $1;
388                         return;
389                 }
390         }
391
392
393         # If we get here, NO PID FILE! -- Check for includes
394         for my $i (@lines) {
395                 $i =~ s/[^=]+=\s(.*)/\1/;
396                 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
397                 {
398                         # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
399                         getpidfile($1);
400                         # Was a PID found?
401                         if ($pidfile ne "") {
402                                 # Yes, Return.
403                                 return;
404                         }
405                 }
406         }
407
408         # End of includes / No includes found. Using default.
409         $pidfile = $datadir . "/inspircd.pid";
410 }
411
412 sub getstatus {
413         my $pid = getprocessid();
414         return 0 if $pid == 0;
415         return kill 0, $pid;
416 }
417
418
419 sub getprocessid {
420         my $pid = 0;
421         open PIDFILE, '<', $pidfile or return 0;
422         while(<PIDFILE>)
423         {
424                 /^(\d+)$/ and $pid = $1;
425         }
426         close PIDFILE;
427         return $pid;
428 }
429
430 sub checkvalgrind
431 {
432         unless(`valgrind --version`)
433         {
434                 print "Couldn't start valgrind: $!\n";
435                 exit GENERIC_EXIT_UNSPECIFIED;
436         }
437 }
438
439 sub checkgdb
440 {
441         unless(`gdb --version`)
442         {
443                 print "Couldn't start gdb: $!\n";
444                 exit GENERIC_EXIT_UNSPECIFIED;
445         }
446 }
447
448 sub checkscreen
449 {
450         unless(`screen --version`)
451         {
452                 print "Couldn't start screen: $!\n";
453                 exit GENERIC_EXIT_UNSPECIFIED;
454         }
455 }