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