系统调用:
asmlinkage int sys_mysyscall(void){
struct task_struct *p;
printk("start mysystemcall\n");
printk("总缺页数:%lu\n",pfcount);
printk("当前进程缺页次数:%lu\n",current->pf);
for_each_process(p){
printk("进程%d的脏页数为%d\n",p->pid,p->nr_dirtied);
}
printk("end mysystemcall\n");
return 0;
}
用户端:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<linux/unistd.h>
#include<sys/syscall.h>
#define __NR_mysyscall 223
#define MAX_LINE 1024
int substrcmp( char *s, char * tag){ // a simple function to match the tag
char *sp;
char *tp;
int flag = 0;
int equal = 1;
int len = strlen(tag);
while(*s){
sp = s;
tp = tag;
equal = 1;
for( int i = 0 ; i < len ; i ++ ){
if(*(sp+i) != *(tp+i))
equal = 0;
}
if( equal == 1 ){
flag = 1;
return flag;
}
s++;
}
return flag;
}
int main(){
syscall(__NR_mysyscall);
FILE *fp;
char *start = "start mysystemcall"; //starttag
char *end = "end mysystemcall"; //endtag
char buf[MAX_LINE]; //buffer for the message
int flag = 0; //the flag of printing
if((fp=fopen("/var/log/kern.log","r"))==NULL){
printf("cannot open the log!\n"); //for test
}
else{
printf("file opened!\n"); //for test
int count = 0;
while(fgets(buf,MAX_LINE,fp)!=NULL){
if( substrcmp(buf,start) == 1 ) //find the beginning
count++;
}
rewind(fp);
while(fgets(buf,MAX_LINE,fp)!=NULL){
if( substrcmp(buf,end) == 1 ) //find the end
flag = 0;
if( flag == 1 && count == 0 ){
char *pos = strchr(buf,']');// ']' is a signal tha between unnessary messsage
printf("%s",pos+1); // and the important message
}
if( substrcmp(buf,start) == 1 ){ //find the beginning
flag = 1;
count--;
}
}
}
if(fclose(fp)!=0){
printf("the log cannot be closed!\n"); //for test
}
else{
printf("close the log\n"); //for test
}
return 0;
}
0 条评论