簡體   English   中英

無法打印從用戶空間 C 應用程序發送到 linux kernel 模塊的消息

[英]Unable to print the message sent from user space C application to linux kernel module

我開發了一個簡單的 linux kernel 模塊:

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


ssize_t exer_open(struct inode *pinode, struct file *pfile) {

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

    return length;
}

ssize_t exer_close(struct inode *pinode, struct file *pfile) {

    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_ALERT "Inside the %s function\n", __FUNCTION__);
    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);

我使用insmod命令將此模塊插入 kernel 沒有任何問題。

我想使用這個模塊來打印由我開發的用戶空間程序發送給它的消息:

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


int main()

{

int ret, fd;
char stringToSend[] = "Hello World !";

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

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;

}

當我使用tail -f /var/log/messages命令執行程序並檢查 kernel 日志時,我可以看到: user.alert kernel: Inside the exer_read function看到消息“Hello World!”

我不知道我在這里缺少什么,尤其是我仍然是開發模塊和使用它的初學者。 請幫幫我!

對於仍然找不到解決方案的人,我有一個答案。

這是模塊:

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


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

Ans 這是應用程序:

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

}

你會看到這會正常工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM