Project

General

Profile

Download (6.08 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/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
	if (empty($name))
86
		return;
87

    
88
	/* make sure service is stopped before starting */
89
	stop_service($name);
90
	sleep(2);
91

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

    
116
function stop_service($name) {
117
	global $config;
118
	global $rcfileprefix;
119

    
120
	if (empty($name))
121
		return;
122

    
123
	if ($config['installedpackages']['service']) {
124
		foreach($config['installedpackages']['service'] as $service) {
125
			if(strtolower($service['name']) == strtolower($name)) {
126
				if($service['rcfile']) {
127
					$prefix = $rcfileprefix;
128
					if(!empty($service['prefix'])) {
129
						$prefix =& $service['prefix'];
130
					}
131
					if(file_exists("{$prefix}{$service['rcfile']}")) {
132
						mwexec("{$prefix}{$service['rcfile']} stop");
133
					}
134
					return;
135
				}
136
				if (!empty($service['stopcmd']))
137
					eval($service['stopcmd']);
138

    
139
				if(!($service['rcfile'] or $service['stopcmd'])) {
140
					if(is_process_running("{$service['executable']}"))
141
						mwexec("/usr/bin/killall {$service['executable']}");
142
					return;
143
				}
144
				break;
145
			}
146
		}
147
	}
148
	/* finally if we get here lets simply kill the service name */
149
	if(is_process_running("{$name}"))
150
		mwexec("/usr/bin/killall {$name}");
151
}
152

    
153
function restart_service($name) {
154
	global $config;
155

    
156
	if (empty($name))
157
		return;
158

    
159
	stop_service($name);
160
	start_service($name);
161

    
162
	if($config['installedpackages']['service']) {
163
		foreach($config['installedpackages']['service'] as $service) {
164
			if(strtolower($service['name']) == strtolower($name)) {
165
				if($service['restartcmd']) {
166
					eval($service['restartcmd']);
167
				}
168
				break;
169
			}
170
		}
171
	}
172
}
173

    
174
function is_pid_running($pidfile) {
175
	if (!file_exists($pidfile))
176
		return false;
177
	$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
178

    
179
	return (!empty($running));
180
}
181

    
182
function is_dhcp_running($interface) {
183
	$status = find_dhclient_process($interface);
184
	if($status <> "")
185
		return true;
186
	return false;
187
}
188

    
189
function restart_service_if_running($service) {
190
	global $config;
191
	if(is_service_running($service))
192
		restart_service($service);
193
	return;
194
}
195

    
196
function is_service_running($service, $ps = "") {
197
	global $config;
198

    
199
	if(is_array($config['installedpackages']['service'])) {
200
		foreach($config['installedpackages']['service'] as $aservice) {
201
			if(strtolower($service) == strtolower($aservice['name'])) {
202
				if ($aservice['custom_php_service_status_command'] <> "") {
203
					eval("\$rc={$aservice['custom_php_service_status_command']};");
204
					return $rc;
205
				}
206
				if(empty($aservice['executable']))
207
					return false;
208
				if (is_process_running($aservice['executable']))
209
					return true;
210

    
211
				return false;
212
			}
213
		}
214
	}
215

    
216
	if (is_process_running($service))
217
		return true;
218

    
219
	return false;
220
}
221

    
222
?>
(46-46/65)