Skip to content

Commit

Permalink
drivers/main.{c,h}, drivers/dstate.c: introduce main_instcmd_fallback…
Browse files Browse the repository at this point in the history
…() so drivers might implement their own "shutdown.default" at will [networkupstools#2670]

Signed-off-by: Jim Klimov <[email protected]>
  • Loading branch information
jimklimov committed Nov 19, 2024
1 parent 00b291a commit 25250b0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
25 changes: 18 additions & 7 deletions drivers/dstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -847,15 +847,26 @@ static int sock_arg(conn_t *conn, size_t numarg, char **arg)
if (upsh.instcmd) {
ret = upsh.instcmd(cmdname, cmdparam);

/* send back execution result if requested */
if (cmdid)
send_tracking(conn, cmdid, ret);
if (ret != STAT_INSTCMD_UNKNOWN) {
/* send back execution result if requested */
if (cmdid)
send_tracking(conn, cmdid, ret);

/* The command was handled, status is a separate consideration */
return 1;
}
/* The command was handled, status is a separate consideration */
return 1;
} /* else try other handler(s) */
} /* else try other handler(s) */

/* Finally try the fallback handler shared by all drivers */
ret = main_instcmd_fallback(cmdname, cmdparam, conn);
/* send back execution result if requested */
if (cmdid)
send_tracking(conn, cmdid, ret);

upslogx(LOG_NOTICE, "Got INSTCMD, but driver lacks a handler");
if (ret == STAT_INSTCMD_UNKNOWN)
upslogx(LOG_NOTICE,
"Got INSTCMD '%s', but driver lacks a handler",
NUT_STRARG(cmdname));
return 1;
}

Expand Down
28 changes: 26 additions & 2 deletions drivers/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -909,8 +909,9 @@ int upsdrv_shutdown_sdcommands_or_default(const char *sdcmds_default, char **cmd
return sdret;
}

/* handle instant commands common for all drivers */
int main_instcmd(const char *cmdname, const char *extra, conn_t *conn) {
/* handle instant commands common for all drivers - fallback for common
* command names that could be implemented in a driver but were not */
int main_instcmd_fallback(const char *cmdname, const char *extra, conn_t *conn) {
char buf[SMALLBUF];
if (conn)
#ifndef WIN32
Expand Down Expand Up @@ -942,6 +943,29 @@ int main_instcmd(const char *cmdname, const char *extra, conn_t *conn) {
return STAT_INSTCMD_HANDLED;
}

/* By default, the driver-specific values are
* unknown to shared standard handler */
upsdebugx(2, "shared %s() does not handle command %s, "
"proceeding to driver-specific handler",
__func__, cmdname);
return STAT_INSTCMD_UNKNOWN;
}

/* handle instant commands common for all drivers */
int main_instcmd(const char *cmdname, const char *extra, conn_t *conn) {
char buf[SMALLBUF];
if (conn)
#ifndef WIN32
snprintf(buf, sizeof(buf), "socket %d", conn->fd);
#else
snprintf(buf, sizeof(buf), "handle %p", conn->fd);
#endif
else
snprintf(buf, sizeof(buf), "(null)");

upsdebugx(2, "entering main_instcmd(%s, %s) for [%s] on %s",
cmdname, extra, NUT_STRARG(upsname), buf);

if (!strcmp(cmdname, "driver.killpower")) {
/* An implementation of `drivername -k` requested from
* the running and connected driver instance by protocol
Expand Down
4 changes: 4 additions & 0 deletions drivers/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ int upsdrv_shutdown_sdcommands_or_default(const char *sdcmds_default, char **cmd
*/
int main_instcmd(const char *cmdname, const char *extra, conn_t *conn);

/* handle instant commands common for all drivers - fallback for common
* command names that could be implemented in a driver but were not */
int main_instcmd_fallback(const char *cmdname, const char *extra, conn_t *conn);

/* handle setting variables common for all drivers
* (returns STAT_SET_* state values per enum in upshandler.h)
*/
Expand Down

0 comments on commit 25250b0

Please sign in to comment.