Re: Path to executable of current process?



On 2007-Jul-19 22:00:23 -0400, Michael B Allen <ioplex@xxxxxxxxx> 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 <ioplex@xxxxxxxxx> wrote:
After more digging I see sysctl seems to be the way to do this but can I
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] = KERN_PROC_PID;
This should be KERN_PROC_ARGS

len = sizeof(struct kinfo_proc);
if (sysctl(mib, 4, &ki_proc, &len, NULL, 0) == -1)

And the buffer argument should be char[], not struct kinfo_proc.

--
Peter Jeremy

Attachment: pgpLKlFaq58jn.pgp
Description: PGP signature



Relevant Pages

  • Re: Path to executable of current process?
    ... googling around I can't seem to find an example. ... GDB to do a backtrace and I need to give GDB the path to the ... backtrace unless it so happens to be running as root. ...
    (freebsd-hackers)
  • Re: how do you find the root namespace at runtime?
    ... But there is no guarantee that the assembly's startup object is not ... contained within a deeper namespace than the root. ... Ugh, but again no guarantee... ...
    (microsoft.public.dotnet.languages.vb)
  • Re: bound on eigenvalues...
    ... comtech wrote: ... -0.0117421, there must be a root between 0 and 0.1, a second one between ... and takes on opposite signs at a and b, the intermediate value theorem tells you that it is zero at some point between a and b. ... You can use estimates of the value of the function to guarantee the sign change. ...
    (sci.math)