首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何解决以/dev文件为内核模块的读写操作?

如何解决以/dev文件为内核模块的读写操作?
EN

Stack Overflow用户
提问于 2019-12-03 08:28:03
回答 1查看 1.1K关注 0票数 0

显然,在经历了许多内核编程的麻烦之后,这是一个令人不快的新手问题。我试图启动一个程序,在/dev文件夹中获取驱动程序文件,以便进行一些读写(确实,我意识到这是一个相当不安全的想法,但我需要继续进行所有这些体验)。让我们看一下模块源代码:

代码语言:javascript
运行
复制
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <asm/uaccess.h>

MODULE_LICENSE("GPL");

int init_module(void); // driver file initialization as opening it
void cleanup_module(void); // exec files removal ahead of shutting driver file
static int device_open(struct inode *, struct file *); // driver file opening
static int device_release(struct inode *, struct file *); // return of system resource control
static ssize_t device_read(struct file *, char *, size_t, loff_t *); // reading from driver file
static ssize_t device_write(struct file *, const char *, size_t, loff_t *); // writing into driver file

#define SUCCESS 1
#define DEVICE_NAME "sample device"
#define BUF_LEN 80

static int Major; // device's major number
static int Device_Open = 0; // device access counter
static char message[BUF_LEN]; // buffer for both read and write operations
static char *message_ptr;

// list of basic operations executable by driver
static struct file_operations ops = {
        .read = device_read,
        .write = device_write,
        .open = device_open,
        .release = device_release
};

int init_module(void)
{
    Major = register_chrdev(0, DEVICE_NAME, &ops); // major number assignment

    // evaluate whether driver file is accessible
    if(Major < 0) {
        printk(KERN_ALERT "Device registration attempt failed\n");
        return Major;
    }

    return SUCCESS;
}

void cleanup_module(void)
{
    unregister_chrdev(Major, DEVICE_NAME); // cancelling driver registration in file system before exit
    printk(KERN_ALERT "Driver file of /dev/%s c %d 0 has been destroyed\n", DEVICE_NAME, Major);
    return;
}

static int device_open(struct inode * node, struct file * file)
{
    printk(KERN_INFO "Trying access /dev/%s c %d 0\n", DEVICE_NAME, Major);
    static int counter = 0; // access counter initializing

    // file control evaluation
    if(Device_Open)
        return -EBUSY;

    Device_Open++; // increment counter to avert driver's immanent running
    sprintf(message, "This sentence displayed %d times\n", counter++);
    message_ptr = message;

    try_module_get(THIS_MODULE);

    return SUCCESS;
}

static int device_release(struct inode * node, struct file * file)
{
    printk(KERN_INFO "Trying closure of /dev/%s c %d 0\n", DEVICE_NAME, Major);
    Device_Open--; // decrement counter to keep driver file removable as well
    module_put(THIS_MODULE);

    return SUCCESS;
}

static ssize_t device_read(struct file * file, char * ch, size_t num, loff_t * off)
{
    int read_bytes = 0; // output size
    printk(KERN_INFO "Trying read from /dev/%s c %d 0\n", DEVICE_NAME, Major);
    if(*message_ptr == 0)
        return 0;

    // loop-executed reading from file
    while(num && *message_ptr) {
        put_user(*(message_ptr++), ch++);
        num--;
        read_bytes++;
    }

    printk("%d bytes read, %d bytes to be handled", read_bytes, num);

    return read_bytes;
}


// updated stuff    
static ssize_t device_write(struct file *filp, const char *buff, size_t len, loff_t * off) 
{
    char message_from_user[BUF_LEN];    
    if(copy_from_user(message_from_user, buff, len)) return -EINVAL;
    printk(KERN_INFO "length of message:%d message:'%s'", (int)len,   message_from_user);
    return len; 
}

为了测试读写,我使用以下代码:

代码语言:javascript
运行
复制
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <linux/unistd.h>

extern int errno;

int main()
{
    int fd; // file descriptor id
    size_t cnt = 0; // input / output number of bytes
    size_t cnt_2 = 0;
    char inputBuffer[30] = "Device file is open"; // write operation buffer
    char outputBuffer[50]; // read operation buffer

    printf("Continuing with basics of Linux drivers...\n");

    // evaluate accessibility of driver file
    fd = open("/dev/dev", O_RDWR);
    if(fd == -1) {
        close(fd);
        printf("File opening isn't completed\n");
        return 1;
    }

    printf("Driver file is open now\n");

    // writing from file
    cnt = write(fd, inputBuffer, sizeof(inputBuffer));
    printf("Driver got written %d bytes\n", cnt);

    // read into file
    cnt = read(fd, outputBuffer, sizeof(outputBuffer));
    printf("Driver received %d bytes\n", cnt);

    int i = 0;

    // display an input message
    while(i < cnt) {
        printf("%c", outputBuffer[i]);
        printf("%s", "\n");
        i++;
    }

    close(fd); // wrap up driver connection and clear memory
    printf("Driver file is close\n");
    return 0;
}         

尽管模块是内置的,开发文件也是由mknod创建的(我在Ubuntu18.04上运行它),但由于用户/内核空间中对驱动程序调用的一些误解,我仍然无法执行写操作。一旦我启动了我的程序,输出如下:

继续学习Linux驱动程序的基础知识..。

驱动程序文件现在打开。

驱动程序被写入-1字节

接着是最后一行输出,系统变得无法操作(在我关闭PC之前没有响应)。我认为这是一个内存控制问题,或者很可能是一些驱动程序文件属性的问题。但是,已经授予了读取/写入/执行的用户权限,没有任何访问限制是可以推断的。希望能够指出这里发布的代码中的错误之处。

EN

回答 1

Stack Overflow用户

发布于 2019-12-03 08:54:03

看到你的代码,你不处理写作部分。

代码语言:javascript
运行
复制
static ssize_t device_write(struct file * file, const char * ch, size_t num, loff_t * off)
{
    printk(KERN_ALERT "Operation denied\n");
    return -EINVAL;
} 

因此,您的模块不可能工作。

但是,您的崩溃来自于读取函数中的内存访问(用strace检查)。我让你明白了你的问题。dmesg应该会有所帮助(或者在您的系统恐慌的情况下,您可以在重新启动系统之后使日志持久地调试它)。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59152880

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档