新增获取硬件信息,固件版本号,扇区数量,磁盘类型

This commit is contained in:
shaozhimin 2022-10-13 19:16:35 +08:00
parent e8802a7c99
commit f8b106a50c
4 changed files with 51 additions and 21 deletions

View File

@ -141,28 +141,34 @@ static int get_disk_identifier(int fd, kdk_diskinfo *di)
klog_warning("ioctl 执行失败:%s\n", strerror(errno));
return -1;
}
char *model = strndup((char*)&hd[27], 40);
// char *fwrev = strndup(&hd[23], 8); // 固件版本
char *fwrev = strndup(&hd[23], 8); // 固件版本
char *serno = strndup((char*)&hd[10], 20);
di->serial = malloc(strlen(serno) + 1);
di->model = malloc(strlen(model) + 1);
if (! di->serial || ! di->model)
di->fwrev = malloc(strlen(fwrev) + 1);
if (! di->serial || ! di->model || !di->fwrev)
{
klog_emerg("内存分配失败:%s\n", strerror(errno));
SAFE_FREE(model);
SAFE_FREE(serno);
SAFE_FREE(fwrev);
return KDK_ENOMEM;
}
strcpy(di->serial, serno);
strcpy(di->model, model);
strcpy(di->fwrev, fwrev);
strstripspace(di->serial);
strstripspace(di->model);
strstripspace(di->fwrev);
SAFE_FREE(model);
SAFE_FREE(serno);
SAFE_FREE(fwrev);
return 0;
}
@ -270,6 +276,8 @@ static int get_disk_type(kdk_diskinfo *di)
}
// 获取转速信息(固态除外)
out:
return 0;
@ -458,7 +466,20 @@ kdk_diskinfo *kdk_get_diskinfo(const char *diskname)
klog_err("No disk MODEL info matched :%s\n",diskname);
}
const char *p_name = res->name;
res->fwrev = (char *)malloc(FWREV_SIZE * sizeof(char));
p_name += strlastof(p_name, '/');
char syspath[PATH_MAX];
sprintf(syspath, "/sys/block/%s/device/firmware_rev", p_name);
int tfd = open(syspath, O_RDONLY);
if (tfd <= 0)
{
strcpy(res->fwrev, "None");
}else{
char buf[10] = {0};
read(tfd, buf, FWREV_SIZE);
strcpy(res->fwrev, buf);
}
}
if (get_disk_space(fd, res))
@ -471,7 +492,10 @@ kdk_diskinfo *kdk_get_diskinfo(const char *diskname)
goto err_out;
}
if (get_disk_type)
if (get_disk_type(res))
{
goto err_out;
}
out:
close(fd);
@ -491,5 +515,6 @@ void kdk_free_diskinfo(kdk_diskinfo *disk)
SAFE_FREE(disk->vendor);
SAFE_FREE(disk->model);
SAFE_FREE(disk->mount_path);
SAFE_FREE(disk->fwrev);
SAFE_FREE(disk);
}

View File

@ -5,6 +5,8 @@
extern "C" {
#endif
#define FWREV_SIZE 20
typedef enum _kdk_disk_type{
DISK_TYPE_HDD, // 机械
DISK_TYPE_SSD, // 固态
@ -66,6 +68,7 @@ typedef struct _kdk_diskinfo{
char *mount_path; // 挂载路径
kdk_disk_format format; // 格式化类型
kdk_disk_partition_type part_type; // 分区类型
char *fwrev; //固件版本信息
}kdk_diskinfo;

View File

@ -10,6 +10,8 @@ static void display_disk_info(kdk_diskinfo *di)
printf("Disk Model:\t%s\n", di->model ? di->model : "None");
printf("Disk Serial:\t%s\n", di->serial ? di->serial : "None");
printf("Disk Partition Number:\t%u\n", di->partition_nums);
printf("Disk Type:\t%d\n", di->disk_type);
printf("Disk fwrev:\t%s\n", di->fwrev);
}
static void display_disk_list(char **disklist)

View File

@ -4,24 +4,24 @@
int main()
{
// printf("获取某一进程的CPU利用率%0.1f\n", kdk_get_process_cpu_usage_percent(4849));
// printf("获取某一进程的内存占用率:%0.1f\n", kdk_get_process_mem_usage_percent(4849));
// printf("获取某一进程的进程状态:%s\n", kdk_get_process_status(4849));
// printf("获取某一进程的进程端口号:%d\n", kdk_get_process_port(4849));
// printf("获取某一进程的启动时间:%s\n", kdk_get_process_start_time(4849));
// printf("获取某一进程的运行时间:%s\n", kdk_get_process_running_time(4849));
// printf("获取某一进程的CPU时间%s\n", kdk_get_process_cpu_time(4849));
// printf("获取某一进程的Command%s\n", kdk_get_process_command(7391));
// printf("获取某一进程的属主:%s\n", kdk_get_process_user(1134));
printf("获取某一进程的CPU利用率%0.1f\n", kdk_get_process_cpu_usage_percent(3554));
printf("获取某一进程的内存占用率:%0.1f\n", kdk_get_process_mem_usage_percent(3554));
printf("获取某一进程的进程状态:%s\n", kdk_get_process_status(3554));
printf("获取某一进程的进程端口号:%d\n", kdk_get_process_port(3554));
printf("获取某一进程的启动时间:%s\n", kdk_get_process_start_time(3554));
printf("获取某一进程的运行时间:%s\n", kdk_get_process_running_time(3554));
printf("获取某一进程的CPU时间%s\n", kdk_get_process_cpu_time(3554));
printf("获取某一进程的Command%s\n", kdk_get_process_command(3554));
printf("获取某一进程的属主:%s\n", kdk_get_process_user(3554));
// char** pid = kdk_procname_get_process_infomation("qaxbrowser");
// size_t index = 0;
// while (pid[index])
// {
// printf("pid %s\n", pid[index]);
// index ++;
// }
// kdk_proc_freeall(pid);
char** pid = kdk_procname_get_process_infomation("qaxbrowser");
size_t index = 0;
while (pid[index])
{
printf("pid %s\n", pid[index]);
index ++;
}
kdk_proc_freeall(pid);
char** info = kdk_get_process_all_information();
size_t count = 0;