Project

General

Profile

Download (5.96 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
define("RCFILEPREFIX", "/usr/local/etc/rc.d/");
42
function write_rcfile($params) {
43
	global $g;
44

    
45
	$rcfile_fullname = RCFILEPREFIX . $params['file'];
46
	if (!file_exists($rcfile_fullname) && !touch($rcfile_fullname))
47
		return false;
48

    
49
	if (!is_writable($rcfile_fullname) || 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($rcfile_fullname, $towrite);
76
	@chmod("{$rcfile_fullname}", 0755);
77

    
78
	return;
79
}
80

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

    
84
	if (empty($name))
85
		return;
86

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

    
91
	$rcfile_fullname = RCFILEPREFIX . $name . '.sh';
92
	if(file_exists($rcfile_fullname)) {
93
		mwexec_bg("/bin/sh {$rcfile_fullname} 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

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

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

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

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

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

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

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

    
173
function is_pid_running($pidfile) {
174
	if (!file_exists($pidfile))
175
		return false;
176
	return isvalidpid($pidfile);
177
}
178

    
179
function is_dhcp_running($interface) {
180
	$status = find_dhclient_process($interface);
181
	if($status <> "")
182
		return true;
183
	return false;
184
}
185

    
186
function restart_service_if_running($service) {
187
	global $config;
188
	if(is_service_running($service))
189
		restart_service($service);
190
	return;
191
}
192

    
193
function is_service_running($service, $ps = "") {
194
	global $config;
195

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

    
208
				return false;
209
			}
210
		}
211
	}
212

    
213
	if (is_process_running($service))
214
		return true;
215

    
216
	return false;
217
}
218

    
219
?>
(43-43/61)