From 20abe8974c61851015a04ce7aa65c54955d18a35 Mon Sep 17 00:00:00 2001 From: nirenjan Date: Sun, 7 Nov 2021 05:36:51 -0800 Subject: [PATCH] Fix x52d_send_command prototype Prior to this change, the recv call was using the same buflen as that of the send, which meant that the response would be truncated at by the client, while the server was sending the entire message. This was evident by running a Python client which manually called recv with the maximum buffer size. This change updates the prototype to take both a bufin (length of the input buffer), and a bufout (length of the output buffer) argument, instead of a single buflen. With this change, commands work as expected in x52ctl. --- daemon/x52ctl.c | 2 +- daemon/x52d_comm_client.c | 6 +++--- daemon/x52dcomm.h | 5 +++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/daemon/x52ctl.c b/daemon/x52ctl.c index fa291d0..5ab3fd7 100644 --- a/daemon/x52ctl.c +++ b/daemon/x52ctl.c @@ -71,7 +71,7 @@ static int send_command(int sock_fd, int argc, char **argv) buflen += arglen; } - rc = x52d_send_command(sock_fd, buffer, buflen); + rc = x52d_send_command(sock_fd, buffer, buflen, sizeof(buffer)); if (rc >= 0) { if (write(STDOUT_FILENO, buffer, rc) < 0) { perror("write"); diff --git a/daemon/x52d_comm_client.c b/daemon/x52d_comm_client.c index 61905f6..3c40a73 100644 --- a/daemon/x52d_comm_client.c +++ b/daemon/x52d_comm_client.c @@ -51,7 +51,7 @@ int x52d_dial_command(const char *sock_path) return sock; } -int x52d_send_command(int sock_fd, char *buffer, size_t buflen) +int x52d_send_command(int sock_fd, char *buffer, size_t bufin, size_t bufout) { int rc; @@ -60,7 +60,7 @@ int x52d_send_command(int sock_fd, char *buffer, size_t buflen) * Unix sockets should have sufficient capacity to send the full * datagram in a single message. Assume that is the case. */ - rc = send(sock_fd, buffer, buflen, 0); + rc = send(sock_fd, buffer, bufin, 0); if (rc < 0) { // Error if (errno == EINTR) { @@ -77,7 +77,7 @@ int x52d_send_command(int sock_fd, char *buffer, size_t buflen) /* Wait till we get a response */ for (;;) { - rc = recv(sock_fd, buffer, buflen, 0); + rc = recv(sock_fd, buffer, bufout, 0); if (rc < 0) { // Error if (errno == EINTR) { diff --git a/daemon/x52dcomm.h b/daemon/x52dcomm.h index 902facc..b4bc4a7 100644 --- a/daemon/x52dcomm.h +++ b/daemon/x52dcomm.h @@ -75,12 +75,13 @@ int x52d_dial_command(const char *sock_path); * parameters. This is also used to save the returned * response. * - * @param[in] buflen Length of the buffer to hold the returned response. + * @param[in] bufin Length of the command in the input buffer + * @param[in] bufout Maximum length of the response * * @returns number of bytes returned from the server * @returns -1 on an error condition, and \c errno is set accordingly. */ -int x52d_send_command(int sock_fd, char *buffer, size_t buflen); +int x52d_send_command(int sock_fd, char *buffer, size_t bufin, size_t bufout); /** @} */ #ifdef __cplusplus