Bug 239692 - rc: Environment variables set via ${name}_env are not passed to ${rc_arg}_cmd (e.g., start_cmd)
Summary: rc: Environment variables set via ${name}_env are not passed to ${rc_arg}_cmd...
Status: Closed FIXED
Alias: None
Product: Base System
Classification: Unclassified
Component: conf (show other bugs)
Version: 11.2-RELEASE
Hardware: Any Any
: --- Affects Some People
Assignee: Mateusz Piotrowski
URL: https://reviews.freebsd.org/D21228
Keywords:
Depends on:
Blocks:
 
Reported: 2019-08-07 12:12 UTC by Mateusz Piotrowski
Modified: 2020-11-11 00:27 UTC (History)
5 users (show)

See Also:
0mp: mfc-stable12+
koobs: mfc-stable11-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Mateusz Piotrowski freebsd_committer freebsd_triage 2019-08-07 12:12:40 UTC
Here are the steps to reproduce:

1. Save the following script as /usr/local/etc/rc.d/teststartcmd and make it executable:

```
#!/bin/sh
# PROVIDE: teststartcmd
. /etc/rc.subr
name=teststartcmd
load_rc_config $name
start_cmd="teststartcmd_start"
teststartcmd_start() {
	env
}
run_rc_command "$1"
```

2. Run `sysrc teststartcmd_env="TESTVARIABLE=foo"` to modify rc.conf.

3. Run `service teststartcmd onestart` and observe that TESTVARIABLE is not in the output.

4. Save the following script as /usr/local/etc/rc.d/testcommand and make it executable:

```
#!/bin/sh
# PROVIDE: testcommand
. /etc/rc.subr
name=testcommand
load_rc_config $name
command="/usr/bin/env"
run_rc_command "$1"
```

5. Run `sysrc testcommand_env="TESTVARIABLE=foo"` to modify rc.conf.

6. Run `service testcommand onestart` and observe that TESTVARIABLE is in the output this time.
Comment 1 Mateusz Piotrowski freebsd_committer freebsd_triage 2019-08-12 15:29:43 UTC
I've posted an attempt at addressing this issue: https://reviews.freebsd.org/D21228
Comment 2 commit-hook freebsd_committer freebsd_triage 2019-09-05 14:53:07 UTC
A commit references this bug:

Author: 0mp
Date: Thu Sep  5 14:52:23 UTC 2019
New revision: 351863
URL: https://svnweb.freebsd.org/changeset/base/351863

Log:
  rc: Honor ${name}_env when a custom *_cmd is defined (e.g., start_cmd)

  A user may set ${name}_env variable in rc.conf(5) in order to set additional
  environment variables for a service command.  Unfortunately, at the moment
  this variable is only honored when the command is specified via the command
  variable. Those additional environment variables coming from ${name}_env
  are never set if the service is started via the ${rc_arg}_cmd variable (for
  example start_cmd).

  PR:		239692
  Reviewed by:	bcr, jilles
  Approved by:	src (jilles)
  Differential Revision:	https://reviews.freebsd.org/D21228

Changes:
  head/libexec/rc/rc.subr
  head/share/man/man8/rc.subr.8
Comment 3 Kubilay Kocak freebsd_committer freebsd_triage 2019-09-06 03:33:01 UTC
Re-open for MFC (requested earlier), given the desirability of having this behaviour consistent across supported FreeBSD versions and the limited scope of the change.

Would be great to add this to RELNOTES too
Comment 4 commit-hook freebsd_committer freebsd_triage 2020-11-10 10:20:02 UTC
A commit references this bug:

Author: 0mp
Date: Tue Nov 10 10:19:55 UTC 2020
New revision: 367550
URL: https://svnweb.freebsd.org/changeset/base/367550

Log:
  MFC r351863:

  rc: Honor ${name}_env when a custom *_cmd is defined (e.g., start_cmd)

  A user may set ${name}_env variable in rc.conf(5) in order to set additional
  environment variables for a service command.  Unfortunately, at the moment
  this variable is only honored when the command is specified via the command
  variable. Those additional environment variables coming from ${name}_env
  are never set if the service is started via the ${rc_arg}_cmd variable (for
  example start_cmd).

  PR:		239692
  Reviewed by:	bcr, jilles
  Approved by:	src (jilles)
  Requested by:	koobs

Changes:
_U  stable/12/
  stable/12/libexec/rc/rc.subr
  stable/12/share/man/man8/rc.subr.8
Comment 5 commit-hook freebsd_committer freebsd_triage 2020-11-10 10:41:05 UTC
A commit references this bug:

Author: 0mp
Date: Tue Nov 10 10:40:45 UTC 2020
New revision: 367551
URL: https://svnweb.freebsd.org/changeset/base/367551

Log:
  Add an entry for r351863 (honoring ${name}_env in rc(8) scripts)

  PR:		239692
  Requested by:	koobs

Changes:
  head/RELNOTES
Comment 6 Mateusz Piotrowski freebsd_committer freebsd_triage 2020-11-10 10:42:04 UTC
Done. I'm not MFCing the change to 11-STABLE as there seem to be some conflicts which make my Subversion client hang.
Comment 7 Kubilay Kocak freebsd_committer freebsd_triage 2020-11-10 22:26:52 UTC
Thanks Mateusz!

^Triage: Track (current) explicit non-merge to stable/11

@Kyle Do you have cycles to check the MFC for stable/11 ? Consistency would be nice for this one.
Comment 8 Kyle Evans freebsd_committer freebsd_triage 2020-11-10 22:57:46 UTC
(In reply to Kubilay Kocak from comment #7)

Unfortunately not, but almost certainly the conflict is that the file doesn't exist as written in stable/11 so the diff would not apply cleanly. To be honest, if it's not something we'd issue an EN for, there's not much point in trying to keep stable/11 consistent anymore -- there will not be another release from that branch, so most folks will not see the difference anyways.
Comment 9 Kubilay Kocak freebsd_committer freebsd_triage 2020-11-11 00:27:19 UTC
(In reply to Kyle Evans from comment #8)

Roger, just asking :) Thanks!