]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
And its all done and working!
[user/henk/code/inspircd.git] / .inspircd.inc
1 #!/usr/bin/perl
2 #       +------------------------------------+
3 #       | Inspire Internet Relay Chat Daemon |
4 #       +------------------------------------+
5 #
6 #  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
7 #                       E-mail:
8 #                <brain@chatspike.net>
9 #                <Craig@chatspike.net>
10 #
11 # Written by Craig Edwards, Craig McLure, and others.
12 # This program is free but copyrighted software; see
13 #            the file COPYING for details.
14 #
15 #               I HATE PERL.. kthxbye
16 # ---------------------------------------------------
17
18 my $basepath    =       "@BASE_DIR@";
19 my $confpath    =       "@CONFIG_DIR@/";
20 my $binpath     =       "@BINARY_DIR@";
21 my $libpath     =       "@LIBRARY_DIR@";
22 my $executable  =       "@EXECUTABLE@";
23 my $version     =       "@VERSION@";
24 my @filesparsed;
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 "screenvaldebug") { screenvaldebug(); exit(); }
35 if ($arg eq "stop") { stop(); exit(); }
36 if ($arg eq "status") {
37         if (getstatus() == 1) { 
38                 my $pid = getprocessid();
39                 print "InspIRCd is running (PID: $pid)\n";
40                 exit();
41         } else {
42                 print "InspIRCd is not running. (Or PID File not found)\n";
43                 exit();
44         }
45 }
46 if ($arg eq "rehash") {
47         if (getstatus() == 1) {
48                 my $pid = getprocessid();
49                 system("kill -HUP $pid >/dev/null 2>&1");
50                 print "InspIRCd rehashed (pid: $pid).\n";
51                 exit();
52         } else {
53                 print "InspIRCd is not running. (Or PID File not found)\n";
54                 exit();
55         }
56 }
57
58 if ($arg eq "cron") {
59         if (getstatus() == 0) { start(); }
60         exit();
61 }
62
63 if ($arg eq "version") {
64         print "InspIRCd version: $version\n";
65         exit();
66 }
67
68 if ($arg eq "restart") {
69         stop();
70         unlink($pidfile) if (-e $pidfile);
71         start();
72         # kthxbye();
73         exit();
74 }
75
76 if ($arg eq "Cheese-Sandwich") {
77         print "Creating Cheese Sandwich..\n";
78         print "Done.\n";
79         exit();
80 }
81
82 ###
83 # If we get here.. bad / no parameters.
84 ###
85 print "Invalid Argument: $arg\n";
86 print "Usage: inspircd (start|stop|restart|rehash|status|cron|version)\n";
87 exit();
88
89 ###
90 # Generic Helper Functions.
91 ###
92
93 sub start {
94         # Check to see its not 'running' already.
95         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
96         # If we are still alive here.. Try starting the IRCd..
97         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
98
99         system("$binpath/$executable");
100         return 1;
101 }
102
103 sub debug {
104         # Check to see its not 'running' already.
105         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
106         
107         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
108         
109         # Check we have gdb
110         checkgdb();
111                 
112         # If we are still alive here.. Try starting the IRCd..
113         system("gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
114 }
115
116 sub screendebug
117 {
118         # Check to see its not 'running' already.
119         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
120         
121         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
122
123         #Check we have gdb
124         checkgdb();
125         checkscreen();
126         
127         # If we are still alive here.. Try starting the IRCd..
128         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the gdb output and get a backtrace.\n";
129         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
130         system("screen -m -d gdb --command=$basepath/.gdbargs --args $binpath/$executable -nofork -debug -nolog");
131 }
132
133 sub valdebug
134 {
135         # Check to see its not 'running' already.
136         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
137
138         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
139
140         # Check we have valgrind and gdb
141         checkvalgrind();
142         checkgdb();
143         
144         # If we are still alive here.. Try starting the IRCd..
145         # May want to do something with these args at some point: --suppressions=.inspircd.sup --gen-suppressions=yes
146         # Could be useful when we want to stop it complaining about things we're sure aren't issues.
147         system("valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
148 }
149
150 sub screenvaldebug
151 {
152         # Check to see its not 'running' already.
153         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
154         
155         print "$binpath/$executable doesn't exist\n" and return 0 unless(-e "$binpath/$executable");
156
157         #Check we have gdb
158         checkvalgrind();
159         checkgdb();
160         checkscreen();
161         
162         # If we are still alive here.. Try starting the IRCd..
163         print "Starting InspIRCd in `screen`, type `screen -r` when the ircd crashes to view the valgrind and gdb output and get a backtrace.\n";
164         print "Once you're inside the screen session press ^C + d to re-detach from the session\n";
165         system("screen -m -d valgrind -v --tool=memcheck --leak-check=yes --db-attach=yes --num-callers=10 $binpath/$executable -nofork -debug -nolog");
166 }
167
168 sub stop {
169         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
170         # Get to here, we have something to kill.
171         my $pid = getprocessid();
172         print "Stopping InspIRCd (pid: $pid)...\n";
173         system("kill -TERM $pid >/dev/null 2>&1");
174         # Give it a second to exit
175         sleep(1);
176         if (getstatus() == 1)
177         {
178                 print "InspIRCd not dying quietly -- forcing kill\n";
179                 system("kill -9 $pid >/dev/null 2>&1");
180         }
181         print "InspIRCd Stopped.\n";
182 }
183
184 # GetPidfile Version 2 - Now With Include Support..
185 # I beg for months for include support in insp, then.. 
186 # when it is added, it comes around and BITES ME IN THE ASS,
187 # because i then have to code support into this script.. Evil.
188
189 # Craig got bitten in the ass again --
190 # in 1.1 beta the include file is manditory, therefore
191 # if we cant find it, default to %conf%/inspircd.pid.
192 # Note, this also contains a fix for when the pid file is
193 # defined, but defined in a comment (line starts with #)
194 # -- Brain
195
196 sub getpidfile {
197   my ($file) = @_;
198   # Before we start, do we have a PID already? (Should never occur)
199   if ($pid ne "") {
200     return;
201   }
202   # Are We using a relative path?
203   if ($file !~ /^\//) {
204     # Convert it to a full path..
205     $file = $confpath . $file;
206   }
207
208   # Have we checked this file before?
209   for (my $i = 0; $i < $filesparsed; $i++) {
210     if ($filesparsed[$i] eq $file) {
211       # Already Parsed, Possible recursive loop..
212       return;
213     }
214   }
215   
216   # If we get here, Mark as 'Read'
217   $filesparsed[$filesparsed] = $file;
218
219   # Open the File..
220   open INFILE, "< $file" or die "Unable to open file $file\n";
221   # Grab entire file contents..
222   my(@lines) = <INFILE>;
223   # Close the file
224   close INFILE;
225
226   # Clean up the file, no newlines etc..
227   chomp(@lines);
228   foreach $i (@lines) {
229     $i =~ s/[^=]+=\s(.*)/\1/;
230   }
231   my $tmp = join("",@lines);
232
233   # Does this file have a pid?
234   if (($tmp =~ /<pid file=\"(\S+)\">/i) && ($tmp !~ /^#/))
235   {
236     # Set the PID file and return.
237     $pidfile = $1;
238     return;
239   }
240
241   # If we get here, NO PID FILE! -- Check for includes (Seeing as we will eventually return,
242   # The while (1) is safe.)
243   while (1) {
244     if (($tmp =~ s/\<include file=\"(.+?)\"\>//i) && ($tmp !~ /^#/))
245     {
246       # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
247       getpidfile($1);
248       # Was a PID found?
249       if ($pidfile ne "") {
250         # Yes, Return.
251         return;
252       }
253     } else {
254       # End of includes / No includes found.
255       $pidfile = $confpath . "inspircd.pid";
256       return;
257     }
258   }
259 }
260
261 sub getstatus {
262         my $pid = getprocessid();
263         if ($pid == 0) { return 0; }
264         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
265         if ($status == 0) { return 1; }
266         else { return 0; }
267 }
268
269
270 sub getprocessid {
271         my $pid;
272         open PIDFILE, "< $pidfile" or return 0;
273         while($i = <PIDFILE>)
274         {
275                 $pid = $i;
276         }
277         close PIDFILE;
278         return $pid;
279 }
280
281 sub checkvalgrind
282 {
283         unless(`valgrind --version`)
284         {
285                 print "Couldn't start valgrind: $!\n";
286                 exit;
287         }
288 }
289
290 sub checkgdb
291 {
292         unless(`gdb --version`)
293         {
294                 print "Couldn't start gdb: $!\n";
295                 exit;
296         }
297 }
298
299 sub checkscreen
300 {
301         unless(`screen --version`)
302         {
303                 print "Couldn't start screen: $!\n";
304                 exit;
305         }
306 }