如何使用来自 Linux 内核模块的 Linux 系统调用 [英] How do I use a Linux System call from a Linux Kernel Module
问题描述
我在从 Linux 内核模块内部调用系统调用时遇到了一些困难.系统调用已经过标准 c 用户空间程序的测试和正常工作,但我似乎无法让内核模块编译和运行它们.
I am having some difficulty calling a system call from inside a Linux Kernel Module. The system calls have been tested and work properly from a standard c user space program but I can't seem to get the kernel module to compile and run them.
在我的用户程序中,我包含以下代码并且系统调用有效:
In my user program I include the following code and the system call works:
#include <linux/unistd.h>
#define __NR_sys_mycall 343
extern long int _syscall(long int_sysno,...)__THROW;
//and then a simple call is done as such
long value = syscall(__NR_sys_mycall);
printf("The value is %ld
",value);
但是当我在我的 Linux 内核模块中尝试同样的事情时,我得到了一堆错误,要么说错误:函数 'syscall' 的隐式声明(如果我不包括 _syscall 定义)或一长串错误关于语法,如果我这样做...所以我的假设是我需要内核空间版本来调用系统调用.我是对还是错?
But when I try the same thing in my Linux Kernel Module I get a bunch of errors that either say error: implicit declaration of function 'syscall' (if I don't include the _syscall definition) or a long list of errors about syntax if I do...so my assumption is that I need the kernel space version to call the system call. Am I right or wrong?
//My LKM code
#include <linux/module.h>
#include <linux/unistd.h>
#define __NR_sys_mycall 343
static int start_init(void)
{
long value = syscall(__NR_sys_mycall);
printk("The value is %ld
",value);
return 0;
}
static void finish_exit(void)
{
printk("Done!
");
}
module_init(start_init);
module_exit(finish_exit);
推荐答案
可以直接调用sys_mycall.
#include <linux/module.h>
#include <linux/unistd.h>
static int start_init(void)
{
long value = sys_mycall (pass_arguments)
printk("The value is %ld
",value);
return 0;
}
static void finish_exit(void)
{
printk("Done!
");
}
module_init(start_init);
module_exit(finish_exit);
这篇关于如何使用来自 Linux 内核模块的 Linux 系统调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!