Skip to content

Commit 3177fff

Browse files
committed
Introduce NUT_DEBUG_PROCNAME envvar (handled for debug logging if setproctag() is called) [networkupstools#3302, networkupstools#3368]
Signed-off-by: Jim Klimov <jimklimov+nut@gmail.com>
1 parent 686a888 commit 3177fff

7 files changed

Lines changed: 102 additions & 5 deletions

File tree

NEWS.adoc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ https://github.com/networkupstools/nut/milestone/12
4646
- common code:
4747
* Introduced `setproctag()` and `getproctag()` (see examples in `upsmon`)
4848
to help track the log messages from massively-forking NUT daemons. [#3084]
49+
* Introduced `NUT_DEBUG_PROCNAME` environment variable support to optionally
50+
log also the process name (or however the program chose to identify itself).
51+
This may be useful when multiple NUT daemons log into the same file or
52+
console, without syslog to prefix the name into each line (e.g. in tests,
53+
single init script systems like Home Assistant, etc.) [PR 3368]
4954
* Extended with plural `checkprocnames()` and `compareprocnames()`,
5055
as well as `sendsignalpidaliases()` and `sendsignalfnaliases()`, for
5156
binaries that expect to have one of several names at the moment (e.g.

common/common.c

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4248,9 +4248,40 @@ static char *proctag = NULL, *proctag_for_upsdebug = NULL,
42484248
static void proctag_cleanup(void)
42494249
{
42504250
if (proctag) {
4251-
upsdebugx(2, "a %s sub-process (%s) is exiting now",
4252-
NUT_STRARG(getmyprocbasename()), getproctag());
4251+
char *pn = xstrdup(getmyprocbasename());
4252+
char *tn = xstrdup(proctag);
4253+
4254+
if (strlen(EXEEXT) > 0) {
4255+
/* TOTHINK: Generalize provided-if-missing strcasestr()? */
4256+
char *s;
4257+
if (pn) {
4258+
s = strstr(pn, EXEEXT);
4259+
if (s) *s='\0';
4260+
}
4261+
4262+
if (tn) {
4263+
s = strstr(tn, EXEEXT);
4264+
if (s) *s='\0';
4265+
}
4266+
}
4267+
4268+
if (pn && tn && !strcmp(pn, tn)) {
4269+
/* Avoid reporting this line as misleading "sub-process"
4270+
* after a plain singular setproctag(progname) call in
4271+
* a NUT program: */
4272+
upsdebugx(2, "a process (%s) is exiting now", pn);
4273+
} else {
4274+
/* Some ptr not set, or strings not equal */
4275+
upsdebugx(2, "a %s sub-process (%s) is exiting now",
4276+
NUT_STRARG(pn), proctag);
4277+
}
4278+
4279+
if (pn)
4280+
free(pn);
4281+
if (tn)
4282+
free(tn);
42534283
}
4284+
42544285
setproctag(NULL);
42554286
}
42564287

@@ -4292,7 +4323,50 @@ void setproctag(const char *tag)
42924323
proctag_for_upsdebug_buflen = strlen(tag) + 2;
42934324
proctag_for_upsdebug = (char *)xcalloc(proctag_for_upsdebug_buflen, sizeof(char));
42944325
if (proctag_for_upsdebug) {
4295-
snprintf(proctag_for_upsdebug, proctag_for_upsdebug_buflen, ":%s", tag);
4326+
char *pn = xstrdup(getmyprocbasename());
4327+
char *tn = xstrdup(tag);
4328+
int tagged = 0;
4329+
4330+
if (strlen(EXEEXT) > 0) {
4331+
/* TOTHINK: Generalize provided-if-missing strcasestr()?
4332+
* One implementation is currently tucked away in
4333+
* libusb0.c because net-snmp may provide another...
4334+
*/
4335+
char *s;
4336+
if (pn) {
4337+
s = strstr(pn, EXEEXT);
4338+
if (s) *s='\0';
4339+
}
4340+
4341+
if (tn) {
4342+
s = strstr(tn, EXEEXT);
4343+
if (s) *s='\0';
4344+
}
4345+
}
4346+
4347+
if (pn && tn && getenv("NUT_DEBUG_PROCNAME") != NULL && strcmp(pn, tn)) {
4348+
/* Only add the process name if asked for and substantially
4349+
* different from tag value -- e.g. do not duplicate text
4350+
* when callers initialize with settagname(progname) */
4351+
char *s = NULL;
4352+
proctag_for_upsdebug_buflen += strlen(pn) + 1;
4353+
s = (char *)xcalloc(proctag_for_upsdebug_buflen, sizeof(char));
4354+
if (s) {
4355+
snprintf(s, proctag_for_upsdebug_buflen, ":%s:%s", pn, tag);
4356+
free(proctag_for_upsdebug);
4357+
proctag_for_upsdebug = s;
4358+
tagged = 1;
4359+
}
4360+
}
4361+
4362+
if (!tagged) {
4363+
snprintf(proctag_for_upsdebug, proctag_for_upsdebug_buflen, ":%s", tag);
4364+
}
4365+
4366+
if (pn)
4367+
free(pn);
4368+
if (tn)
4369+
free(tn);
42964370
}
42974371
}
42984372

conf/nut.conf.sample

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,13 @@ MODE=none
106106
#NUT_DEBUG_PID=true
107107
#export NUT_DEBUG_PID
108108

109+
# Optionally log process name (or however the program chose to identify itself).
110+
# This may be useful when multiple NUT daemons log into the same file or console,
111+
# without syslog to prefix the name into each line (e.g. in tests, single init
112+
# script systems like Home Assistant, etc.).
113+
#NUT_DEBUG_PROCNAME=true
114+
#export NUT_DEBUG_PROCNAME
115+
109116
# Normally NUT can (attempt to) use the syslog or Event Log (WIN32), but the
110117
# environment variable 'NUT_DEBUG_SYSLOG' allows to bypass it, and perhaps keep
111118
# the daemons logging to stderr (useful e.g. in NUT Integration Test suite to

docs/man/nut.conf.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,12 @@ Optionally add current process ID to tags with debug-level identifiers.
139139
This may be useful when many NUT daemons write to the same console or log
140140
file, such as in containers/plugins for Home Assistant, storage appliances...
141141

142+
*NUT_DEBUG_PROCNAME*::
143+
Optionally log process name (or however the program chose to identify itself).
144+
This may be useful when multiple NUT daemons log into the same file or console,
145+
without syslog to prefix the name into each line (e.g. in tests, single init
146+
script systems like Home Assistant, etc.)
147+
142148
*NUT_DEBUG_SYSLOG*::
143149
Optional, unset by default.
144150
Normally NUT can (attempt to) use the syslog or Event Log (WIN32), but the

docs/nut.dict

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
personal_ws-1.1 en 3706 utf-8
1+
personal_ws-1.1 en 3707 utf-8
22
AAC
33
AAS
44
ABI
@@ -963,6 +963,7 @@ PPP
963963
PR
964964
PR'ed
965965
PRIuSIZE
966+
PROCNAME
966967
PROGRA
967968
PROGS
968969
PROTVER

scripts/systemd/README.adoc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ configuration files rather than editing the installed unit files directly
1717
----
1818
# cat /etc/systemd/system/nut-monitor.service.d/debug.conf
1919
[Service]
20-
Environment="NUT_DEBUG_PID=yes"
20+
Environment="NUT_DEBUG_PID=true"
21+
#Environment="NUT_DEBUG_PROCNAME=true"
2122
----
2223
...followed up by `systemctl daemon-reload` and a restart of the unit itself.
2324

tests/NIT/nit.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ export NUT_DEBUG_LEVEL
9898
NUT_DEBUG_PID="true"
9999
export NUT_DEBUG_PID
100100

101+
NUT_DEBUG_PROCNAME="true"
102+
export NUT_DEBUG_PROCNAME
103+
101104
# Just keep upsdrvctl quiet if used in test builds or with the sandbox
102105
NUT_QUIET_INIT_NDE_WARNING="true"
103106
export NUT_QUIET_INIT_NDE_WARNING

0 commit comments

Comments
 (0)