]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
b0933f07d1e39424b80c3b91224214809cb809f0
[user/henk/code/inspircd.git] / .inspircd.inc
1 #!/usr/bin/perl
2 #       +------------------------------------+
3 #       | Inspire Internet Relay Chat Daemon |
4 #       +------------------------------------+
5 #
6 #      (C) 2002-2008 InspIRCd Development Team
7 #   http://www.inspircd.org/wiki/index.php/Credits
8 #
9 # Written by Craig Edwards, Craig McLure, and others.
10 # This program is free but copyrighted software; see
11 #            the file COPYING for details.
12 #
13 # ---------------------------------------------------
14 use POSIX;
15
16 my $basepath    =       "@BASE_DIR@";
17 my $confpath    =       "@CONFIG_DIR@/";
18 my $binpath     =       "@BINARY_DIR@";
19 my $libpath     =       "@LIBRARY_DIR@";
20 my $valgrindlogpath     =       "$basepath/valgrindlogs";
21 my $executable  =       "@EXECUTABLE@";
22 my $version     =       "@VERSION@";
23 my @filesparsed;
24 my @filechecked;
25
26 # Lets see what they want to do.. Set the variable (Cause i'm a lazy coder)
27 my $arg = $ARGV[0];
28 getpidfile($confpath."inspircd.conf");
29
30 if ($arg eq "start") { start(); exit(); }
31 if ($arg eq "debug") { debug(); exit(); }
32 if ($arg eq "screendebug") { screendebug(); exit() }
33 if ($arg eq "valdebug") { valdebug(); exit(); }
34 if ($arg eq "valdebug-unattended") { valdebugunattended(); exit(); }
35 if ($arg eq "screenvaldebug") { screenvaldebug(); exit(); }
36 if ($arg eq "stop") { stop(); exit(); }
37 if ($arg eq "status") {
38         if (getstatus() == 1) {
39                 my $pid = getprocessid();
40                 print "InspIRCd is running (PID: $pid)\n";
41                 exit();
42         } else {
43                 print "InspIRCd is not running. (Or PID File not found)\n";
44                 exit();
45         }
46 }
47 if ($arg eq "rehash") {
48         if (getstatus() == 1) {
49                 my $pid = getprocessid();
50                 system("kill -HUP $pid >/dev/null 2>&1");
51                 print "InspIRCd rehashed (pid: $pid).\n";
52                 exit();
53         } else {
54                 print "InspIRCd is not running. (Or PID File not found)\n";
55                 exit();
56         }
57 }
58
59 if ($arg eq "cron") {
60         if (getstatus() == 0) { start(); }
61         exit();
62 }
63
64 if ($arg eq "version") {
65         print "InspIRCd version: $version\n";
66         exit();
67 }
68
69 if ($arg eq "restart") {
70         stop();
71         unlink($pidfile) if (-e $pidfile);
72         start();
73         # kthxbye();
74         exit();
75 }
76
77 if ($arg eq "checkconf") {
78         checkconf();
79         exit();
80 }
81
82 if ($arg eq "Cheese-Sandwich") {
83         print "Creating Cheese Sandwich..\n";
84         print "Done.\n";
85         exit();
86 }
87
88 ###
89 # If we get here.. bad / no parameters.
90 ###
91 print "Invalid Argument: $arg\n";
92 print "Usage: inspircd (start|stop|restart|rehash|status|cron|checkconf|version)\n";
93 print "Developer arguments: (debug|screendebug|valdebug|valdebug-unattended|screenvaldebug)\n";
94 exit();
95
96 ###
97 # Generic Helper Functions.
98 ###
99
100 sub start {
101         # Check to see its not 'running' already.
102         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
103         # If we are still alive here.. Try starting the IRCd..
104         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
105         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
106
107         system("$binpath/$executable");
108         return 1;
109 }
110
111 sub debug {
112         # Check to see its not 'running' already.
113         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
114
115         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
116         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
117
118         # Check we have gdb
119         checkgdb();
120
121         # If we are still alive here.. Try starting the IRCd..
122         system("gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug");
123 }
124
125 sub screendebug
126 {
127         # Check to see its not 'running' already.
128         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
129
130         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
131
132         #Check we have gdb
133         checkgdb();
134         checkscreen();
135
136         # If we are still alive here.. Try starting the IRCd..
137         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
138         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
139         system("screen -m -d gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
140 }
141
142 sub valdebug
143 {
144         # Check to see its not 'running' already.
145         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
146
147         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
148         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
149
150         # Check we have valgrind and gdb
151         checkvalgrind();
152         checkgdb();
153
154         # If we are still alive here.. Try starting the IRCd..
155         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
156         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
157         system("valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
158 }
159
160 sub valdebugunattended
161 {
162         # NOTE: To make sure valgrind generates coredumps, set soft core limit in /etc/security/limits.conf to unlimited
163         # Check to see its not 'running' already.
164         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
165  
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 valgrind and gdb
170         checkvalgrind();
171         checkgdb();
172  
173         # If we are still alive here.. Try starting the IRCd..
174         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
175         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
176         #
177         # 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!
178         # Redirect stdout to /dev/null if you're worried about the security.
179         #
180         my $pid = fork;
181         if ($pid == 0) {
182                 POSIX::setsid();
183                 umask 066;
184                 open STDIN, '/dev/null' or die $!;
185                 open STDOUT, '>', "$valgrindlogpath/out.$$" or die $!;
186                 open STDERR, '>', "$valgrindlogpath/err.$$" or die $!;
187                 umask 022;
188                 exec "valgrind -v --tool=memcheck --leak-check=yes --num-callers=10 --time-stamp=yes --log-file=$valgrindlogpath/valdebug.\%p $binpath/$executable -nofork -debug -nolog";
189                 exit 1;
190         }
191         # Sleep 5 seconds to give valgrind time to initialize before chmodding the logdir for apache access.
192         sleep(5);
193         system("chmod 644 $valgrindlogpath/valdebug.*");
194 }
195
196 sub screenvaldebug
197 {
198         # Check to see its not 'running' already.
199         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
200
201         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
202         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
203
204         #Check we have gdb
205         checkvalgrind();
206         checkgdb();
207         checkscreen();
208
209         # If we are still alive here.. Try starting the IRCd..
210         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
211         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
212         system("screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
213 }
214
215 sub stop {
216         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
217         # Get to here, we have something to kill.
218         my $pid = getprocessid();
219         print "Stopping InspIRCd (pid: $pid)...\n";
220         system("kill -TERM $pid >/dev/null 2>&1");
221         # Give it twenty seconds to exit, otherwise valgrind can't write out all output before being killed -9
222         # TODO make this a variable number based on if it's valgrind or normal start.
223         sleep(20);
224         if (getstatus() == 1)
225         {
226                 print "InspIRCd not dying quietly -- forcing kill\n";
227                 system("kill -9 $pid >/dev/null 2>&1");
228         }
229         print "InspIRCd Stopped.\n";
230 }
231
232 # GetPidfile Version 2 - Now With Include Support..
233 # I beg for months for include support in insp, then..
234 # when it is added, it comes around and BITES ME IN THE ASS,
235 # because i then have to code support into this script.. Evil.
236
237 # Craig got bitten in the ass again --
238 # in 1.1 beta the include file is manditory, therefore
239 # if we cant find it, default to %conf%/inspircd.pid.
240 # Note, this also contains a fix for when the pid file is
241 # defined, but defined in a comment (line starts with #)
242 # -- Brain
243
244 sub getpidfile {
245   my ($file) = @_;
246   # Before we start, do we have a PID already? (Should never occur)
247   if ($pid ne "") {
248     return;
249   }
250   # Are We using a relative path?
251   if ($file !~ /^\//) {
252     # Convert it to a full path..
253     $file = $confpath . $file;
254   }
255
256   # Have we checked this file before?
257   for (my $i = 0; $i < $filesparsed; $i++) {
258     if ($filesparsed[$i] eq $file) {
259       # Already Parsed, Possible recursive loop..
260       return;
261     }
262   }
263
264   # If we get here, Mark as 'Read'
265   $filesparsed[$filesparsed] = $file;
266
267   # Open the File..
268   open INFILE, "< $file" or die "Unable to open file $file\n";
269   # Grab entire file contents..
270   my(@lines) = <INFILE>;
271   # Close the file
272   close INFILE;
273
274   # remove trailing spaces
275   chomp(@lines);
276   foreach $i (@lines) {
277     # clean it up
278     $i =~ s/[^=]+=\s(.*)/\1/;
279     # Does this file have a pid?
280     if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
281     {
282       # Set the PID file and return.
283       $pidfile = $1;
284       return;
285     }
286   }
287
288   # If we get here, NO PID FILE! -- Check for includes
289   foreach $i (@lines) {
290     $i =~ s/[^=]+=\s(.*)/\1/;
291     if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
292     {
293       # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
294       getpidfile($1);
295       # Was a PID found?
296       if ($pidfile ne "") {
297         # Yes, Return.
298         return;
299       }
300     }
301   }
302
303   # End of includes / No includes found. Using default.
304   $pidfile = $confpath . "inspircd.pid";
305 }
306
307 sub getstatus {
308         my $pid = getprocessid();
309         if ($pid == 0) { return 0; }
310         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
311         if ($status == 0) { return 1; }
312         else { return 0; }
313 }
314
315
316 sub getprocessid {
317         my $pid;
318         open PIDFILE, "< $pidfile" or return 0;
319         while($i = <PIDFILE>)
320         {
321                 $pid = $i;
322         }
323         close PIDFILE;
324         return $pid;
325 }
326
327 sub checkvalgrind
328 {
329         unless(`valgrind --version`)
330         {
331                 print "Couldn't start valgrind: $!\n";
332                 exit;
333         }
334 }
335
336 sub checkgdb
337 {
338         unless(`gdb --version`)
339         {
340                 print "Couldn't start gdb: $!\n";
341                 exit;
342         }
343 }
344
345 sub checkscreen
346 {
347         unless(`screen --version`)
348         {
349                 print "Couldn't start screen: $!\n";
350                 exit;
351         }
352 }
353
354 sub checkxmllint
355 {
356         open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
357 }
358
359 sub checkconf
360 {
361         checkxmllint();
362         validateconf($confpath."inspircd.conf");
363         print "Config check complete\n";
364 }
365
366 sub validateconf
367 {
368         my ($file) = @_;
369
370         # Are We using a relative path?
371         if ($file !~ /^\//) {
372                 # Convert it to a full path..
373                 $file = $confpath . $file;
374         }
375
376         # Have we checked this file before?
377         for (my $i = 0; $i < $filechecked; $i++) {
378                 if ($filechecked[$i] eq $file) {
379                         # Already Parsed, Possible recursive loop..
380                         return;
381                 }
382         }
383
384         # If we get here, Mark as 'Read'
385         $filechecked[$filechecked] = $file;
386
387         # Open the File..
388         open INFILE, "< $file" or die "Unable to open file $file\n";
389         # Grab entire file contents..
390         my(@lines) = <INFILE>;
391         # Close the file
392         close INFILE;
393
394         # remove trailing spaces
395         chomp(@lines);
396
397         my @newlines = ();
398         my @blanks = ();
399         my $conline;
400
401         push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
402 #       push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
403         push @newlines, "<config>";
404
405         foreach $i (@lines)
406         {
407                 # remove trailing newlines
408                 chomp($i);
409
410                 # convert tabs to spaces
411                 $i =~ s/\t/ /g;
412
413                 # remove leading spaces
414                 $i =~ s/^ *//;
415
416                 # remove comments
417                 $i =~ s/^#.*//;
418
419                 # remove trailing #s
420                 $i =~ s/(.*)#$/\1/;
421
422                 # remove trailing comments
423                 my $line = "";
424                 my $quote = 0;
425                 for (my $j = 0; $j < length($i); $j++)
426                 {
427                         if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
428                         $line .= substr($i,$j, 1);
429                 }
430                 $i = $line;
431
432                 # remove trailing spaces
433                 $i =~ s/ *$//;
434
435                 # setup incf for include check and clean it up, since this breaks parsing use local var
436                 my $incf = $i;
437                 $incf =~ s/[^=]+=\s(.*)/\1/;
438
439                 # include file?
440                 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
441                 {
442                         # yes, process it
443                         validateconf($1);
444                 }
445
446                 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
447                 {
448                         push @newlines, $conline;
449                         push @newlines, @blanks;
450                         $conline = $i;
451                 }
452
453                 if ($i =~ /^<.*>$/)
454                 {
455                         $i =~ s/(.*)>$/\1 \/>/;
456                         push @newlines, $i;
457                 }
458                 elsif ($i =~ /.*>$/)
459                 {
460                         $conline .= " $i";
461                         $conline =~ s/(.*)>$/\1 \/>/;
462                         push @blanks, "";
463                         push @newlines, $conline;
464                         push @newlines, @blanks;
465                         $conline = "";
466                         undef @blanks;
467                 }
468                 elsif ($i =~ /^<.*/)
469                 {
470                         $conline = $i;
471                 }
472                 elsif ($conline =~ /^<.*/ && $i)
473                 {
474                         $conline .= " $i";
475                         push @blanks, "";
476                 }
477                 else
478                 {
479                         if ($conline)
480                         {
481                                 push @blanks, $i;
482                         }
483                         else
484                         {
485                                 push @newlines, $i;
486                         }
487                 }
488         }
489         if ($conline)
490         {
491                 push @newlines, $conline;
492                 push @newlines, @blanks;
493         }
494
495         push @newlines, "</config>";
496
497         my $tmpfile;
498         do
499         {
500                 $tmpfile = tmpnam();
501         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
502
503         foreach $n (@newlines)
504         {
505                 print TF "$n\n";
506         }
507         close TF;
508
509         my @result = `xmllint -noout $tmpfile 2>&1`;
510         chomp(@result);
511
512         my $skip = 0;
513         foreach $n (@result)
514         {
515                 if ($skip)
516                 {
517                         $skip = 0;
518                         next;
519                 }
520                 $n =~ s/$tmpfile\:\d*\: *//g;
521                 if ($n =~ /.*config>.*/)
522                 {
523                         $n = "";
524                         $skip = 1;
525                 }
526
527                 if ($n && !$skip)
528                 {
529                         if ($n =~ /line \d*/)
530                         {
531                                 my $lineno = $n;
532                                 $lineno =~ s/.*line (\d*).*/\1/;
533                                 $lineno = $lineno-2;
534                                 $n =~ s/line (\d*)/line $lineno/;
535                         }
536                         print "$file : $n\n";
537                 }
538         }
539         unlink($tmpfile);
540 }