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