how can i get a file name knowing its descriptor?
Robert Watson
rwatson at FreeBSD.org
Thu Jul 3 15:26:20 UTC 2008
On Thu, 3 Jul 2008, Uladzislau Rezki wrote:
> I've been writing a small kernel module, that provides information about
> modification of the filesystem to user_land/userspace through the character
> device. I'm using FreeBSD 4.10
>
> So, my question is: Is there any way to get file name knowing its
> descriptor?
Later versions of FreeBSD include a generic routine, vn_fullpath(9), to
convert a vnode reference into a pathname. It's not a particularly reliable
routine, in that it depends on the name cache, but it does work in most cases.
FreeBSD 4.x includes textvp_fullpath(9), which became the foundation for that
routine in later versions; it generates the path to the vnode used for the
text of a process, but could easily be generalized in much the same way
vn_fullpath(9) has been to return the pathname for arbitrary vnodes. Be aware
that pathnames are very much ephemeral in the UNIX design -- vnodes can and do
have one name, no names, or many names, and generating a name for an arbitrary
node wasn't part of the design requirements, so is quite difficult to do;
likewise, not all file systems use the name cache well or at all. If this is
just for debugging purposes, vn_fullpath(9) will do the trick, though, much of
the time.
Robert N M Watson
Computer Laboratory
University of Cambridge
>
> static int
> xxx_write (struct proc *p, struct write_args *uap)
> {
> struct vnode *vn;
> struct file *file;
> int sys_error;
>
> /* do system call */
> sys_error = write(p, uap);
> if (sys_error != 0)
> goto leave_call;
>
> /* get the file */
> file = curproc->p_fd->fd_ofiles[uap->fd];
> /* get the vnode */
> vn = (struct vnode *) file->f_data;
>
> /* do we have a regular */
> if (vn->v_type == VREG) {
> ...
> ...
> ...
> }
>
> As you can see we just know uap->fd.
>
> Thanks.
>
> --
> Uladzislau Rezki
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe at freebsd.org"
>
More information about the freebsd-hackers
mailing list