Path to executable of current process?

Michael B Allen ioplex at gmail.com
Fri Jul 20 03:23:47 UTC 2007


> 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


More information about the freebsd-hackers mailing list