bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[just for fun] add support to uname for parsing /proc/cpuinfo on Linux


From: Mike Frysinger
Subject: [just for fun] add support to uname for parsing /proc/cpuinfo on Linux
Date: Mon, 30 Jan 2006 19:55:32 -0500
User-agent: KMail/1.9

i'm aware that this probably won't be accepted after reading some of the 
archives [1], i'm just posting it in the hopes that others will find it 
useful ... i've seen too many half-assed patches out there that are almost 
always x86-specific :)

this patch was originally done by a fellow named Carlos E. Gorges 
(address@hidden) but it had quite a few bugs (such as memleaks and 
segfaults) so i rewrote most of it from scratch for Gentoo ... it supports 
most linux architectures now (ive tested on all but cris, frv, and ppc64)

the patch isnt perfect, but it's pretty decent now i think ... comments ? :)

[1] http://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00053.html
-mike
On linux platforms, grok /proc/cpuinfo for the CPU/vendor info.

Prob not suitable for upstream seeing as how it's 100% linux-specific
http://lists.gnu.org/archive/html/bug-coreutils/2005-09/msg00063.html

Patch originally by Carlos E. Gorges <address@hidden>, but 
heavily reworked to suck less.

To add support for additional platforms, check out the show_cpuinfo()
func in the linux/arch/<ARCH>/ source tree of the kernel.

--- coreutils/src/uname.c
+++ coreutils/src/uname.c
@@ -51,6 +51,11 @@
 # include <mach-o/arch.h>
 #endif
 
+#if defined (__linux__)
+# define USE_PROCINFO
+# define UNAME_HARDWARE_PLATFORM
+#endif
+
 #include "system.h"
 #include "error.h"
 #include "quote.h"
@@ -138,6 +143,95 @@ Print certain system information.  With 
   exit (status);
 }
 
+#if defined(USE_PROCINFO)
+
+# if defined(__s390__) || defined(__s390x__)
+#  define CPUINFO_FILE    "/proc/sysinfo"
+#  define CPUINFO_FORMAT  "%[^\t :]%*[ :]%[^\n]\n"
+# else
+#  define CPUINFO_FILE    "/proc/cpuinfo"
+#  define CPUINFO_FORMAT  "%[^\t:]\t:%[^\n]\n"
+# endif
+
+# define PROCINFO_ARCHITECTURE      0
+# define PROCINFO_HARDWARE_PLATFORM 1
+
+static void __eat_cpuinfo_space(char *buf)
+{
+       /* first eat trailing space */
+       char *tmp = buf + strlen(buf) - 1;
+       while (tmp > buf && isspace(*tmp))
+               *tmp-- = '\0';
+       /* then eat leading space */
+       tmp = buf;
+       while (*tmp && isspace(*tmp))
+               *tmp++;
+       if (tmp != buf)
+               memmove(buf, tmp, strlen(tmp)+1);
+}
+
+static int __linux_procinfo (int x, char *fstr, size_t s)
+{
+       FILE *fp;
+
+       char *procinfo_keys[] = {
+               #if defined(__i386__) || defined(__x86_64__)
+                       "model name", "vendor_id"
+               #elif defined(__ia64__)
+                       "family", "vendor"
+               #elif defined(__alpha__)
+                       "cpu model", "system type"
+               #elif defined(sparc) || defined(__sparc__)
+                       "type", "cpu"
+               #elif defined(__hppa__)
+                       "cpu", "model"
+               #elif defined(__mips__)
+                       "cpu model", "system type"
+               #elif defined(__powerpc__) || defined(__powerpc64__)
+                       "cpu", "machine"
+               #elif defined(__arm__)
+                       "Processor", "Hardware"
+               #elif defined(__s390__) || defined(__s390x__)
+                       "Type", "Manufacturer"
+               #elif defined(__sh__)
+                       "cpu family", "machine"
+               #elif defined(__m68k__)
+                       "CPU", "MMU"
+               #elif defined(__cris__)
+                       "cpu", "cpu model"
+               #elif defined(__frv__)
+                       "CPU-Core", "System"
+               #elif defined(bfin)
+                       "CPU", "BOARD Name"
+               #else
+                       "unknown", "unknown"
+               #endif
+       };
+
+       if ((fp = fopen(CPUINFO_FILE, "r")) != NULL) {
+               char key[64], value[257], *ret = NULL;
+
+               while (fscanf(fp, CPUINFO_FORMAT, key, value) != EOF) {
+                       __eat_cpuinfo_space(key);
+                       if (!strcmp(key, procinfo_keys[x])) {
+                               __eat_cpuinfo_space(value);
+                               ret = value;
+                               break;
+                       }
+               }
+               fclose(fp);
+
+               if (ret) {
+                       strncpy(fstr, ret, s);
+                       return 0;
+               }
+       }
+
+       return -1;
+}
+
+#endif
+
 /* Print ELEMENT, preceded by a space if something has already been
    printed.  */
 
@@ -250,10 +344,14 @@ main (int argc, char **argv)
   if (toprint & PRINT_PROCESSOR)
     {
       char const *element = unknown;
-#if HAVE_SYSINFO && defined SI_ARCHITECTURE
+#if ( HAVE_SYSINFO && defined SI_ARCHITECTURE ) || defined(USE_PROCINFO)
       {
        static char processor[257];
+#if defined(USE_PROCINFO)
+       if (0 <= __linux_procinfo (PROCINFO_ARCHITECTURE, processor, sizeof 
processor))
+#else
        if (0 <= sysinfo (SI_ARCHITECTURE, processor, sizeof processor))
+#endif
          element = processor;
       }
 #endif
@@ -306,9 +404,13 @@ main (int argc, char **argv)
       if (element == unknown)
        {
          static char hardware_platform[257];
+#if defined(USE_PROCINFO)
+         if (0 <= __linux_procinfo (PROCINFO_HARDWARE_PLATFORM, 
hardware_platform, sizeof hardware_platform))
+#else
          size_t s = sizeof hardware_platform;
          static int mib[] = { CTL_HW, UNAME_HARDWARE_PLATFORM };
          if (sysctl (mib, 2, hardware_platform, &s, 0, 0) >= 0)
+#endif
            element = hardware_platform;
        }
 #endif

reply via email to

[Prev in Thread] Current Thread [Next in Thread]