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