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