Line data Source code
1 : /* 2 : This file is part of TALER 3 : Copyright (C) 2019 Taler Systems SA 4 : 5 : TALER is free software; you can redistribute it and/or modify it under the 6 : terms of the GNU Lesser General Public License as published by the Free Software 7 : Foundation; either version 3, or (at your option) any later version. 8 : 9 : TALER is distributed in the hope that it will be useful, but WITHOUT ANY 10 : WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR 11 : A PARTICULAR PURPOSE. See the GNU General Public License for more details. 12 : 13 : You should have received a copy of the GNU General Public License along with 14 : TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 : */ 16 : /** 17 : * @file syncdb/sync_db_plugin.c 18 : * @brief Logic to load database plugin 19 : * @author Christian Grothoff 20 : * @author Sree Harsha Totakura <sreeharsha@totakura.in> 21 : */ 22 : #include "platform.h" 23 : #include "sync_database_plugin.h" 24 : #include <ltdl.h> 25 : 26 : 27 : /** 28 : * Initialize the plugin. 29 : * 30 : * @param cfg configuration to use 31 : * @return #GNUNET_OK on success 32 : */ 33 : struct SYNC_DatabasePlugin * 34 2 : SYNC_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) 35 : { 36 : char *plugin_name; 37 : char *lib_name; 38 : struct GNUNET_CONFIGURATION_Handle *cfg_dup; 39 : struct SYNC_DatabasePlugin *plugin; 40 : 41 2 : if (GNUNET_SYSERR == 42 2 : GNUNET_CONFIGURATION_get_value_string (cfg, 43 : "sync", 44 : "db", 45 : &plugin_name)) 46 : { 47 0 : GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 48 : "sync", 49 : "db"); 50 0 : return NULL; 51 : } 52 2 : (void) GNUNET_asprintf (&lib_name, 53 : "libsync_plugin_db_%s", 54 : plugin_name); 55 2 : GNUNET_free (plugin_name); 56 2 : cfg_dup = GNUNET_CONFIGURATION_dup (cfg); 57 2 : plugin = GNUNET_PLUGIN_load (lib_name, cfg_dup); 58 2 : if (NULL != plugin) 59 2 : plugin->library_name = lib_name; 60 : else 61 0 : GNUNET_free (lib_name); 62 2 : GNUNET_CONFIGURATION_destroy (cfg_dup); 63 2 : return plugin; 64 : } 65 : 66 : 67 : /** 68 : * Shutdown the plugin. 69 : * 70 : * @param plugin the plugin to unload 71 : */ 72 : void 73 2 : SYNC_DB_plugin_unload (struct SYNC_DatabasePlugin *plugin) 74 : { 75 : char *lib_name; 76 : 77 2 : if (NULL == plugin) 78 0 : return; 79 2 : lib_name = plugin->library_name; 80 2 : GNUNET_assert (NULL == GNUNET_PLUGIN_unload (lib_name, 81 : plugin)); 82 2 : GNUNET_free (lib_name); 83 : } 84 : 85 : 86 : /** 87 : * Libtool search path before we started. 88 : */ 89 : static char *old_dlsearchpath; 90 : 91 : 92 : /** 93 : * Setup libtool paths. 94 : */ 95 : void __attribute__ ((constructor)) 96 1 : plugin_init () 97 : { 98 : int err; 99 : const char *opath; 100 : char *path; 101 : char *cpath; 102 : 103 1 : err = lt_dlinit (); 104 1 : if (err > 0) 105 : { 106 0 : fprintf (stderr, 107 : _ ("Initialization of plugin mechanism failed: %s!\n"), 108 : lt_dlerror ()); 109 0 : return; 110 : } 111 1 : opath = lt_dlgetsearchpath (); 112 1 : if (NULL != opath) 113 0 : old_dlsearchpath = GNUNET_strdup (opath); 114 1 : path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); 115 1 : if (NULL != path) 116 : { 117 1 : if (NULL != opath) 118 : { 119 0 : GNUNET_asprintf (&cpath, "%s:%s", opath, path); 120 0 : lt_dlsetsearchpath (cpath); 121 0 : GNUNET_free (path); 122 0 : GNUNET_free (cpath); 123 : } 124 : else 125 : { 126 1 : lt_dlsetsearchpath (path); 127 1 : GNUNET_free (path); 128 : } 129 : } 130 : } 131 : 132 : 133 : /** 134 : * Shutdown libtool. 135 : */ 136 : void __attribute__ ((destructor)) 137 1 : plugin_fini () 138 : { 139 1 : lt_dlsetsearchpath (old_dlsearchpath); 140 1 : if (NULL != old_dlsearchpath) 141 : { 142 0 : GNUNET_free (old_dlsearchpath); 143 0 : old_dlsearchpath = NULL; 144 : } 145 1 : lt_dlexit (); 146 1 : } 147 : 148 : 149 : /* end of sync_db_plugin.c */