On Wed, 3 Jun 2020 at 15:23, David CARLIER <devne...@gmail.com> wrote: > > Good point even tough the libproc api is here in this form since quite a time. > > From d23bf60961ee036f8298794f879d1b8b9bd717dc Mon Sep 17 00:00:00 2001 > From: David Carlier <devne...@gmail.com> > Date: Tue, 26 May 2020 21:35:27 +0100 > Subject: [PATCH] util/oslib: current process full path resolution on MacOS > > Using existing libproc to fill the path.
Could you send new versions of the patch as their own emails (ie not replies to the first one) with "[PATCH v2]" "[PATCH v3]" etc in the subject line, please? Otherwise it gets tricky to figure out which is the most recent version of the patch. > Signed-off-by: David Carlier <devne...@gmail.com> > --- > util/oslib-posix.c | 13 +++++++++++++ > 1 file changed, 13 insertions(+) > > diff --git a/util/oslib-posix.c b/util/oslib-posix.c > index 062236a1ab..9dd1e1a18b 100644 > --- a/util/oslib-posix.c > +++ b/util/oslib-posix.c > @@ -55,6 +55,10 @@ > #include <sys/sysctl.h> > #endif > > +#ifdef __APPLE__ > +#include <libproc.h> > +#endif > + > #include "qemu/mmap-alloc.h" > > #ifdef CONFIG_DEBUG_STACK_USAGE > @@ -366,6 +370,15 @@ void qemu_init_exec_dir(const char *argv0) > p = buf; > } > } > +#elif defined(__APPLE__) > + { > + int len; > + len = proc_pidpath(getpid(), buf, sizeof(buf) - 1); > + if (len <= 0) { > + return; I think that if proc_pidpath() doesn't work we should fall back to the "try argv[0]" code below, not return without initializing exec_dir[]. This is what the existing Linux and BSD codepaths do. > + } > + p = buf; > + } > #endif > /* If we don't have any way of figuring out the actual executable > location then try argv[0]. */ I did a bit of searching to see whether there were any alternatives to proc_pidpath(), and I found _NSGetExecutablePath(). This function *is* documented: https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dyld.3.html Will it do what we want here? You'll need to call realpath() on the results, and we should test that it actually does better than just looking at argv[0] -- ie that if you start QEMU via execve(/path/to/qemu, argv_with_bogus_argv0, ...) then we get the /path/to/qemu, not whatever the bogus argv0 value was. There's also the "get the ui/cocoa.m code to find the path via the AppKit API" approach that Roman suggested, though I think that is more awkward than _NSGetExecutablePath() as you'd need to get the ui/cocoa.m code to save the path for you. If that doesn't work then I guess we can use proc_pidpath(), but I'd rather avoid that if we can. If we do have to go that route we should have a comment noting that it's an undocumented and unsupported API but that it's also used by well-known applications X, Y... thanks -- PMM