File libgtop-bsc1020294-cpu-buffer.patch of Package libgtop

From b22fddaaa5dc3d667f4b5168c7e95979cf8f7910 Mon Sep 17 00:00:00 2001
From: Mike Gorse <mgorse@suse.com>
Date: Wed, 18 Jan 2017 16:40:52 -0600
Subject: [PATCH] linux: Dynamically allocate the buffer to parse /proc/cpuinfo

On a server with many cores, /proc/cpuinfo might contain more than 64k of
data, so we weren't fully parsing it, meaning that we'd see fewer cores than
were present.

https://bugzilla.gnome.org/show_bug.cgi?id=323354
---
 sysdeps/linux/glibtop_private.c | 63 +++++++++++++++++++++++++++++++++++++++++
 sysdeps/linux/glibtop_private.h |  4 +++
 sysdeps/linux/sysinfo.c         |  6 ++--
 3 files changed, 71 insertions(+), 2 deletions(-)

diff --git a/sysdeps/linux/glibtop_private.c b/sysdeps/linux/glibtop_private.c
index 52c3782..8c50723 100644
--- a/sysdeps/linux/glibtop_private.c
+++ b/sysdeps/linux/glibtop_private.c
@@ -133,7 +133,70 @@ file_to_buffer(glibtop *server, char *buffer, size_t bufsiz, const char *filenam
 	}
 }
 
+char *file_to_buffer_alloc(const char *format, ...)
+{
+	char path[4096];
+	int fd;
+	char *buffer = NULL;
+	size_t len = 0;
+	size_t bufsiz = 16384;
+	ssize_t nread = 0;
+	va_list pa;
+
+	va_start(pa, format);
+
+	/* C99 also provides vsnprintf */
+	g_vsnprintf(path, sizeof path, format, pa);
 
+	va_end(pa);
+
+	buffer = (char *)malloc(bufsiz);
+	if (!buffer)
+		return NULL;
+
+	buffer [0] = '\0';
+
+	if((fd = open (path, O_RDONLY)) < 0)
+		return NULL;
+
+	for (;;) {
+		nread = read (fd, buffer + len, bufsiz - len - 1);
+
+		if (G_UNLIKELY(nread < 0)) {
+			if (errno == EINTR)
+				continue;
+			else
+				break;
+		}
+
+		len += nread;
+
+		if (nread == 0)
+			break;
+
+		if (len >= bufsiz - 1) {
+			char *new;
+			bufsiz *= 2;
+			new = realloc (buffer, bufsiz);
+			if (!new) {
+				free (buffer);
+			return NULL;
+			}
+			buffer = new;
+		}
+	}
+
+	close (fd);
+
+	if (nread < 0) {
+		free (buffer);
+		return NULL;
+	}
+
+	buffer [len] = '\0';
+
+	return buffer;
+}
 
 
 static unsigned long
diff --git a/sysdeps/linux/glibtop_private.h b/sysdeps/linux/glibtop_private.h
index 149832a..0ff2ea0 100644
--- a/sysdeps/linux/glibtop_private.h
+++ b/sysdeps/linux/glibtop_private.h
@@ -78,6 +78,10 @@ file_to_buffer(glibtop *server, char *buffer, size_t bufsiz, const char *filenam
 int
 try_file_to_buffer(char *buffer, size_t bufsiz, const char *format, ...) G_GNUC_PRINTF(3, 4);
 
+/* Returns an allocated buffer to be freed with free(), or NULL on error */
+char *
+file_to_buffer_alloc(const char *format, ...) G_GNUC_PRINTF(1, 2);
+
 
 /* some inline functions that wrap proc path
  * as fast as macros :)
diff --git a/sysdeps/linux/sysinfo.c b/sysdeps/linux/sysinfo.c
index bd80b0d..14bfd8b 100644
--- a/sysdeps/linux/sysinfo.c
+++ b/sysdeps/linux/sysinfo.c
@@ -36,12 +36,13 @@ static glibtop_sysinfo sysinfo = { .flags = 0 };
 static void
 init_sysinfo (glibtop *server)
 {
-	char buffer [65536];
+	char *buffer;
 	gchar ** processors;
 
 	if(G_LIKELY(sysinfo.flags)) return;
 
-	file_to_buffer(server, buffer, sizeof buffer, FILENAME);
+	buffer = file_to_buffer_alloc(FILENAME);
+	if (!buffer) return;
 
 	/* cpuinfo records are seperated by a blank line */
 	processors = g_strsplit(buffer, "\n\n", 0);
@@ -88,6 +89,7 @@ init_sysinfo (glibtop *server)
 	}
 
 	g_strfreev(processors);
+	g_free(buffer);
 
 	sysinfo.flags = _glibtop_sysdeps_sysinfo;
 }
-- 
2.6.6

openSUSE Build Service is sponsored by