Не удалось получить данные ядра с помощью copy_to_user, не работающего с debugfs - PullRequest
0 голосов
/ 03 апреля 2020

Я пытаюсь реализовать простой интерфейсный модуль debugfs. Код прилагается для справки. Для записи данных я использую echo 'string'> / sys / kernel / debug / debugexercise / text и работает так, как ожидаемые данные копируются в буфер ядра. Но когда я пытаюсь получить данные обратно с помощью команды cat , то есть cat / sys / kernel / debug / debugexercise / text , данные на терминале не печатаются.

Я также пытался использовать simple_read_from_buffer вместо copy_to_user, но получил тот же результат. Кто-нибудь есть идея, в чем проблема с этим кодом. 4.13.0-45-generi c - это версия ядра в моей системе.

#include <linux/module.h>
#include <linux/debugfs.h>
#include <linux/fs.h>
#include <linux/uaccess.h>

#define LEN 512 

static struct dentry *test_dir;
static struct dentry *test_file;

static char ker_buf[LEN] ;
/* read file operation */
static ssize_t test_read(struct file *fp, char __user *user_buffer, size_t count, loff_t *position){
    printk(KERN_NOTICE "debugfs_read called, count %d\n", count);
    return copy_to_user(user_buffer, ker_buf, LEN);

}

static ssize_t test_write(struct file *fp, const char __user *user_buffer, size_t count, loff_t *position){
    printk(KERN_NOTICE "debugfs_write called, count %d\n",count);
    if(count > LEN )
        return -EINVAL;

    copy_from_user(ker_buf, user_buffer, count);
    printk(KERN_NOTICE "write buffer complete: %s\n",ker_buf);
    return count;
}

static struct file_operations fops_debug = {
    .read = test_read,
    .write = test_write,
};

static int __init init_debug(void)
{
    test_dir = debugfs_create_dir("debugexercise", NULL);
    if(NULL == test_dir){
        printk(KERN_ERR "debugfs_create_dir() Failed\n");
        return -1;
    }
    else
        printk(KERN_NOTICE "debugexercise created\n");

    test_file = debugfs_create_file("text", 0644, test_dir, NULL, &fops_debug);
    if(NULL == test_file){
        printk(KERN_ERR "debugfs_create_file() Failed\n");
        debugfs_remove(test_dir);
        return -1;
    }
    else
        printk(KERN_NOTICE "text under debugexercise created\n");

    return 0;
}

static void __exit exit_debug(void)
{
    printk(KERN_NOTICE "removing module\n");
    debugfs_remove(test_file);
    debugfs_remove(test_dir);
}
module_init(init_debug)
module_exit(exit_debug)
MODULE_LICENSE("GPL");



1 Ответ

0 голосов
/ 03 апреля 2020

copy_to_user возвращает количество байтов, которые не удалось скопировать. В случае успеха это будет ноль. Следовательно, кошка отображает 0 символов. Я считаю, что вы должны сделать:

if (copy_to_user(user_buffer, ker_buf, LEN)){
         printk(KERN_INFO "copy to user failed.\n");
         return -EINVAL; /* For instance ... */
}

return LEN;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...