我正在为
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); }
我想知道是否有办法获得次要号码.我查看了linux / fs.h,看到文件结构有一个名为private_data的成员,但每当我试图调用它时,它会使我的系统崩溃,因为它当前设置为null.
解决方法
您可以像这样得到次要号码:
iminor(filp->f_path.dentry->d_inode)