Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

clients/, common/, drivers/: refactor debug/log strerror to helper funcs #2631

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
26 changes: 12 additions & 14 deletions common/common.c
desertwitch marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ static int open_sdbus_once(const char *caller) {
r = -EINVAL;
} else {
if (!faultReported)
upsdebugx(1, "%s: Failed to acquire bus for %s() (%d): %s",
__func__, NUT_STRARG(caller), r, strerror(-r));
upsdebug_with_errno(1, "%s: Failed to acquire bus for %s()",
__func__, NUT_STRARG(caller));
}
faultReported = 1;
} else {
Expand Down Expand Up @@ -177,8 +177,8 @@ static int reopen_sdbus_once(int r, const char *caller, const char *purpose)

/* An "Invalid request descriptor" might fit this bill */
default:
upsdebugx(1, "%s for %s() for %s failed (%d) once, will retry D-Bus connection: %s",
__func__, caller, purpose, r, strerror(-r));
upsdebug_with_errno(1, "%s for %s() for %s failed once, will retry D-Bus connection",
__func__, caller, purpose);

close_sdbus_once();
r = open_sdbus_once(caller);
Expand Down Expand Up @@ -244,8 +244,8 @@ TYPE_FD Inhibit(const char *arg_what, const char *arg_who, const char *arg_why,
}

if (r < 0) {
upsdebugx(1, "%s: %s() failed (%d): %s",
__func__, "sd_bus_call_method", r, strerror(-r));
upsdebug_with_errno(1, "%s: %s() failed",
__func__, "sd_bus_call_method");
if (error.message && *(error.message))
upsdebugx(2, "%s: details from libsystemd: %s",
__func__, error.message);
Expand All @@ -258,8 +258,8 @@ TYPE_FD Inhibit(const char *arg_what, const char *arg_who, const char *arg_why,

r = sd_bus_message_read_basic(reply, SD_BUS_TYPE_UNIX_FD, &fd);
if (r < 0) {
upsdebugx(1, "%s: %s() failed (%d): %s",
__func__, "sd_bus_message_read_basic", r, strerror(-r));
upsdebug_with_errno(1, "%s: %s() failed",
__func__, "sd_bus_message_read_basic");
if (isSupported_Inhibit < 0 && !would_reopen_sdbus(r)) {
upsdebugx(1, "%s: %s() failed seemingly due to permissions, marking %s as not supported",
__func__, "sd_bus_message_read_basic", "Inhibit");
Expand All @@ -276,8 +276,7 @@ TYPE_FD Inhibit(const char *arg_what, const char *arg_who, const char *arg_why,
* an alternative sequence of options if needed on older systems */
r = RET_NERRNO(fcntl(fd, F_DUPFD_CLOEXEC, 3));
if (r < 0) {
upsdebugx(1, "%s: fcntl() failed (%d): %s",
__func__, r, strerror(-r));
upsdebug_with_errno(1, "%s: fcntl() failed", __func__);
return fd;
}

Expand Down Expand Up @@ -340,8 +339,8 @@ int isPreparingForSleep(void)
}

if (r < 0) {
upsdebugx(1, "%s: %s() failed (%d): %s",
__func__, "sd_bus_get_property_trivial", r, strerror(-r));
upsdebug_with_errno(1, "%s: %s() failed",
__func__, "sd_bus_get_property_trivial");
if (error.message && *(error.message))
upsdebugx(2, "%s: details from libsystemd: %s",
__func__, error.message);
Expand Down Expand Up @@ -2398,8 +2397,7 @@ int upsnotify(upsnotify_state_t state, const char *fmt, ...)
# if ! DEBUG_SYSTEMD_WATCHDOG
if (!upsnotify_reported_watchdog_systemd)
# endif
upsdebugx(6, "%s: sd_enabled_watchdog query failed: %s",
__func__, strerror(postit));
upsdebug_with_errno(6, "%s: sd_enabled_watchdog query failed", __func__);
} else {
# if ! DEBUG_SYSTEMD_WATCHDOG
if (!upsnotify_reported_watchdog_systemd || postit > 0)
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");
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");
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");
return err;
}

Expand Down
Loading