]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/template/inspircd
Replace the SERVER stub command with something actually useful.
[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 $datadir     =       "@DATA_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)_// foreach (@cmds, @devs);
153         $_ =~ s/_/-/g foreach (@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                 system("kill -HUP $pid >/dev/null 2>&1");
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 # GetPidfile Version 2 - Now With Include Support..
363 # I beg for months for include support in insp, then..
364 # when it is added, it comes around and BITES ME IN THE ASS,
365 # because i then have to code support into this script.. Evil.
366
367 # Craig got bitten in the ass again --
368 # in 1.1 beta the include file is manditory, therefore
369 # if we cant find it, default to %conf%/inspircd.pid.
370 # Note, this also contains a fix for when the pid file is
371 # defined, but defined in a comment (line starts with #)
372 # -- Brain
373
374 my %filesparsed;
375
376 sub getpidfile
377 {
378         my ($file) = @_;
379         # Before we start, do we have a PID already? (Should never occur)
380         if ($pid ne "") {
381                 return;
382         }
383
384         # Expand any relative paths.
385         $file = expand_fragment $confpath, $file;
386
387         # Have we checked this file before?
388         return if $filesparsed{$file};
389         $filesparsed{$file} = 1;
390
391         # Open the File..
392         open INFILE, '<', $file or return;
393         # Grab entire file contents..
394         my(@lines) = <INFILE>;
395         # Close the file
396         close INFILE;
397
398         # remove trailing spaces
399         chomp(@lines);
400         for my $i (@lines) {
401                 # clean it up
402                 $i =~ s/[^=]+=\s(.*)/\1/;
403                 # Does this file have a pid?
404                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
405                 {
406                         # Set the PID file and return.
407                         $pidfile = expand_fragment $datadir, $1;
408                         return;
409                 }
410         }
411
412
413         # If we get here, NO PID FILE! -- Check for includes
414         for my $i (@lines) {
415                 $i =~ s/[^=]+=\s(.*)/\1/;
416                 if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
417                 {
418                         # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
419                         getpidfile($1);
420                         # Was a PID found?
421                         if ($pidfile ne "") {
422                                 # Yes, Return.
423                                 return;
424                         }
425                 }
426         }
427
428         # End of includes / No includes found. Using default.
429         $pidfile = $datadir . "/inspircd.pid";
430 }
431
432 sub getstatus {
433         my $pid = getprocessid();
434         return 0 if $pid == 0;
435         return kill 0, $pid;
436 }
437
438
439 sub getprocessid {
440         my $pid = 0;
441         open PIDFILE, '<', $pidfile or return 0;
442         while(<PIDFILE>)
443         {
444                 /^(\d+)$/ and $pid = $1;
445         }
446         close PIDFILE;
447         return $pid;
448 }
449
450 sub checkvalgrind
451 {
452         unless(`valgrind --version`)
453         {
454                 print "Couldn't start valgrind: $!\n";
455                 exit GENERIC_EXIT_UNSPECIFIED;
456         }
457 }
458
459 sub checkgdb
460 {
461         unless(`gdb --version`)
462         {
463                 print "Couldn't start gdb: $!\n";
464                 exit GENERIC_EXIT_UNSPECIFIED;
465         }
466 }
467
468 sub checkscreen
469 {
470         unless(`screen --version`)
471         {
472                 print "Couldn't start screen: $!\n";
473                 exit GENERIC_EXIT_UNSPECIFIED;
474         }
475 }