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