From owner-freebsd-hackers@FreeBSD.ORG Wed Jul 21 15:12:21 2004 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A8DE816A4CE for ; Wed, 21 Jul 2004 15:12:21 +0000 (GMT) Received: from dan.emsphone.com (dan.emsphone.com [199.67.51.101]) by mx1.FreeBSD.org (Postfix) with ESMTP id 3FC3E43D4C for ; Wed, 21 Jul 2004 15:12:21 +0000 (GMT) (envelope-from dan@dan.emsphone.com) Received: (from dan@localhost) by dan.emsphone.com (8.12.10/8.12.10) id i6LFCKqu007645; Wed, 21 Jul 2004 10:12:20 -0500 (CDT) (envelope-from dan) Date: Wed, 21 Jul 2004 10:12:20 -0500 From: Dan Nelson To: Joe Marcus Clarke Message-ID: <20040721151220.GB42575@dan.emsphone.com> References: <1090378066.90026.105.camel@shumai.marcuscom.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1090378066.90026.105.camel@shumai.marcuscom.com> X-OS: FreeBSD 5.2-CURRENT X-message-flag: Outlook Error User-Agent: Mutt/1.5.6i cc: hackers@freebsd.org Subject: Re: Getting a fully-qualified path from a PID X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Jul 2004 15:12:21 -0000 In the last episode (Jul 20), Joe Marcus Clarke said: > What is the canonical way for a userland application to get the > fully-qualified path of an executable from its running PID? I know I > can do a readlink(2) on /proc/pid/file, but procfs is deprecated on > 5.X, correct? Is there a more appropriate way to do this? Thanks. realpath(argv[0]) works for commands not run from $PATH. Commands found through a PATH earch will just have the basename in argv[0] so you would have to check each PATH element until you found it. Note that /proc/pid/file won't work if vn_fullpath() fails (say the orignal file has been unlinked, or the filename has expired from the kernel's cache). If you are examining another process, you can use the kvm_getargv() and kvm_getenvv() functions to fetch argv[0] and PATH out of the target process. -- Dan Nelson dnelson@allantgroup.com