Project

General

Profile

Download (5.81 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
function write_rcfile($params) {
37 b662f54f Colin Smith
	$fileprefix = "/usr/local/etc/rc.d/";
38 c83068f6 Colin Smith
	if(!(is_writable($fileprefix . $params['file']) or $params['start'])) return false;
39 36d0358b Scott Ullrich
	$towrite .= "#!/bin/sh\n# This file was automatically generated\n# by the {$g['product_website']} service handler.\n\n";
40 c83068f6 Colin Smith
	/* write our rc functions */
41
	$towrite .= "rc_start() {\n\t" . $params['start'] . "\n}\n\n";
42
	if($params['stop']) {
43
		$tokill =& $params['stop'];
44 dc312bf5 Colin Smith
	} elseif($params['executable']) {
45 a8dbad89 Colin Smith
		/* just nuke the executable */
46 dc312bf5 Colin Smith
		$tokill = "/usr/bin/killall {$params['executable']}";
47 c83068f6 Colin Smith
	} else {
48 a8dbad89 Colin Smith
		/* make an educated guess (bad) */
49 c83068f6 Colin Smith
		$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
50
	}
51
	$towrite .= "rc_stop() {\n\t" . $tokill . "\n}\n\n";
52 4685e464 Scott Ullrich
53 c83068f6 Colin Smith
	/* begin rcfile logic */
54
	$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";
55
	$fout = fopen($fileprefix . $params['file'], "w");
56
	fwrite($fout, $towrite);
57
	fclose($fout);
58 815af8fb Colin Smith
	chmod($fileprefix . $params['file'], 0755);
59 c83068f6 Colin Smith
	return;
60
}
61 31f346a8 Colin Smith
62 58826c73 Colin Smith
function start_service($name) {
63 669e1adb Bill Marquette
	global $config;
64 2523de53 Scott Ullrich
	/* make sure service is stopped before starting */
65
	stop_service($name);
66 34f7437f Scott Ullrich
	sleep(2);
67 4685e464 Scott Ullrich
	if(file_exists("/usr/local/etc/rc.d/{$name}.sh")) {
68 34f7437f Scott Ullrich
		exec("/bin/sh /usr/local/etc/rc.d/{$name}.sh start");
69 4685e464 Scott Ullrich
		return;
70
	}
71 31f346a8 Colin Smith
	if($config['installedpackages']['service']) {
72
		foreach($config['installedpackages']['service'] as $service) {
73 a8dbad89 Colin Smith
			if(strtolower($service['name']) == strtolower($name)) {
74 31f346a8 Colin Smith
				if($service['rcfile']) {
75
					if($service['prefix']) {
76
						$prefix =& $service['prefix'];
77
					} else {
78
						$prefix = "/usr/local/etc/rc.d/";
79
					}
80 a4ea3340 Scott Ullrich
					if(file_exists($prefix . $service['rcfile'])) {
81 c19a49e0 Scott Ullrich
						mwexec_bg($prefix . $service['rcfile'] . " start");
82 a4ea3340 Scott Ullrich
					} else {
83
						if(file_exists("/usr/local/etc/rc.d/{$name}.sh"))
84
							mwexec_bg("/usr/local/etc/rc.d/{$name}.sh start");
85
					}
86 31f346a8 Colin Smith
				}
87
				if($service['startcmd']) {
88
					eval($service['startcmd']);
89
				}
90
				break;
91
			}
92
		}
93
	}
94
}
95
96 58826c73 Colin Smith
function stop_service($name) {
97 cd72ded3 Timo Boettcher
	global $config;
98
	if($config['installedpackages']['service']) {
99
		foreach($config['installedpackages']['service'] as $service) {
100
			if(strtolower($service['name']) == strtolower($name)) {
101
				if($service['rcfile']) {
102
					if($service['prefix']) {
103
						$prefix =& $service['prefix'];
104
					} else {
105
						$prefix = "/usr/local/etc/rc.d/";
106
					}
107
					mwexec_bg($prefix . $service['rcfile'] . " stop");
108
				}
109
				if($service['stopcmd']) {
110
					eval($service['stopcmd']);
111
				}
112
				if(!($service['rcfile'] or $service['stopcmd'])) {
113
					mwexec_bg("/usr/bin/killall {$service['executable']}");
114
					return;
115
				}
116
				break;
117
			}
118
		}
119
	}
120
	/* finally if we get here lets simply kill the service name */
121
	mwexec_bg("/usr/bin/killall {$name}");
122 31f346a8 Colin Smith
}
123
124 58826c73 Colin Smith
function restart_service($name) {
125 cd72ded3 Timo Boettcher
	global $config;
126 636ab238 Colin Smith
	stop_service($name);
127
	start_service($name);
128 cd72ded3 Timo Boettcher
	if($config['installedpackages']['service']) {
129
		foreach($config['installedpackages']['service'] as $service) {
130
			if(strtolower($service['name']) == strtolower($name)) {
131
				if($service['restartcmd']) {
132
					eval($service['restartcmd']);
133
				}
134
				break;
135
			}
136
		}
137
	}
138 31f346a8 Colin Smith
}
139 ec4e071a Colin Smith
140 a8dbad89 Colin Smith
function is_process_running($process) {
141 f80f3194 Scott Ullrich
	$running = (trim(shell_exec("ps axwu | grep '\b{$process}\b' | grep -v 'grep'")) != '');
142 5b67f7ac Scott Ullrich
	return $running;
143 fbeb6d02 Colin Smith
}
144
145 cd72ded3 Timo Boettcher
function is_pid_running($pidfile) {
146
	$pid = trim(file_get_contents($pidfile));
147
	$running = (trim(shell_exec("ps axwu | grep '\b{$pid}\b' | grep -v 'grep'")) != '');
148
	return $running;
149
}
150
151 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
152 ca88e48f Scott Ullrich
	$interface = convert_friendly_interface_to_real_interface_name($interface);
153 fda8dc28 Seth Mos
	$status = find_dhclient_process($interface);
154
	if($status <> "")
155 ca88e48f Scott Ullrich
		return true;
156
	return false;
157 8ff8520f Scott Ullrich
}
158
159 0661a033 Colin Smith
function restart_service_if_running($service) {
160
	global $config;
161 4685e464 Scott Ullrich
	if(is_service_running($service))
162 0661a033 Colin Smith
		restart_service($service);
163
	return;
164
}
165
166 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
167
	global $config;
168 c2b6723a Scott Ullrich
	/*
169 a8dbad89 Colin Smith
	if(!$ps) {
170
		exec("/bin/ps ax | awk '{ print $5 }'", $psout);
171
	}
172 c2b6723a Scott Ullrich
	*/
173
	if(is_array($config['installedpackages']['service'])) {
174 cd72ded3 Timo Boettcher
		foreach($config['installedpackages']['service'] as $aservice) {
175
			if(strtolower($service) == strtolower($aservice['name'])) {
176 a93f79c1 Colin Smith
				if(!$aservice['executable']) return false;
177 c2b6723a Scott Ullrich
				/*
178
				if(count(preg_grep("/{$aservice['executable']}/i", $ps))) {
179 ec4e071a Colin Smith
					return true;
180
				} else {
181
					return false;
182
				}
183 c2b6723a Scott Ullrich
				*/
184 2cd5c340 Timo Boettcher
				return is_process_running($aservice['executable']) ? true : false;
185 cd72ded3 Timo Boettcher
			}
186
		}
187
	}
188 ec4e071a Colin Smith
}
189 ff074bf9 Scott Ullrich
190 cd72ded3 Timo Boettcher
?>