如何在用户 space 程序和 Linux 内核模块之间进行通信,以便打印从用户程序发送到内核的消息
How to make communication between user space program and Linux kernel module in order to print a message sent from user program to the kernel
我开发了一个简单的 linux 内核模块,我将向它发送一条来自用户 space 程序的字符消息。
这是模块:
#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;
}
我开发了一个简单的 linux 内核模块,我将向它发送一条来自用户 space 程序的字符消息。
这是模块:
#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;
}