Hunter McMillen
Hunter McMillen

Reputation: 61540

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

I am trying to find what member(s) of the struct fdtable or struct file will let me determine whether or not an open file is a socket or a pipe.

the only path I can seem to find is:

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

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

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

Note: I am using the kernel definitions from 2.6.24

Upvotes: 10

Views: 2050

Answers (1)

Ilya Matveychikov
Ilya Matveychikov

Reputation: 4024

There are special macro definitions at 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 that you'll need to use 2 of them - S_ISFIFO and S_ISSOCK in a such way:

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

Upvotes: 12

Related Questions