我正在开发与用户空间程序通信的Linux内核模块 。 该模块等待从用户空间发送的消息,以便在内核模式下打印该消息。
这是模块:
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gaston");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");
#define MAX 256
static char message[MAX] =""; ///< Memory for the string that is passed from userspace
ssize_t exer_open(struct inode *pinode, struct file *pfile) {
printk(KERN_INFO "Device has been opened\n");
return 0;
}
ssize_t exer_read(struct file *pfile, char __user *buffer, size_t length, loff_t *offset) {
return 0;
}
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {
if (length > MAX)
return -EINVAL;
if (copy_from_user(message, buffer, length) != 0)
return -EFAULT;
printk(KERN_INFO "Received %s characters from the user\n", message);
return 0;
}
ssize_t exer_close(struct inode *pinode, struct file *pfile) {
printk(KERN_INFO "Device successfully closed\n");
return 0;
}
struct file_operations exer_file_operations = {
.owner = THIS_MODULE,
.open = exer_open,
.read = exer_read,
.write = exer_write,
.release = exer_close,
};
int exer_simple_module_init(void) {
printk(KERN_INFO "Initializing the LKM\n");
register_chrdev(240, "Simple Char Drv", &exer_file_operations);
return 0;
}
void exer_simple_module_exit(void) {
unregister_chrdev(240, "Simple Char Drv");
}
module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);
这是用户空间程序:
#include
#include
#include
#include
#include
#include
#define BUFFER_LENGTH 256
int main()
{
int ret, fd;
char stringToSend[BUFFER_LENGTH];
fd = open("/dev/char_device", O_RDWR); // Open the device with read/write access
if (fd < 0)
{
perror("Failed to open the device...");
return errno;
}
printf("Type in a short string to send to the kernel module:\n");
scanf("%s", stringToSend); // Read in a string (with spaces)
printf("Writing message to the device [%s].\n", stringToSend);
ret = write(fd, stringToSend, strlen(stringToSend)); // Send the string to the LKM
if (ret < 0)
{
perror("Failed to write the message to the device.");
return errno;
}
return 0;
}
这工作正常,我从用户空间输入的消息被打印在内核空间中。
现在,我使用这个示例来学习内核线程编程 。
我试图使LKM 写函数成为内核线程必须执行的函数,所以我像这样修改了我的模块:
#include
#include
#include
#include
#include
#include
#include
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gaston");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");
#define MAX 256
static struct task_struct *thread1;
static char message[MAX] =""; ///< Memory for the string that is passed from userspace
ssize_t exer_open(struct inode *pinode, struct file *pfile) {
printk(KERN_INFO "Device has been opened\n");
return 0;
}
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset) {
if (length > MAX)
return -EINVAL;
printk("Thread_fuction is running ...\n");
if (copy_from_user(message, buffer, length) != 0)
return -EFAULT;
printk(KERN_INFO "Received this message : %s ,from the user\n", message);
return 0;
}
struct file_operations exer_file_operations = {
.owner = THIS_MODULE,
.open = exer_open,
.write = exer_write,
};
int exer_simple_module_init(void) {
char our_thread[8]="thread1";
printk(KERN_INFO "Initializing the LKM\n");
register_chrdev(240, "Simple Char Drv", &exer_file_operations);
thread1 = kthread_create(exer_write,NULL,our_thread);
if((thread1))
{
printk(KERN_INFO "Thread is created");
wake_up_process(thread1);
}
return 0;
}
void exer_simple_module_exit(void) {
int ret;
unregister_chrdev(240, "Simple Char Drv");
ret = kthread_stop(thread1);
if(!ret)
printk(KERN_INFO "Thread stopped");
}
module_init(exer_simple_module_init);
module_exit(exer_simple_module_exit);
这里的问题,当我编译模块时,发生错误:
/exer_simple_char_drv.c:63:27:错误:从不兼容的指针类型[-Werror = incompatible-pointer-types]中传递'kthread_create_on_node'的参数1线程1 = kthread_create(exer_write,NULL,our_thread);
搜索网络,发现通过调用该函数创建了一个线程:
struct task_struct *kthread_create(int (*function)(void *data), void *data, const char name[], ...)
该函数具有第一个参数,即线程必须执行的功能,并且具有参数(void *data) ,但是,我的写入内核函数具有不同的参数:
ssize_t exer_write(struct file *pfile, const char __user *buffer, size_t length, loff_t *offset)
请问有办法解决这个问题吗? 如何修改我的Linux内核以将write功能作为线程执行?
谢谢。