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