1 |
c83068f6
|
Colin Smith
|
<?php
|
2 |
|
|
/****h* pfSense/service-utils
|
3 |
|
|
* NAME
|
4 |
|
|
* service-utils.inc - Service facility
|
5 |
|
|
* DESCRIPTION
|
6 |
|
|
* This file contains various functions used by the pfSense service facility.
|
7 |
|
|
* HISTORY
|
8 |
|
|
* $Id$
|
9 |
|
|
******
|
10 |
|
|
*
|
11 |
0e16b9ca
|
Scott Ullrich
|
* Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
|
12 |
c83068f6
|
Colin Smith
|
* All rights reserved.
|
13 |
|
|
* Redistribution and use in source and binary forms, with or without
|
14 |
|
|
* modification, are permitted provided that the following conditions are met:
|
15 |
|
|
*
|
16 |
|
|
* 1. Redistributions of source code must retain the above copyright notice,
|
17 |
|
|
* this list of conditions and the following disclaimer.
|
18 |
|
|
*
|
19 |
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
20 |
|
|
* notice, this list of conditions and the following disclaimer in the
|
21 |
|
|
* documentation and/or other materials provided with the distribution.
|
22 |
|
|
*
|
23 |
|
|
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
|
24 |
|
|
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
|
25 |
|
|
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
26 |
|
|
* AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
|
27 |
|
|
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
28 |
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
29 |
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
30 |
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
31 |
|
|
* RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
32 |
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
33 |
|
|
*
|
34 |
|
|
*/
|
35 |
|
|
|
36 |
523855b0
|
Scott Ullrich
|
/*
|
37 |
05c4bfa0
|
Ermal
|
pfSense_BUILDER_BINARIES: /bin/pgrep /bin/sh /usr/bin/killall
|
38 |
523855b0
|
Scott Ullrich
|
pfSense_MODULE: utils
|
39 |
|
|
*/
|
40 |
|
|
|
41 |
ba8495f0
|
Ermal
|
$rcfileprefix = "/usr/local/etc/rc.d/";
|
42 |
c83068f6
|
Colin Smith
|
function write_rcfile($params) {
|
43 |
c2151973
|
Ermal
|
global $g;
|
44 |
af799b48
|
Renato Botelho
|
global $rcfileprefix;
|
45 |
|
|
|
46 |
|
|
if (!file_exists("{$rcfileprefix}{$params['file']}") && !touch("{$rcfileprefix}{$params['file']}"))
|
47 |
|
|
return false;
|
48 |
c2151973
|
Ermal
|
|
49 |
ba8495f0
|
Ermal
|
if (!is_writable("{$rcfileprefix}{$params['file']}") || empty($params['start']))
|
50 |
|
|
return false;
|
51 |
af799b48
|
Renato Botelho
|
|
52 |
ba8495f0
|
Ermal
|
$towrite = "#!/bin/sh\n";
|
53 |
4dddf8c3
|
Warren Baker
|
$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
|
54 |
ba8495f0
|
Ermal
|
|
55 |
c83068f6
|
Colin Smith
|
/* write our rc functions */
|
56 |
ba8495f0
|
Ermal
|
$towrite .= "rc_start() {\n";
|
57 |
|
|
$towrite .= "\t{$params['start']}\n";
|
58 |
|
|
$towrite .= "}\n\n";
|
59 |
|
|
if(!empty($params['stop'])) {
|
60 |
c83068f6
|
Colin Smith
|
$tokill =& $params['stop'];
|
61 |
ba8495f0
|
Ermal
|
} else if(!empty($params['executable'])) {
|
62 |
a8dbad89
|
Colin Smith
|
/* just nuke the executable */
|
63 |
dc312bf5
|
Colin Smith
|
$tokill = "/usr/bin/killall {$params['executable']}";
|
64 |
c83068f6
|
Colin Smith
|
} else {
|
65 |
a8dbad89
|
Colin Smith
|
/* make an educated guess (bad) */
|
66 |
c83068f6
|
Colin Smith
|
$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
|
67 |
|
|
}
|
68 |
ba8495f0
|
Ermal
|
$towrite .= "rc_stop() {\n";
|
69 |
|
|
$towrite .= "\t{$tokill}\n";
|
70 |
|
|
$towrite .= "}\n\n";
|
71 |
4685e464
|
Scott Ullrich
|
|
72 |
c83068f6
|
Colin Smith
|
/* begin rcfile logic */
|
73 |
ba8495f0
|
Ermal
|
$towrite .= "case \$1 in\n\tstart)\n\t\trc_start\n\t\t;;\n\tstop)\n\t\trc_stop\n\t\t;;\n\trestart)\n\t\trc_stop\n\t\trc_start\n\t\t;;\nesac\n\n";
|
74 |
|
|
|
75 |
|
|
file_put_contents("{$rcfileprefix}{$params['file']}", $towrite);
|
76 |
|
|
@chmod("{$rcfileprefix}{$params['file']}", 0755);
|
77 |
|
|
|
78 |
c83068f6
|
Colin Smith
|
return;
|
79 |
|
|
}
|
80 |
31f346a8
|
Colin Smith
|
|
81 |
58826c73
|
Colin Smith
|
function start_service($name) {
|
82 |
669e1adb
|
Bill Marquette
|
global $config;
|
83 |
af799b48
|
Renato Botelho
|
global $rcfileprefix;
|
84 |
ba8495f0
|
Ermal
|
|
85 |
b27ade8e
|
Ermal
|
if (empty($name))
|
86 |
|
|
return;
|
87 |
|
|
|
88 |
2523de53
|
Scott Ullrich
|
/* make sure service is stopped before starting */
|
89 |
|
|
stop_service($name);
|
90 |
34f7437f
|
Scott Ullrich
|
sleep(2);
|
91 |
ba8495f0
|
Ermal
|
|
92 |
|
|
if(file_exists("{$rcfileprefix}{$name}.sh")) {
|
93 |
aeebce1b
|
Erik Fonnesbeck
|
mwexec_bg("/bin/sh {$rcfileprefix}{$name}.sh start");
|
94 |
4685e464
|
Scott Ullrich
|
return;
|
95 |
|
|
}
|
96 |
31f346a8
|
Colin Smith
|
if($config['installedpackages']['service']) {
|
97 |
|
|
foreach($config['installedpackages']['service'] as $service) {
|
98 |
a8dbad89
|
Colin Smith
|
if(strtolower($service['name']) == strtolower($name)) {
|
99 |
31f346a8
|
Colin Smith
|
if($service['rcfile']) {
|
100 |
ba8495f0
|
Ermal
|
$prefix = $rcfileprefix;
|
101 |
|
|
if (!empty($service['prefix'])) {
|
102 |
31f346a8
|
Colin Smith
|
$prefix =& $service['prefix'];
|
103 |
|
|
}
|
104 |
ba8495f0
|
Ermal
|
if(file_exists("{$prefix}{$service['rcfile']}")) {
|
105 |
|
|
mwexec_bg("{$prefix}{$service['rcfile']} start");
|
106 |
a4ea3340
|
Scott Ullrich
|
}
|
107 |
31f346a8
|
Colin Smith
|
}
|
108 |
ba8495f0
|
Ermal
|
if (!empty($service['startcmd']))
|
109 |
31f346a8
|
Colin Smith
|
eval($service['startcmd']);
|
110 |
|
|
break;
|
111 |
|
|
}
|
112 |
|
|
}
|
113 |
|
|
}
|
114 |
|
|
}
|
115 |
|
|
|
116 |
58826c73
|
Colin Smith
|
function stop_service($name) {
|
117 |
cd72ded3
|
Timo Boettcher
|
global $config;
|
118 |
af799b48
|
Renato Botelho
|
global $rcfileprefix;
|
119 |
246a887a
|
Ermal
|
|
120 |
8bf2e9e5
|
Ermal
|
if (empty($name))
|
121 |
|
|
return;
|
122 |
|
|
|
123 |
246a887a
|
Ermal
|
if ($config['installedpackages']['service']) {
|
124 |
cd72ded3
|
Timo Boettcher
|
foreach($config['installedpackages']['service'] as $service) {
|
125 |
|
|
if(strtolower($service['name']) == strtolower($name)) {
|
126 |
|
|
if($service['rcfile']) {
|
127 |
ba8495f0
|
Ermal
|
$prefix = $rcfileprefix;
|
128 |
|
|
if(!empty($service['prefix'])) {
|
129 |
cd72ded3
|
Timo Boettcher
|
$prefix =& $service['prefix'];
|
130 |
|
|
}
|
131 |
098820e2
|
Ermal
|
if(file_exists("{$prefix}{$service['rcfile']}")) {
|
132 |
6ae78f08
|
Marcello Coutinho
|
mwexec("{$prefix}{$service['rcfile']} stop");
|
133 |
098820e2
|
Ermal
|
}
|
134 |
|
|
return;
|
135 |
cd72ded3
|
Timo Boettcher
|
}
|
136 |
ba8495f0
|
Ermal
|
if (!empty($service['stopcmd']))
|
137 |
cd72ded3
|
Timo Boettcher
|
eval($service['stopcmd']);
|
138 |
ba8495f0
|
Ermal
|
|
139 |
cd72ded3
|
Timo Boettcher
|
if(!($service['rcfile'] or $service['stopcmd'])) {
|
140 |
99f98b80
|
sullrich
|
if(is_process_running("{$service['executable']}"))
|
141 |
6ae78f08
|
Marcello Coutinho
|
mwexec("/usr/bin/killall {$service['executable']}");
|
142 |
cd72ded3
|
Timo Boettcher
|
return;
|
143 |
|
|
}
|
144 |
|
|
break;
|
145 |
|
|
}
|
146 |
|
|
}
|
147 |
|
|
}
|
148 |
|
|
/* finally if we get here lets simply kill the service name */
|
149 |
246a887a
|
Ermal
|
if(is_process_running("{$name}"))
|
150 |
6ae78f08
|
Marcello Coutinho
|
mwexec("/usr/bin/killall {$name}");
|
151 |
31f346a8
|
Colin Smith
|
}
|
152 |
|
|
|
153 |
58826c73
|
Colin Smith
|
function restart_service($name) {
|
154 |
cd72ded3
|
Timo Boettcher
|
global $config;
|
155 |
ba8495f0
|
Ermal
|
|
156 |
8bf2e9e5
|
Ermal
|
if (empty($name))
|
157 |
|
|
return;
|
158 |
|
|
|
159 |
636ab238
|
Colin Smith
|
stop_service($name);
|
160 |
|
|
start_service($name);
|
161 |
ba8495f0
|
Ermal
|
|
162 |
cd72ded3
|
Timo Boettcher
|
if($config['installedpackages']['service']) {
|
163 |
|
|
foreach($config['installedpackages']['service'] as $service) {
|
164 |
|
|
if(strtolower($service['name']) == strtolower($name)) {
|
165 |
|
|
if($service['restartcmd']) {
|
166 |
|
|
eval($service['restartcmd']);
|
167 |
|
|
}
|
168 |
|
|
break;
|
169 |
|
|
}
|
170 |
|
|
}
|
171 |
|
|
}
|
172 |
31f346a8
|
Colin Smith
|
}
|
173 |
ec4e071a
|
Colin Smith
|
|
174 |
cd72ded3
|
Timo Boettcher
|
function is_pid_running($pidfile) {
|
175 |
ba8495f0
|
Ermal
|
if (!file_exists($pidfile))
|
176 |
|
|
return false;
|
177 |
|
|
$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
|
178 |
|
|
|
179 |
22d323b3
|
Erik Fonnesbeck
|
return (!empty($running));
|
180 |
cd72ded3
|
Timo Boettcher
|
}
|
181 |
|
|
|
182 |
8ff8520f
|
Scott Ullrich
|
function is_dhcp_running($interface) {
|
183 |
fda8dc28
|
Seth Mos
|
$status = find_dhclient_process($interface);
|
184 |
|
|
if($status <> "")
|
185 |
ca88e48f
|
Scott Ullrich
|
return true;
|
186 |
|
|
return false;
|
187 |
8ff8520f
|
Scott Ullrich
|
}
|
188 |
|
|
|
189 |
0661a033
|
Colin Smith
|
function restart_service_if_running($service) {
|
190 |
|
|
global $config;
|
191 |
4685e464
|
Scott Ullrich
|
if(is_service_running($service))
|
192 |
0661a033
|
Colin Smith
|
restart_service($service);
|
193 |
|
|
return;
|
194 |
|
|
}
|
195 |
|
|
|
196 |
fbeb6d02
|
Colin Smith
|
function is_service_running($service, $ps = "") {
|
197 |
|
|
global $config;
|
198 |
ba8495f0
|
Ermal
|
|
199 |
c2b6723a
|
Scott Ullrich
|
if(is_array($config['installedpackages']['service'])) {
|
200 |
cd72ded3
|
Timo Boettcher
|
foreach($config['installedpackages']['service'] as $aservice) {
|
201 |
|
|
if(strtolower($service) == strtolower($aservice['name'])) {
|
202 |
ba8495f0
|
Ermal
|
if ($aservice['custom_php_service_status_command'] <> "") {
|
203 |
|
|
eval("\$rc={$aservice['custom_php_service_status_command']};");
|
204 |
c2d49311
|
Eirik Oeverby
|
return $rc;
|
205 |
|
|
}
|
206 |
ba8495f0
|
Ermal
|
if(empty($aservice['executable']))
|
207 |
ec4e071a
|
Colin Smith
|
return false;
|
208 |
ba8495f0
|
Ermal
|
if (is_process_running($aservice['executable']))
|
209 |
|
|
return true;
|
210 |
|
|
|
211 |
|
|
return false;
|
212 |
cd72ded3
|
Timo Boettcher
|
}
|
213 |
|
|
}
|
214 |
|
|
}
|
215 |
ba8495f0
|
Ermal
|
|
216 |
|
|
if (is_process_running($service))
|
217 |
|
|
return true;
|
218 |
|
|
|
219 |
|
|
return false;
|
220 |
ec4e071a
|
Colin Smith
|
}
|
221 |
ff074bf9
|
Scott Ullrich
|
|
222 |
05c4bfa0
|
Ermal
|
?>
|