Project

General

Profile

Download (6.37 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
	$towrite .= "#!/bin/sh\n# This file was automatically generated\n# by the pfSense service handler.\n\n";
40
	/* 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
	
53
	/* 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 31f346a8 Colin Smith
	if($config['installedpackages']['service']) {
67
		foreach($config['installedpackages']['service'] as $service) {
68 a8dbad89 Colin Smith
			if(strtolower($service['name']) == strtolower($name)) {
69 31f346a8 Colin Smith
				if($service['rcfile']) {
70
					if($service['prefix']) {
71
						$prefix =& $service['prefix'];
72
					} else {
73
						$prefix = "/usr/local/etc/rc.d/";
74
					}
75 a4ea3340 Scott Ullrich
					if(file_exists($prefix . $service['rcfile'])) {
76 c19a49e0 Scott Ullrich
						mwexec_bg($prefix . $service['rcfile'] . " start");
77 a4ea3340 Scott Ullrich
					} else {
78
						if(file_exists("/usr/local/etc/rc.d/{$name}.sh"))
79
							mwexec_bg("/usr/local/etc/rc.d/{$name}.sh start");
80
					}
81 31f346a8 Colin Smith
				}
82
				if($service['startcmd']) {
83
					eval($service['startcmd']);
84
				}
85
				break;
86
			}
87
		}
88
	}
89
}
90
91 58826c73 Colin Smith
function stop_service($name) {
92 669e1adb Bill Marquette
        global $config;
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
                                        if($service['prefix']) {
98
                                                $prefix =& $service['prefix'];
99
                                        } else {
100
                                                $prefix = "/usr/local/etc/rc.d/";
101
                                        }
102 636ab238 Colin Smith
                                        mwexec_bg($prefix . $service['rcfile'] . " stop");
103 31f346a8 Colin Smith
                                }
104
                                if($service['stopcmd']) {
105
                                        eval($service['stopcmd']);
106
                                }
107 b39dc701 Scott Ullrich
								if(!($service['rcfile'] or $service['stopcmd'])) {
108
									mwexec_bg("/usr/bin/killall {$service['executable']}");
109
									return;
110
								}
111 31f346a8 Colin Smith
                                break;
112
                        }
113
                }
114
        }
115 b39dc701 Scott Ullrich
		/* finally if we get here lets simply kill the service name */
116
		mwexec_bg("/usr/bin/killall {$name}");
117 31f346a8 Colin Smith
}
118
119 58826c73 Colin Smith
function restart_service($name) {
120 669e1adb Bill Marquette
    global $config;
121 636ab238 Colin Smith
	stop_service($name);
122
	start_service($name);
123 31f346a8 Colin Smith
        if($config['installedpackages']['service']) {
124
                foreach($config['installedpackages']['service'] as $service) {
125 a8dbad89 Colin Smith
                        if(strtolower($service['name']) == strtolower($name)) {
126 31f346a8 Colin Smith
                                if($service['restartcmd']) {
127
                                        eval($service['restartcmd']);
128
                                }
129
                                break;
130
                        }
131
                }
132
        }
133
}
134 ec4e071a Colin Smith
135 a8dbad89 Colin Smith
function is_process_running($process) {
136 6a3464b7 Scott Ullrich
    $status = `/bin/ps awux | /usr/bin/grep {$process} | grep -v grep | wc -l`;
137 d96e7a05 Colin Smith
    if($status > 0) return 1;
138 a8dbad89 Colin Smith
    return 0;
139 fbeb6d02 Colin Smith
}
140
141 8ff8520f Scott Ullrich
function is_dhcp_running($interface) {
142
    if(filter_translate_type_to_real_interface($interface) <> "")
143
	$interface = filter_translate_type_to_real_interface($interface);
144
    $status = `/bin/ps ax | /usr/bin/grep dhclient | grep $interface | grep -v grep | wc -l`;
145
    if($status > 0) return 1;
146
    return 0;
147
}
148
149 0661a033 Colin Smith
function restart_service_if_running($service) {
150
	global $config;
151
	if(is_service_running($service)) 
152
		restart_service($service);
153
	return;
154
}
155
156 fbeb6d02 Colin Smith
function is_service_running($service, $ps = "") {
157
	global $config;
158 c2b6723a Scott Ullrich
	/*
159 a8dbad89 Colin Smith
	if(!$ps) {
160
		exec("/bin/ps ax | awk '{ print $5 }'", $psout);
161
	}
162 c2b6723a Scott Ullrich
	*/
163
	if(is_array($config['installedpackages']['service'])) {
164 a93f79c1 Colin Smith
                foreach($config['installedpackages']['service'] as $aservice) {
165 a8dbad89 Colin Smith
                        if(strtolower($service) == strtolower($aservice['name'])) {
166 a93f79c1 Colin Smith
				if(!$aservice['executable']) return false;
167 c2b6723a Scott Ullrich
				/*
168
				if(count(preg_grep("/{$aservice['executable']}/i", $ps))) {
169 ec4e071a Colin Smith
					return true;
170
				} else {
171
					return false;
172
				}
173 c2b6723a Scott Ullrich
				*/
174
				return is_process_running($aservice['executable']) ? true : false;
175 ec4e071a Colin Smith
                        }
176
                }
177
        }
178
}
179 ff074bf9 Scott Ullrich
180
?>