C Linux设备驱动程序,是否可以使用文件描述符获取次要编号?

C Linux设备驱动程序,是否可以使用文件描述符获取次要编号?,c,linux-kernel,linux-device-driver,C,Linux Kernel,Linux Device Driver,我正在为Linux编写一个设备驱动程序。它创建了一个带有4个小数字的设备。每当我们试图在小号3处写入设备时,我们都假设要杀死该设备,并且目前该设备除了打印之外不做任何其他操作,它正在写入booga设备。以下是我当前的一些代码,如果需要,我可以发布更多代码: 写入方法: static ssize_t booga_write (struct file *filp, const char *buf, size_t count, loff_t *f_pose) { printk("Attempt

我正在为Linux编写一个设备驱动程序。它创建了一个带有4个小数字的设备。每当我们试图在小号3处写入设备时,我们都假设要杀死该设备,并且目前该设备除了打印之外不做任何其他操作,它正在写入booga设备。以下是我当前的一些代码,如果需要,我可以发布更多代码:

写入方法:

static ssize_t booga_write (struct file *filp, const char *buf, size_t count, loff_t *f_pose) {
    printk("Attempting to write to booga device\n");
    /* need to protect this with a semaphore if multiple processes
       will invoke this driver to prevent a race condition */

    if (down_interruptible (&booga_device_stats->sem))
        return (-ERESTARTSYS);
    booga_device_stats->num_bytes_written += count; 
    up(&booga_device_stats->sem);
    return count; // pretend that count bytes were written

}
测试方法:

static void run_write_test(char *device, int bufsize)
{
    char *buf;
    int src;
    int out;

    src = open(device, O_WRONLY);
    if (src < 0) {
        perror("Open for write failed:");
        exit(1);
    }
    buf = (char *) malloc(sizeof(char)*(bufsize+1));
    fprintf(stderr, "Attempting to write to booga device\n");
    out = write(src, buf, bufsize);
    fprintf(stderr, "Wrote %d bytes.\n", out);
    free(buf);
    close(src);

}
静态无效运行写入测试(char*device,int bufsize)
{
char*buf;
int-src;
指出;
src=打开(仅限装置,O_);
if(src<0){
perror(“打开写入失败:”;
出口(1);
}
buf=(char*)malloc(sizeof(char)*(bufsize+1));
fprintf(stderr,“试图写入booga设备\n”);
out=写入(src、buf、bufsize);
fprintf(stderr,“写入%d字节。\n”,输出);
免费(buf);
关闭(src);
}
我想知道是否有办法得到小号码。我查看了linux/fs.h,发现文件struct有一个名为private_data的成员,但每当我尝试调用它时,它都会使我的系统崩溃,因为它当前设置为null


或者我根本不应该尝试从结构文件中获取次要编号,而是应该在我第一次打开设备时尝试跟踪它?

您可以这样获取次要编号:

iminor(filp->f_path.dentry->d_inode)