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 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
|
function write_rcfile($params) {
|
37
|
global $config, $g;
|
38
|
$fileprefix = "/usr/local/etc/rc.d/";
|
39
|
if(!(is_writable($fileprefix . $params['file']) or $params['start'])) return false;
|
40
|
$towrite .= "#!/bin/sh\n# This file was automatically generated\n# by the pfSense service handler.\n\n";
|
41
|
/* write our rc functions */
|
42
|
$towrite .= "rc_start() {\n\t" . $params['start'] . "\n}\n\n";
|
43
|
if($params['stop']) {
|
44
|
$tokill =& $params['stop'];
|
45
|
} elseif($params['executable']) {
|
46
|
/* just nuke the executable */
|
47
|
$tokill = "/usr/bin/killall {$params['executable']}";
|
48
|
} else {
|
49
|
/* make an educated guess (bad) */
|
50
|
$tokill = array_pop(explode('/', array_shift(explode(' ', $params['start']))));
|
51
|
}
|
52
|
$towrite .= "rc_stop() {\n\t" . $tokill . "\n}\n\n";
|
53
|
|
54
|
/* begin rcfile logic */
|
55
|
$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";
|
56
|
$fout = fopen($fileprefix . $params['file'], "w");
|
57
|
fwrite($fout, $towrite);
|
58
|
fclose($fout);
|
59
|
chmod($fileprefix . $params['file'], 0755);
|
60
|
return;
|
61
|
}
|
62
|
|
63
|
function start_service($name) {
|
64
|
global $config, $g;
|
65
|
if($config['installedpackages']['service']) {
|
66
|
foreach($config['installedpackages']['service'] as $service) {
|
67
|
if($service['name'] == $name) {
|
68
|
if($service['rcfile']) {
|
69
|
if($service['prefix']) {
|
70
|
$prefix =& $service['prefix'];
|
71
|
} else {
|
72
|
$prefix = "/usr/local/etc/rc.d/";
|
73
|
}
|
74
|
mwexec_bg($prefix . $service['rcfile'] . " start");
|
75
|
}
|
76
|
if($service['startcmd']) {
|
77
|
eval($service['startcmd']);
|
78
|
}
|
79
|
break;
|
80
|
}
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
|
85
|
function stop_service($name) {
|
86
|
global $config, $g;
|
87
|
if($config['installedpackages']['service']) {
|
88
|
foreach($config['installedpackages']['service'] as $service) {
|
89
|
if($service['name'] == $name) {
|
90
|
if($service['rcfile']) {
|
91
|
if($service['prefix']) {
|
92
|
$prefix =& $service['prefix'];
|
93
|
} else {
|
94
|
$prefix = "/usr/local/etc/rc.d/";
|
95
|
}
|
96
|
mwexec($prefix . $service['rcfile'] . " stop");
|
97
|
}
|
98
|
if($service['stopcmd']) {
|
99
|
eval($service['stopcmd']);
|
100
|
}
|
101
|
if(!($service['rcfile'] or $service['stopcmd'])) {
|
102
|
mwexec("/usr/bin/killall {$service['executable']}");
|
103
|
}
|
104
|
break;
|
105
|
}
|
106
|
}
|
107
|
}
|
108
|
}
|
109
|
|
110
|
function restart_service($name) {
|
111
|
global $config, $g;
|
112
|
stop_service($service);
|
113
|
start_service($service);
|
114
|
if($config['installedpackages']['service']) {
|
115
|
foreach($config['installedpackages']['service'] as $service) {
|
116
|
if($service['name'] == $name) {
|
117
|
if($service['restartcmd']) {
|
118
|
eval($service['restartcmd']);
|
119
|
}
|
120
|
break;
|
121
|
}
|
122
|
}
|
123
|
}
|
124
|
}
|
125
|
|
126
|
function is_process_running($process) {
|
127
|
$status = `/bin/ps ax | /usr/bin/grep {$process} | wc -l`;
|
128
|
if($status > 2) return 1;
|
129
|
return 0;
|
130
|
}
|
131
|
|
132
|
function is_service_running($service, $ps = "") {
|
133
|
global $config;
|
134
|
if($config['installedpackages']['service']) {
|
135
|
foreach($config['installedpackages']['service'] as $aservice) {
|
136
|
if($service == $aservice['name']) {
|
137
|
if(!$aservice['executable']) return false;
|
138
|
if(is_process_running($aservice['executable'], $ps)) {
|
139
|
return true;
|
140
|
} else {
|
141
|
return false;
|
142
|
}
|
143
|
break;
|
144
|
}
|
145
|
}
|
146
|
}
|
147
|
return false;
|
148
|
}
|