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