Linux C 获取硬件信息的一些方法

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

Linux get hardware information over C

ioctl(input output controller)函数获取磁盘信息

  • 出现同一份代码RedHat上可以拿到 /dev/sda serial number, 在Ubuntu上出错

获取主板信息

出现有些主板找不到serial number,暂时有两种方法:

  • dmidecode工具,github.com上有repo fork
  • /sys/class/dmi/id/ 中直接读取

get mac info

获得Unix/Linux系统中的IP、MAC地址等信息_

.. _获得Unix/Linux系统中的IP、MAC地址等信息: http://programmerdigest.cn/2010/07/1050.html

  • ioctl函数没有纳入POXIS规范,各系统对ioctl的实现也不尽相同
  • ioctl函数的参数只有3个,但却是Unix中少有的几个“家族类”复杂函数.在传统上ioctl函数是用于那些普遍使用、但不适合归入其他类别的任何特殊的系统接口……网络程序(一般是服务器程序)中ioctl常用于在程序启动时获得主机上所有接口的信息:接口的地址、接口是否支持广播、是否支持多播,等等。

get cpu info

  • 方法一:C语言内部实现,使用 libcpuid库_

.. _libcpuid库: http://libcpuid.sourceforge.net/

.. code-block:: c

struct cpu_raw_data_t raw;                                             
struct cpu_id_t data;                                                  

if (cpuid_get_raw_data(&raw) < 0) {                                   
    printf("Sorry, cannot get the CPUID raw data.\n");
    printf("Error: %s\n", cpuid_error());                         
    return -2;
}

if (cpu_identify(&raw, &data) < 0) {                                  
    printf("Sorrry, CPU identification failed.\n");
    printf("Error: %s\n", cpuid_error());
    return -3;
}
printf("Found: %s CPU\n", data.vendor_str);                           
printf("Processor model is `%s'\n", data.cpu_codename);               
printf("The full brand string is `%s'\n", data.brand_str);            
printf("The processor has %dK L1 cache and %dK L2 cache\n", data.l1_data_cache, data.l2_cache);         
  • 方法二:从 /proc/cpuinfo 中直接读取, /proc/cpuinfo信息的格式说明在kernel中的代码_

.. _信息的格式说明在kernel中的代码: http://lxr.free-electrons.com/source/arch/x86/kernel/cpu/proc.c#L57

.. code-block:: c

#define _GNU_SOURCE
#include 
#include 

int main(int argc, char **argv)
{
   FILE *cmdline = fopen("/proc/cpuinfo", "rb");
   char *arg = 0;
   size_t size = 0;
   while(getdelim(&arg, &size, 0, cmdline) != -1)
   {
      puts(arg);
   }
   free(arg);
   fclose(cmdline);
   return 0;
}

判断是否虚拟机

使用RedHat virt-what工具/库,此工具可以判断很多VM

.. code-block:: c

#if defined(__i386__) || defined(__x86_64__)

static unsigned int
cpuid (unsigned int eax, char *sig)
{
  unsigned int *sig32 = (unsigned int *) sig;

  asm volatile (
        "xchgl %%ebx,%1; xor %%ebx,%%ebx; cpuid; xchgl %%ebx,%1"
        : "=a" (eax), "+r" (sig32[0]), "=c" (sig32[1]), "=d" (sig32[2])
        : "0" (eax));
  sig[12] = 0;

  return eax;
}

static void
cpu_sig (char* sig)
{
  unsigned int base = 0x40000000, leaf = base;
  unsigned int max_entries;

  memset (sig, 0, sizeof sig);
  max_entries = cpuid (leaf, sig);
  puts (sig);

  // return for determine
  if(strlen(siq) > 0){
    return;
  }

  /* Most hypervisors only have information in leaf 0x40000000, but
   * upstream Xen contains further leaf entries (in particular when
   * used with Viridian [HyperV] extensions).  CPUID is supposed to
   * return the maximum leaf offset in %eax, so that's what we use,
   * but only if it looks sensible.
   */
  if (max_entries > 3 && max_entries < 0x10000) {
    for (leaf = base + 0x100; leaf <= base + max_entries; leaf += 0x100) {
      memset (sig, 0, sizeof sig);
      cpuid (leaf, sig);
      puts (sig);
      // if not empty, is ok
      if(strlen(sig) > 0){
        return;
      }
    }
  }
}

#else /* !i386, !x86_64 */

static void
cpu_sig (void)
{
  /* nothing for other architectures */
}

#endif

系统所在磁盘信息

先从 /etc/mtab 获取 /boot 所在硬盘,然后用 ioctl 获取,未确定此方式的稳定性

.. code-block:: c

FILE *fp;
char disk_name[10];
char str_find[] = "/boot";
char buf_line[128];
static struct hd_driveid hd;
int fd;
int tmp_len = 0;

fp = fopen ("/etc/mtab", "rb");
if (fp == NULL)
{
fprintf(stderr, "No /etc/mtab file.\n");
return 0;
}

while(fgets(buf_line, sizeof(buf_line), fp)) {
    if(strstr(buf_line, str_find) != NULL){
        printf("debug: %s\n", buf_line);
        break;
    }
}   
// TODO "/boot" extract
if(sscanf(buf_line, "%s /boot", disk_name) == -1)
{
    printf("Error: can not get disk name\n");   
}

// delete number, e.g. /dev/sda1
tmp_len = strlen(disk_name);
disk_name[tmp_len -1 ] = '\0';

if (geteuid() >  0) {
  printf("ERROR: Must be root to use\n");
  exit(1);
}
// TODO
if ((fd = open(disk_name, O_RDONLY|O_NONBLOCK)) < 0) {
  printf("ERROR: Cannot open device %s\n", disk_name);
  exit(1);
}

if (!ioctl(fd, HDIO_GET_IDENTITY, &hd)) {
    const int nSNLength = 20;
    strSN = "";
        
    for(int i=0; i < nSNLength; i++){
        if(hd.serial_no[i] < 32 || hd.serial_no[i] == 127)
            {
            strSN += '.';
            }
        else{
            strSN += (char)hd.serial_no[i];
            }
    }
    printf("debug: Disk Serial Number: %.20s\n", strSN.GetData());
    return 1;
} else if (errno == -ENOMSG) {
  printf("No hard disk identification information available\n");
  return 0;
} else {
  perror("ERROR: HDIO_GET_IDENTITY");
  exit(1);
}

PS: 虽rst与markdown格式相近,但OSC对rst不支持,很多地方没渲染啊。

转载于:https://my.oschina.net/leopardsaga/blog/172001

你可能感兴趣的:(Linux C 获取硬件信息的一些方法)