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