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