qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH] oslib-posix: Use sysctl(2) call to resolve exec


From: Peter Maydell
Subject: Re: [Qemu-devel] [PATCH] oslib-posix: Use sysctl(2) call to resolve exec_dir on NetBSD
Date: Thu, 2 Nov 2017 16:55:00 +0000

On 28 October 2017 at 20:48, Kamil Rytarowski <address@hidden> wrote:
> NetBSD 8.0(beta) ships with KERN_PROC_PATHNAME in sysctl(2).
> Older NetBSD versions can use argv[0] parsing fallback.
>
> This code section is partly shared with FreeBSD.
>
> Signed-off-by: Kamil Rytarowski <address@hidden>
> ---
>  util/oslib-posix.c | 11 ++++++++++-
>  1 file changed, 10 insertions(+), 1 deletion(-)
>
> diff --git a/util/oslib-posix.c b/util/oslib-posix.c
> index 382bd4a231..77369c92ce 100644
> --- a/util/oslib-posix.c
> +++ b/util/oslib-posix.c
> @@ -49,6 +49,10 @@
>  #include <libutil.h>
>  #endif
>
> +#ifdef __NetBSD__
> +#include <sys/sysctl.h>
> +#endif
> +
>  #include "qemu/mmap-alloc.h"
>
>  #ifdef CONFIG_DEBUG_STACK_USAGE
> @@ -250,9 +254,14 @@ void qemu_init_exec_dir(const char *argv0)
>              p = buf;
>          }
>      }
> -#elif defined(__FreeBSD__)
> +#elif defined(__FreeBSD__) \
> +      || (defined(__NetBSD__) && defined(KERN_PROC_PATHNAME))
>      {
> +#if defined(__FreeBSD__)
>          static int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1};
> +#else
> +        static int mib[4] = {CTL_KERN, KERN_PROC_ARGS, -1, 
> KERN_PROC_PATHNAME};
> +#endif
>          size_t len = sizeof(buf) - 1;
>
>          *buf = '\0';

It's a shame the BSDs can't agree on a single way to implement this :-(

I had a look at how Go implements this:
https://github.com/golang/go/commit/2fc67e71af142bfa1e7662a4fde361f43509d2d7

and for NetBSD it uses readlink("/proc/curproc/exe"), which would be
a better fallback for "sysctl not implemented" than the argv[0]
stuff, perhaps (but then nobody's complained much so perhaps not
worth the effort now). It also has /proc/curproc/file for OpenBSD, but
my OpenBSD VM doesn't mount /proc/, which reduces my enthusiasm
for trying for a "generic for BSDs try various /proc/ paths" approach.

In any case, I think this is better than what we have today,
so I've applied it to master.

thanks
-- PMM



reply via email to

[Prev in Thread] Current Thread [Next in Thread]