Programmatically retrieving the absolute path of an OS X command-line app
On Linux, an application can easily get its absolute path by querying /proc/self/exe
. On FreeBSD, it's more involved, since you have to build up a sysctl call:
int mib[4];
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
char buf[1024];
size_t cb = sizeof(buf);
sysctl(mib, 4, buf, &cb, NULL, 0);
but it's still completely doable. Yet I cannot find a way to determine this on OS X for a command-line application. If you're running from within an app bundle, you can determine it by running [[NSBundle mainBundle] bundlePath]
, but because command-line applications are not in bundles, this doesn't help.
(Note: consulting argv[0]
is not a reasonable answer, since, if launched from a symlink, argv[0]
will be that symlink--not the ultimate path to the executable called. argv[0]
can also lie if a dumb application uses an exec()
call and forget to initialize argv properly, which I have seen in the wild.)
Solution 1:
The function _NSGetExecutablePath
will return a full path to the executable (GUI or not). The path may contain symbolic links, "..
", etc. but the realpath
function can be used to clean those up if needed. See man
3
dyld
for more information.
char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0)
printf("executable path is %s\n", path);
else
printf("buffer too small; need size %u\n", size);
The secret to this function is that the Darwin kernel puts the executable path on the process stack immediately after the envp
array when it creates the process. The dynamic link editor dyld
grabs this on initialization and keeps a pointer to it. This function uses that pointer.
Solution 2:
I believe there is much more elegant solution, which actually works for any PID, and also returns the absolute path directly:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libproc.h>
int main (int argc, char* argv[])
{
int ret;
pid_t pid;
char pathbuf[PROC_PIDPATHINFO_MAXSIZE];
pid = getpid();
ret = proc_pidpath (pid, pathbuf, sizeof(pathbuf));
if ( ret <= 0 ) {
fprintf(stderr, "PID %d: proc_pidpath ();\n", pid);
fprintf(stderr, " %s\n", strerror(errno));
} else {
printf("proc %d: %s\n", pid, pathbuf);
}
return 0;
}
Solution 3:
Looks like the answer is that you can't do it:
I'm trying to achieve something like lsof's functionality and gather a whole bunch of statistics and info about running processes. If lsof weren't so slow, I'd be happy sticking with it.
If you reimplement lsof, you will find that it's slow because it's doing a lot of work.
I guess that's not really because lsof is user-mode, it's more that it has to scan through a task's address space looking for things backed by an external pager. Is there any quicker way of doing this when I'm in the kernel?
No. lsof is not stupid; it's doing what it has to do. If you just want a subset of its functionality, you might want to consider starting with the lsof source (which is available) and trimming it down to meet your requirements.
Out of curiosity, is
p_textvp
used at all? It looks like it's set to the parent'sp_textvp
inkern_fork
(and then getting released??) but it's not getting touched in any ofkern_exec
's routines.
p_textvp
is not used. In Darwin, the proc is not the root of the address space; the task is. There is no concept of "the vnode" for a task's address space, as it is not necessarily initially populated by mapping one.If exec were to populate p_textvp, it would pander to the assumption that all processes are backed by a vnode. Then programmers would assume that it was possible to get a path to the vnode, and from there it is a short jump to the assumption that the current path to the vnode is the path from which it was launched, and that text processing on the string might lead to the application bundle name... all of which would be impossible to guarantee without substantial penalty.
Solution 4:
This is late, but [[NSBundle mainBundle] executablePath]
works just fine for non-bundled, command-line programs.