From owner-svn-src-head@freebsd.org Mon Jan 22 03:38:11 2018 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id D3F36ECB493; Mon, 22 Jan 2018 03:38:11 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id AED526896F; Mon, 22 Jan 2018 03:38:11 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E42A514D67; Mon, 22 Jan 2018 03:38:10 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id w0M3cAZ9004584; Mon, 22 Jan 2018 03:38:10 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id w0M3cAtb004583; Mon, 22 Jan 2018 03:38:10 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201801220338.w0M3cAtb004583@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Mon, 22 Jan 2018 03:38:10 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r328243 - head/usr.sbin/service X-SVN-Group: head X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: head/usr.sbin/service X-SVN-Commit-Revision: 328243 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Jan 2018 03:38:11 -0000 Author: kevans Date: Mon Jan 22 03:38:10 2018 New Revision: 328243 URL: https://svnweb.freebsd.org/changeset/base/328243 Log: usr.sbin/service: Fix -j to not be order dependant The introduced -j option is highly dependant on the ordering of arguments, and it exhibited broken behavior in some other circumstances. Fix these issues, and simplify the feature by removing the unneessary double parsing of options. Reviewed by: jilles Differential Revision: https://reviews.freebsd.org/D13952 Modified: head/usr.sbin/service/service.sh Modified: head/usr.sbin/service/service.sh ============================================================================== --- head/usr.sbin/service/service.sh Mon Jan 22 03:12:26 2018 (r328242) +++ head/usr.sbin/service/service.sh Mon Jan 22 03:38:10 2018 (r328243) @@ -49,39 +49,9 @@ usage () { echo '' } -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 +while getopts 'j:ehlrRv' 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 - -OPTIND=1 -while getopts ${accepted_argstr} COMMAND_LINE_ARGUMENT ; do - case "${COMMAND_LINE_ARGUMENT}" in + j) JAIL="${OPTARG}" ;; e) ENABLED=eopt ;; h) usage ; exit 0 ;; l) LIST=lopt ;; @@ -92,6 +62,22 @@ while getopts ${accepted_argstr} COMMAND_LINE_ARGUMENT esac done shift $(( $OPTIND - 1 )) + +if [ -n "${JAIL}" ]; then + # We need to rebuild the command line before passing it on. + # We do not send the -j argument into the jail. + args="" + [ -n "${ENABLED}" ] && args="${args} -e" + [ -n "${LIST}" ] && args="${args} -l" + [ -n "${RCORDER}" ] && args="${args} -r" + [ -n "${RESTART}" ] && args="${args} -R" + [ -n "${VERBOSE}" ] && args="${args} -v" + + # Call jexec(8) with the rebuild args and any positional args that + # were left in $@ + /usr/sbin/jexec -l "${JAIL}" /usr/sbin/service $args "$@" + exit $? +fi if [ -n "$RESTART" ]; then skip="-s nostart"