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