use*_*920 5 system-calls parameter-passing linux-kernel
我开发了一个自定义系统调用来记录被杀死的进程。AC 程序杀死进程并调用自定义系统调用,传递被杀死进程的进程 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;
}
Run Code Online (Sandbox Code Playgroud)
这是我的 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;
}
Run Code Online (Sandbox Code Playgroud)
运行 C 程序只是在终端中打印“Killed”。再次运行该程序会使我的虚拟机崩溃。当我使用dmesg实用程序检查时,内核日志中没有打印任何内容。我究竟做错了什么?
需要使用 pid_t 变量而不是 String。这是修改后的系统调用:
#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;
}
Run Code Online (Sandbox Code Playgroud)
这是使用系统调用的修改后的 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;
}
Run Code Online (Sandbox Code Playgroud)