]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
cmdmon: fix handling of client access command
authorMiroslav Lichvar <mlichvar@redhat.com>
Tue, 7 Apr 2015 12:07:40 +0000 (14:07 +0200)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 7 Apr 2015 12:07:40 +0000 (14:07 +0200)
Rework the loop to limit the number of iterations to MAX_CLIENT_ACCESSES
and not waste CPU.

cmdmon.c

index 5a8441881964f49e1da53e2ad7afbe8a4d766d24..969098902f2571cc2a1346b07f24cc9711305241 100644 (file)
--- 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);
 }