4
我开发了一个自定义系统调用来记录死亡进程。 C程序会终止进程,并调用自定义系统调用,传递被终止进程的进程ID,然后系统调用会将被终止进程的ID输出到内核日志中。在这里,我只是传递一个虚拟来测试系统调用是否写入内核日志。系统调用的系统调用表号为329将参数传递给自定义系统调用
下面是我的系统调用
#include <linux/kernel.h>
asmlinkage long sys_killa(char* proc_id)
{
printk("The process %s has been killed", proc_id);
return 0;
}
这是我的C程序中调用系统调用。
#include <stdio.h>
#include <linux/kernel.h>
#include <sys/syscall.h>
#include <unistd.h>
int main()
{
char proc_id[5] = "3219";
long int sys = syscall(329, proc_id);
printf("System call sys_killa returned %ld\n", sys);
return 0;
}
运行C程序只是在终端打印“Killed”。再次运行该程序会使我的虚拟机崩溃。当我使用“dmesg”进行检查时,内核日志中没有打印出来。我究竟做错了什么?
正在使用的指针在内核空间中按原样从用户空间获得。哎呀。你一定要使用'copy_from_user()'等。查看现有的系统调用以获取正确的方法。更好的是,不要传递字符串,传递进程ID('pid_t')。 –
这解决了我的问题。谢谢一堆! – user3163920