diff options
author | Phil Davis <phil.davis@inf.org> | 2015-03-03 00:11:10 +0545 |
---|---|---|
committer | Phil Davis <phil.davis@inf.org> | 2015-03-03 00:11:10 +0545 |
commit | 61e047a50339da4a0b6a9202f163950573e8a578 (patch) | |
tree | 138a33434240f27f42e88483ed66cd1743cadf2a /etc/inc/service-utils.inc | |
parent | 8f5f40c9fca0bfb637589b4c86e53bfb80753c87 (diff) | |
download | pfsense-61e047a50339da4a0b6a9202f163950573e8a578.zip pfsense-61e047a50339da4a0b6a9202f163950573e8a578.tar.gz |
Code style etc in r s
Diffstat (limited to 'etc/inc/service-utils.inc')
-rw-r--r-- | etc/inc/service-utils.inc | 210 |
1 files changed, 120 insertions, 90 deletions
diff --git a/etc/inc/service-utils.inc b/etc/inc/service-utils.inc index a78446e..8323924 100644 --- a/etc/inc/service-utils.inc +++ b/etc/inc/service-utils.inc @@ -1,36 +1,35 @@ <?php /****h* pfSense/service-utils - * NAME - * service-utils.inc - Service facility - * DESCRIPTION - * This file contains various functions used by the pfSense service facility. - * HISTORY - * $Id$ - ****** - * - * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com) - * All rights reserved. - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * - * 1. Redistributions of source code must retain the above copyright notice, - * this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY - * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE - * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, - * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF - * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS - * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN - * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - * POSSIBILITY OF SUCH DAMAGE. - * + NAME + service-utils.inc - Service facility + DESCRIPTION + This file contains various functions used by the pfSense service facility. + HISTORY + $Id$ + + Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com) + All rights reserved. + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, + INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY + AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, + OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + */ /* @@ -51,11 +50,13 @@ function write_rcfile($params) { safe_mkdir(RCFILEPREFIX); $rcfile_fullname = RCFILEPREFIX . $params['file']; - if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) + if (!file_exists($rcfile_fullname) && !is_link($rcfile_fullname) && !touch($rcfile_fullname)) { return false; + } - if (!is_writable($rcfile_fullname) || empty($params['start'])) + if (!is_writable($rcfile_fullname) || empty($params['start'])) { return false; + } $towrite = "#!/bin/sh\n"; $towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n"; @@ -64,9 +65,9 @@ function write_rcfile($params) { $towrite .= "rc_start() {\n"; $towrite .= "\t{$params['start']}\n"; $towrite .= "}\n\n"; - if(!empty($params['stop'])) { + if (!empty($params['stop'])) { $tokill =& $params['stop']; - } else if(!empty($params['executable'])) { + } else if (!empty($params['executable'])) { /* just nuke the executable */ $tokill = "/usr/bin/killall " . escapeshellarg($params['executable']); } else { @@ -90,23 +91,25 @@ function write_rcfile($params) { function start_service($name) { global $config; - if (empty($name)) + if (empty($name)) { return; + } if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) { - foreach($config['installedpackages']['service'] as $service) { - if(strtolower($service['name']) == strtolower($name)) { - if($service['rcfile']) { + foreach ($config['installedpackages']['service'] as $service) { + if (strtolower($service['name']) == strtolower($name)) { + if ($service['rcfile']) { $prefix = RCFILEPREFIX; if (!empty($service['prefix'])) { $prefix =& $service['prefix']; } - if(file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { + if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { mwexec_bg("{$prefix}{$service['rcfile']} start"); } } - if (!empty($service['startcmd'])) + if (!empty($service['startcmd'])) { eval($service['startcmd']); + } break; } } @@ -116,24 +119,26 @@ function start_service($name) { function stop_service($name) { global $config; - if (empty($name)) + if (empty($name)) { return; + } if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) { - foreach($config['installedpackages']['service'] as $service) { - if(strtolower($service['name']) == strtolower($name)) { - if($service['rcfile']) { + foreach ($config['installedpackages']['service'] as $service) { + if (strtolower($service['name']) == strtolower($name)) { + if ($service['rcfile']) { $prefix = RCFILEPREFIX; - if(!empty($service['prefix'])) { + if (!empty($service['prefix'])) { $prefix =& $service['prefix']; } - if(file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { + if (file_exists("{$prefix}{$service['rcfile']}") || is_link("{$prefix}{$service['rcfile']}")) { mwexec("{$prefix}{$service['rcfile']} stop"); } return; } - if (!empty($service['stopcmd'])) + if (!empty($service['stopcmd'])) { eval($service['stopcmd']); + } break; } @@ -144,16 +149,17 @@ function stop_service($name) { function restart_service($name) { global $config; - if (empty($name)) + if (empty($name)) { return; + } stop_service($name); start_service($name); if (is_array($config['installedpackages']) && is_array($config['installedpackages']['service'])) { - foreach($config['installedpackages']['service'] as $service) { - if(strtolower($service['name']) == strtolower($name)) { - if($service['restartcmd']) { + foreach ($config['installedpackages']['service'] as $service) { + if (strtolower($service['name']) == strtolower($name)) { + if ($service['restartcmd']) { eval($service['restartcmd']); } break; @@ -163,34 +169,38 @@ function restart_service($name) { } function is_pid_running($pidfile) { - if (!file_exists($pidfile)) + if (!file_exists($pidfile)) { return false; + } return (isvalidpid($pidfile)); } function is_dhcp_running($interface) { $status = find_dhclient_process($interface); - if($status != 0) + if ($status != 0) { return true; + } return false; } function restart_service_if_running($service) { global $config; - if(is_service_running($service)) + if (is_service_running($service)) { restart_service($service); + } return; } function is_service_enabled($service_name) { global $config; - if ($service_name == "") + if ($service_name == "") { return false; + } if (is_array($config['installedpackages'])) { if (isset($config['installedpackages'][$service_name]['config'][0]['enable']) && - ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) || - ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) { + ((empty($config['installedpackages'][$service_name]['config'][0]['enable'])) || + ($config['installedpackages'][$service_name]['config'][0]['enable'] === 'off'))) { return false; } } @@ -200,38 +210,42 @@ function is_service_enabled($service_name) { function is_service_running($service, $ps = "") { global $config; - if(is_array($config['installedpackages']['service'])) { - foreach($config['installedpackages']['service'] as $aservice) { - if(strtolower($service) == strtolower($aservice['name'])) { + if (is_array($config['installedpackages']['service'])) { + foreach ($config['installedpackages']['service'] as $aservice) { + if (strtolower($service) == strtolower($aservice['name'])) { if ($aservice['custom_php_service_status_command'] <> "") { eval("\$rc={$aservice['custom_php_service_status_command']};"); return $rc; } - if(empty($aservice['executable'])) + if (empty($aservice['executable'])) { return false; - if (is_process_running($aservice['executable'])) + } + if (is_process_running($aservice['executable'])) { return true; + } return false; } } } - if (is_process_running($service)) + if (is_process_running($service)) { return true; + } return false; } function get_services() { global $config; - if (is_array($config['installedpackages']['service'])) + if (is_array($config['installedpackages']['service'])) { $services = $config['installedpackages']['service']; - else + } else { $services = array(); + } - /* Add services that are in the base. - * + /* + * Add services that are in the base. */ if (is_radvd_enabled()) { $pconfig = array(); @@ -275,8 +289,9 @@ function get_services() { $ifdescrs = get_configured_interface_list(); foreach ($ifdescrs as $if) { $oc = $config['interfaces'][$if]; - if ($oc['if'] && (!link_interface_to_bridge($if))) + if ($oc['if'] && (!link_interface_to_bridge($if))) { $iflist[$if] = $if; + } } if (isset($config['dhcrelay']['enable'])) { @@ -377,40 +392,48 @@ function get_services() { function find_service_by_name($name) { $services = get_services(); - foreach ($services as $service) - if ($service["name"] == $name) + foreach ($services as $service) { + if ($service["name"] == $name) { return $service; + } + } return array(); } function find_service_by_openvpn_vpnid($vpnid) { $services = get_services(); - foreach ($services as $service) - if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) + foreach ($services as $service) { + if (($service["name"] == "openvpn") && isset($service["vpnid"]) && ($service["vpnid"] == $vpnid)) { return $service; + } + } return array(); } function find_service_by_cp_zone($zone) { $services = get_services(); - foreach ($services as $service) - if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) + foreach ($services as $service) { + if (($service["name"] == "captiveportal") && isset($service["zone"]) && ($service["zone"] == $zone)) { return $service; + } + } return array(); } function service_name_compare($a, $b) { - if (strtolower($a['name']) == strtolower($b['name'])) + if (strtolower($a['name']) == strtolower($b['name'])) { return 0; + } return (strtolower($a['name']) < strtolower($b['name'])) ? -1 : 1; } function get_pkg_descr($package_name) { global $config; if (is_array($config['installedpackages']['package'])) { - foreach($config['installedpackages']['package'] as $pkg) { - if($pkg['name'] == $package_name) + foreach ($config['installedpackages']['package'] as $pkg) { + if ($pkg['name'] == $package_name) { return $pkg['descr']; + } } } return gettext("Not available."); @@ -424,8 +447,9 @@ function get_service_status($service) { break; case "captiveportal": $running = is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal.pid"); - if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) + if (isset($config['captiveportal'][$service['zone']]['httpslogin'])) { $running = $running && is_pid_running("{$g['varrun_path']}/lighty-{$service['zone']}-CaptivePortal-SSL.pid"); + } break; case "vhosts-http": $running = is_pid_running("{$g['varrun_path']}/vhosts-http.pid"); @@ -445,21 +469,23 @@ function get_service_status($service) { function get_service_status_icon($service, $withtext = true, $smallicon = false) { global $g; $output = ""; - if(get_service_status($service)) { + if (get_service_status($service)) { $statustext = gettext("Running"); $output .= "<img style=\"vertical-align:middle\" title=\"" . sprintf(gettext("%s Service is"),$service["name"]) . " {$statustext}\" src=\"/themes/" . $g["theme"] . "/images/icons/"; $output .= ($smallicon) ? "icon_pass.gif" : "icon_service_running.gif"; $output .= "\" alt=\"status\" /> "; - if ($withtext) + if ($withtext) { $output .= " " . $statustext; + } } else { $service_enabled = is_service_enabled($service['name']); $statustext = ($service_enabled) ? gettext("Stopped") : gettext("Disabled"); $output .= "<img style=\"vertical-align:middle\" title=\"" . sprintf(gettext("%s Service is"),$service["name"]) . " {$statustext}\" src=\"/themes/" . $g["theme"] . "/images/icons/"; $output .= ($smallicon) ? "icon_block.gif" : "icon_service_stopped.gif"; $output .= "\" alt=\"status\" /> "; - if ($withtext) + if ($withtext) { $output .= " <font color=\"white\">{$statustext}</font>"; + } } return $output; } @@ -468,7 +494,7 @@ function get_service_control_links($service, $addname = false) { global $g; $output = ""; $stitle = ($addname) ? $service['name'] . " " : ""; - if(get_service_status($service)) { + if (get_service_status($service)) { switch ($service['name']) { case "openvpn": $output .= "<a href='status_services.php?mode=restartservice&service={$service['name']}&vpnmode={$service['mode']}&id={$service['vpnid']}'>"; @@ -502,18 +528,20 @@ function get_service_control_links($service, $addname = false) { $output .= "<a href='status_services.php?mode=startservice&service={$service['name']}&zone={$service['zone']}'>"; break; default: - if ($service_enabled) + if ($service_enabled) { $output .= "<a href='status_services.php?mode=startservice&service={$service['name']}'>"; + } } - if ($service_enabled) + if ($service_enabled) { $output .= "<img style=\"vertical-align:middle\" title='" . sprintf(gettext("Start %sService"),$stitle) . "' border='0' src='./themes/".$g['theme']."/images/icons/icon_service_start.gif' alt='start' /></a>\n"; + } } return $output; } function service_control_start($name, $extras) { global $g; - switch($name) { + switch ($name) { case 'radvd': services_radvd_configure(); break; @@ -563,8 +591,9 @@ function service_control_start($name, $extras) { if (($vpnmode == "server") || ($vpnmode == "client")) { $id = isset($extras['vpnid']) ? htmlspecialchars($extras['vpnid']) : htmlspecialchars($extras['id']); $configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf"; - if (file_exists($configfile)) + if (file_exists($configfile)) { openvpn_restart_by_vpnid($vpnmode, $id); + } } break; case 'relayd': @@ -578,7 +607,7 @@ function service_control_start($name, $extras) { } function service_control_stop($name, $extras) { global $g; - switch($name) { + switch ($name) { case 'radvd': killbypid("{$g['varrun_path']}/radvd.pid"); break; @@ -649,7 +678,7 @@ function service_control_stop($name, $extras) { function service_control_restart($name, $extras) { global $g; - switch($name) { + switch ($name) { case 'radvd': services_radvd_configure(); break; @@ -702,8 +731,9 @@ function service_control_restart($name, $extras) { if ($vpnmode == "server" || $vpnmode == "client") { $id = htmlspecialchars($extras['id']); $configfile = "{$g['varetc_path']}/openvpn/{$vpnmode}{$id}.conf"; - if (file_exists($configfile)) + if (file_exists($configfile)) { openvpn_restart_by_vpnid($vpnmode, $id); + } } break; case 'relayd': |