2016-11-04 30 views
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”进行检查时,内核日志中没有打印出来。我究竟做错了什么?

+1

正在使用的指针在内核空间中按原样从用户空间获得。哎呀。你一定要使用'copy_from_user()'等。查看现有的系统调用以获取正确的方法。更好的是,不要传递字符串,传递进程ID('pid_t')。 –

+0

这解决了我的问题。谢谢一堆! – user3163920

回答

2

需要使用将为pid_t变量,而不是String.This是修改的系统调用:

#include <linux/kernel.h> 

asmlinkage long sys_killa(pid_t pid) 
{ 
    long pid_l = (long) pid; 

    printk("The process %ld has been killed", pid_l); 

    return 0; 

} 

这是修改后的C代码使用系统调用:

#include <stdio.h> 
#include <linux/kernel.h> 
#include <sys/syscall.h> 
#include <unistd.h> 

int main() 
{ 
    pid_t pid = 3249; 
    long int sys = syscall(329, pid); 
    printf("System call sys_killa returned %ld\n", sys); 
    return 0; 
} 
相关问题