frosix_db_plugin.c (3513B)
1 /* 2 This file is part of Frosix 3 Copyright (C) 2015, 2016 Anastasis SARL 4 5 Frosix is free software; you can redistribute it and/or modify it under the 6 terms of the GNU Affero General Public License as published by the Free Software 7 Foundation; either version 3, or (at your option) any later version. 8 9 Frosix 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 Affero General Public License for more details. 12 13 You should have received a copy of the GNU Affero General Public License along with 14 Frosix; see the file COPYING. If not, see <http://www.gnu.org/licenses/> 15 */ 16 /** 17 * @file frosix_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 "frosix_database_plugin.h" 24 #include <ltdl.h> 25 #include <gnunet/gnunet_util_lib.h> 26 27 28 /** 29 * Initialize the plugin. 30 * 31 * @param cfg configuration to use 32 * @return #GNUNET_OK on success 33 */ 34 struct FROSIX_DatabasePlugin * 35 FROSIX_DB_plugin_load (const struct GNUNET_CONFIGURATION_Handle *cfg) 36 { 37 char *plugin_name; 38 char *lib_name; 39 struct FROSIX_DatabasePlugin *plugin; 40 41 if (GNUNET_SYSERR == 42 GNUNET_CONFIGURATION_get_value_string (cfg, 43 "frosix", 44 "db", 45 &plugin_name)) 46 { 47 GNUNET_log_config_missing (GNUNET_ERROR_TYPE_ERROR, 48 "frosix", 49 "db"); 50 return NULL; 51 } 52 (void) GNUNET_asprintf (&lib_name, 53 "libfrosix_plugin_db_%s", 54 plugin_name); 55 GNUNET_free (plugin_name); 56 plugin = GNUNET_PLUGIN_load (lib_name, 57 (void *) cfg); 58 if (NULL != plugin) 59 plugin->library_name = lib_name; 60 else 61 lib_name = NULL; 62 return plugin; 63 } 64 65 66 /** 67 * Shutdown the plugin. 68 * 69 * @param plugin the plugin to unload 70 */ 71 void 72 FROSIX_DB_plugin_unload (struct FROSIX_DatabasePlugin *plugin) 73 { 74 char *lib_name; 75 76 if (NULL == plugin) 77 return; 78 lib_name = plugin->library_name; 79 GNUNET_assert (NULL == GNUNET_PLUGIN_unload (lib_name, 80 plugin)); 81 GNUNET_free (lib_name); 82 } 83 84 85 /** 86 * Libtool search path before we started. 87 */ 88 static char *old_dlsearchpath; 89 90 91 /** 92 * Setup libtool paths. 93 */ 94 void __attribute__ ((constructor)) 95 plugin_init () 96 { 97 int err; 98 const char *opath; 99 char *path; 100 char *cpath; 101 102 err = lt_dlinit (); 103 if (err > 0) 104 { 105 fprintf (stderr, 106 ("Initialization of plugin mechanism failed: %s!\n"), 107 lt_dlerror ()); 108 return; 109 } 110 opath = lt_dlgetsearchpath (); 111 if (NULL != opath) 112 old_dlsearchpath = GNUNET_strdup (opath); 113 path = GNUNET_OS_installation_get_path (GNUNET_OS_IPK_LIBDIR); 114 if (NULL != path) 115 { 116 if (NULL != opath) 117 { 118 GNUNET_asprintf (&cpath, "%s:%s", opath, path); 119 lt_dlsetsearchpath (cpath); 120 GNUNET_free (path); 121 GNUNET_free (cpath); 122 } 123 else 124 { 125 lt_dlsetsearchpath (path); 126 GNUNET_free (path); 127 } 128 } 129 } 130 131 132 /** 133 * Shutdown libtool. 134 */ 135 void __attribute__ ((destructor)) 136 plugin_fini () 137 { 138 lt_dlsetsearchpath (old_dlsearchpath); 139 if (NULL != old_dlsearchpath) 140 { 141 GNUNET_free (old_dlsearchpath); 142 } 143 lt_dlexit (); 144 } 145 146 147 /* end of frosix_db_plugin.c */