Skip to content

Support envvar for ALTPIDPATH#473

Closed
jimklimov wants to merge 3 commits intonetworkupstools:masterfrom
jimklimov:driver-statepath
Closed

Support envvar for ALTPIDPATH#473
jimklimov wants to merge 3 commits intonetworkupstools:masterfrom
jimklimov:driver-statepath

Conversation

@jimklimov
Copy link
Copy Markdown
Member

@jimklimov jimklimov commented Sep 6, 2017

The STATEPATH defined during build can be overridden via envvar NUT_STATEPATH during run-time, which is useful to e.g. run the recently added "dump" + "cmd line defined" mode of drivers with an otherwise unprivileged account (not nut, not root). Such execution however claims errors saving the PID file of the driver - because that still uses strictly the macro value of ALTPIDPATH if it is defined (and per the configure script, it is always defined - either by user-provided value, or as fallback to STATEPATH value).

This PR adds the same level of flexibility to ALTPIDPATH, allowing it to use NUT_ALTPIDPATH or NUT_STATEPATH envvars if defined, and only then fall back to the built-in macros (ALTPIDPATH if defined - which is always in practice, or STATEPATH otherwise) to match the old, expected and documented behavior.

Also this PR adds handling of empty values for these envvars as if they are not defined (otherwise the combined PID file paths effectively resolve to filesystem-root-based paths which is not what the user may have wanted - and for a root user this can cause unexpected actual writes; the user is still free to define the envvar == "/" explicitly.

Screenshot from before the PR: the state path can be overridden so the process at least starts, but it complains about the inability to save the PID file:

jim@jimoi:~/nut$ NUT_STATEPATH=/tmp ./drivers/snmp-ups -s testsnmp -x port=10.10.10.10 -d 1
Network UPS Tools - Generic SNMP UPS driver 1.02 (2.7.4-1672-gf1b4343c)
writepid: fopen /var/state/ups/snmp-ups-dmf-testsnmp.pid: Permission denied
Detected Eaton 9PX on host 10.10.10.10 (mib: mge 0.51)

ambient.humidity: 0
ambient.temperature: 0.0
...

Screenshots with this PR in place:

  • Both values defined and empty:
$ NUT_ALTPIDPATH="" NUT_STATEPATH="" ./drivers/snmp-ups -s testsnmp -x port=10.10.10.10 -d 1
Network UPS Tools - Generic SNMP UPS driver 1.02 (2.7.4-422-gd7c728c4)
Can't chdir to /var/state/ups: Permission denied
  • NUT_ALTPIDPATH empty, NUT_STATEPATH value defined - so it is the fallback for PID too:
$ NUT_ALTPIDPATH="" NUT_STATEPATH="/tmp/nut" ./drivers/snmp-ups -s testsnmp -x port=10.10.10.10 -d 1
Network UPS Tools - Generic SNMP UPS driver 1.02 (2.7.4-422-gd7c728c4)
Detected Eaton 9PX on host 10.10.10.10 (mib: mge 0.51)
^Z

$ find /tmp/nut \! -type d -ls
2173496361    4 -rw-r--r--   1 jim      jim             6 Sep  6 08:36 /tmp/nut/snmp-ups-testsnmp.pid
3025529069    0 srw-rw----   1 jim      jim             0 Sep  6 08:36 /tmp/nut/snmp-ups-testsnmp
  • ... same as when NUT_ALTPIDPATH is not defined at all:
$ NUT_STATEPATH="/tmp/nut" ./drivers/snmp-ups -s testsnmp -x port=10.10.10.10 -d 1
Network UPS Tools - Generic SNMP UPS driver 1.02 (2.7.4-422-gd7c728c4)
Detected Eaton 9PX on host 10.10.10.10 (mib: mge 0.51)
^Z

$ find /tmp/nut \! -type d -ls
3025529069    4 -rw-r--r--   1 jim      jim             6 Sep  6 08:37 /tmp/nut/snmp-ups-testsnmp.pid
3025508985    0 srw-rw----   1 jim      jim             0 Sep  6 08:37 /tmp/nut/snmp-ups-testsnmp
  • Both defined and different:
$ NUT_ALTPIDPATH="/tmp/nut/pid" NUT_STATEPATH="/tmp/nut/state" ./drivers/snmp-ups -s testsnmp -x port=10.10.10.10 -d 1
Network UPS Tools - Generic SNMP UPS driver 1.02 (2.7.4-422-gd7c728c4)
Detected Eaton 9PX on host 10.10.10.10 (mib: mge 0.51)
^Z

$ find /tmp/nut \! -type d -ls
2184524222    0 srw-rw----   1 jim      jim             0 Sep  6 08:40 /tmp/nut/state/snmp-ups-testsnmp
3025511200    4 -rw-r--r--   1 jim      jim             6 Sep  6 08:40 /tmp/nut/pid/snmp-ups-testsnmp.pid

Interim screenshot - before adding support for empty envvars same as undefined:

$ NUT_ALTPIDPATH="" NUT_STATEPATH=/tmp ./drivers/snmp-ups -s testsnmp -x port=10.10.10.10 -d 1
Network UPS Tools - Generic SNMP UPS driver 1.02 (2.7.4-422-gd7c728c4)
writepid: fopen /snmp-ups-testsnmp.pid: Permission denied
Detected Eaton 9PX on host 10.10.10.10 (mib: mge 0.51)

@aquette
Copy link
Copy Markdown
Member

aquette commented Nov 20, 2017

@jimklimov as for NUT_ALTPIDPATH, please also document in docs/configure.txt and docs/man/upsd.txt.
Would you also be kind enough to add to docs/man/nutupsdrv.txt an "ENVIRONMENT VARIABLES" section, similar to upsd.txt?
thanks

geraldguillaume added a commit to geraldguillaume/nut that referenced this pull request Nov 29, 2017
Signed-off-by: Gerald Guillaume <geraldguillaume@eaton.com>
@aquette
Copy link
Copy Markdown
Member

aquette commented Nov 29, 2017

I've completed the documentation in #507, therefor closing this one

@aquette aquette closed this Nov 29, 2017
clepple added a commit that referenced this pull request Dec 3, 2017
Closes: #300

Notable commits from master:

 * Documentation
 * statepath environment variables (#473 / #507)
 * nut-scanner (issue #500 / pull #502)
 * Fix one parallel build problem (#418)
 * Compute output load if not provided (#484)
@jimklimov jimklimov deleted the driver-statepath branch November 10, 2021 13:17
@jimklimov jimklimov restored the driver-statepath branch December 3, 2021 14:20
@jimklimov jimklimov deleted the driver-statepath branch December 3, 2021 14:40
jimklimov added a commit to jimklimov/nut that referenced this pull request Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants