[libvirt] [PATCH 1/1] Making path detection more robust in VirtualBox
Daniel P. Berrange
berrange at redhat.com
Thu Apr 30 11:16:06 UTC 2009
On Wed, Apr 29, 2009 at 01:14:02PM +0200, Pritesh Kothari wrote:
> Hi All,
>
> PATCH 1/1: contains changes in the glue code for making path detection more
> robust in VirtualBox
ACK
> commit 0f31b4cce710ab6efea352174049e305b29a7a2f
> Author: Pritesh Kothari <Pritesh.Kothari at Sun.com>
> Date: Wed Apr 29 10:52:37 2009 +0200
>
> made the path detection more robust in the glue library
>
> diff --git a/src/vbox/vbox_XPCOMCGlue.c b/src/vbox/vbox_XPCOMCGlue.c
> index bcda3ce..258663a 100644
> --- a/src/vbox/vbox_XPCOMCGlue.c
> +++ b/src/vbox/vbox_XPCOMCGlue.c
> @@ -36,6 +36,7 @@
> #include <stdio.h>
> #include <string.h>
> #include <stdlib.h>
> +#include <stdarg.h>
> #include <dlfcn.h>
>
> #include "vbox_XPCOMCGlue.h"
> @@ -61,41 +62,72 @@
> /** The dlopen handle for VBoxXPCOMC. */
> void *g_hVBoxXPCOMC = NULL;
> /** The last load error. */
> +char g_szVBoxErrMsg[256];
> +/** Pointer to the VBoxXPCOMC function table. */
> PCVBOXXPCOM g_pVBoxFuncs = NULL;
> /** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */
> PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions = NULL;
>
>
> /**
> + * Wrapper for setting g_szVBoxErrMsg. Can be an empty stub.
> + *
> + * @param fAlways When 0 the g_szVBoxErrMsg is only set if empty.
> + * @param pszFormat The format string.
> + * @param ... The arguments.
> + */
> +static void setErrMsg(int fAlways, const char *pszFormat, ...)
> +{
> +#ifndef LIBVIRT_VERSION
> + if ( fAlways
> + || !g_szVBoxErrMsg[0])
> + {
> + va_list va;
> + va_start(va, pszFormat);
> + vsnprintf(g_szVBoxErrMsg, sizeof(g_szVBoxErrMsg), pszFormat, va);
> + va_end(va);
> + }
> +#else /* libvirt */
> + (void)fAlways;
> + (void)pszFormat;
> +#endif /* libvirt */
> +}
> +
> +
> +/**
> * Try load VBoxXPCOMC.so/dylib/dll from the specified location and resolve all
> * the symbols we need.
> *
> * @returns 0 on success, -1 on failure.
> - * @param pszHome The director where to try load VBoxXPCOMC from. Can be NULL.
> - * @param fSetAppHome Whether to set the VBOX_APP_HOME env.var. or not (boolean).
> + * @param pszHome The director where to try load VBoxXPCOMC from. Can
> + * be NULL.
> + * @param fSetAppHome Whether to set the VBOX_APP_HOME env.var. or not
> + * (boolean).
> */
> static int tryLoadOne(const char *pszHome, int fSetAppHome)
> {
> size_t cchHome = pszHome ? strlen(pszHome) : 0;
> size_t cbBufNeeded;
> - char szBuf[4096];
> + char szName[4096];
> int rc = -1;
>
> /*
> * Construct the full name.
> */
> cbBufNeeded = cchHome + sizeof("/" DYNLIB_NAME);
> - if (cbBufNeeded > sizeof(szBuf))
> + if (cbBufNeeded > sizeof(szName))
> {
> + setErrMsg(1, "path buffer too small: %u bytes needed",
> + (unsigned)cbBufNeeded);
> return -1;
> }
> if (cchHome)
> {
> - memcpy(szBuf, pszHome, cchHome);
> - szBuf[cchHome] = '/';
> + memcpy(szName, pszHome, cchHome);
> + szName[cchHome] = '/';
> cchHome++;
> }
> - memcpy(&szBuf[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
> + memcpy(&szName[cchHome], DYNLIB_NAME, sizeof(DYNLIB_NAME));
>
> /*
> * Try load it by that name, setting the VBOX_APP_HOME first (for now).
> @@ -108,7 +140,7 @@ static int tryLoadOne(const char *pszHome, int fSetAppHome)
> else
> unsetenv("VBOX_APP_HOME");
> }
> - g_hVBoxXPCOMC = dlopen(szBuf, RTLD_NOW | RTLD_LOCAL);
> + g_hVBoxXPCOMC = dlopen(szName, RTLD_NOW | RTLD_LOCAL);
> if (g_hVBoxXPCOMC)
> {
> PFNVBOXGETXPCOMCFUNCTIONS pfnGetFunctions;
> @@ -120,15 +152,21 @@ static int tryLoadOne(const char *pszHome, int fSetAppHome)
> if (g_pVBoxFuncs)
> {
> g_pfnGetFunctions = pfnGetFunctions;
> - rc = 0;
> + return 0;
> }
> +
> + /* bail out */
> + setErrMsg(1, "%.80s: pfnGetFunctions(%#x) failed",
> + szName, VBOX_XPCOMC_VERSION);
> }
> - if (rc != 0)
> - {
> - dlclose(g_hVBoxXPCOMC);
> - g_hVBoxXPCOMC = NULL;
> - }
> + else
> + setErrMsg(1, "dlsym(%.80s/%.32s): %.128s",
> + szName, VBOX_GET_XPCOMC_FUNCTIONS_SYMBOL_NAME, dlerror());
> + dlclose(g_hVBoxXPCOMC);
> + g_hVBoxXPCOMC = NULL;
> }
> + else
> + setErrMsg(0, "dlopen(%.80s): %.160s", szName, dlerror());
> return rc;
> }
>
> @@ -156,6 +194,7 @@ int VBoxCGlueInit(void)
> /*
> * Try the known standard locations.
> */
> + g_szVBoxErrMsg[0] = '\0';
> #if defined(__gnu__linux__) || defined(__linux__)
> if (tryLoadOne("/opt/VirtualBox", 1) == 0)
> return 0;
> @@ -201,5 +240,6 @@ void VBoxCGlueTerm(void)
> }
> g_pVBoxFuncs = NULL;
> g_pfnGetFunctions = NULL;
> + memset(g_szVBoxErrMsg, 0, sizeof(g_szVBoxErrMsg));
> }
>
> diff --git a/src/vbox/vbox_XPCOMCGlue.h b/src/vbox/vbox_XPCOMCGlue.h
> index cf2e947..f93adaf 100644
> --- a/src/vbox/vbox_XPCOMCGlue.h
> +++ b/src/vbox/vbox_XPCOMCGlue.h
> @@ -29,6 +29,7 @@
> #ifndef ___VBoxXPCOMC_cglue_h
> #define ___VBoxXPCOMC_cglue_h
>
> +/* This has to be the oldest version we support. */
> #include "vbox_CAPI_v2_2.h"
>
> #ifdef __cplusplus
> @@ -39,7 +40,7 @@ extern "C" {
> extern void *g_hVBoxXPCOMC;
> /** The last load error. */
> extern char g_szVBoxErrMsg[256];
> -/** Pointer to the VBoxXPCOMC function table. */
> +/** Pointer to the VBoxXPCOMC function table. */
> extern PCVBOXXPCOM g_pVBoxFuncs;
> /** Pointer to VBoxGetXPCOMCFunctions for the loaded VBoxXPCOMC so/dylib/dll. */
> extern PFNVBOXGETXPCOMCFUNCTIONS g_pfnGetFunctions;
> --
> Libvir-list mailing list
> Libvir-list at redhat.com
> https://www.redhat.com/mailman/listinfo/libvir-list
--
|: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :|
|: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :|
|: http://autobuild.org -o- http://search.cpan.org/~danberr/ :|
|: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|
More information about the libvir-list
mailing list