]> git.proxmox.com Git - mirror_kronosnet.git/commitdiff
[test] simplify flush log
authorFabio M. Di Nitto <fdinitto@redhat.com>
Wed, 24 Jul 2019 11:46:51 +0000 (13:46 +0200)
committerFabio M. Di Nitto <fdinitto@redhat.com>
Thu, 25 Jul 2019 08:27:34 +0000 (10:27 +0200)
allocate on stack only once and make sure strings are null terminated
drop useless read loop since log msg are always smaller than PAGE_SIZE
and read are atomic at that level

Signed-off-by: Fabio M. Di Nitto <fdinitto@redhat.com>
libknet/tests/test-common.c

index 02afde6cf3c69622fa8d7f9728f2a9143413c288..58216083a3b9a6979da5df1daf19e019ac1da435 100644 (file)
@@ -217,20 +217,17 @@ void close_logpipes(int *logfds)
 
 void flush_logs(int logfd, FILE *std)
 {
+       struct knet_log_msg msg;
+       int len;
+
        while (1) {
-               struct knet_log_msg msg;
-
-               for (size_t bytes_read = 0; bytes_read < sizeof(msg); ) {
-                       int len = read(logfd, &msg + bytes_read,
-                                      sizeof(msg) - bytes_read);
-                       if (len <= 0) {
-                               /*
-                                * clear errno to avoid incorrect propagation
-                                */
-                               errno = 0;
-                               return;
-                       }
-                       bytes_read += len;
+               len = read(logfd, &msg, sizeof(msg));
+               if (len != sizeof(msg)) {
+                       /*
+                        * clear errno to avoid incorrect propagation
+                        */
+                       errno = 0;
+                       return;
                }
 
                if (!msg.knet_h) {
@@ -242,6 +239,9 @@ void flush_logs(int logfd, FILE *std)
                        fprintf(std, "NO HANDLE INFO IN LOG MSG!!\n");
                        abort();
                }
+
+               msg.msg[sizeof(msg.msg) - 1] = 0;
+
                fprintf(std, "[knet]: [%s] %s: %.*s\n",
                        knet_log_get_loglevel_name(msg.msglevel),
                        knet_log_get_subsystem_name(msg.subsystem),