Changeset View
Changeset View
Standalone View
Standalone View
usr.sbin/service/service.sh
#!/bin/sh | #!/bin/sh | ||||
# $FreeBSD$ | # $FreeBSD$ | ||||
# Copyright (c) 2009 Douglas Barton | # Copyright (c) 2009 Douglas Barton | ||||
# Copyright (c) 2014 Lars Engels | |||||
# Copyright (c) 2016 Allan Jude <allanjude@FreeBSD.org> | |||||
# All rights reserved. | # All rights reserved. | ||||
# | # | ||||
# Redistribution and use in source and binary forms, with or without | # Redistribution and use in source and binary forms, with or without | ||||
# modification, are permitted provided that the following conditions | # modification, are permitted provided that the following conditions | ||||
# are met: | # are met: | ||||
# 1. Redistributions of source code must retain the above copyright | # 1. Redistributions of source code must retain the above copyright | ||||
# notice, this list of conditions and the following disclaimer. | # notice, this list of conditions and the following disclaimer. | ||||
# 2. Redistributions in binary form must reproduce the above copyright | # 2. Redistributions in binary form must reproduce the above copyright | ||||
Show All 13 Lines | |||||
# SUCH DAMAGE. | # SUCH DAMAGE. | ||||
. /etc/rc.subr | . /etc/rc.subr | ||||
load_rc_config 'XXX' | load_rc_config 'XXX' | ||||
usage () { | usage () { | ||||
echo '' | echo '' | ||||
echo 'Usage:' | echo 'Usage:' | ||||
echo "${0##*/} -e" | echo "${0##*/} [-c /path/to/rc.conf] <name> enable|disable|delete|opts" | ||||
echo "${0##*/} -R" | echo "${0##*/} -e [-n]" | ||||
echo "${0##*/} [-v] -l | -r" | echo "${0##*/} [-v] -l | -r" | ||||
echo "${0##*/} [-v] <rc.d script> start|stop|etc." | echo "${0##*/} -R" | ||||
echo "${0##*/} -s" | |||||
echo "${0##*/} [-v] <name> start|stop|..." | |||||
echo "${0##*/} -h" | echo "${0##*/} -h" | ||||
echo '' | echo '' | ||||
echo '-c Operate on a specific config file' | |||||
echo '-e Show services that are enabled' | echo '-e Show services that are enabled' | ||||
echo "-R Stop and start enabled $local_startup services" | |||||
echo "-l List all scripts in /etc/rc.d and $local_startup" | echo "-l List all scripts in /etc/rc.d and $local_startup" | ||||
echo '-n Display service name, rather than path to rc.d script' | |||||
echo '-r Show the results of boot time rcorder' | echo '-r Show the results of boot time rcorder' | ||||
echo "-R Stop and start enabled $local_startup services" | |||||
echo '-s Show status of all services that are enabled' | |||||
echo '-v Verbose' | echo '-v Verbose' | ||||
echo '' | echo '' | ||||
} | } | ||||
while getopts 'ehlrRv' COMMAND_LINE_ARGUMENT ; do | do_rc_action () { | ||||
script=$1 | |||||
action=$2 | |||||
cd / | |||||
_rcd= | |||||
for dir in /etc/rc.d $local_startup; do | |||||
if [ -x "$dir/$script" ]; then | |||||
if grep -q ^rcvar "$dir/$script"; then | |||||
_rcd="$dir/$script" | |||||
eval `grep ^name= "$_rcd"` | |||||
eval `grep ^rcvar "$_rcd"` | |||||
if [ -n "$rcvar" ]; then | |||||
load_rc_config_var "${name}" "${rcvar}" | |||||
fi | |||||
fi | |||||
fi | |||||
done | |||||
if [ -z "$rcvar" ]; then | |||||
if [ -n "$VERBOSE" ]; then | |||||
echo "Unable to determine rcvar for $script" | |||||
fi | |||||
exit 1 | |||||
fi | |||||
_sysrc_flags="-s $name -e" | |||||
if [ -d "/etc/rc.conf.d" ]; then | |||||
_conf="${CONFFILE:-/etc/rc.conf.d/$name}" | |||||
_sysrc_conf="-f $_conf" | |||||
else | |||||
_conf="${CONFFILE:-/etc/rc.conf}" | |||||
fi | |||||
CONFDIR="$( dirname "$_conf" )" | |||||
if [ ! -e "$CONFDIR" ]; then | |||||
mkdir -p "$CONFDIR" | |||||
fi | |||||
case "$action" in | |||||
"enable") | |||||
/usr/sbin/sysrc $_sysrc_conf $_sysrc_flags $rcvar=YES | |||||
exitflag=$? | |||||
;; | |||||
"disable") | |||||
/usr/sbin/sysrc $_sysrc_conf $_sysrc_flags $rcvar=NO | |||||
exitflag=$? | |||||
;; | |||||
"delete") | |||||
/usr/sbin/sysrc $_sysrc_conf $_sysrc_flags -x $rcvar | |||||
exitflag=$? | |||||
;; | |||||
"opts") | |||||
_rcopts= | |||||
_oifs=$IFS | |||||
IFS=' | |||||
' | |||||
for _l in $(grep '^: ${' $_rcd); do | |||||
_v=${_l##: \$\{} #strip leading bits | |||||
_v=${_v%%:*} #strip conditional sets | |||||
_v=${_v%%=*} #strip unconditional sets | |||||
if [ -z "$_v" ]; then | |||||
continue | |||||
fi | |||||
_rcopts="$_rcopts $_v" | |||||
done | |||||
IFS=$_oifs | |||||
echo "$rcvar" | |||||
for _v in $_rcopts; do | |||||
if [ "$_v" = "$rcvar" ]; then | |||||
continue | |||||
fi | |||||
echo $_v | |||||
done | |||||
;; | |||||
*) | |||||
echo "Error: unexpected action $action" | |||||
exit 1 | |||||
;; | |||||
esac | |||||
return $exitflag | |||||
} | |||||
while getopts 'c:ehlnrRsv' COMMAND_LINE_ARGUMENT ; do | |||||
case "${COMMAND_LINE_ARGUMENT}" in | case "${COMMAND_LINE_ARGUMENT}" in | ||||
c) CONFFILE=$OPTARG ;; | |||||
e) ENABLED=eopt ;; | e) ENABLED=eopt ;; | ||||
h) usage ; exit 0 ;; | h) usage ; exit 0 ;; | ||||
l) LIST=lopt ;; | l) LIST=lopt ;; | ||||
n) NAME=nopt ;; | |||||
r) RCORDER=ropt ;; | r) RCORDER=ropt ;; | ||||
R) RESTART=Ropt ;; | R) RESTART=Ropt ;; | ||||
s) ENABLED=sopt ;; | |||||
v) VERBOSE=vopt ;; | v) VERBOSE=vopt ;; | ||||
*) usage ; exit 1 ;; | *) usage ; exit 1 ;; | ||||
esac | esac | ||||
done | done | ||||
shift $(( $OPTIND - 1 )) | shift $(( $OPTIND - 1 )) | ||||
if [ -n "$RESTART" ]; then | if [ -n "$RESTART" ]; then | ||||
skip="-s nostart" | skip="-s nostart" | ||||
if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then | if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then | ||||
skip="$skip -s nojail" | skip="$skip -s nojail" | ||||
fi | fi | ||||
[ -n "$local_startup" ] && find_local_scripts_new | [ -n "$local_startup" ] && find_local_scripts_new | ||||
files=`rcorder ${skip} ${local_rc} 2>/dev/null` | files=`rcorder ${skip} ${local_rc} 2>/dev/null` | ||||
for file in `reverse_list ${files}`; do | for file in `reverse_list ${files}`; do | ||||
if grep -q ^rcvar $file; then | if grep -q ^rcvar $file; then | ||||
eval `grep ^name= $file` | eval `grep ^name= "$file"` | ||||
eval `grep ^rcvar $file` | eval `grep ^rcvar "$file"` | ||||
if [ -n "$rcvar" ]; then | if [ -n "$rcvar" ]; then | ||||
load_rc_config_var ${name} ${rcvar} | load_rc_config_var "${name}" "${rcvar}" | ||||
fi | fi | ||||
checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop | checkyesno $rcvar 2>/dev/null && | ||||
run_rc_script "${file}" stop | |||||
fi | fi | ||||
done | done | ||||
for file in $files; do | for file in $files; do | ||||
if grep -q ^rcvar $file; then | if grep -q ^rcvar "$file"; then | ||||
eval `grep ^name= $file` | eval `grep ^name= "$file"` | ||||
eval `grep ^rcvar $file` | eval `grep ^rcvar "$file"` | ||||
checkyesno $rcvar 2>/dev/null && run_rc_script ${file} start | checkyesno $rcvar 2>/dev/null && | ||||
run_rc_script "${file}" start | |||||
fi | fi | ||||
done | done | ||||
exit 0 | exit 0 | ||||
fi | fi | ||||
if [ -n "$ENABLED" -o -n "$RCORDER" ]; then | if [ -n "$ENABLED" -o -n "$RCORDER" ]; then | ||||
# Copied from /etc/rc | # Copied from /etc/rc | ||||
skip="-s nostart" | skip="-s nostart" | ||||
if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then | if [ `/sbin/sysctl -n security.jail.jailed` -eq 1 ]; then | ||||
skip="$skip -s nojail" | skip="$skip -s nojail" | ||||
fi | fi | ||||
[ -n "$local_startup" ] && find_local_scripts_new | [ -n "$local_startup" ] && find_local_scripts_new | ||||
files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null` | files=`rcorder ${skip} /etc/rc.d/* ${local_rc} 2>/dev/null` | ||||
fi | fi | ||||
if [ -n "$ENABLED" ]; then | if [ -n "$ENABLED" ]; then | ||||
for file in $files; do | for file in $files; do | ||||
if grep -q ^rcvar $file; then | if grep -q ^rcvar "$file"; then | ||||
eval `grep ^name= $file` | eval `grep ^name= "$file"` | ||||
eval `grep ^rcvar $file` | eval `grep ^rcvar "$file"` | ||||
if [ -n "$rcvar" ]; then | if [ -n "$rcvar" ]; then | ||||
load_rc_config_var ${name} ${rcvar} | load_rc_config_var "${name}" "${rcvar}" | ||||
fi | fi | ||||
checkyesno $rcvar 2>/dev/null && echo $file | if [ "$ENABLED" = "eopt" ]; then | ||||
if [ -n "$NAME" -a -n "$name" ]; then | |||||
checkyesno $rcvar 2>/dev/null && | |||||
echo $name | |||||
else | |||||
checkyesno $rcvar 2>/dev/null && | |||||
echo $file | |||||
fi | fi | ||||
elif [ "$ENABLED" = "sopt" ]; then | |||||
# Not all services have 'status', only those | |||||
# with a command defined | |||||
command= | |||||
eval `grep ^command= "$file"` | |||||
if [ -n "$command" ]; then | |||||
checkyesno $rcvar 2>/dev/null && | |||||
run_rc_script "${file}" quietstatus | |||||
fi | |||||
fi | |||||
fi | |||||
done | done | ||||
exit 0 | exit 0 | ||||
fi | fi | ||||
if [ -n "$LIST" ]; then | if [ -n "$LIST" ]; then | ||||
for dir in /etc/rc.d $local_startup; do | for dir in /etc/rc.d $local_startup; do | ||||
[ -n "$VERBOSE" ] && echo "From ${dir}:" | [ -n "$VERBOSE" ] && echo "From ${dir}:" | ||||
[ -d ${dir} ] && /bin/ls -1 ${dir} | [ -d "${dir}" ] && /bin/ls -1 "${dir}" | ||||
done | done | ||||
exit 0 | exit 0 | ||||
fi | fi | ||||
if [ -n "$RCORDER" ]; then | if [ -n "$RCORDER" ]; then | ||||
for file in $files; do | for file in $files; do | ||||
echo $file | echo $file | ||||
if [ -n "$VERBOSE" ]; then | if [ -n "$VERBOSE" ]; then | ||||
Show All 9 Lines | |||||
if [ $# -gt 1 ]; then | if [ $# -gt 1 ]; then | ||||
script=$1 | script=$1 | ||||
shift | shift | ||||
else | else | ||||
usage | usage | ||||
exit 1 | exit 1 | ||||
fi | fi | ||||
case "$1" in | |||||
enable | disable | delete | opts) | |||||
# Special command, further processing required | |||||
do_rc_action "$script" "$1" | |||||
exit $? | |||||
;; | |||||
*) | |||||
# Regular command, fall through to normal processing | |||||
;; | |||||
esac | |||||
cd / | cd / | ||||
for dir in /etc/rc.d $local_startup; do | for dir in /etc/rc.d $local_startup; do | ||||
if [ -x "$dir/$script" ]; then | if [ -x "$dir/$script" ]; then | ||||
[ -n "$VERBOSE" ] && echo "$script is located in $dir" | [ -n "$VERBOSE" ] && echo "$script is located in $dir" | ||||
exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin $dir/$script $* | exec env -i HOME=/ PATH=/sbin:/bin:/usr/sbin:/usr/bin "$dir/$script" "$@" | ||||
fi | fi | ||||
done | done | ||||
# If the script was not found | # If the script was not found | ||||
echo "$script does not exist in /etc/rc.d or the local startup" | echo "$script does not exist in /etc/rc.d or the local startup" | ||||
echo "directories (${local_startup}), or is not executable" | echo "directories (${local_startup}), or is not executable" | ||||
exit 1 | exit 1 |