Skip to content

Commit

Permalink
Introduce NUT_IGNORE_CHECKPROCNAME=true support to skip PID process n…
Browse files Browse the repository at this point in the history
…ame validation if it causes problems [networkupstools#2463]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Jun 14, 2024
1 parent 55ae3e1 commit 0904b93
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 4 deletions.
8 changes: 7 additions & 1 deletion NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,13 @@ https://github.com/networkupstools/nut/milestone/11
that if they can resolve the program name of a running process with
this identifier, that such name matches the current program (avoid
failures to start NUT daemons if PID files are on persistent storage,
and some unrelated program got that PID after a reboot). [#2463]
and some unrelated program got that PID after a reboot). This might
introduce regressions for heavily customized NUT builds (e.g. those
embedded in NAS or similar devices) where binary file names differ
significantly from a `progname` string defined in the respective NUT
source file, so a boolean `NUT_IGNORE_CHECKPROCNAME` environment
variable support was added to optionally disable this verification.
[issue #2463]
- various recipe, documentation and source files were revised to address
respective warnings issued by the new generations of analysis tools.
Expand Down
4 changes: 3 additions & 1 deletion UPGRADING.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ Changes from 2.8.2 to 2.8.3
determine one). This might introduce regressions for heavily customized
NUT builds (e.g. embedded in NAS or similar devices) whose binary file
names differ significantly from a `progname` defined in the respective
NUT source file. [issue #2463]
NUT source file, so a boolean `NUT_IGNORE_CHECKPROCNAME` environment
variable support was added to optionally disable this verification.
[issue #2463]
Changes from 2.8.1 to 2.8.2
Expand Down
17 changes: 16 additions & 1 deletion common/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,7 @@ int checkprocname(pid_t pid, const char *progname)
/* If we can determine the binary path name of the specified "pid",
* check if it matches the assumed name of the current program.
* Returns:
* -3 Skipped because NUT_IGNORE_CHECKPROCNAME is set
* -2 Could not parse a program name (ok to proceed,
* risky - but matches legacy behavior)
* -1 Could not identify a program name (ok to proceed,
Expand All @@ -933,7 +934,7 @@ int checkprocname(pid_t pid, const char *progname)
* varying precision
* Generally speaking, if (checkprocname(...)) then ok to proceed
*/
char *procname = getprocname(pid);
char *procname = NULL, *s;
int ret = -127;
size_t procbasenamelen = 0, progbasenamelen = 0;
/* Track where the last dot is in the basename; 0 means none */
Expand All @@ -944,6 +945,16 @@ int checkprocname(pid_t pid, const char *progname)
char procbasename[PATH_MAX], progbasename[PATH_MAX];
#endif

if ((s = getenv("NUT_IGNORE_CHECKPROCNAME"))) {
/* FIXME: Make server/conf.c::parse_boolean() reusable */
if ( (!strcasecmp(s, "true")) || (!strcasecmp(s, "on")) || (!strcasecmp(s, "yes")) || (!strcasecmp(s, "1"))) {
upsdebugx(1, "%s: skipping because caller set NUT_IGNORE_CHECKPROCNAME", __func__);
ret = -3;
goto finish;
}
}

procname = getprocname(pid);
if (!procname || !progname) {
ret = -1;
goto finish;
Expand Down Expand Up @@ -1064,6 +1075,10 @@ int checkprocname(pid_t pid, const char *progname)
__func__);
break;

case -3:
/* skipped due to envvar, logged above */
break;

default:
upsdebugx(1,
"%s: unexpected result looking for process name "
Expand Down
9 changes: 9 additions & 0 deletions conf/nut.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,12 @@ MODE=none
# `none` Disabled and background() detaches stderr as usual
# `default`/unset/other Not disabled
#NUT_DEBUG_SYSLOG=stderr

# Normally NUT can (attempt to) verify that the program file name matches the
# name associated with a running process, when using PID files to send signals.
# The `NUT_IGNORE_CHECKPROCNAME` boolean toggle allows to quickly skip such
# verification, in case it causes problems (e.g. NUT programs were renamed and
# do not match built-in expectations). This environment variable can also be
# optionally set in init-scripts or service methods for `upsd`, `upsmon` and
# NUT drivers/`upsdrvctl`.
#NUT_IGNORE_CHECKPROCNAME=true
12 changes: 12 additions & 0 deletions docs/man/nut.conf.txt
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ the same journal). Recognized values:
| unset/other | Not disabled
|===========================================================================

*NUT_IGNORE_CHECKPROCNAME*::
Optional, defaults to `false`. Normally NUT can (attempt to) verify that
the program file name matches the name associated with a running process,
when using PID files to send signals.
+
The `NUT_IGNORE_CHECKPROCNAME` boolean toggle allows to quickly skip such
verification, in case it causes problems (e.g. NUT programs were renamed
and do not match built-in expectations).
+
This environment variable can also be optionally set in init-scripts or
service methods for `upsd`, `upsmon` and NUT drivers/`upsdrvctl`.

EXAMPLE
-------

Expand Down
3 changes: 2 additions & 1 deletion docs/nut.dict
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
personal_ws-1.1 en 3168 utf-8
personal_ws-1.1 en 3169 utf-8
AAC
AAS
ABI
Expand Down Expand Up @@ -154,6 +154,7 @@ CERTIDENT
CERTREQUEST
CERTVERIF
CEST
CHECKPROCNAME
CHOST
CHRG
CL
Expand Down
1 change: 1 addition & 0 deletions include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ size_t parseprogbasename(char *buf, size_t buflen, const char *progname, size_t
/* If we can determine the binary path name of the specified "pid",
* check if it matches the assumed name of the current program.
* Returns:
* -3 Skipped because NUT_IGNORE_CHECKPROCNAME is set
* -2 Could not parse a program name (ok to proceed,
* risky - but matches legacy behavior)
* -1 Could not identify a program name (ok to proceed,
Expand Down

0 comments on commit 0904b93

Please sign in to comment.