38 |
38 |
pfSense_MODULE: utils
|
39 |
39 |
*/
|
40 |
40 |
|
|
41 |
$rcfileprefix = "/usr/local/etc/rc.d/";
|
41 |
42 |
function write_rcfile($params) {
|
42 |
|
$fileprefix = "/usr/local/etc/rc.d/";
|
43 |
|
if(!(is_writable($fileprefix . $params['file']) or $params['start'])) return false;
|
44 |
|
$towrite .= "#!/bin/sh\n# This file was automatically generated\n# by the {$g['product_website']} service handler.\n\n";
|
|
43 |
if (!is_writable("{$rcfileprefix}{$params['file']}") || empty($params['start']))
|
|
44 |
return false;
|
|
45 |
$towrite = "#!/bin/sh\n";
|
|
46 |
$towrite .= "# This file was automatically generated\n# by the {$g['product_website']} service handler.\n\n";
|
|
47 |
|
45 |
48 |
/* write our rc functions */
|
46 |
|
$towrite .= "rc_start() {\n\t" . $params['start'] . "\n}\n\n";
|
47 |
|
if($params['stop']) {
|
|
49 |
$towrite .= "rc_start() {\n";
|
|
50 |
$towrite .= "\t{$params['start']}\n";
|
|
51 |
$towrite .= "}\n\n";
|
|
52 |
if(!empty($params['stop'])) {
|
48 |
53 |
$tokill =& $params['stop'];
|
49 |
|
} elseif($params['executable']) {
|
|
54 |
} else if(!empty($params['executable'])) {
|
50 |
55 |
/* just nuke the executable */
|
51 |
56 |
$tokill = "/usr/bin/killall {$params['executable']}";
|
52 |
57 |
} else {
|
53 |
58 |
/* make an educated guess (bad) */
|
54 |
59 |
$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
|
55 |
60 |
}
|
56 |
|
$towrite .= "rc_stop() {\n\t" . $tokill . "\n}\n\n";
|
|
61 |
$towrite .= "rc_stop() {\n";
|
|
62 |
$towrite .= "\t{$tokill}\n";
|
|
63 |
$towrite .= "}\n\n";
|
57 |
64 |
|
58 |
65 |
/* 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 |
|
chmod($fileprefix . $params['file'], 0755);
|
|
66 |
$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";
|
|
67 |
|
|
68 |
file_put_contents("{$rcfileprefix}{$params['file']}", $towrite);
|
|
69 |
@chmod("{$rcfileprefix}{$params['file']}", 0755);
|
|
70 |
|
64 |
71 |
return;
|
65 |
72 |
}
|
66 |
73 |
|
67 |
74 |
function start_service($name) {
|
68 |
75 |
global $config;
|
|
76 |
|
69 |
77 |
/* make sure service is stopped before starting */
|
70 |
78 |
stop_service($name);
|
71 |
79 |
sleep(2);
|
72 |
|
if(file_exists("/usr/local/etc/rc.d/{$name}.sh")) {
|
73 |
|
exec("/bin/sh /usr/local/etc/rc.d/{$name}.sh start");
|
|
80 |
|
|
81 |
if(file_exists("{$rcfileprefix}{$name}.sh")) {
|
|
82 |
exec("/bin/sh {$rcfileprefix}{$name}.sh start");
|
74 |
83 |
return;
|
75 |
84 |
}
|
76 |
85 |
if($config['installedpackages']['service']) {
|
77 |
86 |
foreach($config['installedpackages']['service'] as $service) {
|
78 |
87 |
if(strtolower($service['name']) == strtolower($name)) {
|
79 |
88 |
if($service['rcfile']) {
|
80 |
|
if($service['prefix']) {
|
|
89 |
$prefix = $rcfileprefix;
|
|
90 |
if (!empty($service['prefix'])) {
|
81 |
91 |
$prefix =& $service['prefix'];
|
82 |
|
} else {
|
83 |
|
$prefix = "/usr/local/etc/rc.d/";
|
84 |
92 |
}
|
85 |
|
if(file_exists($prefix . $service['rcfile'])) {
|
86 |
|
mwexec_bg($prefix . $service['rcfile'] . " start");
|
87 |
|
} else {
|
88 |
|
if(file_exists("/usr/local/etc/rc.d/{$name}.sh"))
|
89 |
|
mwexec_bg("/usr/local/etc/rc.d/{$name}.sh start");
|
|
93 |
if(file_exists("{$prefix}{$service['rcfile']}")) {
|
|
94 |
mwexec_bg("{$prefix}{$service['rcfile']} start");
|
90 |
95 |
}
|
91 |
96 |
}
|
92 |
|
if($service['startcmd']) {
|
|
97 |
if (!empty($service['startcmd']))
|
93 |
98 |
eval($service['startcmd']);
|
94 |
|
}
|
95 |
99 |
break;
|
96 |
100 |
}
|
97 |
101 |
}
|
... | ... | |
105 |
109 |
foreach($config['installedpackages']['service'] as $service) {
|
106 |
110 |
if(strtolower($service['name']) == strtolower($name)) {
|
107 |
111 |
if($service['rcfile']) {
|
108 |
|
if($service['prefix']) {
|
|
112 |
$prefix = $rcfileprefix;
|
|
113 |
if(!empty($service['prefix'])) {
|
109 |
114 |
$prefix =& $service['prefix'];
|
110 |
|
} else {
|
111 |
|
$prefix = "/usr/local/etc/rc.d/";
|
112 |
115 |
}
|
113 |
116 |
mwexec_bg("{$prefix}{$service['rcfile']} stop");
|
114 |
117 |
}
|
115 |
|
if($service['stopcmd']) {
|
|
118 |
if (!empty($service['stopcmd']))
|
116 |
119 |
eval($service['stopcmd']);
|
117 |
|
}
|
|
120 |
|
118 |
121 |
if(!($service['rcfile'] or $service['stopcmd'])) {
|
119 |
122 |
if(is_process_running("{$service['executable']}"))
|
120 |
123 |
mwexec_bg("/usr/bin/killall {$service['executable']}");
|
... | ... | |
131 |
134 |
|
132 |
135 |
function restart_service($name) {
|
133 |
136 |
global $config;
|
|
137 |
|
134 |
138 |
stop_service($name);
|
135 |
139 |
start_service($name);
|
|
140 |
|
136 |
141 |
if($config['installedpackages']['service']) {
|
137 |
142 |
foreach($config['installedpackages']['service'] as $service) {
|
138 |
143 |
if(strtolower($service['name']) == strtolower($name)) {
|
... | ... | |
146 |
151 |
}
|
147 |
152 |
|
148 |
153 |
function is_pid_running($pidfile) {
|
149 |
|
$pid = trim(file_get_contents($pidfile));
|
150 |
|
$running = (trim(shell_exec("/usr/bin/procstat {$pid} 2>/dev/null")) != '');
|
151 |
|
return $running;
|
|
154 |
if (!file_exists($pidfile))
|
|
155 |
return false;
|
|
156 |
$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
|
|
157 |
|
|
158 |
return (intval($running) == 0);
|
152 |
159 |
}
|
153 |
160 |
|
154 |
161 |
function is_dhcp_running($interface) {
|
... | ... | |
167 |
174 |
|
168 |
175 |
function is_service_running($service, $ps = "") {
|
169 |
176 |
global $config;
|
170 |
|
/*
|
171 |
|
if(!$ps) {
|
172 |
|
exec("/bin/ps ax | awk '{ print $5 }'", $psout);
|
173 |
|
}
|
174 |
|
*/
|
|
177 |
|
175 |
178 |
if(is_array($config['installedpackages']['service'])) {
|
176 |
179 |
foreach($config['installedpackages']['service'] as $aservice) {
|
177 |
180 |
if(strtolower($service) == strtolower($aservice['name'])) {
|
178 |
|
if($aservice['custom_php_service_status_command'] <> "") {
|
179 |
|
$_cmd=explode(';', $aservice['custom_php_service_status_command']);
|
180 |
|
foreach($_cmd as $_acmd) {
|
181 |
|
if($_acmd) eval('$rc='.$_acmd.';');
|
182 |
|
}
|
|
181 |
if ($aservice['custom_php_service_status_command'] <> "") {
|
|
182 |
eval("\$rc={$aservice['custom_php_service_status_command']};");
|
183 |
183 |
return $rc;
|
184 |
184 |
}
|
185 |
|
if(!$aservice['executable']) return false;
|
186 |
|
/*
|
187 |
|
if(count(preg_grep("/{$aservice['executable']}/i", $ps))) {
|
188 |
|
return true;
|
189 |
|
} else {
|
|
185 |
if(empty($aservice['executable']))
|
190 |
186 |
return false;
|
191 |
|
}
|
192 |
|
*/
|
193 |
|
return is_process_running($aservice['executable']) ? true : false;
|
|
187 |
if (is_process_running($aservice['executable']))
|
|
188 |
return true;
|
|
189 |
|
|
190 |
return false;
|
194 |
191 |
}
|
195 |
192 |
}
|
196 |
193 |
}
|
|
194 |
|
|
195 |
if (is_process_running($service))
|
|
196 |
return true;
|
|
197 |
|
|
198 |
return false;
|
197 |
199 |
}
|
198 |
200 |
|
199 |
201 |
?>
|
Ticket #485. Correct code and use pkill/pgrep to match or kill process. Since these utilities know how to handle pidfiles and exact matches on process names.