]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
Make ./inspircd ignore SIGPIPE and start running without the user having to type...
[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 "stop") { stop(); exit(); }
31 if ($arg eq "status") {
32         if (getstatus() == 1) { 
33                 my $pid = getprocessid();
34                 print "InspIRCd is running (PID: $pid)\n";
35                 exit();
36         } else {
37                 print "InspIRCd is not running. (Or PID File not found)\n";
38                 exit();
39         }
40 }
41 if ($arg eq "rehash") {
42         if (getstatus() == 1) {
43                 my $pid = getprocessid();
44                 system("kill -HUP $pid >/dev/null 2>&1");
45                 print "InspIRCd rehashed.\n";
46                 exit();
47         } else {
48                 print "InspIRCd is not running. (Or PID File not found)\n";
49                 exit();
50         }
51 }
52
53 if ($arg eq "cron") {
54         if (getstatus() == 0) { start(); }
55         exit();
56 }
57
58 if ($arg eq "restart") {
59         stop();
60         start();
61         # kthxbye();
62         exit();
63 }
64
65 if ($arg eq "Cheese-Sandwich") {
66         print "Creating Cheese Sandwich..\n";
67         print "Done.\n";
68         exit();
69 }
70
71 ###
72 # If we get here.. bad / no parameters.
73 ###
74 print "Invalid Argument: $arg\n";
75 print "Usage: inspircd (start|stop|restart|rehash|status|cron)\n";
76 exit();
77
78 ###
79 # Generic Helper Functions.
80 ###
81
82 sub start {
83         # Check to see its not 'running' already.
84         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
85         # If we are still alive here.. Try starting the IRCd..
86         system("$binpath/$executable");
87         sleep 2;
88         return 1;
89 }
90
91 sub debug {
92         # Check to see its not 'running' already.
93         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
94         # If we are still alive here.. Try starting the IRCd..
95         system("gdb --eval-command=\"handle SIGPIPE pass nostop noprint\" --eval-command=\"run\" --args $binpath/$executable -nofork -debug");
96 }
97
98
99 sub stop {
100         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
101         # Get to here, we have something to kill.
102         my $pid = getprocessid();
103         print "Stopping InspIRCd...\n";
104         system("kill -TERM $pid >/dev/null 2>&1");
105         sleep 2;
106         if (getstatus() == 1)
107         {
108                 print "InspIRCd not dying Quietly -- Forcing Kill\n";
109                 system("kill -9 $pid >/dev/null 2>&1");
110         }
111         print "InspIRCd Stopped.\n";
112 }
113
114 # GetPidfile Version 2 - Now With Include Support..
115 # I beg for months for include support in insp, then.. 
116 # when it is added, it comes around and BITES ME IN THE ASS,
117 # because i then have to code support into this script.. Evil.
118
119 sub getpidfile {
120   my ($file) = @_;
121   # Before we start, do we have a PID already? (Should never occur)
122   if ($pid ne "") {
123     return;
124   }
125   # Are We using a relative path?
126   if ($file !~ /^\//) {
127     # Convert it to a full path..
128     $file = $confpath . $file;
129   }
130
131   # Have we checked this file before?
132   for (my $i = 0; $i < $filesparsed; $i++) {
133     if ($filesparsed[$i] eq $file) {
134       # Already Parsed, Possible recursive loop..
135       return;
136     }
137   }
138   
139   # If we get here, Mark as 'Read'
140   $filesparsed[$filesparsed] = $file;
141
142   # Open the File..
143   open INFILE, "< $file" or die "Unable to Open file $file\n";
144   # Grab entire file contents..
145   my(@lines) = <INFILE>;
146   # Close the file
147   close INFILE;
148
149   # Clean up the file, no newlines etc..
150   chomp(@lines);
151   foreach $i (@lines) {
152     $i =~ s/[^=]+=\s(.*)/\1/;
153   }
154   my $tmp = join("",@lines);
155
156   # Does this file have a pid?
157   if ($tmp =~ /<pid file=\"(\S+)\">/i) {
158     # Set the PID file and return.
159     $pidfile = $1;
160     return;
161   }
162
163   # If we get here, NO PID FILE! -- Check for includes (Seeing as we will eventually return,
164   # The while (1) is safe.)
165   while (1) {
166     if ($tmp =~ s/\<include file=\"(.+?)\"\>//i)
167     {
168       # Decend into that file, and check for PIDs.. (that sounds like an STD ;/)
169       getpidfile($1);
170       # Was a PID found?
171       if ($pidfile ne "") {
172         # Yes, Return.
173         return;
174       }
175     } else {
176       # End of includes / No includes found.
177       return;
178     }
179   }
180 }
181
182 sub getstatus {
183         my $pid = getprocessid();
184         if ($pid == 0) { return 0; }
185         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
186         if ($status == 0) { return 1; }
187         else { return 0; }
188 }
189
190
191 sub getprocessid {
192         my $pid;
193         open PIDFILE, "< $pidfile" or return 0;
194         while($i = <PIDFILE>)
195         {
196                 $pid = $i;
197         }
198         close PIDFILE;
199         return $pid;
200 }