3

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

这是模块:

#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 程序时,我无法看到我手动输入的消息。请问我在这里缺少什么?

4

1 回答 1

1

第一个问题:你从不修改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;

}   
于 2019-10-23T14:00:16.333 回答