Project

General

Profile

Download (5.94 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-2006 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
/*
37
	pfSense_BUILDER_BINARIES:	/bin/pkill /bin/pgrep /bin/sh /usr/bin/killall
38
	pfSense_MODULE:	utils
39
*/
40

    
41
$rcfileprefix = "/usr/local/etc/rc.d/";
42
function write_rcfile($params) {
43
	global $g;
44
	global $rcfileprefix;
45

    
46
	if (!file_exists("{$rcfileprefix}{$params['file']}") && !touch("{$rcfileprefix}{$params['file']}"))
47
		return false;
48

    
49
	if (!is_writable("{$rcfileprefix}{$params['file']}") || empty($params['start']))
50
		return false;
51

    
52
	$towrite = "#!/bin/sh\n";
53
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
54

    
55
	/* write our rc functions */
56
	$towrite .= "rc_start() {\n";
57
	$towrite .= "\t{$params['start']}\n";
58
	$towrite .= "}\n\n";
59
	if(!empty($params['stop'])) {
60
		$tokill =& $params['stop'];
61
	} else if(!empty($params['executable'])) {
62
		/* just nuke the executable */
63
		$tokill = "/usr/bin/killall {$params['executable']}";
64
	} else {
65
		/* make an educated guess (bad) */
66
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
67
	}
68
	$towrite .= "rc_stop() {\n";
69
	$towrite .= "\t{$tokill}\n";
70
	$towrite .= "}\n\n";
71

    
72
	/* begin rcfile logic */
73
	$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";
74

    
75
	file_put_contents("{$rcfileprefix}{$params['file']}", $towrite);
76
	@chmod("{$rcfileprefix}{$params['file']}", 0755);
77

    
78
	return;
79
}
80

    
81
function start_service($name) {
82
	global $config;
83
	global $rcfileprefix;
84

    
85
	/* make sure service is stopped before starting */
86
	stop_service($name);
87
	sleep(2);
88

    
89
	if(file_exists("{$rcfileprefix}{$name}.sh")) {
90
		mwexec_bg("/bin/sh {$rcfileprefix}{$name}.sh start");
91
		return;
92
	}
93
	if($config['installedpackages']['service']) {
94
		foreach($config['installedpackages']['service'] as $service) {
95
			if(strtolower($service['name']) == strtolower($name)) {
96
				if($service['rcfile']) {
97
					$prefix = $rcfileprefix;
98
					if (!empty($service['prefix'])) {
99
						$prefix =& $service['prefix'];
100
					}
101
					if(file_exists("{$prefix}{$service['rcfile']}")) {
102
						mwexec_bg("{$prefix}{$service['rcfile']} start");
103
					}
104
				}
105
				if (!empty($service['startcmd']))
106
					eval($service['startcmd']);
107
				break;
108
			}
109
		}
110
	}
111
}
112

    
113
function stop_service($name) {
114
	global $config;
115
	global $rcfileprefix;
116

    
117
	if ($config['installedpackages']['service']) {
118
		foreach($config['installedpackages']['service'] as $service) {
119
			if(strtolower($service['name']) == strtolower($name)) {
120
				if($service['rcfile']) {
121
					$prefix = $rcfileprefix;
122
					if(!empty($service['prefix'])) {
123
						$prefix =& $service['prefix'];
124
					}
125
					mwexec_bg("{$prefix}{$service['rcfile']} stop");
126
				}
127
				if (!empty($service['stopcmd']))
128
					eval($service['stopcmd']);
129

    
130
				if(!($service['rcfile'] or $service['stopcmd'])) {
131
					if(is_process_running("{$service['executable']}"))
132
						mwexec_bg("/usr/bin/killall {$service['executable']}");
133
					return;
134
				}
135
				break;
136
			}
137
		}
138
	}
139
	/* finally if we get here lets simply kill the service name */
140
	if(is_process_running("{$name}"))
141
		mwexec_bg("/usr/bin/killall {$name}");
142
}
143

    
144
function restart_service($name) {
145
	global $config;
146

    
147
	stop_service($name);
148
	start_service($name);
149

    
150
	if($config['installedpackages']['service']) {
151
		foreach($config['installedpackages']['service'] as $service) {
152
			if(strtolower($service['name']) == strtolower($name)) {
153
				if($service['restartcmd']) {
154
					eval($service['restartcmd']);
155
				}
156
				break;
157
			}
158
		}
159
	}
160
}
161

    
162
function is_pid_running($pidfile) {
163
	if (!file_exists($pidfile))
164
		return false;
165
	$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
166

    
167
	return (!empty($running));
168
}
169

    
170
function is_dhcp_running($interface) {
171
	$status = find_dhclient_process($interface);
172
	if($status <> "")
173
		return true;
174
	return false;
175
}
176

    
177
function restart_service_if_running($service) {
178
	global $config;
179
	if(is_service_running($service))
180
		restart_service($service);
181
	return;
182
}
183

    
184
function is_service_running($service, $ps = "") {
185
	global $config;
186

    
187
	if(is_array($config['installedpackages']['service'])) {
188
		foreach($config['installedpackages']['service'] as $aservice) {
189
			if(strtolower($service) == strtolower($aservice['name'])) {
190
				if ($aservice['custom_php_service_status_command'] <> "") {
191
					eval("\$rc={$aservice['custom_php_service_status_command']};");
192
					return $rc;
193
				}
194
				if(empty($aservice['executable']))
195
					return false;
196
				if (is_process_running($aservice['executable']))
197
					return true;
198

    
199
				return false;
200
			}
201
		}
202
	}
203

    
204
	if (is_process_running($service))
205
		return true;
206

    
207
	return false;
208
}
209

    
210
?>
(36-36/54)