94084c
commit f988b7f228851370d1faa1e8f28d02f4b4e6dc46
94084c
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
94084c
Date:   Thu Nov 25 09:12:00 2021 -0300
94084c
94084c
    linux: Use /proc/stat fallback for __get_nprocs_conf (BZ #28624)
94084c
    
94084c
    The /proc/statm fallback was removed by f13fb81ad3159 if sysfs is
94084c
    not available, reinstate it.
94084c
    
94084c
    Checked on x86_64-linux-gnu.
94084c
    (cherry-picked from commit 137ed5ac440a4d3cf4178ce97f349b349a9c2c66)
94084c
94084c
diff --git a/sysdeps/unix/sysv/linux/getsysstats.c b/sysdeps/unix/sysv/linux/getsysstats.c
94084c
index d70ed9586950615c..7fc6521942e87293 100644
94084c
--- a/sysdeps/unix/sysv/linux/getsysstats.c
94084c
+++ b/sysdeps/unix/sysv/linux/getsysstats.c
94084c
@@ -108,6 +108,37 @@ next_line (int fd, char *const buffer, char **cp, char **re,
94084c
   return res == *re ? NULL : res;
94084c
 }
94084c
 
94084c
+static int
94084c
+get_nproc_stat (char *buffer, size_t buffer_size)
94084c
+{
94084c
+  char *buffer_end = buffer + buffer_size;
94084c
+  char *cp = buffer_end;
94084c
+  char *re = buffer_end;
94084c
+
94084c
+  /* Default to an SMP system in case we cannot obtain an accurate
94084c
+     number.  */
94084c
+  int result = 2;
94084c
+
94084c
+  const int flags = O_RDONLY | O_CLOEXEC;
94084c
+  int fd = __open_nocancel ("/proc/stat", flags);
94084c
+  if (fd != -1)
94084c
+    {
94084c
+      result = 0;
94084c
+
94084c
+      char *l;
94084c
+      while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
94084c
+	/* The current format of /proc/stat has all the cpu* entries
94084c
+	   at the front.  We assume here that stays this way.  */
94084c
+	if (strncmp (l, "cpu", 3) != 0)
94084c
+	  break;
94084c
+	else if (isdigit (l[3]))
94084c
+	  ++result;
94084c
+
94084c
+      __close_nocancel_nostatus (fd);
94084c
+    }
94084c
+
94084c
+  return result;
94084c
+}
94084c
 
94084c
 int
94084c
 __get_nprocs (void)
94084c
@@ -163,30 +194,7 @@ __get_nprocs (void)
94084c
 	return result;
94084c
     }
94084c
 
94084c
-  cp = buffer_end;
94084c
-  re = buffer_end;
94084c
-
94084c
-  /* Default to an SMP system in case we cannot obtain an accurate
94084c
-     number.  */
94084c
-  result = 2;
94084c
-
94084c
-  fd = __open_nocancel ("/proc/stat", flags);
94084c
-  if (fd != -1)
94084c
-    {
94084c
-      result = 0;
94084c
-
94084c
-      while ((l = next_line (fd, buffer, &cp, &re, buffer_end)) != NULL)
94084c
-	/* The current format of /proc/stat has all the cpu* entries
94084c
-	   at the front.  We assume here that stays this way.  */
94084c
-	if (strncmp (l, "cpu", 3) != 0)
94084c
-	  break;
94084c
-	else if (isdigit (l[3]))
94084c
-	  ++result;
94084c
-
94084c
-      __close_nocancel_nostatus (fd);
94084c
-    }
94084c
-
94084c
-  return result;
94084c
+  return get_nproc_stat (buffer, buffer_size);
94084c
 }
94084c
 libc_hidden_def (__get_nprocs)
94084c
 weak_alias (__get_nprocs, get_nprocs)
94084c
@@ -220,7 +228,9 @@ __get_nprocs_conf (void)
94084c
       return count;
94084c
     }
94084c
 
94084c
-  return 1;
94084c
+  enum { buffer_size = 1024 };
94084c
+  char buffer[buffer_size];
94084c
+  return get_nproc_stat (buffer, buffer_size);
94084c
 }
94084c
 libc_hidden_def (__get_nprocs_conf)
94084c
 weak_alias (__get_nprocs_conf, get_nprocs_conf)