| 1 |
# http://www.novell.com/coolsolutions/feature/11256.html
|
| 2 |
|
| 3 |
#@ pp_backend_rpm_init_svc_vars(): initialises rpm backend variables
|
| 4 |
pp_backend_rpm_init_svc_vars () {
|
| 5 |
pp_rpm_default_start_runlevels= # should be "3 4 5"??
|
| 6 |
pp_rpm_default_svc_description="No description"
|
| 7 |
}
|
| 8 |
|
| 9 |
#@ pp_rpm_service_install_common(): emit common code for service install
|
| 10 |
pp_rpm_service_install_common () {
|
| 11 |
cat <<-'.'
|
| 12 |
|
| 13 |
_pp_install_service () {
|
| 14 |
typeset svc level
|
| 15 |
svc="$1"
|
| 16 |
if [ -x /usr/lib/lsb/install_initd -a ! -r /etc/redhat-release ]
|
| 17 |
then
|
| 18 |
# LSB-style install
|
| 19 |
/usr/lib/lsb/install_initd /etc/init.d/$svc
|
| 20 |
elif [ -x /sbin/chkconfig ]; then
|
| 21 |
# Red Hat/chkconfig-style install
|
| 22 |
/sbin/chkconfig --add $svc
|
| 23 |
/sbin/chkconfig $svc off
|
| 24 |
else
|
| 25 |
: # manual links under /etc/init.d
|
| 26 |
fi
|
| 27 |
}
|
| 28 |
|
| 29 |
_pp_enable_service () {
|
| 30 |
typeset svc level
|
| 31 |
svc="$1"
|
| 32 |
if [ -x /usr/lib/lsb/install_initd -a ! -r /etc/redhat-release ]
|
| 33 |
then
|
| 34 |
# LSB-style install
|
| 35 |
: not sure how to enable
|
| 36 |
elif [ -x /sbin/chkconfig ]; then
|
| 37 |
# Red Hat/chkconfig-style install
|
| 38 |
/sbin/chkconfig $svc on
|
| 39 |
else
|
| 40 |
# manual install
|
| 41 |
set -- `sed -n -e 's/^# Default-Start://p' /etc/init.d/$svc`
|
| 42 |
for level
|
| 43 |
do ln -sf /etc/init.d/$svc /etc/rc.d/rc$level.d/S90$svc; done
|
| 44 |
set -- `sed -n -e 's/^# Default-Stop://p' /etc/init.d/$svc`
|
| 45 |
for level
|
| 46 |
do ln -sf /etc/init.d/$svc /etc/rc.d/rc$level.d/K10$svc; done
|
| 47 |
fi
|
| 48 |
}
|
| 49 |
.
|
| 50 |
}
|
| 51 |
|
| 52 |
#@ pp_rpm_service_remove_common(): emit common code for service removal
|
| 53 |
pp_rpm_service_remove_common () {
|
| 54 |
cat <<-'.'
|
| 55 |
|
| 56 |
_pp_remove_service () {
|
| 57 |
typeset svc
|
| 58 |
svc="$1"
|
| 59 |
/etc/init.d/$svc stop >/dev/null 2>&1
|
| 60 |
if [ -x /usr/lib/lsb/remove_initd -a ! -r /etc/redhat-release ]
|
| 61 |
then
|
| 62 |
/usr/lib/lsb/remove_initd /etc/init.d/$svc
|
| 63 |
elif [ -x /sbin/chkconfig ]; then
|
| 64 |
/sbin/chkconfig --del $svc
|
| 65 |
else
|
| 66 |
rm -f /etc/rc.d/rc?.d/[SK]??$svc
|
| 67 |
fi
|
| 68 |
}
|
| 69 |
.
|
| 70 |
}
|
| 71 |
|
| 72 |
|
| 73 |
#@ pp_rpm_service_install(svc): emit code to install/start a service
|
| 74 |
pp_rpm_service_install () {
|
| 75 |
pp_rpm_service_make_init_script $1 >/dev/null ||
|
| 76 |
pp_error "could not create init script for service $1"
|
| 77 |
echo "_pp_install_service $1"
|
| 78 |
test $enable = yes && echo "_pp_enable_service $1"
|
| 79 |
}
|
| 80 |
|
| 81 |
#@ pp_rpm_service_remove(svc): emit code to stop/remove a service
|
| 82 |
pp_rpm_service_remove () {
|
| 83 |
echo "_pp_remove_service $1"
|
| 84 |
}
|
| 85 |
|
| 86 |
|
| 87 |
#@ pp_backend_rpm_init_svc_vars(): initialise rpm-specific service variables
|
| 88 |
# pp-provided vars are cmd, pidfile, stop_signal and user
|
| 89 |
pp_backend_rpm_init_svc_vars () {
|
| 90 |
|
| 91 |
reload_signal=
|
| 92 |
start_runlevels=${pp_rpm_default_start_runlevels} # == lsb default-start
|
| 93 |
stop_runlevels="0 1 2 6" # == lsb default-stop
|
| 94 |
svc_description="${pp_rpm_default_svc_description}" # == lsb short descr
|
| 95 |
svc_process=
|
| 96 |
|
| 97 |
lsb_required_start='$local_fs $network'
|
| 98 |
lsb_should_start=
|
| 99 |
lsb_required_stop=
|
| 100 |
lsb_description=
|
| 101 |
|
| 102 |
start_priority=50
|
| 103 |
stop_priority=50 #-- stop_priority = 100 - start_priority
|
| 104 |
}
|
| 105 |
|
| 106 |
#@ pp_rpm_service_group_make_init_script (grp svcs): makes a group script
|
| 107 |
pp_rpm_service_group_make_init_script () {
|
| 108 |
typeset grp=$1
|
| 109 |
typeset svcs="$2"
|
| 110 |
typeset script=/etc/init.d/$grp
|
| 111 |
typeset out=$pp_destdir$script
|
| 112 |
|
| 113 |
pp_add_file_if_missing $script run 755 || return 0
|
| 114 |
|
| 115 |
cat <<-. >>$out
|
| 116 |
#!/bin/sh
|
| 117 |
svcs="$svcs"
|
| 118 |
.
|
| 119 |
|
| 120 |
cat <<-'.' >>$out
|
| 121 |
|
| 122 |
#-- prints usage message
|
| 123 |
pp_usage () {
|
| 124 |
echo "usage: $0 {start|stop|status|restart|reload|condrestart|try-restart|force-reload}" >&2
|
| 125 |
return 2
|
| 126 |
}
|
| 127 |
|
| 128 |
#-- starts services in order.. stops them all if any break
|
| 129 |
pp_start () {
|
| 130 |
undo=
|
| 131 |
for svc in $svcs; do
|
| 132 |
if /etc/init.d/$svc start; then
|
| 133 |
undo="$svc $undo"
|
| 134 |
else
|
| 135 |
if test -n "$undo"; then
|
| 136 |
for svc in $undo; do
|
| 137 |
/etc/init.d/$svc stop
|
| 138 |
done
|
| 139 |
return 1
|
| 140 |
fi
|
| 141 |
fi
|
| 142 |
done
|
| 143 |
return 0
|
| 144 |
}
|
| 145 |
|
| 146 |
#-- stops services in reverse
|
| 147 |
pp_stop () {
|
| 148 |
reverse=
|
| 149 |
for svc in $svcs; do
|
| 150 |
reverse="$svc $reverse"
|
| 151 |
done
|
| 152 |
rc=0
|
| 153 |
for svc in $reverse; do
|
| 154 |
/etc/init.d/$svc stop || rc=$?
|
| 155 |
done
|
| 156 |
return $rc
|
| 157 |
}
|
| 158 |
|
| 159 |
#-- returns true only if all services return true status
|
| 160 |
pp_status () {
|
| 161 |
rc=0
|
| 162 |
for svc in $svcs; do
|
| 163 |
/etc/init.d/$svc status || rc=$?
|
| 164 |
done
|
| 165 |
return $rc
|
| 166 |
}
|
| 167 |
|
| 168 |
pp_reload () {
|
| 169 |
rc=0
|
| 170 |
for svc in $svcs; do
|
| 171 |
/etc/init.d/$svc reload || rc=$?
|
| 172 |
done
|
| 173 |
return $rc
|
| 174 |
}
|
| 175 |
|
| 176 |
case "$1" in
|
| 177 |
start) pp_start;;
|
| 178 |
stop) pp_stop;;
|
| 179 |
restart) pp_stop; pp_start;;
|
| 180 |
status) pp_status;;
|
| 181 |
try-restart|condrestart)
|
| 182 |
if pp_status >/dev/null; then
|
| 183 |
pp_restart
|
| 184 |
fi;;
|
| 185 |
reload) pp_reload;;
|
| 186 |
force-reload) if pp_status >/dev/null; then
|
| 187 |
pp_reload
|
| 188 |
else
|
| 189 |
pp_restart
|
| 190 |
fi;;
|
| 191 |
*) pp_usage;;
|
| 192 |
esac
|
| 193 |
.
|
| 194 |
chmod 755 $out
|
| 195 |
}
|
| 196 |
|
| 197 |
#@ pp_rpm_service_make_init_script(svc): generates a service init script
|
| 198 |
# Assumes %service variables have been set before invocation
|
| 199 |
# Does nothing if an /etc/init.d/$svc script exists already.
|
| 200 |
# Otherwise creates an /etc/init.d/$svc script that should work
|
| 201 |
# under both Red Hat's chkconfig and LSB. Because LSB support is sometimes
|
| 202 |
# broken on Red Hat, chkconfig is tried first.
|
| 203 |
pp_rpm_service_make_init_script () {
|
| 204 |
typeset svc=$1
|
| 205 |
typeset script=/etc/init.d/$svc
|
| 206 |
typeset out=$pp_destdir$script
|
| 207 |
typeset _process _cmd _rpmlevels
|
| 208 |
|
| 209 |
pp_add_file_if_missing $script run 755 || return 0
|
| 210 |
|
| 211 |
#-- start out as an empty shell script
|
| 212 |
cat <<-'.' >$out
|
| 213 |
#!/bin/sh
|
| 214 |
.
|
| 215 |
|
| 216 |
#-- determine the process name from $cmd unless $svc_process is given
|
| 217 |
set -- $cmd
|
| 218 |
_process=${svc_process:-"$1"}
|
| 219 |
|
| 220 |
#-- construct a start command that builds a pid file if needed
|
| 221 |
_cmd="$cmd";
|
| 222 |
if test -z "$pidfile"; then
|
| 223 |
pidfile=/var/run/$svc.pid
|
| 224 |
_cmd="$cmd & echo \$! > \$pidfile"
|
| 225 |
fi
|
| 226 |
if test "$user" != "root"; then
|
| 227 |
_cmd="su $user -c exec $_cmd";
|
| 228 |
fi
|
| 229 |
|
| 230 |
#-- generate the Red Hat chkconfig headers
|
| 231 |
_rpmlevels=`echo $start_runlevels | tr -d ' '`
|
| 232 |
cat <<-. >>$out
|
| 233 |
# chkconfig: ${_rpmlevels:--} ${start_priority:-50} ${stop_priority:-50}
|
| 234 |
# description: ${svc_description:-no description}
|
| 235 |
# processname: ${_process}
|
| 236 |
# pidfile: ${pidfile}
|
| 237 |
.
|
| 238 |
|
| 239 |
#-- generate the LSB init info
|
| 240 |
cat <<-. >>$out
|
| 241 |
### BEGIN INIT INFO
|
| 242 |
# Provides: ${svc}
|
| 243 |
# Required-Start: ${lsb_required_start}
|
| 244 |
# Should-Start: ${lsb_should_start}
|
| 245 |
# Required-Stop: ${lsb_required_stop}
|
| 246 |
# Default-Start: ${start_runlevels}
|
| 247 |
# Default-Stop: ${stop_runlevels}
|
| 248 |
# Short-Description: ${svc_description}
|
| 249 |
### END INIT INFO
|
| 250 |
# Generated by PolyPackage ${pp_version}
|
| 251 |
# ${copyright}
|
| 252 |
|
| 253 |
.
|
| 254 |
|
| 255 |
if test x"${svc_description}" = x"${pp_rpm_default_svc_description}"; then
|
| 256 |
svc_description=
|
| 257 |
fi
|
| 258 |
|
| 259 |
#-- write service-specific definitions
|
| 260 |
cat <<. >>$out
|
| 261 |
#-- definitions specific to service ${svc}
|
| 262 |
svc_name="${svc_description:-$svc service}"
|
| 263 |
user="${user}"
|
| 264 |
pidfile="${pidfile}"
|
| 265 |
stop_signal="${stop_signal}"
|
| 266 |
reload_signal="${reload_signal}"
|
| 267 |
pp_exec_cmd () { $_cmd; }
|
| 268 |
.
|
| 269 |
|
| 270 |
#-- write the generic part of the init script
|
| 271 |
cat <<'.' >>$out
|
| 272 |
|
| 273 |
#-- use system message logging, if available
|
| 274 |
if [ -f /lib/lsb/init-functions -a ! -r /etc/redhat-release ]; then
|
| 275 |
. /lib/lsb/init-functions
|
| 276 |
pp_success_msg () { log_success_msg "$@"; }
|
| 277 |
pp_failure_msg () { log_failure_msg "$@"; }
|
| 278 |
pp_warning_msg () { log_warning_msg "$@"; }
|
| 279 |
elif [ -f /etc/init.d/functions ]; then
|
| 280 |
. /etc/init.d/functions
|
| 281 |
pp_success_msg () { echo -n "$*"; success "$@"; echo; }
|
| 282 |
pp_failure_msg () { echo -n "$*"; failure "$@"; echo; }
|
| 283 |
pp_warning_msg () { echo -n "$*"; warning "$@"; echo; }
|
| 284 |
else
|
| 285 |
pp_success_msg () { echo ${1:+"$*:"} OK; }
|
| 286 |
pp_failure_msg () { echo ${1:+"$*:"} FAIL; }
|
| 287 |
pp_warning_msg () { echo ${1:+"$*:"} WARNING; }
|
| 288 |
fi
|
| 289 |
|
| 290 |
#-- prints a status message
|
| 291 |
pp_msg () { echo -n "$*: "; }
|
| 292 |
|
| 293 |
#-- prints usage message
|
| 294 |
pp_usage () {
|
| 295 |
echo "usage: $0 {start|stop|status|restart|reload|condrestart|try-restart|force-reload}" >&2
|
| 296 |
return 2
|
| 297 |
}
|
| 298 |
|
| 299 |
#-- reloads the service, if possible
|
| 300 |
# returns 0=success 1=failure 3=unimplemented
|
| 301 |
pp_reload () {
|
| 302 |
test -n "$reload_signal" || return 3 # unimplemented
|
| 303 |
pp_msg "Reloading ${svc_name}"
|
| 304 |
if pp_signal -${reload_signal}; then
|
| 305 |
pp_success_msg
|
| 306 |
return 0
|
| 307 |
else
|
| 308 |
pp_failure_msg "not running"
|
| 309 |
return 1
|
| 310 |
fi
|
| 311 |
}
|
| 312 |
|
| 313 |
#-- delivers signal $1 to the pidfile
|
| 314 |
# returns 0=success 1=failure
|
| 315 |
pp_signal () {
|
| 316 |
if test -r "$pidfile"; then
|
| 317 |
read pid < $pidfile
|
| 318 |
kill "$@" "$pid" 2>/dev/null
|
| 319 |
else
|
| 320 |
return 1
|
| 321 |
fi
|
| 322 |
}
|
| 323 |
|
| 324 |
#-- prints information about the service status
|
| 325 |
# returns 0=running 1=crashed 3=stopped
|
| 326 |
pp_status () {
|
| 327 |
pp_msg "Checking for ${svc_name}"
|
| 328 |
if pp_signal -0; then
|
| 329 |
pp_success_msg "running"
|
| 330 |
return 0
|
| 331 |
elif test -r "$pidfile"; then
|
| 332 |
pp_failure_msg "not running (crashed)"
|
| 333 |
return 1
|
| 334 |
else
|
| 335 |
pp_failure_msg "not running"
|
| 336 |
return 3
|
| 337 |
fi
|
| 338 |
}
|
| 339 |
|
| 340 |
#-- starts the service
|
| 341 |
# returns 0=success 1=failure
|
| 342 |
pp_start () {
|
| 343 |
pp_msg "Starting ${svc_name}"
|
| 344 |
if pp_status >/dev/null; then
|
| 345 |
pp_warning_msg "already started"
|
| 346 |
return 0
|
| 347 |
elif pp_exec_cmd; then
|
| 348 |
pp_success_msg
|
| 349 |
return 0
|
| 350 |
else
|
| 351 |
pp_failure_msg "cannot start"
|
| 352 |
return 1
|
| 353 |
fi
|
| 354 |
}
|
| 355 |
|
| 356 |
#-- stops the service
|
| 357 |
# returns 0=success (always)
|
| 358 |
pp_stop () {
|
| 359 |
pp_msg "Stopping ${svc_name}"
|
| 360 |
if pp_signal -${stop_signal}; then
|
| 361 |
pp_success_msg
|
| 362 |
else
|
| 363 |
pp_success_msg "already stopped"
|
| 364 |
fi
|
| 365 |
rm -f "$pidfile"
|
| 366 |
return 0
|
| 367 |
}
|
| 368 |
|
| 369 |
#-- stops and starts the service
|
| 370 |
pp_restart () {
|
| 371 |
pp_stop
|
| 372 |
pp_start
|
| 373 |
}
|
| 374 |
|
| 375 |
case "$1" in
|
| 376 |
start) pp_start;;
|
| 377 |
stop) pp_stop;;
|
| 378 |
restart) pp_restart;;
|
| 379 |
status) pp_status;;
|
| 380 |
try-restart|condrestart)
|
| 381 |
if pp_status >/dev/null; then
|
| 382 |
pp_restart
|
| 383 |
fi;;
|
| 384 |
reload) pp_reload;;
|
| 385 |
force-reload) if pp_status >/dev/null; then
|
| 386 |
pp_reload
|
| 387 |
else
|
| 388 |
pp_restart
|
| 389 |
fi;;
|
| 390 |
*) pp_usage;;
|
| 391 |
esac
|
| 392 |
|
| 393 |
.
|
| 394 |
chmod 755 $out
|
| 395 |
}
|