您好,我正在运行 vfs_stat
,fd
是 /dev/tty0
的打开文件描述符:
set_fs (KERNEL_DS);
if (vfs_fstat (fd, &stat))
{
goto out3;
}
if (stat.mode & S_IFCHR)
printk (KERN_INFO "opening %s (dev %d)\n", filename, stat.rdev);
它打印:
[ 8657.480625] opening /dev/tty0 (dev 4194304)
所以现在我需要检索设备的主要编号,但我在 linux 内核中找不到 major()
或 minor()
定义。
我找到了 this answer但这似乎不对:
#define major(dev) ((int)(((unsigned int) (dev) >> 8) & 0xff))
#define minor(dev) ((int)((dev) & 0xff))
因为如果我执行 printk (KERN_INFO "opening %s (dev %d)\n", filename, major (stat.rdev));
第二个字段始终为零。
那我该如何获取主号码呢?
请您参考如下方法:
在最近的 glibc 和内核中,您应该包括 sys/sysmacros.h 而不仅仅是 sys/types.h。以下是描述此决定的评论:
/* BSD defines `major', `minor', and `makedev' in this header.
However, these symbols are likely to collide with user code, so we are
going to stop defining them here in an upcoming release. Code that needs
these macros should include <sys/sysmacros.h> directly. Code that does
not need these macros should #undef them after including this header. */
# define __SYSMACROS_DEPRECATED_INCLUSION
# include <sys/sysmacros.h>
# undef __SYSMACROS_DEPRECATED_INCLUSION