Index: usr.sbin/service/service.8 =================================================================== --- usr.sbin/service/service.8 +++ usr.sbin/service/service.8 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd December 11, 2012 +.Dd September 9, 2015 .Dt SERVICE 8 .Os .Sh NAME @@ -32,15 +32,22 @@ .Nd "control (start/stop/etc.) or list system services" .Sh SYNOPSIS .Nm -.Fl e +.Op Fl c Ar /path/to/rc.conf +.Ar name +.Cm enable|disable|delete|opts .Nm -.Fl R +.Fl e Op Fl n .Nm .Op Fl v .Fl l | r .Nm +.Fl R +.Nm +.Fl s +.Nm .Op Fl v -.Ar start|stop|etc. +.Ar name +.Cm start|stop|... .Sh DESCRIPTION The .Nm @@ -54,8 +61,15 @@ .Pp The options are as follows: .Bl -tag -width F1 +.It Fl c +The enable, disable, delete, and opts sub-commands operate on a specific config +file, rather than defaulting to +.Pa /etc/rc.conf.d/ . .It Fl e -List services that are enabled. +List services that are enabled by their rc.d script. +The +.Fl n +flag causes services to be listed by their name instead. The list of scripts to check is compiled using .Xr rcorder 8 the same way that it is done in @@ -82,9 +96,38 @@ as in .Fl e above, but list all of the files, not just what is enabled. +.It Fl s +Output the status of each enabled service. .It Fl v Be slightly more verbose .El +.Sh SUBCOMMANDS +Any subcommand provided is passed to the rc.d script of the service, with the +exception of the following: +.Bl -tag -width disable +.It Cm enable +Enables the service by setting the _enable rcvar to +.Qq YES . +Unless the +.Fl c +flag is specified, the change is written to +.Pa /etc/rc.conf.d/ . +.It Cm disable +Disables the service by setting the _enable rcvar to +.Qq NO . +Unless the +.Fl c +flag is specified, the change is written to +.Pa /etc/rc.conf.d/ . +.It Cm delete +Deletes any _enable rcvar entries from the config files +If the +.Fl c +flag is specified, only the indicated config file is changed. +.It Cm opts +Attempts to list the possible config options that can be specified for the +service. +.El .Sh ENVIRONMENT When used to run rc.d scripts the .Nm @@ -106,7 +149,10 @@ .Nm command: .Pp +.Dl "service named enable" +.Dl "service named start" .Dl "service named status" +.Dl "service named disable" .Dl "service -rv" .Pp The following programmable completion entry can be use in @@ -123,7 +169,8 @@ .Xr bash 1 Pq Pa ports/shells/bash , .Xr rc.conf 5 , .Xr rc 8 , -.Xr rcorder 8 +.Xr rcorder 8 , +.Xr sysrc 8 .Sh HISTORY The .Nm Index: usr.sbin/service/service.sh =================================================================== --- usr.sbin/service/service.sh +++ usr.sbin/service/service.sh @@ -3,6 +3,8 @@ # $FreeBSD$ # Copyright (c) 2009 Douglas Barton +# Copyright (c) 2014 Lars Engels +# Copyright (c) 2015 Allan Jude # All rights reserved. # # Redistribution and use in source and binary forms, with or without @@ -32,27 +34,120 @@ usage () { echo '' echo 'Usage:' - echo "${0##*/} -e" - echo "${0##*/} -R" + echo "${0##*/} [-c /path/to/rc.conf] enable|disable|delete|opts" + echo "${0##*/} -e [-n]" echo "${0##*/} [-v] -l | -r" - echo "${0##*/} [-v] start|stop|etc." + echo "${0##*/} -R" + echo "${0##*/} -s" + echo "${0##*/} [-v] start|stop|..." echo "${0##*/} -h" echo '' + echo '-c Operate on a specific config file' 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 '-n Display service name, rather than path to rc.d script' 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 '' } -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" + _conf=${CONFFILE:-/etc/rc.conf.d/$name} + CONFDIR=$( dirname $_conf ) + if [ ! -e $CONFDIR ]; then + mkdir -p $CONFDIR + fi + _sysrc_conf="-f $_conf" + 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") + if [ -n "$CONFFILE" ]; then + # Delete from a specific file if indicated + /usr/sbin/sysrc $_sysrc_conf $_sysrc_flags -x $rcvar + exitflag=$? + else + # Delete from any location + /usr/sbin/sysrc $_sysrc_flags -x $rcvar + exitflag=$? + fi + ;; + "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 + c) CONFFILE=$OPTARG ;; e) ENABLED=eopt ;; h) usage ; exit 0 ;; l) LIST=lopt ;; + n) NAME=nopt ;; r) RCORDER=ropt ;; R) RESTART=Ropt ;; + s) ENABLED=sopt ;; v) VERBOSE=vopt ;; *) usage ; exit 1 ;; esac @@ -74,14 +169,16 @@ if [ -n "$rcvar" ]; then load_rc_config_var ${name} ${rcvar} fi - checkyesno $rcvar 2>/dev/null && run_rc_script ${file} stop + checkyesno $rcvar 2>/dev/null && + run_rc_script ${file} stop fi done for file in $files; do if grep -q ^rcvar $file; then eval `grep ^name= $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 done @@ -106,7 +203,24 @@ if [ -n "$rcvar" ]; then load_rc_config_var ${name} ${rcvar} 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 + 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 exit 0 @@ -141,6 +255,17 @@ exit 1 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 / for dir in /etc/rc.d $local_startup; do if [ -x "$dir/$script" ]; then