From patchwork Fri Apr 11 16:50:02 2014 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hannes Weisbach X-Patchwork-Id: 3971111 Return-Path: X-Original-To: patchwork-linux-rdma@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork2.web.kernel.org (Postfix) with ESMTP id 53E21BFF02 for ; Fri, 11 Apr 2014 16:50:16 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 6DF47207F0 for ; Fri, 11 Apr 2014 16:50:15 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4C700207E6 for ; Fri, 11 Apr 2014 16:50:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757970AbaDKQuJ (ORCPT ); Fri, 11 Apr 2014 12:50:09 -0400 Received: from mout.gmx.net ([212.227.17.22]:59199 "EHLO mout.gmx.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754403AbaDKQuC convert rfc822-to-8bit (ORCPT ); Fri, 11 Apr 2014 12:50:02 -0400 Received: from os-dhcp008.inf.tu-dresden.de ([141.76.49.8]) by mail.gmx.com (mrgmx102) with ESMTPSA (Nemesis) id 0M7UUd-1WwlTT0Ukx-00xGDg for ; Fri, 11 Apr 2014 18:50:00 +0200 From: Hannes Weisbach Reply-To: Hannes Weisbach , linux-rdma@vger.kernel.org Subject: [PATCH] Add cleanup routines. Date: Fri, 11 Apr 2014 18:50:02 +0200 Message-Id: To: linux-rdma@vger.kernel.org Mime-Version: 1.0 (Mac OS X Mail 7.2 \(1874\)) X-Mailer: Apple Mail (2.1874) X-Provags-ID: V03:K0:7qzfzN6GJjdUVHVB3rNjdBb9GZuekL/fJyuBapg/3ZGmI3q03J+ pxF7DhzS1MXRDcgPPKM1KB3xBD23YjoNX4FSGBoUvumxPKXyoCEfvwXDx9tkLG75qPKyj1r KO744yhnFQOfeQrQ6rrjeAVplUt8z/ex+9A3ZnkOwF8f7hkfM/Q2RsHg5B4DQxsgYsljtGv CBD+m7powjPj2CHKleJoA== Sender: linux-rdma-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-rdma@vger.kernel.org X-Spam-Status: No, score=-7.3 required=5.0 tests=BAYES_00,FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Dynamically loaded library handles are saved in a list and dlclosed() on exit. The list of struct ibv_driver *, as well as the global struct ibv_device ** list are free()d. Signed-off-by: Hannes Weisbach --- src/device.c | 10 ++++++++++ src/init.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 56 insertions(+), 5 deletions(-) diff --git a/src/device.c b/src/device.c index beb7b3c..d5b76bb 100644 --- a/src/device.c +++ b/src/device.c @@ -305,3 +305,13 @@ void __ibv_ack_async_event(struct ibv_async_event *event) } } default_symver(__ibv_ack_async_event, ibv_ack_async_event); + +FINI static void ibverbs_deinit() +{ + size_t i; + for (i = 0; i < num_devices; i++) { + /* driver callback needed. May not be malloc'd memory */ + free(device_list[i]); + } + free(device_list); +} diff --git a/src/init.c b/src/init.c index d0e4b1c..2a8bca4 100644 --- a/src/init.c +++ b/src/init.c @@ -67,6 +67,11 @@ struct ibv_driver_name { struct ibv_driver_name *next; }; +struct ibv_so_list { + void *dlhandle; + struct ibv_so_list *next; +}; + struct ibv_driver { const char *name; ibv_driver_init_func init_func; @@ -77,6 +82,7 @@ struct ibv_driver { static struct ibv_sysfs_dev *sysfs_dev_list; static struct ibv_driver_name *driver_name_list; static struct ibv_driver *head_driver, *tail_driver; +static struct ibv_so_list *so_list; static int find_sysfs_devs(void) { @@ -193,7 +199,14 @@ void verbs_register_driver(const char *name, verbs_driver_init_func init_func) static void load_driver(const char *name) { char *so_name; - void *dlhandle; + struct ibv_so_list *elem; + struct ibv_so_list **list; + + elem = malloc(sizeof(*elem)); + if(!elem) + return; + + elem->next = NULL; #define __IBV_QUOTE(x) #x #define IBV_QUOTE(x) __IBV_QUOTE(x) @@ -205,16 +218,25 @@ static void load_driver(const char *name) name) < 0) { fprintf(stderr, PFX "Warning: couldn't load driver '%s'.\n", name); - return; + goto err; } - dlhandle = dlopen(so_name, RTLD_NOW); - if (!dlhandle) { + elem->dlhandle = dlopen(so_name, RTLD_NOW); + if (!elem->dlhandle) { fprintf(stderr, PFX "Warning: couldn't load driver '%s': %s\n", name, dlerror()); - goto out; + goto err; } + for (list = &so_list; *list; list = &(*list)->next) + ; + + *list = elem; + + goto out; + +err: + free(elem); out: free(so_name); } @@ -573,3 +595,22 @@ out: return num_devices; } + +FINI static void unload_drivers() +{ + struct ibv_driver *driver; + struct ibv_so_list * list; + + for (driver = head_driver; driver;) { + struct ibv_driver *tmp = driver; + driver = driver->next; + free(tmp); + } + + for (list = so_list; list;) { + struct ibv_so_list *tmp = list; + list = list->next; + dlclose(tmp->dlhandle); + free(tmp); + } +}