Project

General

Profile

Download (6.06 KB) Statistics
| Branch: | Tag: | Revision:
1
<?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
 * Copyright (C) 2005 Colin Smith (ethethlay@gmail.com)
12
 * 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
function write_rcfile($params) {
37
	$fileprefix = "/usr/local/etc/rc.d/";
38
	if(!(is_writable($fileprefix . $params['file']) or $params['start'])) return false;
39
	$towrite .= "#!/bin/sh\n# This file was automatically generated\n# by the pfSense service handler.\n\n";
40
	/* write our rc functions */
41
	$towrite .= "rc_start() {\n\t" . $params['start'] . "\n}\n\n";
42
	if($params['stop']) {
43
		$tokill =& $params['stop'];
44
	} elseif($params['executable']) {
45
		/* just nuke the executable */
46
		$tokill = "/usr/bin/killall {$params['executable']}";
47
	} else {
48
		/* make an educated guess (bad) */
49
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
50
	}
51
	$towrite .= "rc_stop() {\n\t" . $tokill . "\n}\n\n";
52
	
53
	/* begin rcfile logic */
54
	$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";
55
	$fout = fopen($fileprefix . $params['file'], "w");
56
	fwrite($fout, $towrite);
57
	fclose($fout);
58
	chmod($fileprefix . $params['file'], 0755);
59
	return;
60
}
61

    
62
function start_service($name) {
63
	global $config;
64
	if($config['installedpackages']['service']) {
65
		foreach($config['installedpackages']['service'] as $service) {
66
			if(strtolower($service['name']) == strtolower($name)) {
67
				if($service['rcfile']) {
68
					if($service['prefix']) {
69
						$prefix =& $service['prefix'];
70
					} else {
71
						$prefix = "/usr/local/etc/rc.d/";
72
					}
73
					mwexec_bg($prefix . $service['rcfile'] . " start");
74
				}
75
				if($service['startcmd']) {
76
					eval($service['startcmd']);
77
				}
78
				break;
79
			}
80
		}
81
	}
82
}
83

    
84
function stop_service($name) {
85
        global $config;
86
        if($config['installedpackages']['service']) {
87
                foreach($config['installedpackages']['service'] as $service) {
88
                        if(strtolower($service['name']) == strtolower($name)) {
89
                                if($service['rcfile']) {
90
                                        if($service['prefix']) {
91
                                                $prefix =& $service['prefix'];
92
                                        } else {
93
                                                $prefix = "/usr/local/etc/rc.d/";
94
                                        }
95
                                        mwexec_bg($prefix . $service['rcfile'] . " stop");
96
                                }
97
                                if($service['stopcmd']) {
98
                                        eval($service['stopcmd']);
99
                                }
100
				if(!($service['rcfile'] or $service['stopcmd'])) {
101
					mwexec_bg("/usr/bin/killall {$service['executable']}");
102
				}
103
                                break;
104
                        }
105
                }
106
        }
107
}
108

    
109
function restart_service($name) {
110
    global $config;
111
	stop_service($name);
112
	start_service($name);
113
        if($config['installedpackages']['service']) {
114
                foreach($config['installedpackages']['service'] as $service) {
115
                        if(strtolower($service['name']) == strtolower($name)) {
116
                                if($service['restartcmd']) {
117
                                        eval($service['restartcmd']);
118
                                }
119
                                break;
120
                        }
121
                }
122
        }
123
}
124

    
125
function is_process_running($process) {
126
    $status = `/bin/ps ax | /usr/bin/grep {$process} | grep -v grep | wc -l`;
127
    if($status > 0) return 1;
128
    return 0;
129
}
130

    
131
function is_dhcp_running($interface) {
132
    if(filter_translate_type_to_real_interface($interface) <> "")
133
	$interface = filter_translate_type_to_real_interface($interface);
134
    $status = `/bin/ps ax | /usr/bin/grep dhclient | grep $interface | grep -v grep | wc -l`;
135
    if($status > 0) return 1;
136
    return 0;
137
}
138

    
139
function restart_service_if_running($service) {
140
	global $config;
141
	if(is_service_running($service)) 
142
		restart_service($service);
143
	return;
144
}
145

    
146
function is_service_running($service, $ps = "") {
147
	global $config;
148
	if(!$ps) {
149
		exec("/bin/ps ax | awk '{ print $5 }'", $psout);
150
		array_shift($psout);
151
		foreach($psout as $line) {
152
		        $ps[] = trim(array_pop(explode(' ', array_pop(explode('/', $line)))));
153
		}
154
	}
155
	if($config['installedpackages']['service']) {
156
                foreach($config['installedpackages']['service'] as $aservice) {
157
                        if(strtolower($service) == strtolower($aservice['name'])) {
158
				if(!$aservice['executable']) return false;
159
				if(in_array($aservice['executable'], $ps)) {
160
					return true;
161
				} else {
162
					return false;
163
				}
164
				break;
165
                        }
166
                }
167
        }
168
	if(is_process_running($service))
169
		return true;
170
	return false;
171
}
(14-14/23)