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