簡體   English   中英

如何在Linux中將/dev文件作為內核模塊來解決讀寫操作?

[英]How to resolve reading and writing operations with /dev file as a kernel module in Linux?

顯然,在內核編程遇到很多麻煩之后,這是一個不足為奇的新手問題。 我嘗試啟動一個程序,讓 /dev 文件夾中的驅動程序文件可用於一些讀寫(實際上,我意識到這是相當不安全的想法,但我需要強烈地繼續所有這些經驗)。 讓我們看一個模塊源代碼:

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

為了測試讀/寫,我使用以下代碼:

#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 制作的(我在 Ubuntu 18.04 上運行它),但由於對用戶/內核空間中的驅動程序調用的一些誤解,我被困在寫操作上。 一旦我開始我的程序,輸出如下:

繼續 Linux 驅動程序的基礎知識...
驅動程序文件現已打開
驅動程序被寫入 -1 字節

緊接着是最后一行輸出,系統無法運行(直到我關閉 PC 才響應)。 我認為這種情況與內存控制有關,或者很可能與某些驅動程序文件屬性有關。 但是,用戶權限已被授予讀/寫/執行,確實沒有任何訪問限制。 希望可以指出此處發布的代碼中的錯誤。

看到你的代碼你不處理寫作部分。

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應該會有所幫助(或者在您的系統出現混亂的情況下,您可以在重新啟動系統后使日志持久化以調試它)。

暫無
暫無

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

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