[英]Linux device driver, Is it possible to get the minor number using a file descriptor?
我正在為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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.