7

I am looking for type of a file descriptor without the possibility of blocking in the kernel. I am aware I can use fstat(2) but fstat will also get me all sorts of metadata information (access time etc) which may block for an arbitrary amount of time (especially on networked file systems).

EDIT: I am looking for a syscall to do this, spawning a separate process is not acceptable because spawning a process and reading its results is certainly not instant.

The only bit of information I need to know is really if the file descriptor is an on-disk "file" (S_IFREG, S_IFLNK, S_IFDIR) or not. Alternatively, if I could tell if it's a socket (S_IFSOCK), fifo (S_IFIFO), or character device (S_IFCHR) that'd be fine too.

I'm pretty sure any kernel will have this information readily available and I'm interested if that can be surfaced to user-space without blocking.

A portable solution (macOS & Linux at least) would be much appreciated.

Thank you!

5
  • reading the filesystem may always block even for the lightest piece of data, thus I don't think you can do that without dedicating a thread for it (especially if you need portability)...
    – OznOg
    Commented Oct 12, 2019 at 11:11
  • @OznOg Agreed. And I don't want to read the file system at all which is why I don't want to use fstat. But figuring out just the type of an open fd should surely be possible without a disk/fs access. Commented Oct 12, 2019 at 15:23
  • Do you mean by "non-blocking" an instantaneous command?
    – Amessihel
    Commented Oct 31, 2019 at 15:46
  • Non-blocking as the O_NONBLOCK flag to the open call which means that an operation returns immediately, whatever happens. I need an immediately returning function that tells me the file type. Commented Oct 31, 2019 at 21:40
  • @JohannesWeiss, I asked you this because in your question it seemed like you wanted to retrieve the result immediately. That's not the purpose of a non blocking way as you meant by "whatever happens" in your comment. If you're ready to this "whatever", why spawning a process isn't acceptable? You'll have to loop anyway until you get the result.
    – Amessihel
    Commented Nov 2, 2019 at 12:40

2 Answers 2

-1

On Linux you can look into proc pseudo-filesystem, /proc/<pid>/fd, e.g.:

[max@supernova:/proc/7275/fd] $ ls -l /proc/7275/fd/
total 0
lr-x------ 1 max max 64 Oct 12 16:28 0 -> /dev/null
l-wx------ 1 max max 64 Oct 12 16:28 1 -> 'pipe:[69689]'
lrwx------ 1 max max 64 Oct 12 16:28 10 -> 'socket:[69698]'
l-wx------ 1 max max 64 Oct 12 16:28 100 -> '/home/max/.config/google-chrome/Default/Service Worker/Database/MANIFEST-000001'
lr-x------ 1 max max 64 Oct 12 16:28 101 -> '/home/max/.config/google-chrome/Default/Sync Data/LevelDB/001633.ldb'
l-wx------ 1 max max 64 Oct 12 16:28 102 -> '/home/max/.config/google-chrome/Default/Service Worker/Database/000024.log'
lr-x------ 1 max max 64 Oct 12 16:28 103 -> '/home/max/.config/google-chrome/Default/Service Worker/Database/000022.ldb'
lr-x------ 1 max max 64 Oct 12 16:28 104 -> /opt/google/chrome/nacl_irt_x86_64.nexe
lr-x------ 1 max max 64 Oct 12 16:28 105 -> '/home/max/.config/google-chrome/Default/Service Worker/Database/000005.ldb'
lr-x------ 1 max max 64 Oct 12 16:28 106 -> '/home/max/.config/google-chrome/Default/Service Worker/Database/000025.ldb'
lr-x------ 1 max max 64 Oct 12 16:28 107 -> '/home/max/.config/google-chrome/Default/Service Worker/Database/000019.ldb'
lrwx------ 1 max max 64 Oct 12 16:28 108 -> 'socket:[89401]'
lrwx------ 1 max max 64 Oct 12 16:28 109 -> 'socket:[68628]'
lrwx------ 1 max max 64 Oct 12 16:28 11 -> 'anon_inode:[eventfd]'
1
  • 1
    /proc is not cross-platform. MacOS doesn't have it, for example.
    – root
    Commented Oct 14, 2019 at 11:49
-1

You can use the 'lsof' command in non-blocking mode with the arguments '-b'. This argument will cause lsof to avoid kernel functions that might block. For example:

sudo lsof -b | less
COMMAND     PID   TID            USER   FD      TYPE             DEVICE    SIZE/OFF     NODE NAME
systemd       1                  root  cwd       DIR                8,1        4096          2 /
systemd       1                  root  rtd       DIR                8,1        4096          2 /
systemd       1                  root  txt       REG                8,1     1595792      19245 /lib/systemd/systemd

The 'TYPE'will give you the type of file it is. You can pipe this with grep to get information about your file descriptor,

sudo lsof -b | grep <your file descriptor>

Or there are lots of arguments which will allow you to customize the lsof operations.

And for platform independence, these are the platforms on which lsof is supported:-

 Apple Darwin 9 and Mac OS X 10.[567]
 FreeBSD 8.[234], 9.0, 10.0 and 11.0 for AMD64-based systems
 Linux 2.1.72 and above for x86-based systems
 Solaris 9, 10 and 11
1
  • Running lsof is not non-blocking. It needs to at least read the lsof binary before executing it. I need a system call from within my process. Commented Nov 1, 2019 at 11:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.