From owner-freebsd-hackers@FreeBSD.ORG Fri Jul 20 11:12:12 2007 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 2262016A418 for ; Fri, 20 Jul 2007 11:12:12 +0000 (UTC) (envelope-from peterjeremy@optushome.com.au) Received: from turion.vk2pj.dyndns.org (c220-239-20-82.belrs4.nsw.optusnet.com.au [220.239.20.82]) by mx1.freebsd.org (Postfix) with ESMTP id 8356413C46E for ; Fri, 20 Jul 2007 11:12:11 +0000 (UTC) (envelope-from peterjeremy@optushome.com.au) Received: from turion.vk2pj.dyndns.org (localhost.vk2pj.dyndns.org [127.0.0.1]) by turion.vk2pj.dyndns.org (8.14.1/8.14.1) with ESMTP id l6KBC9dY001906; Fri, 20 Jul 2007 21:12:09 +1000 (EST) (envelope-from peter@turion.vk2pj.dyndns.org) Received: (from peter@localhost) by turion.vk2pj.dyndns.org (8.14.1/8.14.1/Submit) id l6KBC9XO001905; Fri, 20 Jul 2007 21:12:09 +1000 (EST) (envelope-from peter) Date: Fri, 20 Jul 2007 21:12:09 +1000 From: Peter Jeremy To: Michael B Allen Message-ID: <20070720111209.GV1176@turion.vk2pj.dyndns.org> References: <78c6bd860707191725r14b8bfe3sf15c1f0e30cf82ca@mail.gmail.com> <78c6bd860707191900g375f98ado8315603feac50247@mail.gmail.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="h31gzZEtNLTqOjlF" Content-Disposition: inline In-Reply-To: <78c6bd860707191900g375f98ado8315603feac50247@mail.gmail.com> X-PGP-Key: http://members.optusnet.com.au/peterjeremy/pubkey.asc User-Agent: Mutt/1.5.16 (2007-06-09) Cc: freebsd-hackers Subject: Re: Path to executable of current process? 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: Fri, 20 Jul 2007 11:12:12 -0000 --h31gzZEtNLTqOjlF Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2007-Jul-19 22:00:23 -0400, Michael B Allen wrote: >Well I figured out how to get kvm_getargv working. Unfortunately it >seems only root can call kvm_open so the faulting process can't >backtrace unless it so happens to be running as root (which it's not). > >Is there any way to get argv[0] for the current process without being root? I suggest you have a look at the source to ps - it does not run with elevated privileges. Note that argv[0] is not necessarily a full pathname - it is whatever the program was invoked with. Even if it _is_ a full pathname, there is no guarantee that it points to the same object - the executable may be in a chroot environment or that pathname may have been replaced. The only guaranteed way to access the executable associated with a running process is via procfs. As you point out earlier, this is not mounted by default on FreeBSD because none of the base system utilities need it (apart from 'ps -e') - you will need to arrange for it to be mounted. On 2007-Jul-19 23:23:45 -0400, Michael B Allen wrote: >After more digging I see sysctl seems to be the way to do this but can I= =20 >get the full path to the executable form kinfo_proc? > >How does ps do this? It uses kvm_getargv(3). If you want to do it directly via sysctl(2), I suggest you examine /usr/src/lib/libkvm/kvm_proc.c. > mib[2] =3D KERN_PROC_PID; This should be KERN_PROC_ARGS > len =3D sizeof(struct kinfo_proc); > if (sysctl(mib, 4, &ki_proc, &len, NULL, 0) =3D=3D -1) And the buffer argument should be char[], not struct kinfo_proc. --=20 Peter Jeremy --h31gzZEtNLTqOjlF Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.7 (FreeBSD) iD8DBQFGoJiJ/opHv/APuIcRAtcpAJ9/zq/z9vLxlJgeM5Q9w1ck7KSdsACgqoqP fGvVxElsOLM2YYC3/SHEH/0= =7xI6 -----END PGP SIGNATURE----- --h31gzZEtNLTqOjlF--