Project

General

Profile

Download (5.75 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

    
45
	if (!is_writable("{$rcfileprefix}{$params['file']}") || empty($params['start']))
46
		return false;
47
	$towrite = "#!/bin/sh\n";
48
	$towrite .= "# This file was automatically generated\n# by the {$g['product_website']} service handler.\n\n";
49

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

    
67
	/* begin rcfile logic */
68
	$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";
69

    
70
	file_put_contents("{$rcfileprefix}{$params['file']}", $towrite);
71
	@chmod("{$rcfileprefix}{$params['file']}", 0755);
72

    
73
	return;
74
}
75

    
76
function start_service($name) {
77
	global $config;
78

    
79
	/* make sure service is stopped before starting */
80
	stop_service($name);
81
	sleep(2);
82

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

    
107
function stop_service($name) {
108
	global $config;
109

    
110
	if ($config['installedpackages']['service']) {
111
		foreach($config['installedpackages']['service'] as $service) {
112
			if(strtolower($service['name']) == strtolower($name)) {
113
				if($service['rcfile']) {
114
					$prefix = $rcfileprefix;
115
					if(!empty($service['prefix'])) {
116
						$prefix =& $service['prefix'];
117
					}
118
					mwexec_bg("{$prefix}{$service['rcfile']} stop");
119
				}
120
				if (!empty($service['stopcmd']))
121
					eval($service['stopcmd']);
122

    
123
				if(!($service['rcfile'] or $service['stopcmd'])) {
124
					if(is_process_running("{$service['executable']}"))
125
						mwexec_bg("/usr/bin/killall {$service['executable']}");
126
					return;
127
				}
128
				break;
129
			}
130
		}
131
	}
132
	/* finally if we get here lets simply kill the service name */
133
	if(is_process_running("{$name}"))
134
		mwexec_bg("/usr/bin/killall {$name}");
135
}
136

    
137
function restart_service($name) {
138
	global $config;
139

    
140
	stop_service($name);
141
	start_service($name);
142

    
143
	if($config['installedpackages']['service']) {
144
		foreach($config['installedpackages']['service'] as $service) {
145
			if(strtolower($service['name']) == strtolower($name)) {
146
				if($service['restartcmd']) {
147
					eval($service['restartcmd']);
148
				}
149
				break;
150
			}
151
		}
152
	}
153
}
154

    
155
function is_pid_running($pidfile) {
156
	if (!file_exists($pidfile))
157
		return false;
158
	$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
159

    
160
	return (!empty($running));
161
}
162

    
163
function is_dhcp_running($interface) {
164
	$status = find_dhclient_process($interface);
165
	if($status <> "")
166
		return true;
167
	return false;
168
}
169

    
170
function restart_service_if_running($service) {
171
	global $config;
172
	if(is_service_running($service))
173
		restart_service($service);
174
	return;
175
}
176

    
177
function is_service_running($service, $ps = "") {
178
	global $config;
179

    
180
	if(is_array($config['installedpackages']['service'])) {
181
		foreach($config['installedpackages']['service'] as $aservice) {
182
			if(strtolower($service) == strtolower($aservice['name'])) {
183
				if ($aservice['custom_php_service_status_command'] <> "") {
184
					eval("\$rc={$aservice['custom_php_service_status_command']};");
185
					return $rc;
186
				}
187
				if(empty($aservice['executable']))
188
					return false;
189
				if (is_process_running($aservice['executable']))
190
					return true;
191

    
192
				return false;
193
			}
194
		}
195
	}
196

    
197
	if (is_process_running($service))
198
		return true;
199

    
200
	return false;
201
}
202

    
203
?>
(36-36/54)