]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/template/inspircd
Always use for in Perl modules.
[user/henk/code/inspircd.git] / make / template / inspircd
1 %mode 0750
2 #!/usr/bin/env perl
3 #
4 # InspIRCd -- Internet Relay Chat Daemon
5 #
6 #   Copyright (C) 2015 Steven Van Acker <steven@singularity.be>
7 #   Copyright (C) 2015 Attila Molnar <attilamolnar@hush.com>
8 #   Copyright (C) 2014 Dan Parsons <dparsons@nyip.net>
9 #   Copyright (C) 2013-2014, 2016-2019 Sadie Powell <sadie@witchery.services>
10 #   Copyright (C) 2012 Robby <robby@chatbelgie.be>
11 #   Copyright (C) 2012 Adam <Adam@anope.org>
12 #   Copyright (C) 2011 DjSlash <djslash@djslash.org>
13 #   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
14 #   Copyright (C) 2008-2009 Robin Burchell <robin+git@viroteck.net>
15 #   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
16 #   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
17 #   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
18 #   Copyright (C) 2006 John Brooks <special@inspircd.org>
19 #   Copyright (C) 2005-2006, 2008-2009 Craig Edwards <brain@inspircd.org>
20 #   Copyright (C) 2005 Craig McLure <craig@frostycoolslug.com>
21 #
22 # This file is part of InspIRCd.  InspIRCd is free software: you can
23 # redistribute it and/or modify it under the terms of the GNU General Public
24 # License as published by the Free Software Foundation, version 2.
25 #
26 # This program is distributed in the hope that it will be useful, but WITHOUT
27 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
28 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
29 # details.
30 #
31 # You should have received a copy of the GNU General Public License
32 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
33 #
34
35 # InspIRCd          Start up the InspIRCd Internet Relay Chat Daemon
36 #
37 # chkconfig: 2345 55 25
38 # description: InspIRCd -- Internet Relay Chat Daemon
39 #
40 # processname: inspircd
41
42 use strict;
43 use POSIX;
44 use Fcntl;
45
46 # From http://refspecs.linuxbase.org/LSB_4.1.0/LSB-Core-generic/LSB-Core-generic/iniscrptact.html
47 use constant {
48     STATUS_EXIT_SUCCESS => 0,
49     STATUS_EXIT_DEAD_WITH_PIDFILE => 1,
50     STATUS_EXIT_DEAD_WITH_LOCKFILE => 2,
51     STATUS_EXIT_NOT_RUNNING => 3,
52     STATUS_EXIT_UNKNOWN => 4,
53
54     GENERIC_EXIT_SUCCESS => 0,
55     GENERIC_EXIT_UNSPECIFIED => 1,
56     GENERIC_EXIT_INVALID_ARGUMENTS => 2,
57     GENERIC_EXIT_UNIMPLEMENTED => 3,
58     GENERIC_EXIT_INSUFFICIENT_PRIVILEGE => 4,
59     GENERIC_EXIT_NOT_INSTALLED => 5,
60     GENERIC_EXIT_NOT_CONFIGURED => 6,
61     GENERIC_EXIT_NOT_RUNNING => 7
62 };
63
64 my $scriptpath = "@SCRIPT_DIR@";
65 my $basepath    =       "@BASE_DIR@";
66 my $confpath    =       "@CONFIG_DIR@";
67 my $binpath     =       "@BINARY_DIR@";
68 my $runpath     =       "@BASE_DIR@";
69 my $runtimedir = "@RUNTIME_DIR@";
70 my $valgrindlogpath     =       "$basepath/valgrindlogs";
71 my $executable  =       "inspircd";
72 my $version     =       "@VERSION_FULL@";
73 my $uid = "@UID@";
74
75 my @gdbargs = (
76         '--eval-command', 'handle SIGPIPE pass nostop noprint',
77         '--eval-command', 'handle SIGHUP pass nostop noprint',
78         '--eval-command', 'run',
79         '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
80 );
81
82 sub expand_fragment($$) {
83         my ($base, $fragment) = @_;
84         if ($fragment =~ /^\//) {
85                 return $fragment;
86         } else {
87                 return "$base/$fragment";
88         }
89 }
90
91 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
92         if ($uid !~ /^\d+$/) {
93                 # Named UID, look it up
94                 $uid = getpwnam $uid;
95         }
96         if (!$uid) {
97                 die "Cannot find a valid UID to change to";
98         }
99         # drop root if we were configured with an ircd UID
100         $< = $uid;
101         $> = $uid;
102         if ($< == 0 || $> == 0) {
103                 die "Could not drop root: $!";
104         }
105 }
106
107 our($pid,$pidfile);
108 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
109 my $arg = shift(@ARGV);
110 my $conf;
111 for my $a (@ARGV)
112 {
113         if ($a =~ m/^--config=(.*)$/)
114         {
115                 $conf = $1;
116                 last;
117         }
118 }
119 if (!defined $conf) {
120         $conf = expand_fragment $confpath, "inspircd.conf";
121         push @ARGV, '--config='.$conf;
122 }
123
124 getpidfile($conf);
125
126 # System for naming script command subs:
127 # cmd_<name> - Normal command for use by users.
128 # dev_<name> - Developer commands.
129 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
130 # Ideally command subs shouldn't return.
131
132 my $subname = $arg;
133 $subname =~ s/-/_/g;
134 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
135 if (!defined($sub))
136 {
137         print STDERR "Invalid command or none given.\n";
138         cmd_help();
139         exit GENERIC_EXIT_UNIMPLEMENTED;
140 }
141 else
142 {
143         exit $sub->(@ARGV); # Error code passed through return value
144 }
145
146 sub cmd_help()
147 {
148         my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
149         my @cmds = grep /^cmd_/, @subs;
150         my @devs = grep /^dev_/, @subs;
151         local $_;
152         $_ =~ s/^(cmd|dev)_// for (@cmds, @devs);
153         $_ =~ s/_/-/g for (@cmds, @devs);
154         print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
155         print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
156         exit GENERIC_EXIT_SUCCESS;
157 }
158
159 sub cmd_status()
160 {
161         if (getstatus() == 1) {
162                 my $pid = getprocessid();
163                 print "InspIRCd is running (PID: $pid)\n";
164                 exit STATUS_EXIT_SUCCESS;
165         } else {
166                 print "InspIRCd is not running. (Or PID File not found)\n";
167                 exit STATUS_EXIT_NOT_RUNNING;
168         }
169 }
170
171 sub cmd_rehash()
172 {
173         if (getstatus() == 1) {
174                 my $pid = getprocessid();
175                 kill HUP => $pid;
176                 print "InspIRCd rehashed (pid: $pid).\n";
177                 exit GENERIC_EXIT_SUCCESS;
178         } else {
179                 print "InspIRCd is not running. (Or PID File not found)\n";
180                 exit GENERIC_EXIT_NOT_RUNNING;
181         }
182 }
183
184 sub cmd_cron()
185 {
186         if (getstatus() == 0) { goto &cmd_start(@_); }
187         exit GENERIC_EXIT_UNSPECIFIED;
188 }
189
190 sub cmd_version()
191 {
192         print "InspIRCd version: $version\n";
193         exit GENERIC_EXIT_SUCCESS;
194 }
195
196 sub cmd_restart(@)
197 {
198         cmd_stop();
199         unlink($pidfile) if (-e $pidfile);
200         goto &cmd_start(@_);
201 }
202
203 sub hid_cheese_sandwich()
204 {
205         print "Creating Cheese Sandwich..\n";
206         print "Done.\n";
207         exit GENERIC_EXIT_SUCCESS;
208 }
209
210 sub cmd_start(@)
211 {
212         # Check to see its not 'running' already.
213         if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
214         # If we are still alive here.. Try starting the IRCd..
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         exec "$binpath/$executable", @_;
220         die "Failed to start IRCd: $!\n";
221 }
222
223 sub dev_debug(@)
224 {
225         # Check to see its not 'running' already.
226         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
227
228         chdir $runpath;
229         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
230         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
231
232         # Check we have gdb
233         checkgdb();
234
235         # If we are still alive here.. Try starting the IRCd..
236         exec 'gdb', @gdbargs, @_;
237         die "Failed to start GDB: $!\n";
238 }
239
240 sub dev_screendebug(@)
241 {
242         # Check to see its not 'running' already.
243         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
244
245         chdir $runpath;
246         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
247
248         #Check we have gdb
249         checkgdb();
250         checkscreen();
251
252         # If we are still alive here.. Try starting the IRCd..
253         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
254         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
255         exec qw(screen -m -d gdb), @gdbargs, @_;
256         die "Failed to start screen: $!\n";
257 }
258
259 sub dev_valdebug(@)
260 {
261         # Check to see its not 'running' already.
262         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
263
264         chdir $runpath;
265         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
266         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
267
268         # Check we have valgrind and gdb
269         checkvalgrind();
270         checkgdb();
271
272         # If we are still alive here.. Try starting the IRCd..
273         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
274         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
275         exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
276         die "Failed to start valgrind: $!\n";
277 }
278
279 sub dev_valdebug_unattended(@)
280 {
281         # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
282         # Check to see its not 'running' already.
283         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
284
285         chdir $runpath;
286         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
287         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
288
289         # Check we have valgrind and gdb
290         checkvalgrind();
291         checkgdb();
292
293         # If we are still alive here.. Try starting the IRCd..
294         #
295         # 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!
296         # Redirect stdout to /dev/null if you're worried about the security.
297         #
298         my $pid = fork;
299         if ($pid == 0) {
300                 POSIX::setsid();
301                 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
302                 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
303                 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
304                 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
305                 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
306                 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
307         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
308         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
309                 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
310                         "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
311                         qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
312                         "$binpath/$executable", qw(--nofork --debug --nolog), @_;
313                 die "Can't execute valgrind: $!\n";
314         }
315 }
316
317 sub dev_screenvaldebug(@)
318 {
319         # Check to see its not 'running' already.
320         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
321
322         chdir $runpath;
323         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
324         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
325
326         #Check we have gdb
327         checkvalgrind();
328         checkgdb();
329         checkscreen();
330
331         # If we are still alive here.. Try starting the IRCd..
332         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
333         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
334         exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
335         die "Failed to start screen: $!\n";
336 }
337
338 sub cmd_stop()
339 {
340         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
341         # Get to here, we have something to kill.
342         my $pid = getprocessid();
343         print "Stopping InspIRCd (pid: $pid)...\n";
344         my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
345         kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
346         for (1..$maxwait) {
347                 sleep 1;
348                 if (getstatus() == 0) {
349                         print "InspIRCd Stopped.\n";
350                         return GENERIC_EXIT_SUCCESS;
351                 }
352         }
353         print "InspIRCd not dying quietly -- forcing kill\n";
354         kill KILL => $pid;
355         return GENERIC_EXIT_SUCCESS;
356 }
357
358 ###
359 # Generic Helper Functions.
360 ###
361
362 my %filesparsed;
363
364 sub getpidfile
365 {
366         my ($file) = @_;
367         # Before we start, do we have a PID already? (Should never occur)
368         if ($pid ne "") {
369                 return;
370         }
371
372         # Expand any relative paths.
373         $file = expand_fragment $confpath, $file;
374
375         # Have we checked this file before?
376         return if $filesparsed{$file};
377         $filesparsed{$file} = 1;
378
379         # Open the File..
380         open INFILE, '<', $file or return;
381         # Grab entire file contents..
382         my(@lines) = <INFILE>;
383         # Close the file
384         close INFILE;
385
386         # remove trailing spaces
387         chomp(@lines);
388         for my $i (@lines) {
389                 # clean it up
390                 $i =~ s/[^=]+=\s(.*)/\1/;
391                 # Does this file have a pid?
392                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
393                 {
394                         # Set the PID file and return.
395                         $pidfile = expand_fragment $runtimedir, $1;
396                         return;
397                 }
398         }
399
400
401         # If we get here, NO PID FILE! -- Check for includes
402         for my $i (@lines) {
403                 $i =~ s/[^=]+=\s(.*)/\1/;
404                 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
405                 {
406                         # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
407                         getpidfile($1);
408                         # Was a PID found?
409                         if ($pidfile ne "") {
410                                 # Yes, Return.
411                                 return;
412                         }
413                 }
414         }
415
416         # End of includes / No includes found. Using default.
417         $pidfile = $runtimedir . "/inspircd.pid";
418 }
419
420 sub getstatus {
421         my $pid = getprocessid();
422         return 0 if $pid == 0;
423         return kill 0, $pid;
424 }
425
426
427 sub getprocessid {
428         my $pid = 0;
429         open PIDFILE, '<', $pidfile or return 0;
430         while(<PIDFILE>)
431         {
432                 /^(\d+)$/ and $pid = $1;
433         }
434         close PIDFILE;
435         return $pid;
436 }
437
438 sub checkvalgrind
439 {
440         unless(`valgrind --version`)
441         {
442                 print "Couldn't start valgrind: $!\n";
443                 exit GENERIC_EXIT_UNSPECIFIED;
444         }
445 }
446
447 sub checkgdb
448 {
449         unless(`gdb --version`)
450         {
451                 print "Couldn't start gdb: $!\n";
452                 exit GENERIC_EXIT_UNSPECIFIED;
453         }
454 }
455
456 sub checkscreen
457 {
458         unless(`screen --version`)
459         {
460                 print "Couldn't start screen: $!\n";
461                 exit GENERIC_EXIT_UNSPECIFIED;
462         }
463 }