Menu

[r1225]: / trunk / etc / inc / rc.inc  Maximize  Restore  History

Download this file

243 lines (212 with data), 12.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/*
rc.inc
Copyright © 2007 Volker Theile (votdev@gmx.de)
All rights reserved.
part of FreeNAS (http://www.freenas.org)
Copyright (C) 2005-2007 Olivier Cochard-Labbe <olivier@freenas.org>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
require_once("util.inc");
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// !! ATTENTION:
// !! It seems to be impossible to call a rc init script that
// !! creates a conf file using 'create_conf_file' command.
// !! A workaround is to call start/restart a service with
// !! the additional parameter 'noconf'. The configuration file
// !! has to be created by using the internal php code.
// !! Example for WebGUI:
// !!
// !! $retval |= rc_update_service("mdnsresponder");
// !!
// !! The configuration file will be created by calling the configured
// !! function services_create_mdnsresponder_conf() in $g_rcservices['mdnsresponder'].
// !!
// !! It seems to be a problem with the lighttpd cgi modul.
// !!
// !! Scenarios:
// !! OK => console -> rc init script (/etc/rc.d/xxx) ->
// !! rc init php wrapper (/etc/rc.d.php/xxx)
// !!
// !! FAILS => WebGUI (lighttpd/mod_cgi) -> rc init script (/etc/rc.d/xxx) ->
// !! rc init php wrapper (/etc/rc.d.php/xxx)
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// Services settings.
// Register new services by adding them to this array:
// start_func - Function executed before service is started.
// stop_func - Function executed before service is stopped.
// is_enabled_func - Function to test if service is enabled
$g_rcservices = array();
$g_rcservices['afpd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['afp']['enable']);", "rcvar" => "afpd", "set_rcvar" => "true");
$g_rcservices['inadyn'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['dynamicdns']['enable']);", "rcvar" => "inadyn", "set_rcvar" => "true");
$g_rcservices['iscsi_target'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = is_array(\$config['iscsitarget']['vdisk']) && count(\$config['iscsitarget']['vdisk']);", "rcvar" => "iscsi_target", "set_rcvar" => "true");
$g_rcservices['iscsi_initiator'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = is_array(\$config['iscsiinit']['vdisk']) && count(\$config['iscsiinit']['vdisk']);", "rcvar" => "iscsi_initiator", "set_rcvar" => "true");
$g_rcservices['lighttpd'] = array("start_func" => "services_create_lighttpd_conf", "stop_func" => "", "is_enabled_func" => "\$enabled = true;", "rcvar" => "lighttpd", "set_rcvar" => "true");
$g_rcservices['mdnsresponder'] = array("start_func" => "services_create_mdnsresponder_conf", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['system']['zeroconf']);", "rcvar" => "mdnsresponder", "set_rcvar" => "true");
$g_rcservices['pureftpd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['ftp']['enable']);", "rcvar" => "pureftpd", "set_rcvar" => "true");
$g_rcservices['snmpd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['snmpd']['enable']);", "rcvar" => "snmpd", "set_rcvar" => "true");
$g_rcservices['ushare'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['upnp']['enable']);", "rcvar" => "ushare", "set_rcvar" => "true");
$g_rcservices['sshd'] = array("start_func" => "services_create_sshd_conf", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['sshd']['enable']);", "rcvar" => "sshd", "set_rcvar" => "true");
$g_rcservices['rsyncd'] = array("start_func" => "services_create_rsyncd_conf", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['rsyncd']['enable']);", "rcvar" => "rsyncd", "set_rcvar" => "true");
$g_rcservices['samba'] = array("start_func" => "services_create_samba_conf", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['samba']['enable']);", "rcvar" => "samba", "set_rcvar" => "true");
$g_rcservices['swap'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['system']['swap_enable']);", "rcvar" => "swap", "set_rcvar" => "true");
$g_rcservices['unison'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['unison']['enable']);", "rcvar" => "unison", "set_rcvar" => "true");
$g_rcservices['mountd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['nfs']['enable']);", "rcvar" => "mountd", "set_rcvar" => "true");
$g_rcservices['rpcbind'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['nfs']['enable']);", "rcvar" => "rpcbind", "set_rcvar" => "true");
$g_rcservices['nfsd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['nfs']['enable']);", "rcvar" => "nfs_server", "set_rcvar" => "true");
$g_rcservices['nfslocking'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['nfs']['enable']);", "rcvar" => "rpc_statd rpc_lockd", "set_rcvar" => "true");
$g_rcservices['hosts'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "\$enabled = true;", "rcvar" => "", "set_rcvar" => "false");
$g_rcservices['rcconf.sh'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "\$enabled = true;", "rcvar" => "", "set_rcvar" => "false");
$g_rcservices['resolv'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "\$enabled = true;", "rcvar" => "", "set_rcvar" => "false");
$g_rcservices['cron'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "\$enabled = true;", "rcvar" => "", "set_rcvar" => "false");
$g_rcservices['routing'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "\$enabled = true;", "rcvar" => "", "set_rcvar" => "false");
$g_rcservices['smartd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['system']['smart']);", "rcvar" => "smartd", "set_rcvar" => "true");
$g_rcservices['powerd'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['system']['powerd']);", "rcvar" => "powerd", "set_rcvar" => "true");
$g_rcservices['systune'] = array("start_func" => "", "stop_func" => "", "is_enabled_func" => "global \$config; \$enabled = isset(\$config['system']['tune']);", "rcvar" => "systune", "set_rcvar" => "true");
// Execute rc script.
// Return 0 if successful, otherwise 1.
function rc_exec_script($scriptname) {
exec("{$scriptname} >/dev/null 2>&1", $output, $retval);
return $retval;
}
// Execute rc script asynchron.
// Return 0 if successful, otherwise 1.
function rc_exec_script_async($scriptname) {
exec("nohup {$scriptname} >/dev/null 2>&1 &", $output, $retval);
return $retval;
}
// Check if service is enabled.
// Looks for xxx_enabled="YES|NO" in rc.conf.
// Return 0 if service is enabled, otherwise 1.
function rc_is_service_enabled($name) {
$retval = mwexec("/usr/local/sbin/rconf service rcvar {$name}");
return $retval;
}
// Check if service is running.
// Return 0 if service is running, otherwise 1.
function rc_is_service_running($name) {
$retval = rc_exec_script("/etc/rc.d/{$name} status");
return $retval;
}
// Execute service.
// Return 0 if successful, otherwise 1.
function rc_exec_service($name) {
$retval = rc_exec_script("/etc/rc.d/{$name}");
if (0 == $retval) {
write_log("{$name} service executed");
} else {
write_log("Failed to execute service {$name}");
}
return $retval;
}
// Start service.
// Return 0 if successful, otherwise 1.
function rc_start_service($name) {
global $g_rcservices;
// Execute service function.
if ($g_rcservices[$name] && is_callable($g_rcservices[$name]['start_func']))
call_user_func($g_rcservices[$name]['start_func']);
// Execute script.
$retval = rc_exec_script("/etc/rc.d/{$name} start noconf");
if (0 == $retval) {
write_log("{$name} service started");
} else {
write_log("Failed to start service {$name}");
}
return $retval;
}
// Restart service.
// Return 0 if successful, otherwise 1.
function rc_restart_service($name) {
global $g_rcservices;
// Execute service function.
if ($g_rcservices[$name] && is_callable($g_rcservices[$name]['start_func']))
call_user_func($g_rcservices[$name]['start_func']);
// Execute script.
$retval = rc_exec_script("/etc/rc.d/{$name} restart noconf");
if (0 == $retval) {
write_log("{$name} service restarted");
} else {
write_log("Failed to restart service {$name}");
}
return $retval;
}
// Stop service.
// Return 0 if successful, otherwise 1.
function rc_stop_service($name) {
global $g_rcservices;
// Execute service function.
if ($g_rcservices[$name] && is_callable($g_rcservices[$name]['stop_func']))
call_user_func($g_rcservices[$name]['stop_func']);
// Execute script.
$retval = rc_exec_script("/etc/rc.d/{$name} stop");
if (0 == $retval) {
write_log("{$name} service stopped");
} else {
write_log("Failed to stop service {$name}");
}
return $retval;
}
// Update service (Modify rc.conf and execute rc script).
// Return 0 if successful, otherwise 1.
function rc_update_service($name) {
global $g_rcservices;
$retval = 0;
// Check if service is running.
$running = rc_is_service_running($name);
// Check if service is enabled.
$enabled = false;
if ($g_rcservices[$name]) {
eval($g_rcservices[$name]['is_enabled_func']);
}
// Get the rcvar name.
$rcvar = $name;
if ($g_rcservices[$name]) {
if (!empty($g_rcservices[$name]['rcvar'])) {
$rcvar = $g_rcservices[$name]['rcvar'];
}
}
// Update rc.conf and execute rc script.
if ($enabled) {
// Check if service needs to set 'xxx_enabled="YES"|"NO"' attribute in rc.conf.
if ("true" === $g_rcservices[$name]['set_rcvar']) {
mwexec("/usr/local/sbin/rconf service enable {$rcvar}");
}
switch ($running) {
case 0:
$retval = rc_restart_service($name);
break;
case 1:
$retval = rc_start_service($name);
break;
}
} else {
// Stop service if necessary.
if (0 == $running) {
$retval = rc_stop_service($name);
}
// Check if service needs to set 'xxx_enabled="YES"|"NO"' attribute in rc.conf.
if ("true" === $g_rcservices[$name]['set_rcvar']) {
mwexec("/usr/local/sbin/rconf service disable {$rcvar}");
}
}
return $retval;
}
?>
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.