Bug 223325

Summary: usr.sbin/service: Add support for interacting with services in jails.
Product: Base System Reporter: David O'Rourke <dor.bsd>
Component: binAssignee: Kyle Evans <kevans>
Status: Closed FIXED    
Severity: Affects Only Me CC: kevans
Priority: --- Keywords: patch
Version: CURRENTFlags: kevans: mfc-stable11?
Hardware: Any   
OS: Any   
Attachments:
Description Flags
Add jails support to /usr/sbin/service and update man page. none

Description David O'Rourke 2017-10-30 16:32:12 UTC
Created attachment 187586 [details]
Add jails support to /usr/sbin/service and update man page.

This patch adds support to `/usr/sbin/service` for interacting with services within jails.

This is mostly useful when upgrading services within jails as it allows users to stay within the usual flow and tools for upgrading packages and restarting services.
I've been using this patch for a while now personally and everything seems to work as intended.

Consider the following when operating on the jail host, this is the usual flow when some services are updated:

---
# Upgrade all packages
pkg upgrade

# Restart a service that needs it
service named restart
---

If named is within a jail, the flow changes in an inconsistent way.

---
# Upgrade all packages within the dns jail
pkg -j dns upgrade

# Restart the named service within the dns jail.
# Just one of many, perhaps ezjail-admin or some other tool was used to execute
# commands within the jail.
jexec dns service named restart
---

With this patch, users can stay within the more familiar workflow of just using the pkg and service utilities.

---
# Upgrade all packages within the dns jail.
pkg -j dns upgrade

# Restart the named service within the dns jail.
service -j dns named restart
---

The man page was updated to reflect this change and is included in the diff.

The patch to `service.sh` includes comments on was is happening around the -j option, as we end up parsing the options twice.
The first time we check for `-j` and use it if we find it, the second time is parsing the rest of the `service` arguments.
There may be a better way of achieving this, but my shell scripting is rusty.

-David
Comment 1 commit-hook freebsd_committer freebsd_triage 2018-01-15 22:25:14 UTC
A commit references this bug:

Author: kevans
Date: Mon Jan 15 22:24:12 UTC 2018
New revision: 328032
URL: https://svnweb.freebsd.org/changeset/base/328032

Log:
  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

Changes:
  head/usr.sbin/service/service.8
  head/usr.sbin/service/service.sh
Comment 2 commit-hook freebsd_committer freebsd_triage 2018-01-30 19:16:50 UTC
A commit references this bug:

Author: kevans
Date: Tue Jan 30 19:16:38 UTC 2018
New revision: 328599
URL: https://svnweb.freebsd.org/changeset/base/328599

Log:
  MFC r328032,r328060,r328243: service(8): Support services in jails

  MFC r328032: 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

  MFC r328060: service(8): Reset OPTIND properly now that we parse args twice

  r328032 introduced a second round of argument parsing to proxy the request
  through to a jail as needed, but failed to reset OPTIND before getting to
  the second round of parsing to allow other flags to be set.

  MFC r328243: 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.

  PR:		223325

Changes:
_U  stable/11/
  stable/11/usr.sbin/service/service.8
  stable/11/usr.sbin/service/service.sh