]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - make/template/inspircd
Fix the base path being used for more than just the install prefix.
[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 $confpath    =       "@CONFIG_DIR@";
66 my $binpath     =       "@BINARY_DIR@";
67 my $runtimedir = "@RUNTIME_DIR@";
68 my $valgrindlogpath = "@LOG_DIR@/valgrind";
69 my $executable  =       "inspircd";
70 my $version     =       "@VERSION_FULL@";
71 my $uid = "@UID@";
72
73 my @gdbargs = (
74         '--eval-command', 'handle SIGPIPE pass nostop noprint',
75         '--eval-command', 'handle SIGHUP pass nostop noprint',
76         '--eval-command', 'run',
77         '--args', "$binpath/$executable", qw(--nofork --nolog --debug)
78 );
79
80 sub expand_fragment($$) {
81         my ($base, $fragment) = @_;
82         if ($fragment =~ /^\//) {
83                 return $fragment;
84         } else {
85                 return "$base/$fragment";
86         }
87 }
88
89 if (!(grep { $_ eq '--runasroot' } @ARGV) && ($< == 0 || $> == 0)) {
90         if ($uid !~ /^\d+$/) {
91                 # Named UID, look it up
92                 $uid = getpwnam $uid;
93         }
94         if (!$uid) {
95                 die "Cannot find a valid UID to change to";
96         }
97         # drop root if we were configured with an ircd UID
98         $< = $uid;
99         $> = $uid;
100         if ($< == 0 || $> == 0) {
101                 die "Could not drop root: $!";
102         }
103 }
104
105 our($pid,$pidfile);
106 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
107 my $arg = shift(@ARGV);
108 my $conf;
109 for my $a (@ARGV)
110 {
111         if ($a =~ m/^--config=(.*)$/)
112         {
113                 $conf = $1;
114                 last;
115         }
116 }
117 if (!defined $conf) {
118         $conf = expand_fragment $confpath, "inspircd.conf";
119         push @ARGV, '--config='.$conf;
120 }
121
122 getpidfile($conf);
123
124 # System for naming script command subs:
125 # cmd_<name> - Normal command for use by users.
126 # dev_<name> - Developer commands.
127 # hid_<name> - Hidden commands (ie Cheese-Sandwich)
128 # Ideally command subs shouldn't return.
129
130 my $subname = $arg;
131 $subname =~ s/-/_/g;
132 my $sub = main->can("cmd_$subname") || main->can("dev_$subname") || main->can("hid_$subname");
133 if (!defined($sub))
134 {
135         print STDERR "Invalid command or none given.\n";
136         cmd_help();
137         exit GENERIC_EXIT_UNIMPLEMENTED;
138 }
139 else
140 {
141         exit $sub->(@ARGV); # Error code passed through return value
142 }
143
144 sub cmd_help()
145 {
146         my @subs = grep { $_ =~ m/^(cmd|dev)_/ && defined(main->can($_)) } keys(%::);
147         my @cmds = grep /^cmd_/, @subs;
148         my @devs = grep /^dev_/, @subs;
149         local $_;
150         $_ =~ s/^(cmd|dev)_// for (@cmds, @devs);
151         $_ =~ s/_/-/g for (@cmds, @devs);
152         print STDERR "Usage: ./inspircd (" . join("|", @cmds) . ")\n";
153         print STDERR "Developer arguments: (" . join("|", @devs) . ")\n";
154         exit GENERIC_EXIT_SUCCESS;
155 }
156
157 sub cmd_status()
158 {
159         if (getstatus() == 1) {
160                 my $pid = getprocessid();
161                 print "InspIRCd is running (PID: $pid)\n";
162                 exit STATUS_EXIT_SUCCESS;
163         } else {
164                 print "InspIRCd is not running. (Or PID File not found)\n";
165                 exit STATUS_EXIT_NOT_RUNNING;
166         }
167 }
168
169 sub cmd_rehash()
170 {
171         if (getstatus() == 1) {
172                 my $pid = getprocessid();
173                 kill HUP => $pid;
174                 print "InspIRCd rehashed (pid: $pid).\n";
175                 exit GENERIC_EXIT_SUCCESS;
176         } else {
177                 print "InspIRCd is not running. (Or PID File not found)\n";
178                 exit GENERIC_EXIT_NOT_RUNNING;
179         }
180 }
181
182 sub cmd_cron()
183 {
184         if (getstatus() == 0) { goto &cmd_start(@_); }
185         exit GENERIC_EXIT_UNSPECIFIED;
186 }
187
188 sub cmd_version()
189 {
190         print "InspIRCd version: $version\n";
191         exit GENERIC_EXIT_SUCCESS;
192 }
193
194 sub cmd_restart(@)
195 {
196         cmd_stop();
197         unlink($pidfile) if (-e $pidfile);
198         goto &cmd_start(@_);
199 }
200
201 sub hid_cheese_sandwich()
202 {
203         print "Creating Cheese Sandwich..\n";
204         print "Done.\n";
205         exit GENERIC_EXIT_SUCCESS;
206 }
207
208 sub cmd_start(@)
209 {
210         # Check to see its not 'running' already.
211         if (getstatus() == 1) { print "InspIRCd is already running.\n"; exit GENERIC_EXIT_SUCCESS; }
212
213         # If we are still alive here.. Try starting the IRCd..
214         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
215         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
216
217         exec "$binpath/$executable", @_;
218         die "Failed to start IRCd: $!\n";
219 }
220
221 sub dev_debug(@)
222 {
223         # Check to see its not 'running' already.
224         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
225
226         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
227         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
228
229         # Check we have gdb
230         checkgdb();
231
232         # If we are still alive here.. Try starting the IRCd..
233         exec 'gdb', @gdbargs, @_;
234         die "Failed to start GDB: $!\n";
235 }
236
237 sub dev_screendebug(@)
238 {
239         # Check to see its not 'running' already.
240         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
241
242         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
243
244         #Check we have gdb
245         checkgdb();
246         checkscreen();
247
248         # If we are still alive here.. Try starting the IRCd..
249         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
250         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
251         exec qw(screen -m -d gdb), @gdbargs, @_;
252         die "Failed to start screen: $!\n";
253 }
254
255 sub dev_valdebug(@)
256 {
257         # Check to see its not 'running' already.
258         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
259
260         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
261         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
262
263         # Check we have valgrind and gdb
264         checkvalgrind();
265         checkgdb();
266
267         # If we are still alive here.. Try starting the IRCd..
268         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
269         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
270         exec qw(valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
271         die "Failed to start valgrind: $!\n";
272 }
273
274 sub dev_valdebug_unattended(@)
275 {
276         # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
277         # Check to see its not 'running' already.
278         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
279
280         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
281         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
282
283         # Check we have valgrind and gdb
284         checkvalgrind();
285         checkgdb();
286
287         # If we are still alive here.. Try starting the IRCd..
288         #
289         # 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!
290         # Redirect stdout to /dev/null if you're worried about the security.
291         #
292         my $pid = fork;
293         if ($pid == 0) {
294                 POSIX::setsid();
295                 -d $valgrindlogpath or mkdir $valgrindlogpath or die "Cannot create $valgrindlogpath: $!\n";
296                 -e "$binpath/valgrind.sup" or do { open my $f, '>', "$binpath/valgrind.sup"; };
297                 my $suffix = strftime("%Y%m%d-%H%M%S", localtime(time)) . ".$$";
298                 open STDIN, '<', '/dev/null' or die "Can't redirect STDIN to /dev/null: $!\n";
299                 sysopen STDOUT, "$valgrindlogpath/out.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0600 or die "Can't open $valgrindlogpath/out.$suffix: $!\n";
300                 sysopen STDERR, "$valgrindlogpath/valdebug.$suffix", O_WRONLY | O_CREAT | O_NOCTTY | O_APPEND, 0666 or die "Can't open $valgrindlogpath/valdebug.$suffix: $!\n";
301         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
302         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
303                 exec qw(valgrind -v --tool=memcheck --leak-check=full --show-reachable=yes --num-callers=30 --track-fds=yes),
304                         "--suppressions=$binpath/valgrind.sup", qw(--gen-suppressions=all),
305                         qw(--leak-resolution=med --time-stamp=yes --log-fd=2 --),
306                         "$binpath/$executable", qw(--nofork --debug --nolog), @_;
307                 die "Can't execute valgrind: $!\n";
308         }
309 }
310
311 sub dev_screenvaldebug(@)
312 {
313         # Check to see its not 'running' already.
314         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
315
316         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
317         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
318
319         #Check we have gdb
320         checkvalgrind();
321         checkgdb();
322         checkscreen();
323
324         # If we are still alive here.. Try starting the IRCd..
325         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
326         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
327         exec qw(screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=30), "$binpath/$executable", qw(--nofork --debug --nolog), @_;
328         die "Failed to start screen: $!\n";
329 }
330
331 sub cmd_stop()
332 {
333         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return GENERIC_EXIT_SUCCESS; }
334         # Get to here, we have something to kill.
335         my $pid = getprocessid();
336         print "Stopping InspIRCd (pid: $pid)...\n";
337         my $maxwait = (`ps -o command $pid 2>/dev/null` =~ /valgrind/i) ? 90 : 15;
338         kill TERM => $pid or die "Cannot terminate IRCd: $!\n";
339         for (1..$maxwait) {
340                 sleep 1;
341                 if (getstatus() == 0) {
342                         print "InspIRCd Stopped.\n";
343                         return GENERIC_EXIT_SUCCESS;
344                 }
345         }
346         print "InspIRCd not dying quietly -- forcing kill\n";
347         kill KILL => $pid;
348         return GENERIC_EXIT_SUCCESS;
349 }
350
351 ###
352 # Generic Helper Functions.
353 ###
354
355 my %filesparsed;
356
357 sub getpidfile
358 {
359         my ($file) = @_;
360         # Before we start, do we have a PID already? (Should never occur)
361         if ($pid ne "") {
362                 return;
363         }
364
365         # Expand any relative paths.
366         $file = expand_fragment $confpath, $file;
367
368         # Have we checked this file before?
369         return if $filesparsed{$file};
370         $filesparsed{$file} = 1;
371
372         # Open the File..
373         open INFILE, '<', $file or return;
374         # Grab entire file contents..
375         my(@lines) = <INFILE>;
376         # Close the file
377         close INFILE;
378
379         # remove trailing spaces
380         chomp(@lines);
381         for my $i (@lines) {
382                 # clean it up
383                 $i =~ s/[^=]+=\s(.*)/\1/;
384                 # Does this file have a pid?
385                 if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
386                 {
387                         # Set the PID file and return.
388                         $pidfile = expand_fragment $runtimedir, $1;
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 = $runtimedir . "/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 }