Skip to content

Commit

Permalink
drivers/*.c, drivers/main.h: introduce and use a common handling_upsd…
Browse files Browse the repository at this point in the history
…rv_shutdown flag to exit or not after upsdrv_shutdown() and individual power-state related INSTCMDs [networkupstools#2670]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Nov 19, 2024
1 parent 33d3b21 commit 2b79651
Show file tree
Hide file tree
Showing 68 changed files with 307 additions and 182 deletions.
7 changes: 5 additions & 2 deletions NEWS.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,11 @@ during a NUT build.
starting the beeper (where supported) to verify that the UPS communications
happen as expected, without compromising the load connected to the UPS.
+
Also defined `EF_EXIT_SUCCESS` and `EF_EXIT_FAILURE` in `include/common.h`
to avoid magic numbers in code like `set_exit_flag(-2)`, and revised whether
it is getting set at all in "killpower" vs. other cases, based on new
`handling_upsdrv_shutdown` internal flag.
+
NOTE: during this overhaul, many older drivers got their first ever supported
INSTCMD such as `shutdown.return`, `shutdown.stayoff` or `load.off`. [#2670]
Expand Down Expand Up @@ -363,8 +368,6 @@ INSTCMD such as `shutdown.return`, `shutdown.stayoff` or `load.off`. [#2670]
`gcc-13`+ whose static analyzers on NUT CI farm complained about some
imperfections after adding newer OS revisions to the population of
build agents. [#2585, #2588]
* defined `EF_EXIT_SUCCESS` and `EF_EXIT_FAILURE` in `include/common.h`
to avoid magic numbers in code like `set_exit_flag(-2)`. [#2670]
- updated `docs/nut-names.txt` with items defined by 42ITy NUT fork. [#2339]
Expand Down
17 changes: 9 additions & 8 deletions drivers/adelsystem_cbi.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ static uint32_t mod_resp_to_us = MODRESP_TIMEOUT_us; /* set the modbus response
static uint32_t mod_byte_to_s = MODBYTE_TIMEOUT_s; /* set the modbus byte time out (us) */
static uint32_t mod_byte_to_us = MODBYTE_TIMEOUT_us; /* set the modbus byte time out (us) */

static char handling_upsdrv_shutdown = 0;

/* initialize alarm structs */
void alrminit(void);

Expand Down Expand Up @@ -482,8 +480,9 @@ void upsdrv_shutdown(void)
* a limitation (on some platforms) of the interface/media
* used for these devices.
*/
handling_upsdrv_shutdown = 1;
loop_shutdown_commands("shutdown.stayoff", NULL);
int ret = loop_shutdown_commands("shutdown.stayoff", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

/* print driver usage info */
Expand Down Expand Up @@ -847,17 +846,19 @@ int upscmd(const char *cmd, const char *arg)
switch (rval) {
case STAT_INSTCMD_FAILED:
case STAT_INSTCMD_INVALID:
if (handling_upsdrv_shutdown)
fatalx(EXIT_FAILURE, "shutdown failed");
upslog_with_errno(LOG_ERR, "instcmd: %s failed", cmd);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
break;
case STAT_INSTCMD_UNKNOWN:
if (handling_upsdrv_shutdown)
fatalx(EXIT_FAILURE, "shutdown not supported");
upslog_with_errno(LOG_ERR, "instcmd: %s not supported", cmd);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
break;
default:
upslogx(LOG_INFO, "shutdown command executed");
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_SUCCESS);
break;
}
} else {
Expand Down
4 changes: 2 additions & 2 deletions drivers/al175.c
Original file line number Diff line number Diff line change
Expand Up @@ -1283,8 +1283,8 @@ void upsdrv_shutdown(void)
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
upslogx(LOG_ERR, "shutdown not supported");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}

/* you may have to check the line status since the commands
Expand Down
4 changes: 3 additions & 1 deletion drivers/apc_modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -1584,7 +1584,9 @@ void upsdrv_shutdown(void)

/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int ret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

Expand Down
4 changes: 3 additions & 1 deletion drivers/apcsmart-old.c
Original file line number Diff line number Diff line change
Expand Up @@ -1069,7 +1069,9 @@ void upsdrv_shutdown(void)

/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int sdret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(sdret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

Expand Down
6 changes: 4 additions & 2 deletions drivers/apcsmart.c
Original file line number Diff line number Diff line change
Expand Up @@ -1753,11 +1753,13 @@ static void upsdrv_shutdown_advanced(void)
/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
char temp[APC_LBUF];
char temp[APC_LBUF];

/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int ret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/apcupsd-ups.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ void upsdrv_shutdown(void)
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
upslogx(LOG_ERR, "shutdown not supported");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/asem.c
Original file line number Diff line number Diff line change
Expand Up @@ -340,8 +340,8 @@ void upsdrv_shutdown(void)
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
upslogx(LOG_ERR, "shutdown not supported");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}

/* you may have to check the line status since the commands
Expand Down
6 changes: 4 additions & 2 deletions drivers/bcmxcp.c
Original file line number Diff line number Diff line change
Expand Up @@ -1954,12 +1954,14 @@ void upsdrv_shutdown(void)
* if the above doesn't work, try shutdown.stayoff */
if (loop_shutdown_commands("shutdown.return,shutdown.stayoff", NULL) == STAT_INSTCMD_HANDLED) {
/* Shutdown successful */
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_SUCCESS);
return;
}

upslogx(LOG_ERR, "Shutdown failed!");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}


Expand Down
4 changes: 3 additions & 1 deletion drivers/belkin.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,9 @@ void upsdrv_updateinfo(void)
/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
loop_shutdown_commands("shutdown.return", NULL);
int ret = loop_shutdown_commands("shutdown.return", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

/* handle "beeper.disable" */
Expand Down
4 changes: 3 additions & 1 deletion drivers/belkinunv.c
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,9 @@ void upsdrv_shutdown(void)

/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int ret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

Expand Down
4 changes: 3 additions & 1 deletion drivers/bestfcom.c
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,9 @@ int instcmd(const char *cmdname, const char *extra)
/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
loop_shutdown_commands("shutdown.return", NULL);
int ret = loop_shutdown_commands("shutdown.return", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

/* list flags and values that you want to receive via -x */
Expand Down
6 changes: 5 additions & 1 deletion drivers/bestfortress.c
Original file line number Diff line number Diff line change
Expand Up @@ -514,9 +514,13 @@ static int upsdrv_setvar (const char *var, const char * data) {
*/
void upsdrv_shutdown(void)
{
int ret = -1;

upsdebugx(2, "%s: begin", __func__);

loop_shutdown_commands("shutdown.return", NULL);
ret = loop_shutdown_commands("shutdown.return", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);

upsdebugx(2, "%s: end", __func__);
}
Expand Down
4 changes: 3 additions & 1 deletion drivers/bestuferrups.c
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,9 @@ int instcmd(const char *cmdname, const char *extra)
/* power down the attached load immediately */
void upsdrv_shutdown(void)
{
loop_shutdown_commands("shutdown.return", NULL);
int ret = loop_shutdown_commands("shutdown.return", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

/* list flags and values that you want to receive via -x */
Expand Down
4 changes: 3 additions & 1 deletion drivers/bestups.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ void upsdrv_shutdown(void)
{
/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int ret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

Expand Down
14 changes: 8 additions & 6 deletions drivers/bicker_ser.c
Original file line number Diff line number Diff line change
Expand Up @@ -860,25 +860,27 @@ void upsdrv_updateinfo(void)

void upsdrv_shutdown(void)
{
int retry;
int retry;

/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int ret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

for (retry = 1; retry <= BICKER_RETRIES; retry++) {
if (bicker_shutdown() > 0) {
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_SUCCESS);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_SUCCESS);
return;
}
}

upslogx(LOG_ERR, "Shutdown failed!");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}

void upsdrv_help(void)
Expand Down
15 changes: 7 additions & 8 deletions drivers/blazer.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,19 +855,19 @@ void upsdrv_shutdown(void)

/* FIXME: Make a name for default original shutdown */
if (device_sdcommands) {
loop_shutdown_commands(NULL, NULL);
int ret = loop_shutdown_commands(NULL, NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
return;
}

/* Stop pending shutdowns */
for (retry = 1; retry <= MAXTRIES; retry++) {

if (blazer_instcmd("shutdown.stop", NULL) != STAT_INSTCMD_HANDLED) {
continue;
}

break;

}

if (retry > MAXTRIES) {
Expand All @@ -876,18 +876,17 @@ void upsdrv_shutdown(void)

/* Shutdown */
for (retry = 1; retry <= MAXTRIES; retry++) {

if (blazer_instcmd("shutdown.return", NULL) != STAT_INSTCMD_HANDLED) {
continue;
}

upslogx(LOG_ERR, "Shutting down in %ld seconds", offdelay);
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_SUCCESS);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_SUCCESS);
return;
}

upslogx(LOG_ERR, "Shutdown failed!");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}
6 changes: 2 additions & 4 deletions drivers/clone-outlet.c
Original file line number Diff line number Diff line change
Expand Up @@ -520,11 +520,9 @@ void upsdrv_shutdown(void)
* impacting the load fed by the UPS.
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
/*
upslogx(LOG_ERR, "shutdown not supported");
set_exit_flag(EF_EXIT_FAILURE);
*/
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/clone.c
Original file line number Diff line number Diff line change
Expand Up @@ -669,8 +669,8 @@ void upsdrv_shutdown(void)
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
upslogx(LOG_ERR, "shutdown not supported");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}
}

Expand Down
4 changes: 2 additions & 2 deletions drivers/dummy-ups.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,8 @@ void upsdrv_shutdown(void)
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
upslogx(LOG_ERR, "shutdown not supported");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}
}

Expand Down
4 changes: 3 additions & 1 deletion drivers/etapro.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,9 @@ upsdrv_updateinfo(void)
void
upsdrv_shutdown(void)
{
loop_shutdown_commands("shutdown.return", NULL);
int ret = loop_shutdown_commands("shutdown.return", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

void
Expand Down
4 changes: 3 additions & 1 deletion drivers/everups.c
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,9 @@ int instcmd(const char *cmdname, const char *extra)

void upsdrv_shutdown(void)
{
loop_shutdown_commands("load.off", NULL);
int ret = loop_shutdown_commands("load.off", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

void upsdrv_help(void)
Expand Down
4 changes: 3 additions & 1 deletion drivers/gamatronic.c
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,9 @@ void upsdrv_updateinfo(void)

void upsdrv_shutdown(void)
{
loop_shutdown_commands("shutdown.return", NULL);
int ret = loop_shutdown_commands("shutdown.return", NULL);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(ret == STAT_INSTCMD_HANDLED ? EF_EXIT_SUCCESS : EF_EXIT_FAILURE);
}

static
Expand Down
4 changes: 2 additions & 2 deletions drivers/generic_gpio_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,8 @@ void upsdrv_shutdown(void)
*/
if (loop_shutdown_commands(NULL, NULL) != STAT_INSTCMD_HANDLED) {
upslogx(LOG_ERR, "shutdown not supported");
/* FIXME: Should the UPS shutdown mean the driver shutdown? */
set_exit_flag(EF_EXIT_FAILURE);
if (handling_upsdrv_shutdown > 0)
set_exit_flag(EF_EXIT_FAILURE);
}
}

Expand Down
Loading

0 comments on commit 2b79651

Please sign in to comment.