[英]How to determine file type from kernel module?
Is there something like struct dirent* -> d_type
that contains DT_REG
, DT_DIR
, DT_SOCK
and etc. for kernel structures, for example for struct file
? 有没有类似struct dirent* -> d_type
内容struct dirent* -> d_type
包含DT_REG
, DT_DIR
, DT_SOCK
等,例如对于struct file
? Looking at its fields, I cant find anything for this purpose. 看着它的领域,我找不到任何东西用于此目的。
Maybe someone knows how readdir
determines d_type
? 也许有人知道readdir
如何确定d_type
? I am looking at its implementation here https://github.com/lattera/glibc/blob/master/dirent/readdir.c and I cant understand what is going here. 我在这里查看它的实现https://github.com/lattera/glibc/blob/master/dirent/readdir.c我无法理解这里发生了什么。
Ubuntu18.04, 4.15.0-45 kernel version Ubuntu18.04,4.15.0-45内核版本
The struct inode
field i_mode
is a bit-field that can be checked using the standard S_ISDIR
, S_ISREG
, S_ISLNK
et al macros: struct inode
字段i_mode
是一个位字段,可以使用标准S_ISDIR
, S_ISREG
, S_ISLNK
等宏来检查:
/*
* Keep mostly read-only and often accessed (especially for
* the RCU path lookup and 'stat' data) fields at the beginning
* of the 'struct inode'
*/
struct inode {
umode_t i_mode;
unsigned short i_opflags;
kuid_t i_uid;
kgid_t i_gid;
.
.
.
An example of its use in ext4 kernel code : 它在ext4内核代码中的使用示例 :
/*
* Test whether an inode is a fast symlink.
* A fast symlink has its symlink data stored in ext4_inode_info->i_data.
*/
int ext4_inode_is_fast_symlink(struct inode *inode)
{
if (!(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL)) {
int ea_blocks = EXT4_I(inode)->i_file_acl ?
EXT4_CLUSTER_SIZE(inode->i_sb) >> 9 : 0;
if (ext4_has_inline_data(inode))
return 0;
return (S_ISLNK(inode->i_mode) && inode->i_blocks - ea_blocks == 0);
}
return S_ISLNK(inode->i_mode) && inode->i_size &&
(inode->i_size < EXT4_N_BLOCKS * 4);
}
Note that you need to be really careful traversing such kernel structures. 请注意,您需要非常小心地遍历此类内核结构。 If you don't take the proper locks, they can change out from under the thread examining them. 如果你没有采取正确的锁,他们可以从检查它们的线程下面更换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.