From 643fc771a209207c8fb282574a84c104d4281872 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 14:32:08 +0200 Subject: [PATCH 01/13] clients/upsmon.c: do not be verbose in loadconfig() if we are just quietly checking killpower flag [#2383] Signed-off-by: Jim Klimov --- clients/upsmon.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clients/upsmon.c b/clients/upsmon.c index 9914307a03..a1fab2bc3c 100644 --- a/clients/upsmon.c +++ b/clients/upsmon.c @@ -3005,6 +3005,13 @@ int main(int argc, char *argv[]) open_syslog(prog); + if (checking_flag) { + /* Do not normally report the UPSes we would monitor, etc. + * from loadconfig() for just checking the killpower flag */ + if (nut_debug_level == 0) + nut_debug_level = -2; + } + loadconfig(); /* CLI debug level can not be smaller than debug_min specified From c441056526ba321ee0650fd6d36c51f2c5286e8c Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 15:49:39 +0200 Subject: [PATCH 02/13] common/common.c, include/common.h: extend with nut_sendsignal_debug_level verbosity toggle [#1782] Signed-off-by: Jim Klimov --- common/common.c | 46 ++++++++++++++++++++++++++++++++++++++-------- include/common.h | 6 ++++++ 2 files changed, 44 insertions(+), 8 deletions(-) diff --git a/common/common.c b/common/common.c index fa6772165c..9525c98869 100644 --- a/common/common.c +++ b/common/common.c @@ -108,6 +108,23 @@ pid_t get_max_pid_t(void) #endif } + /* Normally sendsignalfn(), sendsignalpid() and related methods call + * upslogx() to report issues such as failed fopen() of PID file, + * failed parse of its contents, inability to send a signal (absent + * process or some other issue like permissions). + * Some of these low-level reports look noisy and scary to users, + * others are a bit confusing ("PID file not found... is it bad or + * good, what do I do with that knowledge?") so several consuming + * programs actually parse the returned codes to formulate their + * own messages like "No earlier instance of this daemon was found + * running" and users benefit even less from low-level reports. + * This variable and its values are a bit of internal detail between + * certain NUT programs to hush the low-level reports when they are + * not being otherwise debugged (e.g. nut_debug_level < 1). + * Default value allows all those messages to appear. + */ + int nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; + int nut_debug_level = 0; int nut_log_level = 0; static int upslog_flags = UPSLOG_STDERR; @@ -459,9 +476,10 @@ int sendsignalpid(pid_t pid, int sig) int ret; if (pid < 2 || pid > get_max_pid_t()) { - upslogx(LOG_NOTICE, - "Ignoring invalid pid number %" PRIdMAX, - (intmax_t) pid); + if (nut_debug_level > 0 || nut_sendsignal_debug_level > 0) + upslogx(LOG_NOTICE, + "Ignoring invalid pid number %" PRIdMAX, + (intmax_t) pid); return -1; } @@ -469,7 +487,8 @@ int sendsignalpid(pid_t pid, int sig) ret = kill(pid, 0); if (ret < 0) { - perror("kill"); + if (nut_debug_level > 0 || nut_sendsignal_debug_level > 1) + perror("kill"); return -1; } @@ -478,7 +497,8 @@ int sendsignalpid(pid_t pid, int sig) ret = kill(pid, sig); if (ret < 0) { - perror("kill"); + if (nut_debug_level > 0 || nut_sendsignal_debug_level > 1) + perror("kill"); return -1; } } @@ -513,7 +533,10 @@ pid_t parsepid(const char *buf) if (_pid <= get_max_pid_t()) { pid = (pid_t)_pid; } else { - upslogx(LOG_NOTICE, "Received a pid number too big for a pid_t: %" PRIdMAX, _pid); + if (nut_debug_level > 0 || nut_sendsignal_debug_level > 0) + upslogx(LOG_NOTICE, + "Received a pid number too big for a pid_t: %" + PRIdMAX, _pid); } return pid; @@ -533,12 +556,19 @@ int sendsignalfn(const char *pidfn, int sig) pidf = fopen(pidfn, "r"); if (!pidf) { - upslog_with_errno(LOG_NOTICE, "fopen %s", pidfn); + /* This one happens quite often when a daemon starts + * for the first time and no opponent PID file exists, + * so the cut-off verbosity is higher. + */ + if (nut_debug_level > 0 || + nut_sendsignal_debug_level >= NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE) + upslog_with_errno(LOG_NOTICE, "fopen %s", pidfn); return -3; } if (fgets(buf, sizeof(buf), pidf) == NULL) { - upslogx(LOG_NOTICE, "Failed to read pid from %s", pidfn); + if (nut_debug_level > 0 || nut_sendsignal_debug_level > 2) + upslogx(LOG_NOTICE, "Failed to read pid from %s", pidfn); fclose(pidf); return -2; } diff --git a/include/common.h b/include/common.h index db1168b80d..54e81f31b3 100644 --- a/include/common.h +++ b/include/common.h @@ -347,6 +347,12 @@ void nut_report_config_flags(void); void upsdebugx_report_search_paths(int level, int report_search_paths_builtin); void nut_prepare_search_paths(void); +/* Internal toggle for some NUT programs that deal with signal sending. + * For a detailed rationale comment see common.c */ +extern int nut_sendsignal_debug_level; +#define NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT 6 +#define NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE 5 + extern int nut_debug_level; extern int nut_log_level; From a3525b566952487ca885e76ee031df881e08323b Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 16:02:23 +0200 Subject: [PATCH 03/13] clients/upsmon.c: use nut_sendsignal_debug_level verbosity toggle to hush initial fopen(pidfile) scary noise [#1782] Signed-off-by: Jim Klimov --- clients/upsmon.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/clients/upsmon.c b/clients/upsmon.c index a1fab2bc3c..2fc8970e7d 100644 --- a/clients/upsmon.c +++ b/clients/upsmon.c @@ -2887,6 +2887,8 @@ int main(int argc, char *argv[]) * for probing whether a competing older instance of this program * is running (error if it is). */ + /* Hush the fopen(pidfile) message but let "real errors" be seen */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1; #ifndef WIN32 /* If cmd == 0 we are starting and check if a previous instance * is running by sending signal '0' (i.e. 'kill 0' equivalent) @@ -3000,6 +3002,9 @@ int main(int argc, char *argv[]) exit((cmdret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } + /* Restore the signal errors verbosity */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; + argc -= optind; argv += optind; From 2a4deae8d827e347f603a176a3755c235423cc11 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 17:21:51 +0200 Subject: [PATCH 04/13] drivers/main.c: use nut_sendsignal_debug_level verbosity toggle to hush initial fopen(pidfile) scary noise [#1782] Signed-off-by: Jim Klimov --- drivers/main.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/main.c b/drivers/main.c index cc33fd5f1f..cdd4a1df3f 100644 --- a/drivers/main.c +++ b/drivers/main.c @@ -2034,6 +2034,9 @@ int main(int argc, char **argv) exit(((cmdret < 0) || (((uintmax_t)cmdret) > ((uintmax_t)INT_MAX))) ? 255 : (int)cmdret); } + /* Hush the fopen(pidfile) message but let "real errors" be seen */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1; + #ifndef WIN32 /* Setup PID file to receive signals to communicate with this driver * instance once backgrounded, and to stop a competing older instance. @@ -2224,6 +2227,9 @@ int main(int argc, char **argv) } #endif /* WIN32 */ + /* Restore the signal errors verbosity */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; + /* clear out callback handler data */ memset(&upsh, '\0', sizeof(upsh)); From 90358982e1e146f44ba55e29cb39bfcbc697236f Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 17:22:01 +0200 Subject: [PATCH 05/13] server/upsd.c: use nut_sendsignal_debug_level verbosity toggle to hush initial fopen(pidfile) scary noise [#1782] Signed-off-by: Jim Klimov --- server/upsd.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/server/upsd.c b/server/upsd.c index bd3a1543f7..d8b534487c 100644 --- a/server/upsd.c +++ b/server/upsd.c @@ -2031,6 +2031,8 @@ int main(int argc, char **argv) * for probing whether a competing older instance of this program * is running (error if it is). */ + /* Hush the fopen(pidfile) message but let "real errors" be seen */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1; #ifndef WIN32 /* If cmd == 0 we are starting and check if a previous instance * is running by sending signal '0' (i.e. 'kill 0' equivalent) @@ -2138,6 +2140,9 @@ int main(int argc, char **argv) exit((cmdret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); } + /* Restore the signal errors verbosity */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; + argc -= optind; argv += optind; From e3631d068f759d17b970a89f2491123cdac88f31 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 17:29:31 +0200 Subject: [PATCH 06/13] NEWS.adoc, docs/nut.dict: use nut_sendsignal_debug_level verbosity toggle to hush initial fopen(pidfile) scary noise [#1782] Signed-off-by: Jim Klimov --- NEWS.adoc | 6 ++++++ docs/nut.dict | 4 +++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/NEWS.adoc b/NEWS.adoc index b2a50e3f20..c98d922969 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -40,6 +40,12 @@ https://github.com/networkupstools/nut/milestone/11 - (expected) Bug fixes for fallout possible due to "fightwarn" effort in 2.8.0 + - drivers, upsd, upsmon: reduce "scary noise" about failure to `fopen()` + the PID file (which most of the time means that no previous instance of + the daemon was running to potentially conflict with), especially useless + since in recent NUT releases the verdicts from `sendsignal*()` methods + are analyzed and lead to layman worded situation reports in these programs. + [issue #1782] - upsmon: it was realized that the `POWERDOWNFLAG` must be explicitly set in the configuration file, there is no built-in default in the binary program diff --git a/docs/nut.dict b/docs/nut.dict index 99f099731c..ea9e07aec8 100644 --- a/docs/nut.dict +++ b/docs/nut.dict @@ -1,4 +1,4 @@ -personal_ws-1.1 en 3484 utf-8 +personal_ws-1.1 en 3486 utf-8 AAC AAS ABI @@ -2091,6 +2091,7 @@ fmt fno fontconfig footnoteref +fopen forceshutdown forcessl formatconfig @@ -2988,6 +2989,7 @@ selftest sendback sendline sendmail +sendsignal sequentialized ser seria From 00c24946c20e332378133d09534a80366b2ddcfd Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 6 Apr 2024 18:43:14 +0200 Subject: [PATCH 07/13] clients/upsmon.c: clarify that lack of a running daemon PID file is OK when checking_flag (for POWERDOWNFLAG) Signed-off-by: Jim Klimov --- clients/upsmon.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/clients/upsmon.c b/clients/upsmon.c index 2fc8970e7d..89d85af1d0 100644 --- a/clients/upsmon.c +++ b/clients/upsmon.c @@ -2948,8 +2948,9 @@ int main(int argc, char *argv[]) */ upslogx(LOG_WARNING, "Could not %s PID file " "to see if previous upsmon instance is " - "already running!", - (cmdret == -3 ? "find" : "parse")); + "already running!%s", + (cmdret == -3 ? "find" : "parse"), + (checking_flag ? " This is okay during OS shutdown, which is when checking POWERDOWNFLAG makes most sense." : "")); break; case -1: From 3e6bb04e2b2ea0e06f332234fdf386a22f012d23 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Fri, 12 Apr 2024 11:20:08 +0200 Subject: [PATCH 08/13] drivers/main.c: add debug tracing for interactions with PID file and "Duplicate driver instance" [#2384] Signed-off-by: Jim Klimov --- drivers/main.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/drivers/main.c b/drivers/main.c index 9aa26043cb..f4778c18f8 100644 --- a/drivers/main.c +++ b/drivers/main.c @@ -2141,20 +2141,24 @@ int main(int argc, char **argv) /* away. If not, retry a couple of times. */ for (i = 0; i < 3; i++) { struct stat st; + int sigret; - if (stat(pidfnbuf, &st) != 0) { - /* PID file not found */ + if ((sigret = stat(pidfnbuf, &st)) != 0) { + upsdebugx(1, "PID file %s not found; stat() returned %d (errno=%d): %s", + pidfnbuf, sigret, errno, strerror(errno)); break; } upslogx(LOG_WARNING, "Duplicate driver instance detected (PID file %s exists)! Terminating other driver!", pidfnbuf); - if (sendsignalfn(pidfnbuf, SIGTERM) != 0) { - /* Can't send signal to PID, assume invalid file */ + if ((sigret = sendsignalfn(pidfnbuf, SIGTERM) != 0)) { + upsdebugx(1, "Can't send signal to PID, assume invalid PID file %s; " + "sendsignalfn() returned %d (errno=%d): %s", + pidfnbuf, sigret, errno, strerror(errno)); break; } - /* Allow driver some time to quit */ + upsdebugx(1, "Signal sent without errors, allow the other driver instance some time to quit"); sleep(5); } From a74f65abf7ada2ff553a3fb18cb5e0357788db2b Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Fri, 12 Apr 2024 11:32:14 +0200 Subject: [PATCH 09/13] drivers/main.c, NEWS.adoc: handle "-FF" to process PID files (and competing driver instances) same as when backgrounding [#2384] Signed-off-by: Jim Klimov --- NEWS.adoc | 5 +++++ drivers/main.c | 8 +++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/NEWS.adoc b/NEWS.adoc index 041512fd95..37265e8e8f 100644 --- a/NEWS.adoc +++ b/NEWS.adoc @@ -47,6 +47,11 @@ https://github.com/networkupstools/nut/milestone/11 are analyzed and lead to layman worded situation reports in these programs. [issue #1782, PR #2384] + - drivers started with the `-FF` command-line option (e.g. wrapped into the + systemd units to stay "foregrounded" *and* save a PID file anyway) should + now also handle an existing PID file to interact with the earlier instance + of the driver program, if still running (e.g. started manually). [#2384] + - riello_ser updates: * added `localcalculation` option to compute `battery.runtime` and `battery.charge` if the device provides bogus values [issue #2390, diff --git a/drivers/main.c b/drivers/main.c index f4778c18f8..1e6cbcab2c 100644 --- a/drivers/main.c +++ b/drivers/main.c @@ -2041,10 +2041,11 @@ int main(int argc, char **argv) #ifndef WIN32 /* Setup PID file to receive signals to communicate with this driver - * instance once backgrounded, and to stop a competing older instance. - * Or to send it a signal deliberately. + * instance once backgrounded (or staying foregrounded with `-FF`), + * and to stop a competing older instance. Or to send it a signal + * deliberately. */ - if (cmd || ((foreground == 0) && (!do_forceshutdown))) { + if (cmd || ((foreground == 0 || foreground == 2) && (!do_forceshutdown))) { char pidfnbuf[SMALLBUF]; snprintf(pidfnbuf, sizeof(pidfnbuf), "%s/%s-%s.pid", altpidpath(), progname, upsname); @@ -2462,6 +2463,7 @@ int main(int argc, char **argv) snprintf(pidfnbuf, sizeof(pidfnbuf), "%s/%s-%s.pid", altpidpath(), progname, upsname); pidfn = xstrdup(pidfnbuf); } + /* Probably was saved above already, but better safe than sorry */ upslogx(LOG_WARNING, "Running as foreground process, but saving a PID file anyway"); writepid(pidfn); break; From cfdf63313402a17db323305ad86c81a91b7350de Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Fri, 12 Apr 2024 11:51:50 +0200 Subject: [PATCH 10/13] drivers/main.c: properly indent cmd/pidfile handling and annotate ends of large clauses Signed-off-by: Jim Klimov --- drivers/main.c | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/drivers/main.c b/drivers/main.c index 1e6cbcab2c..982c78747a 100644 --- a/drivers/main.c +++ b/drivers/main.c @@ -2083,21 +2083,21 @@ int main(int argc, char **argv) /* if cmd was nontrivial - speak up below, else be quiet */ upsdebugx(1, "Just failed to send signal, no daemon was running"); break; - } + } /* switch (cmdret) */ - /* We were signalling a daemon, successfully or not - exit now... - * Modulo the possibility of a "reload-or-something" where we - * effectively terminate the old driver and start a new one due - * to configuration changes that were not reloadable. Such mode - * is not implemented currently. - */ - if (cmdret != 0) { - /* sendsignal*() above might have logged more details - * for troubleshooting, e.g. about lack of PID file + /* We were signalling a daemon, successfully or not - exit now... + * Modulo the possibility of a "reload-or-something" where we + * effectively terminate the old driver and start a new one due + * to configuration changes that were not reloadable. Such mode + * is not implemented currently. */ - upslogx(LOG_NOTICE, "Failed to signal the currently running daemon (if any)"); + if (cmdret != 0) { + /* sendsignal*() above might have logged more details + * for troubleshooting, e.g. about lack of PID file + */ + upslogx(LOG_NOTICE, "Failed to signal the currently running daemon (if any)"); # ifdef HAVE_SYSTEMD - switch (cmd) { + switch (cmd) { case SIGCMD_RELOAD: upslogx(LOG_NOTICE, "Try something like " "'systemctl reload nut-driver@%s.service'%s", @@ -2122,7 +2122,7 @@ int main(int argc, char **argv) upsname, (oldpid < 0 ? " or add '-P $PID' argument" : "")); break; - } + } /* switch (cmd) */ /* ... or edit nut-server.service locally to start `upsd -FF` * and so save the PID file for ability to manage the daemon * beside the service framework, possibly confusing things... @@ -2132,10 +2132,10 @@ int main(int argc, char **argv) upslogx(LOG_NOTICE, "Try to add '-P $PID' argument"); } # endif /* HAVE_SYSTEMD */ - } + } /* if (cmdret != 0) */ exit((cmdret == 0) ? EXIT_SUCCESS : EXIT_FAILURE); - } + } /* if (cmd) */ /* Try to prevent that driver is started multiple times. If a PID file */ /* already exists, send a TERM signal to the process and try if it goes */ From b50b5df8e66f0c1ed573fd9149afe06a0ed38406 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Fri, 12 Apr 2024 11:54:22 +0200 Subject: [PATCH 11/13] drivers/main.c: bump (C) year Signed-off-by: Jim Klimov --- drivers/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/main.c b/drivers/main.c index 982c78747a..ed37991edd 100644 --- a/drivers/main.c +++ b/drivers/main.c @@ -4,7 +4,7 @@ 1999 Russell Kroll 2005 - 2017 Arnaud Quette 2017 Eaton (author: Emilien Kia ) - 2017 - 2022 Jim Klimov + 2017 - 2024 Jim Klimov This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by From 4a6cd97ac2325a5b2135cc14c602c5bce6f5bab8 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 13 Apr 2024 13:08:39 +0200 Subject: [PATCH 12/13] drivers/upsdrvctl.c: annotate "ifdef WIN32" locations waiting for #1916 to be solved Signed-off-by: Jim Klimov --- drivers/upsdrvctl.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/drivers/upsdrvctl.c b/drivers/upsdrvctl.c index f01c6b495e..60cb2ba654 100644 --- a/drivers/upsdrvctl.c +++ b/drivers/upsdrvctl.c @@ -233,7 +233,7 @@ static void signal_driver_cmd(const ups_t *ups, } #ifndef WIN32 -/* TODO: implement WIN32 */ +/* TODO: implement WIN32: https://github.com/networkupstools/nut/issues/1916 */ /* handle generally signalling the UPS */ /* Real signals */ #ifndef WIN32 @@ -301,7 +301,7 @@ static void signal_driver_cmd(const ups_t *ups, upslog_with_errno(LOG_ERR, "Signalling %s failed: %d", pidfn, ret); exec_error++; } -#endif /* WIN32 */ +#endif /* WIN32: https://github.com/networkupstools/nut/issues/1916 */ } /* handle generally signalling the UPS with recently raised signal */ @@ -939,7 +939,7 @@ static void help(const char *arg_progname) printf(" -c send via signal to running driver(s)\n"); printf(" supported commands:\n"); #ifndef WIN32 -/* FIXME: port event loop from upsd/upsmon to allow messaging fellow drivers in WIN32 builds */ +/* FIXME: port event loop from upsd/upsmon to allow messaging fellow drivers in WIN32 builds: https://github.com/networkupstools/nut/issues/1916 */ printf(" - data-dump: if the driver still has STDOUT attached (maybe\n"); printf(" to log), dump its currently collected information there\n"); printf(" - reload: re-read configuration files, ignoring changed\n"); @@ -952,7 +952,7 @@ static void help(const char *arg_progname) printf(" based on that count, so the caller can decide the fate of\n"); printf(" the currently running driver instance\n"); #ifndef WIN32 -/* FIXME: port event loop from upsd/upsmon to allow messaging fellow drivers in WIN32 builds */ +/* FIXME: port event loop from upsd/upsmon to allow messaging fellow drivers in WIN32 builds: https://github.com/networkupstools/nut/issues/1916 */ # ifdef SIGCMD_RELOAD_OR_RESTART printf(" - reload-or-restart: re-read configuration files (close the\n"); printf(" old driver instance device connection if needed, and have\n"); @@ -1190,7 +1190,7 @@ int main(int argc, char **argv) signal_flag = SIGCMD_RELOAD_OR_ERROR; } #ifndef WIN32 -/* FIXME: port event loop from upsd/upsmon to allow messaging fellow drivers in WIN32 builds */ +/* FIXME: port event loop from upsd/upsmon to allow messaging fellow drivers in WIN32 builds: https://github.com/networkupstools/nut/issues/1916 */ else if (!strncmp(optarg, "dump", strlen(optarg))) { signal_flag = SIGCMD_DATA_DUMP; @@ -1209,7 +1209,7 @@ int main(int argc, char **argv) if (!strncmp(optarg, "reload-or-exit", strlen(optarg))) { signal_flag = SIGCMD_RELOAD_OR_EXIT; } -#endif /* WIN32 */ +#endif /* WIN32: https://github.com/networkupstools/nut/issues/1916 */ /* bad command given */ if (!signal_flag) { From e9f5133316eb126cd9152d7eebbf0b7c10b0a9c9 Mon Sep 17 00:00:00 2001 From: Jim Klimov Date: Sat, 13 Apr 2024 13:36:18 +0200 Subject: [PATCH 13/13] drivers/upsdrvctl.c: stop_driver(), signal_driver_cmd(): use nut_sendsignal_debug_level verbosity toggle to hush initial fopen(pidfile) scary noise [#1782] Signed-off-by: Jim Klimov --- drivers/upsdrvctl.c | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/drivers/upsdrvctl.c b/drivers/upsdrvctl.c index 60cb2ba654..96d3459f7c 100644 --- a/drivers/upsdrvctl.c +++ b/drivers/upsdrvctl.c @@ -281,6 +281,8 @@ static void signal_driver_cmd(const ups_t *ups, if (testmode) return; + /* Hush the fopen(pidfile) message but let "real errors" be seen */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1; #ifndef WIN32 if (ups->pid == -1) { ret = sendsignalfn(pidfn, cmd); @@ -296,6 +298,8 @@ static void signal_driver_cmd(const ups_t *ups, #else ret = sendsignal(pidfn, cmd); #endif + /* Restore the signal errors verbosity */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; if (ret < 0) { upslog_with_errno(LOG_ERR, "Signalling %s failed: %d", pidfn, ret); @@ -353,6 +357,9 @@ static void stop_driver(const ups_t *ups) if (testmode) return; + /* Hush the fopen(pidfile) message but let "real errors" be seen */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_FOPEN_PIDFILE - 1; + #ifndef WIN32 if (ups->pid == -1) { ret = sendsignalfn(pidfn, SIGTERM); @@ -360,7 +367,7 @@ static void stop_driver(const ups_t *ups) ret = sendsignalpid(ups->pid, SIGTERM); /* reap zombie if this child died */ if (waitpid(ups->pid, NULL, WNOHANG) == ups->pid) { - return; + goto clean_return; } } #else @@ -376,7 +383,7 @@ static void stop_driver(const ups_t *ups) ret = sendsignalpid(ups->pid, SIGKILL); /* reap zombie if this child died */ if (waitpid(ups->pid, NULL, WNOHANG) == ups->pid) { - return; + goto clean_return; } } #else @@ -386,7 +393,7 @@ static void stop_driver(const ups_t *ups) if (ret < 0) { upslog_with_errno(LOG_ERR, "Stopping %s failed", pidfn); exec_error++; - return; + goto clean_return; } } @@ -397,7 +404,7 @@ static void stop_driver(const ups_t *ups) } else { /* reap zombie if this child died */ if (waitpid(ups->pid, NULL, WNOHANG) == ups->pid) { - return; + goto clean_return; } ret = sendsignalpid(ups->pid, 0); } @@ -406,7 +413,7 @@ static void stop_driver(const ups_t *ups) #endif if (ret != 0) { upsdebugx(2, "Sending signal to %s failed, driver is finally down or wrongly owned", pidfn); - return; + goto clean_return; } sleep(1); } @@ -418,7 +425,7 @@ static void stop_driver(const ups_t *ups) } else { /* reap zombie if this child died */ if (waitpid(ups->pid, NULL, WNOHANG) == ups->pid) { - return; + goto clean_return; } ret = sendsignalpid(ups->pid, SIGKILL); } @@ -434,7 +441,7 @@ static void stop_driver(const ups_t *ups) } else { /* reap zombie if this child died */ if (waitpid(ups->pid, NULL, WNOHANG) == ups->pid) { - return; + goto clean_return; } ret = sendsignalpid(ups->pid, 0); } @@ -449,7 +456,7 @@ static void stop_driver(const ups_t *ups) if (ups->pid == -1) { unlink(pidfn); } - return; + goto clean_return; } sleep(1); } @@ -457,6 +464,10 @@ static void stop_driver(const ups_t *ups) upslog_with_errno(LOG_ERR, "Stopping %s failed", pidfn); exec_error++; + +clean_return: + /* Restore the signal errors verbosity */ + nut_sendsignal_debug_level = NUT_SENDSIGNAL_DEBUG_LEVEL_DEFAULT; } void set_exit_flag(const int sig)