How to determine if an open file is a socket or pipe?

I am trying to find which members (s) of struct fdtable or struct file will allow me to determine if the open file is a socket or a pipe.

The only way I can find is:

 struct file f ....; f.path->mnt->mnt_devname 

This returns the device name at the mount point, all sockets / pipes apparently belong to sockfs or pipefs respectively.

Is there a faster way to check if an open file is a socket or pipe using another member of the structure file or fdtable?

Note. I use kernel definitions from 2.6.24

+7
source share
1 answer

There are special macro definitions in linux / stat.h that checks inode->i_mode :

  #define S_ISLNK(m) (((m) & S_IFMT) == S_IFLNK) #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG) #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR) #define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR) #define S_ISBLK(m) (((m) & S_IFMT) == S_IFBLK) #define S_ISFIFO(m) (((m) & S_IFMT) == S_IFIFO) #define S_ISSOCK(m) (((m) & S_IFMT) == S_IFSOCK) 

It seems you need to use 2 of them - S_ISFIFO and S_ISSOCK in this way:

 if (S_ISFIFO(file->f_path.dentry->d_inode->i_mode)) {...} if (S_ISSOCK(file->f_path.dentry->d_inode->i_mode)) {...} 
+10
source

All Articles