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