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 |
|
|
pfSense_BUILDER_BINARIES: /usr/bin/killall /bin/sh /bin/ps
|
38 |
|
|
pfSense_MODULE: utils
|
39 |
|
|
*/
|
40 |
|
|
|
41 |
c83068f6
|
Colin Smith
|
function write_rcfile($params) {
|
42 |
b662f54f
|
Colin Smith
|
$fileprefix = "/usr/local/etc/rc.d/";
|
43 |
c83068f6
|
Colin Smith
|
if(!(is_writable($fileprefix . $params['file']) or $params['start'])) return false;
|
44 |
36d0358b
|
Scott Ullrich
|
$towrite .= "#!/bin/sh\n# This file was automatically generated\n# by the {$g['product_website']} service handler.\n\n";
|
45 |
c83068f6
|
Colin Smith
|
/* write our rc functions */
|
46 |
|
|
$towrite .= "rc_start() {\n\t" . $params['start'] . "\n}\n\n";
|
47 |
|
|
if($params['stop']) {
|
48 |
|
|
$tokill =& $params['stop'];
|
49 |
dc312bf5
|
Colin Smith
|
} elseif($params['executable']) {
|
50 |
a8dbad89
|
Colin Smith
|
/* just nuke the executable */
|
51 |
dc312bf5
|
Colin Smith
|
$tokill = "/usr/bin/killall {$params['executable']}";
|
52 |
c83068f6
|
Colin Smith
|
} else {
|
53 |
a8dbad89
|
Colin Smith
|
/* make an educated guess (bad) */
|
54 |
c83068f6
|
Colin Smith
|
$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
|
55 |
|
|
}
|
56 |
|
|
$towrite .= "rc_stop() {\n\t" . $tokill . "\n}\n\n";
|
57 |
4685e464
|
Scott Ullrich
|
|
58 |
c83068f6
|
Colin Smith
|
/* begin rcfile logic */
|
59 |
|
|
$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";
|
60 |
|
|
$fout = fopen($fileprefix . $params['file'], "w");
|
61 |
|
|
fwrite($fout, $towrite);
|
62 |
|
|
fclose($fout);
|
63 |
815af8fb
|
Colin Smith
|
chmod($fileprefix . $params['file'], 0755);
|
64 |
c83068f6
|
Colin Smith
|
return;
|
65 |
|
|
}
|
66 |
31f346a8
|
Colin Smith
|
|
67 |
58826c73
|
Colin Smith
|
function start_service($name) {
|
68 |
669e1adb
|
Bill Marquette
|
global $config;
|
69 |
2523de53
|
Scott Ullrich
|
/* make sure service is stopped before starting */
|
70 |
|
|
stop_service($name);
|
71 |
34f7437f
|
Scott Ullrich
|
sleep(2);
|
72 |
4685e464
|
Scott Ullrich
|
if(file_exists("/usr/local/etc/rc.d/{$name}.sh")) {
|
73 |
34f7437f
|
Scott Ullrich
|
exec("/bin/sh /usr/local/etc/rc.d/{$name}.sh start");
|
74 |
4685e464
|
Scott Ullrich
|
return;
|
75 |
|
|
}
|
76 |
31f346a8
|
Colin Smith
|
if($config['installedpackages']['service']) {
|
77 |
|
|
foreach($config['installedpackages']['service'] as $service) {
|
78 |
a8dbad89
|
Colin Smith
|
if(strtolower($service['name']) == strtolower($name)) {
|
79 |
31f346a8
|
Colin Smith
|
if($service['rcfile']) {
|
80 |
|
|
if($service['prefix']) {
|
81 |
|
|
$prefix =& $service['prefix'];
|
82 |
|
|
} else {
|
83 |
|
|
$prefix = "/usr/local/etc/rc.d/";
|
84 |
|
|
}
|
85 |
a4ea3340
|
Scott Ullrich
|
if(file_exists($prefix . $service['rcfile'])) {
|
86 |
c19a49e0
|
Scott Ullrich
|
mwexec_bg($prefix . $service['rcfile'] . " start");
|
87 |
a4ea3340
|
Scott Ullrich
|
} else {
|
88 |
|
|
if(file_exists("/usr/local/etc/rc.d/{$name}.sh"))
|
89 |
|
|
mwexec_bg("/usr/local/etc/rc.d/{$name}.sh start");
|
90 |
|
|
}
|
91 |
31f346a8
|
Colin Smith
|
}
|
92 |
|
|
if($service['startcmd']) {
|
93 |
|
|
eval($service['startcmd']);
|
94 |
|
|
}
|
95 |
|
|
break;
|
96 |
|
|
}
|
97 |
|
|
}
|
98 |
|
|
}
|
99 |
|
|
}
|
100 |
|
|
|
101 |
58826c73
|
Colin Smith
|
function stop_service($name) {
|
102 |
cd72ded3
|
Timo Boettcher
|
global $config;
|
103 |
|
|
if($config['installedpackages']['service']) {
|
104 |
|
|
foreach($config['installedpackages']['service'] as $service) {
|
105 |
|
|
if(strtolower($service['name']) == strtolower($name)) {
|
106 |
|
|
if($service['rcfile']) {
|
107 |
|
|
if($service['prefix']) {
|
108 |
|
|
$prefix =& $service['prefix'];
|
109 |
|
|
} else {
|
110 |
|
|
$prefix = "/usr/local/etc/rc.d/";
|
111 |
|
|
}
|
112 |
|
|
mwexec_bg($prefix . $service['rcfile'] . " stop");
|
113 |
|
|
}
|
114 |
|
|
if($service['stopcmd']) {
|
115 |
|
|
eval($service['stopcmd']);
|
116 |
|
|
}
|
117 |
|
|
if(!($service['rcfile'] or $service['stopcmd'])) {
|
118 |
99f98b80
|
sullrich
|
if(is_process_running("{$service['executable']}"))
|
119 |
|
|
mwexec_bg("/usr/bin/killall {$service['executable']}");
|
120 |
cd72ded3
|
Timo Boettcher
|
return;
|
121 |
|
|
}
|
122 |
|
|
break;
|
123 |
|
|
}
|
124 |
|
|
}
|
125 |
|
|
}
|
126 |
|
|
/* finally if we get here lets simply kill the service name */
|
127 |
99f98b80
|
sullrich
|
if(is_process_running("{$name}"))
|
128 |
cd72ded3
|
Timo Boettcher
|
mwexec_bg("/usr/bin/killall {$name}");
|
129 |
31f346a8
|
Colin Smith
|
}
|
130 |
|
|
|
131 |
58826c73
|
Colin Smith
|
function restart_service($name) {
|
132 |
cd72ded3
|
Timo Boettcher
|
global $config;
|
133 |
636ab238
|
Colin Smith
|
stop_service($name);
|
134 |
|
|
start_service($name);
|
135 |
cd72ded3
|
Timo Boettcher
|
if($config['installedpackages']['service']) {
|
136 |
|
|
foreach($config['installedpackages']['service'] as $service) {
|
137 |
|
|
if(strtolower($service['name']) == strtolower($name)) {
|
138 |
|
|
if($service['restartcmd']) {
|
139 |
|
|
eval($service['restartcmd']);
|
140 |
|
|
}
|
141 |
|
|
break;
|
142 |
|
|
}
|
143 |
|
|
}
|
144 |
|
|
}
|
145 |
31f346a8
|
Colin Smith
|
}
|
146 |
ec4e071a
|
Colin Smith
|
|
147 |
cd72ded3
|
Timo Boettcher
|
function is_pid_running($pidfile) {
|
148 |
|
|
$pid = trim(file_get_contents($pidfile));
|
149 |
6661ea25
|
thompsa
|
$running = (trim(shell_exec("/usr/bin/procstat {$pid} 2>/dev/null")) != '');
|
150 |
cd72ded3
|
Timo Boettcher
|
return $running;
|
151 |
|
|
}
|
152 |
|
|
|
153 |
8ff8520f
|
Scott Ullrich
|
function is_dhcp_running($interface) {
|
154 |
fda8dc28
|
Seth Mos
|
$status = find_dhclient_process($interface);
|
155 |
|
|
if($status <> "")
|
156 |
ca88e48f
|
Scott Ullrich
|
return true;
|
157 |
|
|
return false;
|
158 |
8ff8520f
|
Scott Ullrich
|
}
|
159 |
|
|
|
160 |
0661a033
|
Colin Smith
|
function restart_service_if_running($service) {
|
161 |
|
|
global $config;
|
162 |
4685e464
|
Scott Ullrich
|
if(is_service_running($service))
|
163 |
0661a033
|
Colin Smith
|
restart_service($service);
|
164 |
|
|
return;
|
165 |
|
|
}
|
166 |
|
|
|
167 |
fbeb6d02
|
Colin Smith
|
function is_service_running($service, $ps = "") {
|
168 |
|
|
global $config;
|
169 |
c2b6723a
|
Scott Ullrich
|
/*
|
170 |
a8dbad89
|
Colin Smith
|
if(!$ps) {
|
171 |
|
|
exec("/bin/ps ax | awk '{ print $5 }'", $psout);
|
172 |
|
|
}
|
173 |
c2b6723a
|
Scott Ullrich
|
*/
|
174 |
|
|
if(is_array($config['installedpackages']['service'])) {
|
175 |
cd72ded3
|
Timo Boettcher
|
foreach($config['installedpackages']['service'] as $aservice) {
|
176 |
|
|
if(strtolower($service) == strtolower($aservice['name'])) {
|
177 |
c2d49311
|
Eirik Oeverby
|
if($aservice['custom_php_service_status_command'] <> "") {
|
178 |
|
|
$_cmd=explode(';', $aservice['custom_php_service_status_command']);
|
179 |
|
|
foreach($_cmd as $_acmd) {
|
180 |
|
|
if($_acmd) eval('$rc='.$_acmd.';');
|
181 |
|
|
}
|
182 |
|
|
return $rc;
|
183 |
|
|
}
|
184 |
a93f79c1
|
Colin Smith
|
if(!$aservice['executable']) return false;
|
185 |
c2b6723a
|
Scott Ullrich
|
/*
|
186 |
|
|
if(count(preg_grep("/{$aservice['executable']}/i", $ps))) {
|
187 |
ec4e071a
|
Colin Smith
|
return true;
|
188 |
|
|
} else {
|
189 |
|
|
return false;
|
190 |
|
|
}
|
191 |
c2b6723a
|
Scott Ullrich
|
*/
|
192 |
2cd5c340
|
Timo Boettcher
|
return is_process_running($aservice['executable']) ? true : false;
|
193 |
cd72ded3
|
Timo Boettcher
|
}
|
194 |
|
|
}
|
195 |
|
|
}
|
196 |
ec4e071a
|
Colin Smith
|
}
|
197 |
ff074bf9
|
Scott Ullrich
|
|
198 |
cae1b7db
|
thompsa
|
?>
|