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