From owner-freebsd-hackers@FreeBSD.ORG Thu Jul 3 15:26:20 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id BF38A1065679 for ; Thu, 3 Jul 2008 15:26:20 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from cyrus.watson.org (cyrus.watson.org [209.31.154.42]) by mx1.freebsd.org (Postfix) with ESMTP id 940838FC18 for ; Thu, 3 Jul 2008 15:26:20 +0000 (UTC) (envelope-from rwatson@FreeBSD.org) Received: from fledge.watson.org (fledge.watson.org [209.31.154.41]) by cyrus.watson.org (Postfix) with ESMTP id ED22746BA1; Thu, 3 Jul 2008 11:26:19 -0400 (EDT) Date: Thu, 3 Jul 2008 16:26:19 +0100 (BST) From: Robert Watson X-X-Sender: robert@fledge.watson.org To: Uladzislau Rezki In-Reply-To: <200807031428.02286.v.rezkii@sam-solutions.net> Message-ID: <20080703162217.N43170@fledge.watson.org> References: <200807031428.02286.v.rezkii@sam-solutions.net> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII; format=flowed Cc: freebsd-hackers@freebsd.org Subject: Re: how can i get a file name knowing its descriptor? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 03 Jul 2008 15:26:20 -0000 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" >