]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - .inspircd.inc
b7696026a379450eaf6d536f4b0cba0e75a1b78a
[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 $conffile = "@CONFIG_DIR@/inspircd.conf";
19 my $binpath = "@BINARY_DIR@";
20 my $libpath = "@LIBRARY_DIR@";
21 my $executable = "@EXECUTABLE@";
22
23 $ENV{"LD_LIBRARY_PATH"} = $ENV{"LD_LIBRARY_PATH"} . ":/usr/local/lib/mysql:/usr/lib/mysql:$libpath";
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();
28
29 if ($arg eq "start") { start(); exit(); }
30 if ($arg eq "debug") { debug(); exit(); }
31 if ($arg eq "stop") { stop(); exit(); }
32 if ($arg eq "status") {
33         if (getstatus() == 1) { 
34                 my $pid = getprocessid();
35                 print "InspIRCd is running (PID: $pid)\n";
36                 exit();
37         } else {
38                 print "InspIRCd is not running. (Or PID File not found)\n";
39                 exit();
40         }
41 }
42 if ($arg eq "rehash") {
43         if (getstatus() == 1) {
44                 my $pid = getprocessid();
45                 system("kill -HUP $pid >/dev/null 2>&1");
46                 print "InspIRCd rehashed.\n";
47                 exit();
48         } else {
49                 print "InspIRCd is not running. (Or PID File not found)\n";
50                 exit();
51         }
52 }
53
54 if ($arg eq "cron") {
55         if (getstatus() == 0) { start(); }
56         exit();
57 }
58
59 if ($arg eq "restart") {
60         stop();
61         start();
62         # kthxbye();
63         exit();
64 }
65
66 if ($arg eq "Cheese-Sandwich") {
67         print "Creating Cheese Sandwich..\n";
68         print "Done.\n";
69         exit();
70 }
71
72 ###
73 # If we get here.. bad / no parameters.
74 ###
75 print "Invalid Argument: $arg\n";
76 print "Usage: inspircd (start|stop|restart|rehash|status|cron)\n";
77 exit();
78
79 ###
80 # Generic Helper Functions.
81 ###
82
83 sub start {
84         # Check to see its not 'running' already.
85         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
86         # If we are still alive here.. Try starting the IRCd..
87         system("$binpath/$executable");
88         sleep 1;
89         if (getstatus() == 0) {
90                 print "InspIRCd Seemingly not started, Log follows:\n";
91                 system("tail $binpath/ircd.log");
92         } else {
93                 # We're good!
94                 return 1;
95         }
96 }
97
98 sub debug {
99         # Check to see its not 'running' already.
100         if (getstatus() == 1) { print "InspIRCd is already running.\n"; return 0; }
101         # If we are still alive here.. Try starting the IRCd..
102         system("gdb --args $binpath/$executable -nofork -debug");
103 }
104
105
106 sub stop {
107         if (getstatus() == 0) { print "InspIRCd is not running. (Or PID File not found)\n"; return 0; }
108         # Get to here, we have something to kill.
109         my $pid = getprocessid();
110         print "Stopping InspIRCd...\n";
111         system("kill -TERM $pid >/dev/null 2>&1");
112         sleep 2;
113         if (getstatus() == 1)
114         {
115                 print "InspIRCd not dying Quietly -- Forcing Kill\n";
116                 system("kill -9 $pid >/dev/null 2>&1");
117         }
118         print "InspIRCd Stopped.\n";
119 }
120
121
122 sub getpidfile {
123         open INFILE, "<  $conffile" or die "Couldn't open $conffile\n";
124         my(@lines) = <INFILE>;
125         close INFILE;
126         chomp(@lines);
127
128         foreach $i (@lines) {
129             $i =~ s/[^=]+=\s(.*)/\1/;
130         }
131
132         my $tmp = join("",@lines);
133         $tmp =~ /<pid file=\"(\S+)\">/i;
134
135         $pidfile = $1;
136 }
137
138 sub getstatus {
139         my $pid = getprocessid();
140         if ($pid == 0) { return 0; }
141         $status = system("kill -0 $pid >/dev/null 2>&1") / 256;
142         if ($status == 0) { return 1; }
143         else { return 0; }
144 }
145
146
147 sub getprocessid {
148         my $pid;
149         open PIDFILE, "< $pidfile" or return 0;
150         while($i = <PIDFILE>)
151         {
152                 $pid = $i;
153         }
154         close PIDFILE;
155         return $pid;
156 }