Linux内核版本:4.15.1
Ubuntu版本:16.04
虚拟机版本:14
以下是系统调用部分的代码:
错误是在打印进程信息那里,其他同学也是使用这个函数,都可以通过测试代码,我把使用tty部分注释掉就没有问题
asmlinkage long sys_hello_world(char *A,char *B)
{
struct file *fp;
mm_segment_t fs;
loff_t pos;
struct task_struct *p;
struct tty_struct *q;
char buf1[42]={"\0"};
char buf2[78]={"\0"};
//将CPU信息送到数组A
fp=filp_open("/proc/cpuinfo",O_RDONLY,0);
fs=get_fs();
set_fs(KERNEL_DS);
pos=79;
vfs_read(fp,buf1,sizeof(buf1),&pos);
buf1[41]='\0';
copy_to_user(A,buf1,42);
filp_close(fp,NULL);
set_fs(fs);
//将版本信息送到数组B
fp=filp_open("/proc/version",O_RDONLY,0);
fs=get_fs();
set_fs(KERNEL_DS);
pos=0;
vfs_read(fp,buf2,sizeof(buf2),&pos);
copy_to_user(B,buf2,78);
filp_close(fp,NULL);
set_fs(fs);
//打印进程相关信息
q=current->signal->tty;
char correct[20]={"\0"};
char printScreen[80]={"\0"};
q->ops->write(q,"**************process information***********",42);
q->ops->write(q,"\n\r",2);
sprintf(printScreen,"%-20s %-20s %-20s %-20s","Name","pid","State","ParentName");
q->ops->write(q,printScreen,sizeof(printScreen));
q->ops->write(q,"\n\r",2);
for(p=&init_task;(p=next_task(p))!=&init_task; )
{
sprintf(correct,"%-20s",p->comm);
q->ops->write(q,correct,sizeof(correct));
sprintf(correct,"%-20s",p->pid);
q->ops->write(q,correct,sizeof(correct));
sprintf(correct,"%-20s",p->state);
q->ops->write(q,correct,sizeof(correct));
sprintf(correct,"%-20s",p->parent->comm);
q->ops->write(q,correct,sizeof(correct));
q->ops->write(q,"\n\r",2);
}
q->ops->write(q,"\n\r",2);
return 0;
}
以下是我个人的测试代码:
我设计的系统调用号为666
#include <stdio.h>
#include <linux/unistd.h>
int main()
{
char A[42]={"\0"};
char B[78]={"\0"};
syscall(666,A,B);
printf("\n");
printf("*********the cpu info**********\n");
printf("%s\n",A);
printf("*********the version info**********\n");
printf("%s\n",B);
printf("*************end*******************\n");
return 0;
}
求好心大佬帮助,tty卡在这里是为啥,没充值c币,可微信支付宝感谢