From dea403341886d7cc8338fd14461835794625b6a1 Mon Sep 17 00:00:00 2001
From: Jim Warner <james.warner@comcast.net>
Date: Fri, 17 Jun 2016 00:00:00 -0500
Subject: [PATCH] library: bug fix if libnuma.so unavailable, <STAT> api

All our numa logic was well encapsulated under #ifndef
NUMA_DISABLE (as ./configure --disable-numa), with the
exception of 1 case label. The reason it didn't create
any problems is because the sole consumer (top) itself
responded to the presence of #define NUMA_DISABLE too.

But if that option wasn't used, an oops still awaited.

When experimenting with a 32-bit library plus a 64-bit
kernel, a bug was revealed should the numa library not
have been found. Our 'reap' function must tolerate the
potential of a 0 return from that stacks_fetch_tics().

Now, if numa is desired but not possible, we'll be ok.

[ and, we'll build numa history from fresher sources ]

Signed-off-by: Jim Warner <james.warner@comcast.net>
---
 proc/stat.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/proc/stat.c b/proc/stat.c
index 8b7ef63f..e39ef30d 100644
--- a/proc/stat.c
+++ b/proc/stat.c
@@ -751,6 +751,7 @@ static int stacks_fetch_tics (
     this->result.stacks = this->anchor;
     this->fetch.dirty_stacks = 1;
 
+    // callers beware, this might be zero (maybe no libnuma.so) ...
     return this->result.total;
 } // end: stacks_fetch_tics
 
@@ -1029,12 +1030,14 @@ PROCPS_EXPORT struct stat_reaped *procps_stat_reap (
                 return NULL;
             break;
         case STAT_REAP_CPUS_AND_NODES:
-            if (0 > make_numa_hist(info))
-                return NULL;
             if (!stacks_fetch_tics(info, &info->cpus))
                 return NULL;
-            if (!stacks_fetch_tics(info, &info->nodes))
+#ifndef NUMA_DISABLE
+            if (0 > make_numa_hist(info))
                 return NULL;
+            // tolerate an unexpected absence of libnuma.so ...
+            stacks_fetch_tics(info, &info->nodes);
+#endif
             break;
         default:
             return NULL;