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