From nobody Fri Dec 19 06:38:59 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) client-ip=209.132.183.25; envelope-from=libvir-list-bounces@redhat.com; helo=mx4-phx2.redhat.com; Authentication-Results: mx.zoho.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.25 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; Return-Path: Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) by mx.zohomail.com with SMTPS id 1486571399049977.0923809885538; Wed, 8 Feb 2017 08:29:59 -0800 (PST) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v18GQjVW017406; Wed, 8 Feb 2017 11:26:45 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v18GQghW017888 for ; Wed, 8 Feb 2017 11:26:42 -0500 Received: from angien.brq.redhat.com (dhcp129-162.brq.redhat.com [10.34.129.162]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id v18GQbSk027100; Wed, 8 Feb 2017 11:26:42 -0500 From: Peter Krempa To: libvir-list@redhat.com Date: Wed, 8 Feb 2017 17:27:04 +0100 Message-Id: <1e55ec6048fc7d98ea8807b86de7e67dc94f39c4.1486571179.git.pkrempa@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: libvir-list@redhat.com Cc: Peter Krempa Subject: [libvirt] [PATCH 04/11] driver: Split/refactor driver module loading X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Split the convoluted driver loader function into simpler parts which will potentially allow reuse. --- src/driver.c | 133 +++++++++++++++++++++++++++++-------= ---- src/driver.h | 3 + src/libvirt_driver_modules.syms | 1 + 3 files changed, 101 insertions(+), 36 deletions(-) diff --git a/src/driver.c b/src/driver.c index 67ac02006..783e08a28 100644 --- a/src/driver.c +++ b/src/driver.c @@ -43,15 +43,105 @@ VIR_LOG_INIT("driver"); # include # define DEFAULT_DRIVER_DIR LIBDIR "/libvirt/connection-driver" + +static void * +virDriverLoadModuleFile(const char *file) +{ + void *handle =3D NULL; + + VIR_DEBUG("Load module file '%s'", file); + + if (access(file, R_OK) < 0) { + VIR_INFO("Module %s not accessible", file); + return NULL; + } + + virUpdateSelfLastChanged(file); + + if (!(handle =3D dlopen(file, RTLD_NOW | RTLD_GLOBAL))) + VIR_ERROR(_("failed to load module %s %s"), file, dlerror()); + + return handle; +} + + +static void * +virDriverLoadModuleFunc(void *handle, + const char *funcname) +{ + void *regsym; + + VIR_DEBUG("Lookup function '%s'", funcname); + + if (!(regsym =3D dlsym(handle, funcname))) + VIR_ERROR(_("Missing module registration symbol %s"), funcname); + + return regsym; +} + + +/** + * virDriverLoadModuleFull: + * @path: filename of module to load + * @regfunc: name of the function that registers the module + * @handle: Returns handle of the loaded library if not NULL + * + * Loads a loadable module named @path and calls the + * registration function @regfunc. If @handle is not NULL the handle is re= turned + * in the variable. Otherwise the handle is leaked so that the module stays + * loaded forever. + * + * The module is automatically looked up in the appropriate place (git or + * installed directory). + * + * Returns 0 on success, 1 if the module was not found and -1 on any error. + */ +int +virDriverLoadModuleFull(const char *path, + const char *regfunc, + void **handle) +{ + void *rethandle =3D NULL; + int (*regsym)(void); + int ret =3D -1; + + VIR_DEBUG("Module load %s", path); + + if (!(rethandle =3D virDriverLoadModuleFile(path))) { + ret =3D 1; + goto cleanup; + } + + if (!(regsym =3D virDriverLoadModuleFunc(rethandle, regfunc))) + goto cleanup; + + if ((*regsym)() < 0) { + VIR_ERROR(_("Failed module registration %s"), regfunc); + goto cleanup; + } + + if (handle) + VIR_STEAL_PTR(*handle, rethandle); + else + rethandle =3D NULL; + + ret =3D 0; + + cleanup: + if (rethandle) + dlclose(rethandle); + return ret; +} + + void * virDriverLoadModule(const char *name) { - char *modfile =3D NULL, *regfunc =3D NULL, *fixedname =3D NULL; + char *modfile =3D NULL; + char *fixedname =3D NULL; + char *regfunc =3D NULL; char *tmp; void *handle =3D NULL; - int (*regsym)(void); - - VIR_DEBUG("Module load %s", name); if (!(modfile =3D virFileFindResourceFull(name, "libvirt_driver_", @@ -61,19 +151,6 @@ virDriverLoadModule(const char *name) "LIBVIRT_DRIVER_DIR"))) return NULL; - if (access(modfile, R_OK) < 0) { - VIR_INFO("Module %s not accessible", modfile); - goto cleanup; - } - - virUpdateSelfLastChanged(modfile); - - handle =3D dlopen(modfile, RTLD_NOW | RTLD_GLOBAL); - if (!handle) { - VIR_ERROR(_("failed to load module %s %s"), modfile, dlerror()); - goto cleanup; - } - if (VIR_STRDUP_QUIET(fixedname, name) < 0) { VIR_ERROR(_("out of memory")); goto cleanup; @@ -88,29 +165,13 @@ virDriverLoadModule(const char *name) if (virAsprintfQuiet(®func, "%sRegister", fixedname) < 0) goto cleanup; - regsym =3D dlsym(handle, regfunc); - if (!regsym) { - VIR_ERROR(_("Missing module registration symbol %s"), regfunc); - goto cleanup; - } - - if ((*regsym)() < 0) { - VIR_ERROR(_("Failed module registration %s"), regfunc); - goto cleanup; - } - - VIR_FREE(modfile); - VIR_FREE(regfunc); - VIR_FREE(fixedname); - return handle; + virDriverLoadModuleFull(modfile, regfunc, &handle); cleanup: VIR_FREE(modfile); - VIR_FREE(regfunc); VIR_FREE(fixedname); - if (handle) - dlclose(handle); - return NULL; + VIR_FREE(regfunc); + return handle; } diff --git a/src/driver.h b/src/driver.h index e4e382b63..885e8843e 100644 --- a/src/driver.h +++ b/src/driver.h @@ -100,5 +100,8 @@ int virSetSharedSecretDriver(virSecretDriverPtr driver)= ATTRIBUTE_RETURN_CHECK; int virSetSharedStorageDriver(virStorageDriverPtr driver) ATTRIBUTE_RETURN= _CHECK; void *virDriverLoadModule(const char *name); +int virDriverLoadModuleFull(const char *name, + const char *regfunc, + void **handle); #endif /* __VIR_DRIVER_H__ */ diff --git a/src/libvirt_driver_modules.syms b/src/libvirt_driver_modules.s= yms index f9d0ee9b9..bd9bf1c31 100644 --- a/src/libvirt_driver_modules.syms +++ b/src/libvirt_driver_modules.syms @@ -4,6 +4,7 @@ # driver.h virDriverLoadModule; +virDriverLoadModuleFull; # Let emacs know we want case-insensitive sorting # Local Variables: --=20 2.11.0 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list