Project

General

Profile

Download (5.93 KB) Statistics
| Branch: | Tag: | Revision:
1 c83068f6 Colin Smith
<?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 0e16b9ca Scott Ullrich
 * Copyright (C) 2005-2006 Colin Smith (ethethlay@gmail.com)
12 c83068f6 Colin Smith
 * 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 523855b0 Scott Ullrich
/*
37 05c4bfa0 Ermal
	pfSense_BUILDER_BINARIES:	/bin/pgrep /bin/sh /usr/bin/killall
38 523855b0 Scott Ullrich
	pfSense_MODULE:	utils
39
*/
40
41 ba8495f0 Ermal
$rcfileprefix = "/usr/local/etc/rc.d/";
42 c83068f6 Colin Smith
function write_rcfile($params) {
43 c2151973 Ermal
	global $g;
44 af799b48 Renato Botelho
	global $rcfileprefix;
45
46
	if (!file_exists("{$rcfileprefix}{$params['file']}") && !touch("{$rcfileprefix}{$params['file']}"))
47
		return false;
48 c2151973 Ermal
49 ba8495f0 Ermal
	if (!is_writable("{$rcfileprefix}{$params['file']}") || empty($params['start']))
50
		return false;
51 af799b48 Renato Botelho
52 ba8495f0 Ermal
	$towrite = "#!/bin/sh\n";
53 4dddf8c3 Warren Baker
	$towrite .= "# This file was automatically generated\n# by the {$g['product_name']} service handler.\n\n";
54 ba8495f0 Ermal
55 c83068f6 Colin Smith
	/* write our rc functions */
56 ba8495f0 Ermal
	$towrite .= "rc_start() {\n";
57
	$towrite .= "\t{$params['start']}\n";
58
	$towrite .= "}\n\n";
59
	if(!empty($params['stop'])) {
60 c83068f6 Colin Smith
		$tokill =& $params['stop'];
61 ba8495f0 Ermal
	} else if(!empty($params['executable'])) {
62 a8dbad89 Colin Smith
		/* just nuke the executable */
63 dc312bf5 Colin Smith
		$tokill = "/usr/bin/killall {$params['executable']}";
64 c83068f6 Colin Smith
	} else {
65 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
66 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
67
	}
68 ba8495f0 Ermal
	$towrite .= "rc_stop() {\n";
69
	$towrite .= "\t{$tokill}\n";
70
	$towrite .= "}\n\n";
71 4685e464 Scott Ullrich
72 c83068f6 Colin Smith
	/* begin rcfile logic */
73 ba8495f0 Ermal
	$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 c83068f6 Colin Smith
	return;
79
}
80 31f346a8 Colin Smith
81 58826c73 Colin Smith
function start_service($name) {
82 669e1adb Bill Marquette
	global $config;
83 af799b48 Renato Botelho
	global $rcfileprefix;
84 ba8495f0 Ermal
85 2523de53 Scott Ullrich
	/* make sure service is stopped before starting */
86
	stop_service($name);
87 34f7437f Scott Ullrich
	sleep(2);
88 ba8495f0 Ermal
89
	if(file_exists("{$rcfileprefix}{$name}.sh")) {
90 aeebce1b Erik Fonnesbeck
		mwexec_bg("/bin/sh {$rcfileprefix}{$name}.sh start");
91 4685e464 Scott Ullrich
		return;
92
	}
93 31f346a8 Colin Smith
	if($config['installedpackages']['service']) {
94
		foreach($config['installedpackages']['service'] as $service) {
95 a8dbad89 Colin Smith
			if(strtolower($service['name']) == strtolower($name)) {
96 31f346a8 Colin Smith
				if($service['rcfile']) {
97 ba8495f0 Ermal
					$prefix = $rcfileprefix;
98
					if (!empty($service['prefix'])) {
99 31f346a8 Colin Smith
						$prefix =& $service['prefix'];
100
					}
101 ba8495f0 Ermal
					if(file_exists("{$prefix}{$service['rcfile']}")) {
102
						mwexec_bg("{$prefix}{$service['rcfile']} start");
103 a4ea3340 Scott Ullrich
					}
104 31f346a8 Colin Smith
				}
105 ba8495f0 Ermal
				if (!empty($service['startcmd']))
106 31f346a8 Colin Smith
					eval($service['startcmd']);
107
				break;
108
			}
109
		}
110
	}
111
}
112
113 58826c73 Colin Smith
function stop_service($name) {
114 cd72ded3 Timo Boettcher
	global $config;
115 af799b48 Renato Botelho
	global $rcfileprefix;
116 246a887a Ermal
117
	if ($config['installedpackages']['service']) {
118 cd72ded3 Timo Boettcher
		foreach($config['installedpackages']['service'] as $service) {
119
			if(strtolower($service['name']) == strtolower($name)) {
120
				if($service['rcfile']) {
121 ba8495f0 Ermal
					$prefix = $rcfileprefix;
122
					if(!empty($service['prefix'])) {
123 cd72ded3 Timo Boettcher
						$prefix =& $service['prefix'];
124
					}
125 246a887a Ermal
					mwexec_bg("{$prefix}{$service['rcfile']} stop");
126 cd72ded3 Timo Boettcher
				}
127 ba8495f0 Ermal
				if (!empty($service['stopcmd']))
128 cd72ded3 Timo Boettcher
					eval($service['stopcmd']);
129 ba8495f0 Ermal
130 cd72ded3 Timo Boettcher
				if(!($service['rcfile'] or $service['stopcmd'])) {
131 99f98b80 sullrich
					if(is_process_running("{$service['executable']}"))
132
						mwexec_bg("/usr/bin/killall {$service['executable']}");
133 cd72ded3 Timo Boettcher
					return;
134
				}
135
				break;
136
			}
137
		}
138
	}
139
	/* finally if we get here lets simply kill the service name */
140 246a887a Ermal
	if(is_process_running("{$name}"))
141
		mwexec_bg("/usr/bin/killall {$name}");
142 31f346a8 Colin Smith
}
143
144 58826c73 Colin Smith
function restart_service($name) {
145 cd72ded3 Timo Boettcher
	global $config;
146 ba8495f0 Ermal
147 636ab238 Colin Smith
	stop_service($name);
148
	start_service($name);
149 ba8495f0 Ermal
150 cd72ded3 Timo Boettcher
	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 31f346a8 Colin Smith
}
161 ec4e071a Colin Smith
162 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
163 ba8495f0 Ermal
	if (!file_exists($pidfile))
164
		return false;
165
	$running = shell_exec("/bin/pgrep -F {$pidfile} 2>/dev/null");
166
167 22d323b3 Erik Fonnesbeck
	return (!empty($running));
168 cd72ded3 Timo Boettcher
}
169
170 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
171 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
172
	if($status <> "")
173 ca88e48f Scott Ullrich
		return true;
174
	return false;
175 8ff8520f Scott Ullrich
}
176
177 0661a033 Colin Smith
function restart_service_if_running($service) {
178
	global $config;
179 4685e464 Scott Ullrich
	if(is_service_running($service))
180 0661a033 Colin Smith
		restart_service($service);
181
	return;
182
}
183
184 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
185
	global $config;
186 ba8495f0 Ermal
187 c2b6723a Scott Ullrich
	if(is_array($config['installedpackages']['service'])) {
188 cd72ded3 Timo Boettcher
		foreach($config['installedpackages']['service'] as $aservice) {
189
			if(strtolower($service) == strtolower($aservice['name'])) {
190 ba8495f0 Ermal
				if ($aservice['custom_php_service_status_command'] <> "") {
191
					eval("\$rc={$aservice['custom_php_service_status_command']};");
192 c2d49311 Eirik Oeverby
					return $rc;
193
				}
194 ba8495f0 Ermal
				if(empty($aservice['executable']))
195 ec4e071a Colin Smith
					return false;
196 ba8495f0 Ermal
				if (is_process_running($aservice['executable']))
197
					return true;
198
199
				return false;
200 cd72ded3 Timo Boettcher
			}
201
		}
202
	}
203 ba8495f0 Ermal
204
	if (is_process_running($service))
205
		return true;
206
207
	return false;
208 ec4e071a Colin Smith
}
209 ff074bf9 Scott Ullrich
210 05c4bfa0 Ermal
?>