gpt4 book ai didi

c - 如何在用户空间程序和 Linux 内核模块之间进行通信,以便打印从用户程序发送到内核的消息

转载 作者:行者123 更新时间:2023-12-04 11:37:52 24 4
gpt4 key购买 nike

我开发了一个简单的 linux 内核模块,我将从用户空间程序向它发送一个字符消息。

这是模块:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/kernel.h>


MODULE_LICENSE("GPL");
MODULE_AUTHOR("Gaston");
MODULE_DESCRIPTION("A simple Linux char driver");
MODULE_VERSION("0.1");


static char message[256] = {0};

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) {

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);

这是我的 C 程序:

#include<stdio.h>
#include<stdlib.h>
#include<errno.h>
#include<fcntl.h>
#include<string.h>
#include<unistd.h>


#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;

}

使用 insmod 插入模块后,当我执行程序并使用 tail -f/var/log/messages 命令检查内核日志时,我可以看到:

Oct  1 13:57:37 auth.info login[306]: root login on 'ttyS0'
Oct 1 13:58:22 user warn kernel: exer_simple_char_drv: loading out-of-tree module taints kernel.
Oct 1 13:58:22 user.info kernel: Initializing the LKM
Oct 1 13:58:35 user.info kernel: Device has been opened
Oct 1 13:58:39 user.info kernel: Received characters from the user
Oct 1 13:58:39 user.info kernel: Device successfully closed

当我运行 dmesg 时同样的事情:

exer_simple_char_drv: loading out-of-tree module taints kernel.
Initializing the LKM
Device has been opened
Received characters from the user
Device successfully closed

问题是我在执行 C 程序时看不到我手动输入的消息。请问我在这里缺少什么?

最佳答案

第一个问题:你从不修改message

那么,你不能直接在内核上下文中使用用户内存

你必须先翻译它:copy_from_user就是为了这个。

你的写函数看起来像

#define MAX 256
/* here, message is defined as 256 bytes +1 one.
The extra char is here to be compatible with the `%s` formatter */
static char message[MAX+1] ="";

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;

}

关于c - 如何在用户空间程序和 Linux 内核模块之间进行通信,以便打印从用户程序发送到内核的消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58523625/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com