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