简体   繁体   中英

Problem with character device in Linux kernel

I have a problem in my Kernel module, I am writing a module that allows threads from different processes to exchange messages,I am generating different CHRDriver to achieve it. I wrote the skeleton of the functions I need, but I noticed that making the driver have the same file_operations they also shares the variable values? How do I have to do to making them have the same logical variables, but in different addresses? Let me explain with an example:

#include "group_message_manager.h"
#include <linux/kernel.h>

int open_value = 0;

int gmm_open(struct inode *inode, struct file *filp){
    printk(KERN_ALERT "open value %d\n", open_value++);
    return 0;
}

ssize_t gmm_read(struct file * file, char * buffer, size_t lenght, loff_t * offset){
    return 0;
}

ssize_t gmm_write(struct file * file, const char __user * buffer, size_t lenght, loff_t * offset ){
    return 0;
}

struct file_operations file_ops_gmm_origin = {
    open: gmm_open,
    read: gmm_read,
    write: gmm_write,
    // unlocked_ioctl: mydev_ioctl,
    // compat_ioctl: mydev_ioctl,
    // release: mydev_release
};
// EXPORT_SYMBOL(file_ops_gmm_origin);  

ABOVE.c FILE, .h file is NEXT

#pragma once
#include <linux/fs.h>


int gmm_open(struct inode *inode, struct file *filp);

ssize_t gmm_read(struct file * file, char * buffer, size_t lenght, loff_t * offset);

ssize_t gmm_write(struct file * file, const char __user * buffer, size_t lenght, loff_t * offset );

extern struct file_operations file_ops_gmm_origin; 

How do I have to make any driver its own open_value variable starting from 0? Because at this moment if somebody opens a file, and then opens the other, the variable is set to 2, while I want to have two variables set to 1.

UPDATE:

Your answer is useful but I need a mechanism to allow that every time open() is called, the same private_data is found, so a specific character file data, not specific to the single invocation of open() . How can I do it? Thank you in advance

At the end I solved by using an hash table data structure where the key is the inode identifier, and the value is a pointer to a struct containing the value I need to manage

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM