2

In Linux kernel module, while calculating absolute path by file descriptor, fcheck() or fcheck_files() are used. I didn't get much information about these functions. I need to know which function is appropriate for which situation. When these functions fails? Where to find documentation about such functions? As stated here,

... To look up the file structure given an fd, a reader must use either fcheck() or fcheck_files() APIs....

But no information is given about which function should be used when and for what.

Thanks in advance!

Nitinkumar Ambekar
  • 969
  • 20
  • 39

1 Answers1

1

fcheck() is defined as a preprocessor macro in include/linux/fdtable.h, line 90 as:

#define fcheck(fd)      fcheck_files(current->files, fd)

And fcheck_files() is defined as a function in include/linux/fdtable.h, line 77 as:

static inline struct file * fcheck_files(struct files_struct *files, unsigned int fd)
{
        struct file * file = NULL;
        struct fdtable *fdt = files_fdtable(files);

        if (fd < fdt->max_fds)
                file = rcu_dereference_check_fdtable(files, fdt->fd[fd]);
        return file;
}

They are not two functions thus no need to confuse which is appropriate for which function.

The fcheck() function is used to

Check whether the specified fd has an open file.

Wayne Sudo
  • 92
  • 5
  • Thanks @WayneSudo for this nice answer, but what if I use this function for file descriptor and not for directory descriptor? Does I have any way to check fd, whether it's for file or directory? – Nitinkumar Ambekar Aug 28 '15 at 07:42
  • In short, I get _NULL_ for empty directory, but not for file. What do you mean **open file** here? Please elaborate. – Nitinkumar Ambekar Aug 28 '15 at 07:46
  • It means if fd is valid, then return the corresponding file struct, if not return NULL. – Wayne Sudo Aug 28 '15 at 08:04
  • When a fd should be considered as invalid fd? – Nitinkumar Ambekar Aug 28 '15 at 08:09
  • In which cases this will return _null_? 1) fd for non empty dir, 2) fd for empty dir, 3) fd for file. – Nitinkumar Ambekar Aug 28 '15 at 08:18
  • Each running process has its own fd table, if the fd checked by fcheck() is not on the fd table, it would be considered as invalid fd and the function would return NULL. For example, a fail open() would return -1 as an invalid fd. – Wayne Sudo Aug 28 '15 at 08:19
  • It doesn't matter whether the dir is empty or not, whenever a file or other i/o stream is opened, it would get its own fd in the fd table. – Wayne Sudo Aug 28 '15 at 08:28
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/88171/discussion-between-ntn-and-wayne-sudo). – Nitinkumar Ambekar Aug 28 '15 at 09:39