]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
Revert "Remove user no longer being watched, not the quitting one. Fix from Szymek...
[user/henk/code/inspircd.git] / .inspircd.inc
1 #!/usr/bin/perl
2 #       +------------------------------------+
3 #       | Inspire Internet Relay Chat Daemon |
4 #       +------------------------------------+
5 #
6 #  InspIRCd: (C) 2002-2008 InspIRCd Development Team
7 # See: http://www.inspircd.org/wiki/index.php/Credits
8 #
9 # This program is free but copyrighted software; see
10 #          the file COPYING for details.
11 #
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 022;
184                 open STDERR, '>', "$valgrindlogpath/valdebug.$$" or die $!;
185                 umask 066;
186                 open STDIN, '/dev/null' or die $!;
187                 open STDOUT, '>', "$valgrindlogpath/out.$$" or die $!;
188                 exec "valgrind -v --tool=memcheck --leak-check=yes --num-callers=10 --time-stamp=yes --log-fd=2 $binpath/$executable -nofork -debug -nolog";
189                 exit 1;
190         }
191 }
192
193 sub screenvaldebug
194 {
195         # Check to see its not 'running' already.
196         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
197
198         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
199         print "$binpath/$executable is not executable\n" and return 0 unless(-f "$binpath/$executable" && -x "$binpath/$executable");
200
201         #Check we have gdb
202         checkvalgrind();
203         checkgdb();
204         checkscreen();
205
206         # If we are still alive here.. Try starting the IRCd..
207         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
208         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
209         system("screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
210 }
211
212 sub stop {
213         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
214         # Get to here, we have something to kill.
215         my $pid = getprocessid();
216         print "Stopping InspIRCd (pid: $pid)...\n";
217         my $maxwait = (`ps -o command $pid` =~ /valgrind/i) ? 30 : 5;
218         kill TERM => $pid;
219         for (1..$maxwait) {
220                 sleep 1;
221                 if (getstatus() == 1) {
222                         print "InspIRCd Stopped.\n";
223                         return;
224                 }
225         }
226         print "InspIRCd not dying quietly -- forcing kill\n";
227         kill KILL => $pid;
228 }
229
230 # GetPidfile Version 2 - Now With Include Support..
231 # I beg for months for include support in insp, then..
232 # when it is added, it comes around and BITES ME IN THE ASS,
233 # because i then have to code support into this script.. Evil.
234
235 # Craig got bitten in the ass again --
236 # in 1.1 beta the include file is manditory, therefore
237 # if we cant find it, default to %conf%/inspircd.pid.
238 # Note, this also contains a fix for when the pid file is
239 # defined, but defined in a comment (line starts with #)
240 # -- Brain
241
242 sub getpidfile {
243   my ($file) = @_;
244   # Before we start, do we have a PID already? (Should never occur)
245   if ($pid ne "") {
246     return;
247   }
248   # Are We using a relative path?
249   if ($file !~ /^\//) {
250     # Convert it to a full path..
251     $file = $confpath . $file;
252   }
253
254   # Have we checked this file before?
255   for (my $i = 0; $i < $filesparsed; $i++) {
256     if ($filesparsed[$i] eq $file) {
257       # Already Parsed, Possible recursive loop..
258       return;
259     }
260   }
261
262   # If we get here, Mark as 'Read'
263   $filesparsed[$filesparsed] = $file;
264
265   # Open the File..
266   open INFILE, "< $file" or die "Unable to open file $file\n";
267   # Grab entire file contents..
268   my(@lines) = <INFILE>;
269   # Close the file
270   close INFILE;
271
272   # remove trailing spaces
273   chomp(@lines);
274   foreach $i (@lines) {
275     # clean it up
276     $i =~ s/[^=]+=\s(.*)/\1/;
277     # Does this file have a pid?
278     if (($i =~ /<pid file=\"(\S+)\">/i) && ($i !~ /^#/))
279     {
280       # Set the PID file and return.
281       $pidfile = $1;
282       return;
283     }
284   }
285
286   # If we get here, NO PID FILE! -- Check for includes
287   foreach $i (@lines) {
288     $i =~ s/[^=]+=\s(.*)/\1/;
289     if (($i =~ s/\<include file=\"(.+?)\"\>//i) && ($i !~ /^#/))
290     {
291       # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
292       getpidfile($1);
293       # Was a PID found?
294       if ($pidfile ne "") {
295         # Yes, Return.
296         return;
297       }
298     }
299   }
300
301   # End of includes / No includes found. Using default.
302   $pidfile = $confpath . "inspircd.pid";
303 }
304
305 sub getstatus {
306         my $pid = getprocessid();
307         return 0 if $pid == 0;
308         return kill 0, $pid;
309 }
310
311
312 sub getprocessid {
313         my $pid;
314         open PIDFILE, "< $pidfile" or return 0;
315         while($i = <PIDFILE>)
316         {
317                 $pid = $i;
318         }
319         close PIDFILE;
320         return $pid;
321 }
322
323 sub checkvalgrind
324 {
325         unless(`valgrind --version`)
326         {
327                 print "Couldn't start valgrind: $!\n";
328                 exit;
329         }
330 }
331
332 sub checkgdb
333 {
334         unless(`gdb --version`)
335         {
336                 print "Couldn't start gdb: $!\n";
337                 exit;
338         }
339 }
340
341 sub checkscreen
342 {
343         unless(`screen --version`)
344         {
345                 print "Couldn't start screen: $!\n";
346                 exit;
347         }
348 }
349
350 sub checkxmllint
351 {
352         open(FH, "xmllint|") or die "Couldn't start xmllint: $!\n";
353 }
354
355 sub checkconf
356 {
357         checkxmllint();
358         validateconf($confpath."inspircd.conf");
359         print "Config check complete\n";
360 }
361
362 sub validateconf
363 {
364         my ($file) = @_;
365
366         # Are We using a relative path?
367         if ($file !~ /^\//) {
368                 # Convert it to a full path..
369                 $file = $confpath . $file;
370         }
371
372         # Have we checked this file before?
373         for (my $i = 0; $i < $filechecked; $i++) {
374                 if ($filechecked[$i] eq $file) {
375                         # Already Parsed, Possible recursive loop..
376                         return;
377                 }
378         }
379
380         # If we get here, Mark as 'Read'
381         $filechecked[$filechecked] = $file;
382
383         # Open the File..
384         open INFILE, "< $file" or die "Unable to open file $file\n";
385         # Grab entire file contents..
386         my(@lines) = <INFILE>;
387         # Close the file
388         close INFILE;
389
390         # remove trailing spaces
391         chomp(@lines);
392
393         my @newlines = ();
394         my @blanks = ();
395         my $conline;
396
397         push @newlines, "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>";
398 #       push @newlines, "<!DOCTYPE config SYSTEM \"".$confpath."inspircd.dtd\">";
399         push @newlines, "<config>";
400
401         foreach $i (@lines)
402         {
403                 # remove trailing newlines
404                 chomp($i);
405
406                 # convert tabs to spaces
407                 $i =~ s/\t/ /g;
408
409                 # remove leading spaces
410                 $i =~ s/^ *//;
411
412                 # remove comments
413                 $i =~ s/^#.*//;
414
415                 # remove trailing #s
416                 $i =~ s/(.*)#$/\1/;
417
418                 # remove trailing comments
419                 my $line = "";
420                 my $quote = 0;
421                 for (my $j = 0; $j < length($i); $j++)
422                 {
423                         if (substr($i,$j, 1) eq '"') { $quote = ($quote) ? 0 : 1; } elsif (substr($i,$j, 1) eq "#" && !$quote) { last; }
424                         $line .= substr($i,$j, 1);
425                 }
426                 $i = $line;
427
428                 # remove trailing spaces
429                 $i =~ s/ *$//;
430
431                 # setup incf for include check and clean it up, since this breaks parsing use local var
432                 my $incf = $i;
433                 $incf =~ s/[^=]+=\s(.*)/\1/;
434
435                 # include file?
436                 if (($incf =~ s/\<include file=\"(.+?)\"\>//i) && ($incf !~ /^#/))
437                 {
438                         # yes, process it
439                         validateconf($1);
440                 }
441
442                 if ($i =~ /^<.*/ && $conline =~ /^<.*/)
443                 {
444                         push @newlines, $conline;
445                         push @newlines, @blanks;
446                         $conline = $i;
447                 }
448
449                 if ($i =~ /^<.*>$/)
450                 {
451                         $i =~ s/(.*)>$/\1 \/>/;
452                         push @newlines, $i;
453                 }
454                 elsif ($i =~ /.*>$/)
455                 {
456                         $conline .= " $i";
457                         $conline =~ s/(.*)>$/\1 \/>/;
458                         push @blanks, "";
459                         push @newlines, $conline;
460                         push @newlines, @blanks;
461                         $conline = "";
462                         undef @blanks;
463                 }
464                 elsif ($i =~ /^<.*/)
465                 {
466                         $conline = $i;
467                 }
468                 elsif ($conline =~ /^<.*/ && $i)
469                 {
470                         $conline .= " $i";
471                         push @blanks, "";
472                 }
473                 else
474                 {
475                         if ($conline)
476                         {
477                                 push @blanks, $i;
478                         }
479                         else
480                         {
481                                 push @newlines, $i;
482                         }
483                 }
484         }
485         if ($conline)
486         {
487                 push @newlines, $conline;
488                 push @newlines, @blanks;
489         }
490
491         push @newlines, "</config>";
492
493         my $tmpfile;
494         do
495         {
496                 $tmpfile = tmpnam();
497         } until sysopen(TF, $tmpfile, O_RDWR|O_CREAT|O_EXCL|O_NOFOLLOW, 0700);
498
499         foreach $n (@newlines)
500         {
501                 print TF "$n\n";
502         }
503         close TF;
504
505         my @result = `xmllint -noout $tmpfile 2>&1`;
506         chomp(@result);
507
508         my $skip = 0;
509         foreach $n (@result)
510         {
511                 if ($skip)
512                 {
513                         $skip = 0;
514                         next;
515                 }
516                 $n =~ s/$tmpfile\:\d*\: *//g;
517                 if ($n =~ /.*config>.*/)
518                 {
519                         $n = "";
520                         $skip = 1;
521                 }
522
523                 if ($n && !$skip)
524                 {
525                         if ($n =~ /line \d*/)
526                         {
527                                 my $lineno = $n;
528                                 $lineno =~ s/.*line (\d*).*/\1/;
529                                 $lineno = $lineno-2;
530                                 $n =~ s/line (\d*)/line $lineno/;
531                         }
532                         print "$file : $n\n";
533                 }
534         }
535         unlink($tmpfile);
536 }