diff options
author | Kyle Evans <kevans@FreeBSD.org> | 2018-01-15 22:24:11 +0000 |
---|---|---|
committer | Kyle Evans <kevans@FreeBSD.org> | 2018-01-15 22:24:11 +0000 |
commit | 65136f65bd1d3294866e5a331ff2c29474f59463 (patch) | |
tree | 16701a0916bc62a3ed0049bdd166585e7d9993af /usr.sbin/service | |
parent | 082f2fb1a69e15b7f2278443ec07e3012459052a (diff) | |
download | src-test2-65136f65bd1d3294866e5a331ff2c29474f59463.tar.gz src-test2-65136f65bd1d3294866e5a331ff2c29474f59463.zip |
service(8): Add support for interfacing with services in jails
Provide a -j option that can take a jail name or id. If -j is specified,
check that the jail exists and proxy the service request through to
service(8) in the jail.
This allows for cleaner workflows when updating services in a jail, turning
the following:
pkg -j dns upgrade
jexec dns service named restart
into:
pkg -j dns upgrade
service -j dns named restart
PR: 223325
Submitted by: David O'Rourke (with slight changes)
MFC after: 2 weeks
Notes
Notes:
svn path=/head/; revision=328032
Diffstat (limited to 'usr.sbin/service')
-rw-r--r-- | usr.sbin/service/service.8 | 9 | ||||
-rwxr-xr-x | usr.sbin/service/service.sh | 41 |
2 files changed, 44 insertions, 6 deletions
diff --git a/usr.sbin/service/service.8 b/usr.sbin/service/service.8 index 81d24a035c4a..75d62d8fae90 100644 --- a/usr.sbin/service/service.8 +++ b/usr.sbin/service/service.8 @@ -24,7 +24,7 @@ .\" .\" $FreeBSD$ .\" -.Dd December 11, 2012 +.Dd January 15, 2018 .Dt SERVICE 8 .Os .Sh NAME @@ -32,13 +32,17 @@ .Nd "control (start/stop/etc.) or list system services" .Sh SYNOPSIS .Nm +.Op Fl j Ao jail name or id Ac .Fl e .Nm +.Op Fl j Ao jail name or id Ac .Fl R .Nm +.Op Fl j Ao jail name or id Ac .Op Fl v .Fl l | r .Nm +.Op Fl j Ao jail name or id Ac .Op Fl v .Ar <rc.d script> start|stop|etc. .Sh DESCRIPTION @@ -54,6 +58,8 @@ the scripts using various criteria. .Pp The options are as follows: .Bl -tag -width F1 +.It Fl j Ao jail name or id Ac +Perform the given actions under the named jail. .It Fl e List services that are enabled. The list of scripts to check is compiled using @@ -107,6 +113,7 @@ The following are examples of typical usage of the command: .Pp .Dl "service named status" +.Dl "service -j dns named status" .Dl "service -rv" .Pp The following programmable completion entry can be use in diff --git a/usr.sbin/service/service.sh b/usr.sbin/service/service.sh index dbcd282c882d..4433e36fd4db 100755 --- a/usr.sbin/service/service.sh +++ b/usr.sbin/service/service.sh @@ -34,12 +34,13 @@ load_rc_config 'XXX' usage () { echo '' echo 'Usage:' - echo "${0##*/} -e" - echo "${0##*/} -R" - echo "${0##*/} [-v] -l | -r" - echo "${0##*/} [-v] <rc.d script> start|stop|etc." + echo "${0##*/} [-j <jail name or id>] -e" + echo "${0##*/} [-j <jail name or id>] -R" + echo "${0##*/} [-j <jail name or id>] [-v] -l | -r" + echo "${0##*/} [-j <jail name or id>] [-v] <rc.d script> start|stop|etc." echo "${0##*/} -h" echo '' + echo "-j Perform actions within the named jail" 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" @@ -48,7 +49,37 @@ usage () { echo '' } -while getopts 'ehlrRv' COMMAND_LINE_ARGUMENT ; do +accepted_argstr='jehlrRv' + +# Only deal with the -j option here. If found, JAIL is set and the opt and +# arg are shifted out. OPTIND is left untouched. We strip the -j option out +# here because we'll be proxying this invocation through to the jail via +# jls(8) instead of handling it ourselves. +while getopts ${accepted_argstr} COMMAND_LINE_ARGUMENT ; do + case "${COMMAND_LINE_ARGUMENT}" in + j) JAIL="$2" ; shift ; shift ;; + esac +done + +# If -j was provided, then we pass everthing along to the jexec command +# and execute `service` within the named JAIL. Provided that the jail +# actually exists, as checked by `jls`. +# We do this so that if the jail does exist, we can then return the exit +# code of `jexec` and it should be the exit code of whatever ran in the jail. +# There is a race condition here in that the jail might exist at `jls` time +# and be gone by `jexec` time, but it shouldn't be a big deal. +if [ -n "$JAIL" ]; then + /usr/sbin/jls -j "$JAIL" 2>/dev/null >/dev/null + if [ $? -ne 0 ]; then + echo "Jail '$JAIL' does not exist." + exit 1 + fi + + /usr/sbin/jexec -l "$JAIL" /usr/sbin/service $* + exit $? +fi + +while getopts ${accepted_argstr} COMMAND_LINE_ARGUMENT ; do case "${COMMAND_LINE_ARGUMENT}" in e) ENABLED=eopt ;; h) usage ; exit 0 ;; |