Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 3 Jul 2008 16:26:19 +0100 (BST)
From:      Robert Watson <rwatson@FreeBSD.org>
To:        Uladzislau Rezki <v.rezkii@sam-solutions.net>
Cc:        freebsd-hackers@freebsd.org
Subject:   Re: how can i get a file name knowing its descriptor?
Message-ID:  <20080703162217.N43170@fledge.watson.org>
In-Reply-To: <200807031428.02286.v.rezkii@sam-solutions.net>
References:  <200807031428.02286.v.rezkii@sam-solutions.net>

next in thread | previous in thread | raw e-mail | index | archive | help

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@freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org"
>



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20080703162217.N43170>