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