Menu

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

Download this file

175 lines (148 with data), 4.9 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
<?php
/*
rc.inc
Copyright (c) 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("globals.inc");
require_once("util.inc");
// 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 asynchronuously.
// 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 running.
// Return 0 if service is running, otherwise 1.
function rc_is_service_running($name) {
$retval = rc_exec_script("/etc/rc.d/{$name} onestatus");
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) {
// Execute script.
$retval = rc_exec_script("/etc/rc.d/{$name} start");
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) {
// Execute script.
$retval = rc_exec_script("/etc/rc.d/{$name} restart");
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) {
// 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) {
$retval = 0;
// Check if service is running
$running = rc_is_service_running($name);
// Check if service is enabled
$enabled = rc_is_service_enabled($name);
// Update rc.conf and execute rc script
if (0 == $enabled) {
rc_update_rcconf($name, "enable");
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);
}
rc_update_rcconf($name, "disable");
}
return $retval;
}
// Update /etc/rc.conf file.
// Check if KEYWORD 'RCVAR' is defined (e.g. '# RCVAR: xxxx').
function rc_update_rcconf($name,$state) {
$data = @file_get_contents("/etc/rc.d/$name");
$search = "/RCVAR: (.*)/";
if (!preg_match($search, $data, $rcvar)) {
return 0;
}
// Update /etc/rc.conf
$retval = mwexec("/usr/local/sbin/rconf service {$state} {$rcvar[1]}");
return $retval;
}
// Check if service is enabled.
// Use the KEYWORD 'XQUERY' to determine if service is enabled or not.
// Return 0 if service is enabled (default), otherwise 1.
function rc_is_service_enabled($name) {
global $g;
$data = @file_get_contents("/etc/rc.d/$name");
$search = "/XQUERY: (.*)/";
if (!preg_match($search, $data, $xquery)) {
return 0;
}
// Execute query
exec("/usr/local/bin/xml sel -t {$xquery[1]} {$g['conf_path']}/config.xml",$result);
return ("0" === $result[0]) ? 0 : 1;
}
?>
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.