Skip to content

Commit

Permalink
Merge pull request #2631 from desertwitch/strerror-to-helper
Browse files Browse the repository at this point in the history
clients/, common/, drivers/: refactor debug/log strerror to helper funcs
  • Loading branch information
jimklimov authored Sep 23, 2024
2 parents 0f36765 + ee7fba1 commit 7dbf605
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 71 deletions.
20 changes: 8 additions & 12 deletions clients/upssched.c
Original file line number Diff line number Diff line change
Expand Up @@ -692,8 +692,8 @@ static int sock_read(conn_t *conn)
ret = read(conn->fd, &ch, 1);

if (ret > 0)
upsdebugx(6, "read() from fd %d returned %" PRIiSIZE " (bytes): '%c'; errno=%d: %s",
conn->fd, ret, ch, errno, strerror(errno));
upsdebug_with_errno(6, "read() from fd %d returned %" PRIiSIZE " (bytes): '%c'",
conn->fd, ret, ch);

if (ret < 1) {

Expand Down Expand Up @@ -723,15 +723,13 @@ static int sock_read(conn_t *conn)
|| (poll(&pfd, 1, 0) <= 0)
|| errno
) {
upsdebugx(4, "read() from fd %d returned 0; errno=%d: %s",
conn->fd, errno, strerror(errno));
upsdebug_with_errno(4, "read() from fd %d returned 0", conn->fd);
return -1; /* connection closed, probably */
}
if (i == (US_MAX_READ - 1)) {
upsdebugx(4, "read() from fd %d returned 0 "
upsdebug_with_errno(4, "read() from fd %d returned 0 "
"too many times in a row, aborting "
"sock_read(); errno=%d: %s",
conn->fd, errno, strerror(errno));
"sock_read()", conn->fd);
return -1; /* connection closed, probably */
}
continue;
Expand Down Expand Up @@ -783,10 +781,8 @@ static int sock_read(conn_t *conn)
return 1; /* we did some work */
}

upsdebugx(6, "sock_read() from fd %d returned nothing "
"(maybe still collecting the command line); "
"errno=%d: %s",
conn->fd, errno, strerror(errno));
upsdebug_with_errno(6, "sock_read() from fd %d returned nothing "
"(maybe still collecting the command line); ", conn->fd);

return 0; /* fell out without parsing anything */
}
Expand Down Expand Up @@ -1241,7 +1237,7 @@ static void sendcmd(const char *cmd, const char *arg1, const char *arg2)
switch(ret_s) {
/* select error */
case -1:
upslogx(LOG_DEBUG, "parent select error: %s", strerror(errno));
upslog_with_errno(LOG_DEBUG, "parent select error");
break;

/* nothing to read */
Expand Down
4 changes: 2 additions & 2 deletions drivers/adelsystem_cbi.c
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ void upsdrv_shutdown(void)
while ((rval = upscmd("load.off", NULL)) != STAT_INSTCMD_HANDLED && cnt > 0) {
rval = gettimeofday(&start, NULL);
if (rval < 0) {
upslogx(LOG_ERR, "upscmd: gettimeofday: %s", strerror(errno));
upslog_with_errno(LOG_ERR, "upscmd: gettimeofday");
}

/* wait for an increasing time interval before sending shutdown command */
Expand Down Expand Up @@ -792,7 +792,7 @@ long time_elapsed(struct timeval *start)

rval = gettimeofday(&end, NULL);
if (rval < 0) {
upslogx(LOG_ERR, "time_elapsed: %s", strerror(errno));
upslog_with_errno(LOG_ERR, "time_elapsed");
}
if (start->tv_usec < end.tv_usec) {
suseconds_t nsec = (end.tv_usec - start->tv_usec) / 1000000 + 1;
Expand Down
2 changes: 1 addition & 1 deletion drivers/al175.c
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ static int tx(const char *dmsg, /*const*/ raw_data_t frame)

err = ser_send_buf(upsfd, frame.begin, frame_len );
if (err==-1) {
upslogx(LOG_ERR, "failed to send frame to PRS: %s", strerror(errno));
upslog_with_errno(LOG_ERR, "failed to send frame to PRS");
return -1;
}

Expand Down
42 changes: 18 additions & 24 deletions drivers/dstate.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ static void send_to_all(const char *fmt, ...)

if ((ret < 1) || (ret != (ssize_t)buflen)) {
#ifndef WIN32
upsdebugx(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"socket %d failed (ret=%" PRIiSIZE "), disconnecting: %s",
__func__, buflen, (int)conn->fd, ret, strerror(errno));
upsdebug_with_errno(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"socket %d failed (ret=%" PRIiSIZE "), disconnecting.",
__func__, buflen, (int)conn->fd, ret);
#else
upsdebugx(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"handle %p failed (ret=%" PRIiSIZE "), disconnecting: %s",
__func__, buflen, conn->fd, ret, strerror(errno));
upsdebug_with_errno(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"handle %p failed (ret=%" PRIiSIZE "), disconnecting.",
__func__, buflen, conn->fd, ret);
#endif
upsdebugx(6, "%s: failed write: %s", __func__, buf);

Expand Down Expand Up @@ -394,19 +394,13 @@ static int send_to_one(conn_t *conn, const char *fmt, ...)
if (ret < 0) {
/* Hacky bugfix: throttle down for upsd to read that */
#ifndef WIN32
upsdebugx(1, "%s: had to throttle down to retry "
"writing %" PRIiSIZE " bytes to socket %d "
"(ret=%" PRIiSIZE ", errno=%d, strerror=%s): %s",
__func__, buflen, (int)conn->fd,
ret, errno, strerror(errno),
buf);
upsdebug_with_errno(1, "%s: had to throttle down to retry "
"writing %" PRIiSIZE " bytes to socket %d (ret=%" PRIiSIZE ") : %s",
__func__, buflen, (int)conn->fd, ret, buf);
#else
upsdebugx(1, "%s: had to throttle down to retry "
"writing %" PRIiSIZE " bytes to handle %p "
"(ret=%" PRIiSIZE ", errno=%d, strerror=%s): %s",
__func__, buflen, conn->fd,
ret, errno, strerror(errno),
buf);
upsdebug_with_errno(1, "%s: had to throttle down to retry "
"writing %" PRIiSIZE " bytes to handle %p (ret=%" PRIiSIZE ") : %s",
__func__, buflen, conn->fd, ret, buf);
#endif
usleep(200);
#ifndef WIN32
Expand All @@ -427,13 +421,13 @@ static int send_to_one(conn_t *conn, const char *fmt, ...)

if ((ret < 1) || (ret != (ssize_t)buflen)) {
#ifndef WIN32
upsdebugx(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"socket %d failed (ret=%" PRIiSIZE "), disconnecting: %s",
__func__, buflen, (int)conn->fd, ret, strerror(errno));
upsdebug_with_errno(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"socket %d failed (ret=%" PRIiSIZE "), disconnecting.",
__func__, buflen, (int)conn->fd, ret);
#else
upsdebugx(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"handle %p failed (ret=%" PRIiSIZE "), disconnecting: %s",
__func__, buflen, conn->fd, ret, strerror(errno));
upsdebug_with_errno(0, "WARNING: %s: write %" PRIiSIZE " bytes to "
"handle %p failed (ret=%" PRIiSIZE "), disconnecting.",
__func__, buflen, conn->fd, ret);
#endif
upsdebugx(6, "%s: failed write: %s", __func__, buf);
sock_disconnect(conn);
Expand Down
6 changes: 3 additions & 3 deletions drivers/generic_modbus.c
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ void upsdrv_shutdown(void)
while ((rval = upscmd("load.off", NULL)) != STAT_INSTCMD_HANDLED && cnt > 0) {
rval = gettimeofday(&start, NULL);
if (rval < 0) {
upslogx(LOG_ERR, "upscmd: gettimeofday: %s", strerror(errno));
upslog_with_errno(LOG_ERR, "upscmd: gettimeofday");
}

/* wait for an increasing time interval before sending shutdown command */
Expand Down Expand Up @@ -548,7 +548,7 @@ long time_elapsed(struct timeval *start)

rval = gettimeofday(&end, NULL);
if (rval < 0) {
upslogx(LOG_ERR, "time_elapsed: %s", strerror(errno));
upslog_with_errno(LOG_ERR, "time_elapsed");
}
if (start->tv_usec < end.tv_usec) {
suseconds_t nsec = (end.tv_usec - start->tv_usec) / 1000000 + 1;
Expand Down Expand Up @@ -597,7 +597,7 @@ int upscmd(const char *cmd, const char *arg)
if (FSD_pulse_duration != NOTUSED && rval == STAT_INSTCMD_HANDLED) {
rval = gettimeofday(&start, NULL);
if (rval < 0) {
upslogx(LOG_ERR, "upscmd: gettimeofday: %s", strerror(errno));
upslog_with_errno(LOG_ERR, "upscmd: gettimeofday");
}

/* wait for FSD_pulse_duration ms */
Expand Down
37 changes: 11 additions & 26 deletions drivers/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2058,7 +2058,7 @@ int main(int argc, char **argv)
NULL, 0, &tv);

if (cmdret < 0) {
upsdebugx(1, "Socket dialog with the other driver instance: %s", strerror(errno));
upsdebug_with_errno(1, "Socket dialog with the other driver instance");
} else {
upslogx(LOG_INFO, "Request to killpower via running driver returned code %" PRIiSIZE, cmdret);
if (cmdret == 0)
Expand Down Expand Up @@ -2353,17 +2353,15 @@ int main(int argc, char **argv)
int sigret;

if ((sigret = stat(pidfnbuf, &st)) != 0) {
upsdebugx(1, "PID file %s not found; stat() returned %d (errno=%d): %s",
pidfnbuf, sigret, errno, strerror(errno));
upsdebug_with_errno(1, "PID file %s not found; stat() returned %d", pidfnbuf, sigret);
break;
}

upslogx(LOG_WARNING, "Duplicate driver instance detected (PID file %s exists)! Terminating other driver!", pidfnbuf);

if ((sigret = sendsignalfn(pidfnbuf, SIGTERM, progname, 1) != 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));
upsdebug_with_errno(1, "Can't send signal to PID, assume invalid PID file %s; "
"sendsignalfn() returned %d", pidfnbuf, sigret);
break;
}

Expand Down Expand Up @@ -2546,30 +2544,23 @@ int main(int argc, char **argv)
user, group, sockname);

if (grp == NULL) {
upsdebugx(1, "WARNING: could not resolve "
"group name '%s' (%i): %s",
group, errno, strerror(errno)
);
upsdebug_with_errno(1, "WARNING: could not resolve group name '%s'", group);
allOk = 0;
goto sockname_ownership_finished;
} else {
struct stat statbuf;
mode_t mode;

if (INVALID_FD((fd = open(sockname, O_RDWR | O_APPEND)))) {
upsdebugx(1, "WARNING: opening socket file for stat/chown failed "
"(%i), which is rather typical for Unix socket handling: %s",
errno, strerror(errno)
);
upsdebug_with_errno(1, "WARNING: opening socket file for stat/chown failed,"
" which is rather typical for Unix socket handling");
allOk = 0;
}

if ((VALID_FD(fd) && fstat(fd, &statbuf))
|| (INVALID_FD(fd) && stat(sockname, &statbuf))
) {
upsdebugx(1, "WARNING: stat for chown of socket file failed (%i): %s",
errno, strerror(errno)
);
upsdebug_with_errno(1, "WARNING: stat for chown of socket file failed");
allOk = 0;
if (INVALID_FD(fd)) {
/* Can not proceed with ops below */
Expand All @@ -2582,9 +2573,7 @@ int main(int argc, char **argv)
if ((VALID_FD(fd) && fchown(fd, statbuf.st_uid, grp->gr_gid))
|| (INVALID_FD(fd) && chown(sockname, statbuf.st_uid, grp->gr_gid))
) {
upsdebugx(1, "WARNING: chown of socket file failed (%i): %s",
errno, strerror(errno)
);
upsdebug_with_errno(1, "WARNING: chown of socket file failed");
allOk = 0;
}
}
Expand All @@ -2595,9 +2584,7 @@ int main(int argc, char **argv)
) {
/* Logically we'd fail chown above if file
* does not exist or is not accessible */
upsdebugx(1, "WARNING: stat for chmod of socket file failed (%i): %s",
errno, strerror(errno)
);
upsdebug_with_errno(1, "WARNING: stat for chmod of socket file failed");
allOk = 0;
} else {
/* chmod g+rw sockname */
Expand All @@ -2607,9 +2594,7 @@ int main(int argc, char **argv)
if ((VALID_FD(fd) && fchmod(fd, mode))
|| (INVALID_FD(fd) && chmod(sockname, mode))
) {
upsdebugx(1, "WARNING: chmod of socket file failed (%i): %s",
errno, strerror(errno)
);
upsdebug_with_errno(1, "WARNING: chmod of socket file failed");
allOk = 0;
}
}
Expand Down
6 changes: 3 additions & 3 deletions drivers/riello_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ static int Send_USB_Packet(uint8_t *send_str, uint16_t numbytes)
err = usb_bulk_write(udev, 0x2, (usb_ctrl_charbuf) USB_buff_pom, 8, 1000);

if (err < 0) {
upsdebugx(3, "USB: Send_USB_Packet: send_usb_packet, err = %d %s ", err, strerror(errno));
upsdebug_with_errno(3, "USB: Send_USB_Packet: send_usb_packet, err = %d %s ", err, nut_usb_strerror(err));
return err;
}
ussleep(USB_WRITE_DELAY);
Expand Down Expand Up @@ -173,7 +173,7 @@ static int Send_USB_Packet(uint8_t *send_str, uint16_t numbytes)
err = usb_bulk_write(udev, 0x2, (usb_ctrl_charbuf) USB_buff_pom, 8, 1000);

if (err < 0) {
upsdebugx(3, "USB: Send_USB_Packet: send_usb_packet, err = %d %s ", err, strerror(errno));
upsdebug_with_errno(3, "USB: Send_USB_Packet: send_usb_packet, err = %d %s ", err, nut_usb_strerror(err));
return err;
}
ussleep(USB_WRITE_DELAY);
Expand Down Expand Up @@ -201,7 +201,7 @@ static int Get_USB_Packet(uint8_t *buffer)
upsdebugx(3, "read: %02X %02X %02X %02X %02X %02X %02X %02X", inBuf[0], inBuf[1], inBuf[2], inBuf[3], inBuf[4], inBuf[5], inBuf[6], inBuf[7]);

if (err < 0){
upsdebugx(3, "USB: Get_USB_Packet: send_usb_packet, err = %d %s ", err, strerror(errno));
upsdebug_with_errno(3, "USB: Get_USB_Packet: send_usb_packet, err = %d %s ", err, nut_usb_strerror(err));
return err;
}

Expand Down

0 comments on commit 7dbf605

Please sign in to comment.