Is it possible to determine the open file path based on the file descriptor?
Mateusz Guzik
mjguzik at gmail.com
Wed Jul 8 16:59:38 UTC 2020
On 7/8/20, Mateusz Guzik <mjguzik at gmail.com> wrote:
> On 7/8/20, Yuri <yuri at rawbw.com> wrote:
>> Hi Hackers,
>>
>>
>> One of the ports (sysutils/watchman) attempts to find the path of the
>> file based on the file descriptor assuming that this descriptor
>> represents an open file.
>>
>>
>> Is this possible?
>>
>>
>
> Yes and no. procstat -f can report that, but not always. The crux is
> that the namecache is optional -- no name may be present and then we
> don't have good means to find it. There is a side corner case where
> you can have *more* than one name and then the kernel has no idea
> which was used to reach the file for given fd.
>
> I think the namecache should start getting new names in all cases and
> that's perhaps something to change for 13.
>
> Even for the working case the interface used by procstat is a bad pick.
>
> That said, I can add a fcntl which will translate the fd into a full
> path if the name can be found, but I don't know how useful it can be
> with this program given the stipulation above.
>
Now that I wrote that e-mail I remembered I already implemented this,
here is a forward version against head:
https://people.freebsd.org/~mjg/F_GETPATH.diff
#include <fcntl.h>
#define MAXPATHLEN 1024
#define F_GETPATH 22
int
main(int argc, char **argv)
{
char buf[MAXPATHLEN];
int fd;
if (argc != 2)
return (1);
fd = open(argv[1], O_RDONLY);
if (fd == -1)
err(1, "open");
if (fcntl(fd, F_GETPATH, buf) == -1)
err(1, "fcntl");
printf("[%s]\n", buf);
}
I can't be arsed to find all the proper headers, but you get the idea.
If this makes the program work well enough I can commit and merge to
stable/12.
--
Mateusz Guzik <mjguzik gmail.com>
More information about the freebsd-hackers
mailing list