您的位置:首页 > 其它

基于/proc伪文件系统的读取系统常见内核状态信息

2016-11-09 03:08 429 查看
linux实验,需要学生掌握linux下文件的读写。我做了一个基于/proc伪文件系统的读取系统内核信息的一个小程序。

读取cpu信息-------/proc/cpuinfo

读取内存信息-------/proc/meminfo

读取挂载设备-----/proc/mounts

读取已经加载的设备并分类---------/proc/devices

读取支持的文件系统--------/proc/filesystems

读取加载的模块----------/proc/modules

读取系统的版本信息-----------/proc/version

启动时传递给kernel的参数----------/proc/cmdline

这个程序用到了自己写的一个按行读取的函数readline

------------------》》》》》》》》》》》》》按行读取的readline

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>

#define CPU_INFO_FILE "/proc/cpuinfo"
#define MEMORY_INFO_FILE "/proc/meminfo"
#define MOUNTS_INFO_FILE "/proc/mounts"
#define DEVICES_INFO_FILE "/proc/devices"
#define FILESYSTEM_INFO_FILE "/proc/filesystems"
#define MODULES_INFO_FILE "/proc/modules"
#define VERSION_INFO_FILE "/proc/version"
#define CMDLINE_INFO_FILE "/proc/cmdline"

void display_help();
int readline(int fd,char** buff);

int main(int argc,char* argv[])
{
int vfile;//file point
int rel;// the length of read from file
int count=0;
char option,*buff=NULL;
//charge the number of arguments
//we need a argument to help choose a operate
//if the number of arguments is less two ,there is no arguments
//if the number of arguments is greater than two ,some arguments are superluous
if(argc<2)
{
printf("\033[31m the arguments too less \033[0m \n");
display_help();
exit(1);
}
if(argc>2)
{
printf("\033[31m the arguments too much \033[0m \n");
display_help();
exit(1);
}
//get option
option=argv[1][0];
switch(option)
{
case 'c':
vfile=open(CPU_INFO_FILE,O_RDONLY);
break;
case 'm':
vfile=open(MEMORY_INFO_FILE,O_RDONLY);
break;
case 'n':
vfile=open(MOUNTS_INFO_FILE,O_RDONLY);
break;
case 'd':
vfile=open(DEVICES_INFO_FILE,O_RDONLY);
break;
case 'f':
vfile=open(FILESYSTEM_INFO_FILE,O_RDONLY);
break;
case 'l':
vfile=open(MODULES_INFO_FILE,O_RDONLY);
break;
case 'v':
vfile=open(VERSION_INFO_FILE,O_RDONLY);
break;
case 'e':
vfile=open(CMDLINE_INFO_FILE,O_RDONLY);
break;
case 'h':
display_help();
exit(0);
break;
default:
display_help();
exit(0);
}
if(vfile<0)
{
printf("open file error\n");
exit(1);
}
while((rel=readline(vfile,&buff))>0)
{
printf("%s",buff);
free(buff);
count++;
if(count%10==0)
{
printf(":");
option=getchar();
printf("\033[1A");
if(option=='q')
{
close(vfile);
exit(0);
}

}
}
close(vfile);
return 0;

}

void display_help()
{
printf("--------------------------------\n");
printf("welcome to use the system info viewer\n");
printf("--------------------------------\n");
printf("\e[1mNAME\e[0m get host info\n\n");
printf("\e[1mSYNOPSIS\e[0m gethostinfo option\n\n");
printf("\e[1mOPTION\e[0m\n\n");
printf(" \e[1mc\e[0m \n get the cpu info\n");
printf(" m\n get the memory info\n");
printf(" n\n get the list of mounts info\n");
printf(" d\n get the list of devices info\n");
printf(" f\n get the list of filesystem supported\n ");
printf(" l\n get the modules\n");
printf(" v\n get the system version\n");
printf(" e\n get the cmdline info\n\n");
printf(" h\n help\n");
printf("--------------------------------\n");

}

int readline(int fd,char** buff)
{
int rl=-1;
char c;
long maxlength=128;
long count=0;
if(fd<0)
{
printf("open file error\n");
exit(0);
}
*buff=(char*)malloc(sizeof(char)*maxlength);
if(*buff==NULL)
{
printf("allocate memory error");
close(fd);
exit(1);
}
while((rl=read(fd,&c,1))>0)
{
if(count==maxlength)
{
maxlength+=128;
*buff=(char*)realloc(*buff,maxlength);
if(buff==NULL)
{
printf("allocate memory error\n");
close(fd);
exit(0);
}
}
(*buff)[count++]=c;
if(c=='\n' || c==EOF)
{
break;
}

}
(*buff)[count]='\0';
return count;
}


当程序不带参数的时候会输出帮助,有语法提示和选项提示。

运行结果:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: