]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/template/inspircd
Merge insp20
[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 $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@";
59 my $uid = "@UID@";
60
61 if (!("--runasroot" ~~ @ARGV) && ($< == 0 || $> == 0)) {
62         if ($uid !~ /^\d+$/) {
63                 # Named UID, look it up
64                 $uid = getpwnam $uid;
65         }
66         if (!$uid) {
67                 die "Cannot find a valid UID to change to";
68         }
69         # drop root if we were configured with an ircd UID
70         $< = $uid;
71         $> = $uid;
72         if ($< == 0 || $> == 0) {
73                 die "Could not drop root: $!";
74         }
75 }
76
77 our($pid,$pidfile);
78 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
79 my $arg = shift(@ARGV);
80 my $conf;
81 for my $a (@ARGV)
82 {
83         if ($a =~ m/^--config=(.*)$/)
84         {
85                 $conf = $1;
86                 last;
87         }
88 }
89 if (!defined $conf) {
90         $conf = $confpath . "inspircd.conf";
91         push @ARGV, '--config='.$conf;
92 }
93
94 getpidfile($conf);
95
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.
101
102 my $subname = $arg;
103 $subname =~ s/-/_/g;
104 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
105 if (!defined($sub))
106 {
107         print STDERR "Invalid command or none given.\n";
108         cmd_help();
109         exit GENERIC_EXIT_UNIMPLEMENTED;
110 }
111 else
112 {
113         exit $sub->(@ARGV); # Error code passed through return value
114 }
115
116 sub cmd_help()
117 {
118         my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
119         my @cmds = grep /^cmd_/, @subs;
120         my @devs = grep /^dev_/, @subs;
121         local $_;
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;
127 }
128
129 sub cmd_status()
130 {
131         if (getstatus() == 1) {
132                 my $pid = getprocessid();
133                 print "InspIRCd is running (PID: $pid)\n";
134                 exit STATUS_EXIT_SUCCESS;
135         } else {
136                 print "InspIRCd is not running. (Or PID File not found)\n";
137                 exit STATUS_EXIT_NOT_RUNNING;
138         }
139 }
140
141 sub cmd_rehash()
142 {
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;
148         } else {
149                 print "InspIRCd is not running. (Or PID File not found)\n";
150                 exit GENERIC_EXIT_NOT_RUNNING;
151         }
152 }
153
154 sub cmd_cron()
155 {
156         if (getstatus() == 0) { goto &cmd_start(@_); }
157         exit GENERIC_EXIT_UNSPECIFIED;
158 }
159
160 sub cmd_version()
161 {
162         print "InspIRCd version: $version\n";
163         exit GENERIC_EXIT_SUCCESS;
164 }
165
166 sub cmd_restart(@)
167 {
168         cmd_stop();
169         unlink($pidfile) if (-e $pidfile);
170         goto &cmd_start(@_);
171 }
172
173 sub hid_cheese_sandwich()
174 {
175         print "Creating Cheese Sandwich..\n";
176         print "Done.\n";
177         exit GENERIC_EXIT_SUCCESS;
178 }
179
180 sub cmd_start(@)
181 {
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..
185         chdir $runpath;
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");
188
189         exec "$binpath/$executable", @_;
190         die "Failed to start IRCd: $!\n";
191 }
192
193 sub dev_debug(@)
194 {
195         # Check to see its not 'running' already.
196         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
197
198         chdir $runpath;
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");
201
202         # Check we have gdb
203         checkgdb();
204
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";
208 }
209
210 sub dev_screendebug(@)
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
218         #Check we have gdb
219         checkgdb();
220         checkscreen();
221
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";
227 }
228
229 sub dev_valdebug(@)
230 {
231         # Check to see its not 'running' already.
232         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
233
234         chdir $runpath;
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");
237
238         # Check we have valgrind and gdb
239         checkvalgrind();
240         checkgdb();
241
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";
247 }
248
249 sub dev_valdebug_unattended(@)
250 {
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; }
254
255         chdir $runpath;
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");
258
259         # Check we have valgrind and gdb
260         checkvalgrind();
261         checkgdb();
262
263         # If we are still alive here.. Try starting the IRCd..
264         #
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.
267         #
268         my $pid = fork;
269         if ($pid == 0) {
270                 POSIX::setsid();
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";
284         }
285 }
286
287 sub dev_screenvaldebug(@)
288 {
289         # Check to see its not 'running' already.
290         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
291
292         chdir $runpath;
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");
295
296         #Check we have gdb
297         checkvalgrind();
298         checkgdb();
299         checkscreen();
300
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";
306 }
307
308 sub cmd_stop()
309 {
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";
316         for (1..$maxwait) {
317                 sleep 1;
318                 if (getstatus() == 0) {
319                         print "InspIRCd Stopped.\n";
320                         return GENERIC_EXIT_SUCCESS;
321                 }
322         }
323         print "InspIRCd not dying quietly -- forcing kill\n";
324         kill KILL => $pid;
325         return GENERIC_EXIT_SUCCESS;
326 }
327
328 ###
329 # Generic Helper Functions.
330 ###
331
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.
336
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 #)
342 # -- Brain
343
344 my %filesparsed;
345
346 sub getpidfile
347 {
348         my ($file) = @_;
349         # Before we start, do we have a PID already? (Should never occur)
350         if ($pid ne "") {
351                 return;
352         }
353         # Are We using a relative path?
354         if ($file !~ /^\//) {
355                 # Convert it to a full path.
356                 $file = $runpath .'/'. $file;
357         }
358
359         # Have we checked this file before?
360         return if $filesparsed{$file};
361         $filesparsed{$file} = 1;
362
363         # Open the File..
364         open INFILE, '<', $file or return;
365         # Grab entire file contents..
366         my(@lines) = <INFILE>;
367         # Close the file
368         close INFILE;
369
370         # remove trailing spaces
371         chomp(@lines);
372         for my $i (@lines) {
373                 # clean it up
374                 $i =~ s/[^=]+=\s(.*)/\1/;
375                 # Does this file have a pid?
376                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
377                 {
378                         # Set the PID file and return.
379                         $pidfile = $1;
380                         if (-f $pidfile)
381                         {
382                                 return;
383                         }
384                         elsif (-f "$runpath/$pidfile")
385                         {
386                                 $pidfile = "$runpath/$pidfile";
387                                 return;
388                         }
389                         return;
390                 }
391         }
392
393
394         # If we get here, NO PID FILE! -- Check for includes
395         for my $i (@lines) {
396                 $i =~ s/[^=]+=\s(.*)/\1/;
397                 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
398                 {
399                         # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
400                         getpidfile($1);
401                         # Was a PID found?
402                         if ($pidfile ne "") {
403                                 # Yes, Return.
404                                 return;
405                         }
406                 }
407         }
408
409         # End of includes / No includes found. Using default.
410         $pidfile = $datadir . "/inspircd.pid";
411 }
412
413 sub getstatus {
414         my $pid = getprocessid();
415         return 0 if $pid == 0;
416         return kill 0, $pid;
417 }
418
419
420 sub getprocessid {
421         my $pid = 0;
422         open PIDFILE, '<', $pidfile or return 0;
423         while(<PIDFILE>)
424         {
425                 /^(\d+)$/ and $pid = $1;
426         }
427         close PIDFILE;
428         return $pid;
429 }
430
431 sub checkvalgrind
432 {
433         unless(`valgrind --version`)
434         {
435                 print "Couldn't start valgrind: $!\n";
436                 exit GENERIC_EXIT_UNSPECIFIED;
437         }
438 }
439
440 sub checkgdb
441 {
442         unless(`gdb --version`)
443         {
444                 print "Couldn't start gdb: $!\n";
445                 exit GENERIC_EXIT_UNSPECIFIED;
446         }
447 }
448
449 sub checkscreen
450 {
451         unless(`screen --version`)
452         {
453                 print "Couldn't start screen: $!\n";
454                 exit GENERIC_EXIT_UNSPECIFIED;
455         }
456 }