From 27f8ad7fd119cf865be0f6dc08cc0b5e960e6da0 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Tue, 7 Apr 2015 14:07:40 +0200 Subject: [PATCH] cmdmon: fix handling of client access command Rework the loop to limit the number of iterations to MAX_CLIENT_ACCESSES and not waste CPU. --- cmdmon.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/cmdmon.c b/cmdmon.c index 5a84418..9690989 100644 --- a/cmdmon.c +++ b/cmdmon.c @@ -1463,7 +1463,7 @@ handle_client_accesses_by_index(CMD_Request *rx_message, CMD_Reply *tx_message) { CLG_Status result; RPT_ClientAccessByIndex_Report report; - unsigned long first_index, n_indices, last_index, n_indices_in_table; + unsigned long first_index, n_indices, n_indices_in_table; int i, j; struct timeval now; @@ -1471,16 +1471,15 @@ handle_client_accesses_by_index(CMD_Request *rx_message, CMD_Reply *tx_message) first_index = ntohl(rx_message->data.client_accesses_by_index.first_index); n_indices = ntohl(rx_message->data.client_accesses_by_index.n_indices); - last_index = first_index + n_indices - 1; + if (n_indices > MAX_CLIENT_ACCESSES) + n_indices = MAX_CLIENT_ACCESSES; tx_message->status = htons(STT_SUCCESS); tx_message->reply = htons(RPY_CLIENT_ACCESSES_BY_INDEX); - for (i = first_index, j = 0; - (i <= last_index) && (j < MAX_CLIENT_ACCESSES); - i++) { - - result = CLG_GetClientAccessReportByIndex(i, &report, now.tv_sec, &n_indices_in_table); + for (i = 0, j = 0; i < n_indices; i++) { + result = CLG_GetClientAccessReportByIndex(first_index + i, &report, + now.tv_sec, &n_indices_in_table); tx_message->data.client_accesses_by_index.n_indices = htonl(n_indices_in_table); switch (result) { @@ -1506,7 +1505,7 @@ handle_client_accesses_by_index(CMD_Request *rx_message, CMD_Reply *tx_message) } } - tx_message->data.client_accesses_by_index.next_index = htonl(i); + tx_message->data.client_accesses_by_index.next_index = htonl(first_index + i); tx_message->data.client_accesses_by_index.n_clients = htonl(j); }