Revision d2e806c4
Added by Jim Pingle almost 3 years ago
src/etc/inc/service-utils.inc | ||
---|---|---|
85 | 85 |
} |
86 | 86 |
|
87 | 87 |
function start_service($name, $after_sync = false) { |
88 |
global $config; |
|
89 |
|
|
90 | 88 |
if (empty($name)) { |
91 | 89 |
return; |
92 | 90 |
} |
93 | 91 |
|
94 |
if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) { |
|
95 |
foreach ($config['installedpackages']['service'] as $service) { |
|
96 |
if (isset($service['name']) && (strtolower($service['name']) == strtolower($name))) { |
|
97 |
/* Avoid starting twice if this is called just after a |
|
98 |
* package sync which starts the service itself. */ |
|
99 |
if ($after_sync && isset($service['starts_on_sync'])) { |
|
100 |
break; |
|
101 |
} |
|
102 |
if ($service['rcfile']) { |
|
103 |
$prefix = RCFILEPREFIX; |
|
104 |
if (!empty($service['prefix'])) { |
|
105 |
$prefix = &$service['prefix']; |
|
106 |
} |
|
107 |
if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { |
|
108 |
mwexec_bg("{$prefix}{$service['rcfile']} start"); |
|
109 |
} |
|
92 |
foreach (config_get_path('installedpackages/service', []) as $service) { |
|
93 |
if (empty($service)) { |
|
94 |
continue; |
|
95 |
} |
|
96 |
if (isset($service['name']) && (strtolower($service['name']) == strtolower($name))) { |
|
97 |
/* Avoid starting twice if this is called just after a |
|
98 |
* package sync which starts the service itself. */ |
|
99 |
if ($after_sync && isset($service['starts_on_sync'])) { |
|
100 |
break; |
|
101 |
} |
|
102 |
if ($service['rcfile']) { |
|
103 |
$prefix = RCFILEPREFIX; |
|
104 |
if (!empty($service['prefix'])) { |
|
105 |
$prefix = &$service['prefix']; |
|
110 | 106 |
} |
111 |
if (!empty($service['startcmd'])) {
|
|
112 |
eval($service['startcmd']);
|
|
107 |
if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) {
|
|
108 |
mwexec_bg("{$prefix}{$service['rcfile']} start");
|
|
113 | 109 |
} |
114 |
break; |
|
115 | 110 |
} |
111 |
if (!empty($service['startcmd'])) { |
|
112 |
eval($service['startcmd']); |
|
113 |
} |
|
114 |
break; |
|
116 | 115 |
} |
117 | 116 |
} |
118 | 117 |
} |
119 | 118 |
|
120 | 119 |
function stop_service($name) { |
121 |
global $config; |
|
122 |
|
|
123 | 120 |
if (empty($name)) { |
124 | 121 |
return; |
125 | 122 |
} |
126 | 123 |
|
127 |
if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) { |
|
128 |
foreach ($config['installedpackages']['service'] as $service) { |
|
129 |
if (strtolower($service['name']) == strtolower($name)) { |
|
130 |
if ($service['rcfile']) { |
|
131 |
$prefix = RCFILEPREFIX; |
|
132 |
if (!empty($service['prefix'])) { |
|
133 |
$prefix = &$service['prefix']; |
|
134 |
} |
|
135 |
if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { |
|
136 |
mwexec("{$prefix}{$service['rcfile']} stop"); |
|
137 |
} |
|
138 |
return; |
|
124 |
foreach (config_get_path('installedpackages/service', []) as $service) { |
|
125 |
if (empty($service)) { |
|
126 |
continue; |
|
127 |
} |
|
128 |
if (strtolower($service['name']) == strtolower($name)) { |
|
129 |
if ($service['rcfile']) { |
|
130 |
$prefix = RCFILEPREFIX; |
|
131 |
if (!empty($service['prefix'])) { |
|
132 |
$prefix = &$service['prefix']; |
|
139 | 133 |
} |
140 |
if (!empty($service['stopcmd'])) { |
|
141 |
eval($service['stopcmd']); |
|
142 |
} elseif (!empty($service['executable'])) { |
|
143 |
mwexec("/usr/bin/killall " . escapeshellarg($service['executable'])); |
|
134 |
if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { |
|
135 |
mwexec("{$prefix}{$service['rcfile']} stop"); |
|
144 | 136 |
} |
145 |
|
|
146 |
break; |
|
137 |
return; |
|
147 | 138 |
} |
139 |
if (!empty($service['stopcmd'])) { |
|
140 |
eval($service['stopcmd']); |
|
141 |
} elseif (!empty($service['executable'])) { |
|
142 |
mwexec("/usr/bin/killall " . escapeshellarg($service['executable'])); |
|
143 |
} |
|
144 |
|
|
145 |
break; |
|
148 | 146 |
} |
149 | 147 |
} |
150 | 148 |
} |
151 | 149 |
|
152 | 150 |
function restart_service($name) { |
153 |
global $config; |
|
154 |
|
|
155 | 151 |
if (empty($name)) { |
156 | 152 |
return; |
157 | 153 |
} |
... | ... | |
161 | 157 |
} |
162 | 158 |
start_service($name); |
163 | 159 |
|
164 |
if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) {
|
|
165 |
foreach ($config['installedpackages']['service'] as $service) {
|
|
166 |
if (strtolower($service['name']) == strtolower($name)) {
|
|
167 |
if ($service['restartcmd']) {
|
|
168 |
eval($service['restartcmd']);
|
|
169 |
}
|
|
170 |
break;
|
|
160 |
foreach (config_get_path('installedpackages/service', []) as $service) {
|
|
161 |
if (empty($service)) {
|
|
162 |
continue;
|
|
163 |
}
|
|
164 |
if (strtolower($service['name']) == strtolower($name)) {
|
|
165 |
if ($service['restartcmd']) {
|
|
166 |
eval($service['restartcmd']);
|
|
171 | 167 |
} |
168 |
break; |
|
172 | 169 |
} |
173 | 170 |
} |
174 | 171 |
} |
... | ... | |
190 | 187 |
} |
191 | 188 |
|
192 | 189 |
function restart_service_if_running($service) { |
193 |
global $config; |
|
194 | 190 |
if (is_service_running($service)) { |
195 | 191 |
restart_service($service); |
196 | 192 |
} |
... | ... | |
198 | 194 |
} |
199 | 195 |
|
200 | 196 |
function is_service_enabled($service_name) { |
201 |
global $config; |
|
202 | 197 |
if ($service_name == "") { |
203 | 198 |
return false; |
204 | 199 |
} |
205 |
if (is_array($config['installedpackages'])) { |
|
206 |
if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) && |
|
207 |
((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) || |
|
208 |
($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) { |
|
209 |
return false; |
|
210 |
} |
|
200 |
$pkg_enabled = config_get_path("installedpackages/{$service_name}/config/0/enable"); |
|
201 |
if (empty($pkg_enabled) || ($pkg_enabled === 'off')) { |
|
202 |
return false; |
|
211 | 203 |
} |
212 | 204 |
return true; |
213 | 205 |
} |
214 | 206 |
|
215 | 207 |
function is_service_running($service, $ps = "") { |
216 |
global $config; |
|
217 |
|
|
218 |
if (is_array($config['installedpackages']['service'])) { |
|
219 |
foreach ($config['installedpackages']['service'] as $aservice) { |
|
220 |
if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) { |
|
221 |
if ($aservice['custom_php_service_status_command'] <> "") { |
|
222 |
eval("\$rc={$aservice['custom_php_service_status_command']};"); |
|
223 |
return $rc; |
|
224 |
} |
|
225 |
if (empty($aservice['executable'])) { |
|
226 |
return false; |
|
227 |
} |
|
228 |
if (is_process_running($aservice['executable'])) { |
|
229 |
return true; |
|
230 |
} |
|
231 |
|
|
208 |
foreach (config_get_path('installedpackages/service', []) as $aservice) { |
|
209 |
if (empty($aservice)) { |
|
210 |
continue; |
|
211 |
} |
|
212 |
if (isset($aservice['name']) && (strtolower($service) == strtolower($aservice['name']))) { |
|
213 |
if ($aservice['custom_php_service_status_command'] <> "") { |
|
214 |
eval("\$rc={$aservice['custom_php_service_status_command']};"); |
|
215 |
return $rc; |
|
216 |
} |
|
217 |
if (empty($aservice['executable'])) { |
|
232 | 218 |
return false; |
233 | 219 |
} |
220 |
if (is_process_running($aservice['executable'])) { |
|
221 |
return true; |
|
222 |
} |
|
223 |
return false; |
|
234 | 224 |
} |
235 | 225 |
} |
236 |
|
|
237 | 226 |
if (is_process_running($service)) { |
238 | 227 |
return true; |
239 | 228 |
} |
240 |
|
|
241 | 229 |
return false; |
242 | 230 |
} |
243 | 231 |
|
244 | 232 |
function get_services() { |
245 |
global $config; |
|
246 |
if (is_array($config['installedpackages']['service'])) { |
|
247 |
$services = $config['installedpackages']['service']; |
|
248 |
} else { |
|
249 |
$services = array(); |
|
233 |
$services = config_get_path('installedpackages/service', []); |
|
234 |
|
|
235 |
/* Clean up any empty services */ |
|
236 |
foreach ($services as $k => &$s) { |
|
237 |
if (empty($s)) { |
|
238 |
config_del_path('installedpackages/services/' . $k); |
|
239 |
unset($s); |
|
240 |
} |
|
250 | 241 |
} |
251 | 242 |
|
252 | 243 |
/* |
... | ... | |
261 | 252 |
$services[] = $pconfig; |
262 | 253 |
} |
263 | 254 |
|
264 |
if (isset($config['dnsmasq']['enable'])) {
|
|
255 |
if (config_path_enabled('dnsmasq')) {
|
|
265 | 256 |
$pconfig = array(); |
266 | 257 |
$pconfig['name'] = "dnsmasq"; |
267 | 258 |
$pconfig['description'] = gettext("DNS Forwarder"); |
... | ... | |
270 | 261 |
$services[] = $pconfig; |
271 | 262 |
} |
272 | 263 |
|
273 |
if (isset($config['unbound']['enable'])) {
|
|
264 |
if (config_path_enabled('unbound')) {
|
|
274 | 265 |
$pconfig = array(); |
275 | 266 |
$pconfig['name'] = "unbound"; |
276 | 267 |
$pconfig['description'] = gettext("DNS Resolver"); |
... | ... | |
279 | 270 |
$services[] = $pconfig; |
280 | 271 |
} |
281 | 272 |
|
282 |
if (isset($config['ipsec']['pkcs11support'])) {
|
|
273 |
if (config_path_enabled('ipsec', 'pkcs11support')) {
|
|
283 | 274 |
$pconfig = array(); |
284 | 275 |
$pconfig['name'] = "pcscd"; |
285 | 276 |
$pconfig['description'] = gettext("PC/SC Smart Card Daemon"); |
... | ... | |
288 | 279 |
$services[] = $pconfig; |
289 | 280 |
} |
290 | 281 |
|
291 |
init_config_arr(array('ntpd')); |
|
292 |
if ($config['ntpd']['enable'] != 'disabled') { |
|
282 |
if (config_get_path('ntpd/enable') != 'disabled') { |
|
293 | 283 |
$pconfig = array(); |
294 | 284 |
$pconfig['name'] = "ntpd"; |
295 | 285 |
$pconfig['description'] = gettext("NTP clock sync"); |
... | ... | |
305 | 295 |
$pconfig['status'] = get_service_status($pconfig); |
306 | 296 |
$services[] = $pconfig; |
307 | 297 |
|
308 |
if (is_array($config['captiveportal'])) { |
|
309 |
foreach ($config['captiveportal'] as $zone => $setting) { |
|
310 |
if (isset($setting['enable'])) { |
|
311 |
$pconfig = array(); |
|
312 |
$pconfig['name'] = "captiveportal"; |
|
313 |
$pconfig['zone'] = $zone; |
|
314 |
$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']); |
|
315 |
$services[] = $pconfig; |
|
316 |
} |
|
298 |
foreach (config_get_path('captiveportal', []) as $zone => $setting) { |
|
299 |
if (empty($setting)) { |
|
300 |
continue; |
|
301 |
} |
|
302 |
if (isset($setting['enable'])) { |
|
303 |
$pconfig = array(); |
|
304 |
$pconfig['name'] = "captiveportal"; |
|
305 |
$pconfig['zone'] = $zone; |
|
306 |
$pconfig['description'] = gettext("Captive Portal") . ": " . htmlspecialchars($setting['zone']); |
|
307 |
$services[] = $pconfig; |
|
317 | 308 |
} |
318 | 309 |
} |
319 | 310 |
|
320 | 311 |
$iflist = array(); |
321 | 312 |
$ifdescrs = get_configured_interface_list(); |
322 | 313 |
foreach ($ifdescrs as $if) { |
323 |
$oc = $config['interfaces'][$if];
|
|
324 |
if ($oc['if'] && (!link_interface_to_bridge($if))) {
|
|
314 |
if (config_get_path("interfaces/{$if}/if") &&
|
|
315 |
!link_interface_to_bridge($if)) {
|
|
325 | 316 |
$iflist[$if] = $if; |
326 | 317 |
} |
327 | 318 |
} |
328 | 319 |
|
329 |
if (isset($config['dhcrelay']['enable'])) {
|
|
320 |
if (config_path_enabled('dhcrelay')) {
|
|
330 | 321 |
$pconfig = array(); |
331 | 322 |
$pconfig['name'] = "dhcrelay"; |
332 | 323 |
$pconfig['description'] = gettext("DHCP Relay"); |
... | ... | |
335 | 326 |
$services[] = $pconfig; |
336 | 327 |
} |
337 | 328 |
|
338 |
if (isset($config['dhcrelay6']['enable'])) {
|
|
329 |
if (config_path_enabled('dhcrelay6')) {
|
|
339 | 330 |
$pconfig = array(); |
340 | 331 |
$pconfig['name'] = "dhcrelay6"; |
341 | 332 |
$pconfig['description'] = gettext("DHCPv6 Relay"); |
... | ... | |
354 | 345 |
} |
355 | 346 |
|
356 | 347 |
$gateways_arr = return_gateways_array(); |
357 |
if (is_array($gateways_arr)) { |
|
348 |
if (is_array($gateways_arr) && !empty($gateways_arr)) {
|
|
358 | 349 |
$pconfig = array(); |
359 | 350 |
$pconfig['name'] = "dpinger"; |
360 | 351 |
$pconfig['description'] = gettext("Gateway Monitoring Daemon"); |
... | ... | |
363 | 354 |
$services[] = $pconfig; |
364 | 355 |
} |
365 | 356 |
|
366 |
if (isset($config['snmpd']['enable'])) {
|
|
357 |
if (config_path_enabled('snmpd')) {
|
|
367 | 358 |
$pconfig = array(); |
368 | 359 |
$pconfig['name'] = "bsnmpd"; |
369 | 360 |
$pconfig['description'] = gettext("SNMP Service"); |
... | ... | |
372 | 363 |
$services[] = $pconfig; |
373 | 364 |
} |
374 | 365 |
|
375 |
if (is_array($config['igmpproxy']['igmpentry']) && (count($config['igmpproxy']['igmpentry']) > 0)) {
|
|
366 |
if (!empty(config_get_path('igmpproxy/igmpentry', []))) {
|
|
376 | 367 |
$pconfig = array(); |
377 | 368 |
$pconfig['name'] = "igmpproxy"; |
378 | 369 |
$pconfig['description'] = gettext("IGMP proxy"); |
... | ... | |
381 | 372 |
$services[] = $pconfig; |
382 | 373 |
} |
383 | 374 |
|
384 |
if (isset($config['installedpackages']['miniupnpd']) && $config['installedpackages']['miniupnpd']['config'][0]['enable']) {
|
|
375 |
if (config_path_enabled('installedpackages/miniupnpd/config/0')) {
|
|
385 | 376 |
$pconfig = array(); |
386 | 377 |
$pconfig['name'] = "miniupnpd"; |
387 | 378 |
$pconfig['description'] = gettext("UPnP Service"); |
... | ... | |
399 | 390 |
$services[] = $pconfig; |
400 | 391 |
} |
401 | 392 |
|
402 |
if (isset($config['system']['ssh']['enable'])) {
|
|
393 |
if (config_path_enabled('system/ssh')) {
|
|
403 | 394 |
$pconfig = array(); |
404 | 395 |
$pconfig['name'] = "sshd"; |
405 | 396 |
$pconfig['description'] = gettext("Secure Shell Daemon"); |
... | ... | |
409 | 400 |
} |
410 | 401 |
|
411 | 402 |
foreach (array('server', 'client') as $mode) { |
412 |
init_config_arr(array('openvpn')); |
|
413 |
if (is_array($config['openvpn']["openvpn-{$mode}"])) { |
|
414 |
foreach ($config['openvpn']["openvpn-{$mode}"] as $id => $setting) { |
|
415 |
if (!isset($setting['disable'])) { |
|
416 |
$pconfig = array(); |
|
417 |
$pconfig['name'] = "openvpn"; |
|
418 |
$pconfig['mode'] = $mode; |
|
419 |
$pconfig['id'] = $id; |
|
420 |
$pconfig['vpnid'] = $setting['vpnid']; |
|
421 |
$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']); |
|
422 |
$pconfig['enabled'] = is_service_enabled("openvpn"); |
|
423 |
$pconfig['status'] = get_service_status($pconfig); |
|
424 |
$services[] = $pconfig; |
|
425 |
} |
|
403 |
foreach (config_get_path("openvpn/openvpn-{$mode}", []) as $id => $setting) { |
|
404 |
if (!isset($setting['disable'])) { |
|
405 |
$pconfig = array(); |
|
406 |
$pconfig['name'] = "openvpn"; |
|
407 |
$pconfig['mode'] = $mode; |
|
408 |
$pconfig['id'] = $id; |
|
409 |
$pconfig['vpnid'] = $setting['vpnid']; |
|
410 |
$pconfig['description'] = gettext("OpenVPN") . " " . $mode . ": " . htmlspecialchars($setting['description']); |
|
411 |
$pconfig['enabled'] = is_service_enabled("openvpn"); |
|
412 |
$pconfig['status'] = get_service_status($pconfig); |
|
413 |
$services[] = $pconfig; |
|
426 | 414 |
} |
427 | 415 |
} |
428 | 416 |
} |
... | ... | |
499 | 487 |
} |
500 | 488 |
|
501 | 489 |
function get_pkg_descr($package_name) { |
502 |
global $config; |
|
503 |
if (is_array($config['installedpackages']['package'])) { |
|
504 |
foreach ($config['installedpackages']['package'] as $pkg) { |
|
505 |
if ($pkg['name'] == $package_name) { |
|
506 |
return $pkg['descr']; |
|
507 |
} |
|
490 |
foreach (config_get_path('installedpackages/package', []) as $pkg) { |
|
491 |
if ($pkg['name'] == $package_name) { |
|
492 |
return $pkg['descr']; |
|
508 | 493 |
} |
509 | 494 |
} |
510 | 495 |
return gettext("Not available."); |
... | ... | |
518 | 503 |
break; |
519 | 504 |
case "captiveportal": |
520 | 505 |
$running = is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal.pid"); |
521 |
if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) {
|
|
506 |
if (config_path_enabled("captiveportal/{$service['zone']}", 'httpslogin')) {
|
|
522 | 507 |
$running = $running && is_pid_running("{$g['varrun_path']}/nginx-{$service['zone']}-CaptivePortal-SSL.pid"); |
523 | 508 |
} |
524 | 509 |
break; |
... | ... | |
855 | 840 |
break; |
856 | 841 |
case 'openvpn': |
857 | 842 |
$vpnmode = htmlspecialchars($extras['vpnmode']); |
858 |
if ($vpnmode == "server" || $vpnmode == "client") {
|
|
843 |
if (($vpnmode == "server") || ($vpnmode == "client")) {
|
|
859 | 844 |
$id = htmlspecialchars($extras['id']); |
860 | 845 |
$configfile = "{$g['openvpn_base']}/{$vpnmode}{$id}/config.ovpn"; |
861 | 846 |
if (file_exists($configfile)) { |
Also available in: Unified diff
service-utils PHP8.1 fixes. Issue #13446