Re: Path to executable of current process?

2007-07-20 Thread Steve Watt
In <[EMAIL PROTECTED]>,
Michael B Allen <[EMAIL PROTECTED]> wrote:

>Actually what I'm *really* trying to do is port some code that invokes
>GDB to do a backtrace and I need to give GDB the path to the
>executable of the current process (e.g. on linux this is
>/proc//exe) and the pid of the process to trace (easy - getpid).
>The first argument is trickey since FreeBSD frequently does not have a
>/proc filesystem. So it seems kvm_getargv should have this path no?

# mount_procfs proc /proc

# /bin/ls -l /proc//file

Note that if the executable on disk gets replaced, this won't work.
-- 
Steve Watt KD6GGD  PP-ASEL-IA  ICBM: 121W 56' 57.5" / 37N 20' 15.3"
 Internet: steve @ Watt.COM  Whois: SW32-ARIN
   Free time?  There's no such thing.  It just comes in varying prices...
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Path to executable of current process?

2007-07-20 Thread Peter Jeremy
On 2007-Jul-19 22:00:23 -0400, Michael B Allen <[EMAIL PROTECTED]> 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 <[EMAIL PROTECTED]> 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


pgp5pk4M5uA63.pgp
Description: PGP signature


Re: Path to executable of current process?

2007-07-19 Thread Michael B Allen

Is there any way to get argv[0] for [a particular] process without being root?


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?

static const char *
getcmdline(pid_t pid)
{
   static struct kinfo_proc ki_proc;
   int mib[4], len;

   mib[0] = CTL_KERN;
   mib[1] = KERN_PROC;
   mib[2] = KERN_PROC_PID;
   mib[3] = pid;

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

   return ki_proc.ki_???
}

Mike
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Re: Path to executable of current process?

2007-07-19 Thread Michael B Allen

On 7/19/07, Michael B Allen <[EMAIL PROTECTED]> wrote:

Hello,

I'm looking for an example that uses kvm_getargv but from just
googling around I can't seem to find an example. Can someone give me a
pointer?

Actually what I'm *really* trying to do is port some code that invokes
GDB to do a backtrace and I need to give GDB the path to the
executable of the current process (e.g. on linux this is
/proc//exe) and the pid of the process to trace (easy - getpid).
The first argument is trickey since FreeBSD frequently does not have a
/proc filesystem. So it seems kvm_getargv should have this path no?


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?

Mike
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"


Path to executable of current process?

2007-07-19 Thread Michael B Allen

Hello,

I'm looking for an example that uses kvm_getargv but from just
googling around I can't seem to find an example. Can someone give me a
pointer?

Actually what I'm *really* trying to do is port some code that invokes
GDB to do a backtrace and I need to give GDB the path to the
executable of the current process (e.g. on linux this is
/proc//exe) and the pid of the process to trace (easy - getpid).
The first argument is trickey since FreeBSD frequently does not have a
/proc filesystem. So it seems kvm_getargv should have this path no?

Mike
___
freebsd-hackers@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
To unsubscribe, send any mail to "[EMAIL PROTECTED]"