| 1 |
#include <dlfcn.h> |
| 2 |
#include <blockdev/utils.h> |
| 3 |
#include "blockdev.h" |
| 4 |
#include "plugins.h" |
| 5 |
|
| 6 |
#include "plugin_apis/lvm.h" |
| 7 |
#include "plugin_apis/lvm.c" |
| 8 |
#include "plugin_apis/btrfs.h" |
| 9 |
#include "plugin_apis/btrfs.c" |
| 10 |
#include "plugin_apis/swap.h" |
| 11 |
#include "plugin_apis/swap.c" |
| 12 |
#include "plugin_apis/loop.h" |
| 13 |
#include "plugin_apis/loop.c" |
| 14 |
#include "plugin_apis/crypto.h" |
| 15 |
#include "plugin_apis/crypto.c" |
| 16 |
#include "plugin_apis/mpath.h" |
| 17 |
#include "plugin_apis/mpath.c" |
| 18 |
#include "plugin_apis/dm.h" |
| 19 |
#include "plugin_apis/dm.c" |
| 20 |
#include "plugin_apis/mdraid.h" |
| 21 |
#include "plugin_apis/mdraid.c" |
| 22 |
#include "plugin_apis/part.h" |
| 23 |
#include "plugin_apis/part.c" |
| 24 |
#include "plugin_apis/fs.h" |
| 25 |
#include "plugin_apis/fs.c" |
| 26 |
#include "plugin_apis/nvdimm.h" |
| 27 |
#include "plugin_apis/nvdimm.c" |
| 28 |
#include "plugin_apis/nvme.h" |
| 29 |
#include "plugin_apis/nvme.c" |
| 30 |
#include "plugin_apis/smart.h" |
| 31 |
#include "plugin_apis/smart.c" |
| 32 |
|
| 33 |
#if defined(__s390__) || defined(__s390x__) |
| 34 |
#include "plugin_apis/s390.h" |
| 35 |
#include "plugin_apis/s390.c" |
| 36 |
#endif |
| 37 |
|
| 38 |
/** |
| 39 |
* SECTION: blockdev |
| 40 |
* @short_description: a library for doing low-level operations with block devices |
| 41 |
* @title: blockdev library |
| 42 |
* @include: blockdev.h |
| 43 |
* |
| 44 |
*/ |
| 45 |
|
| 46 |
#define DEFAULT_CONF_DIR_PATH "/etc/libblockdev/@MAJOR_VER@/conf.d/" |
| 47 |
|
| 48 |
static GMutex init_lock; |
| 49 |
static gboolean initialized = FALSE; |
| 50 |
|
| 51 |
typedef struct BDPluginStatus { |
| 52 |
BDPluginSpec spec; |
| 53 |
gpointer handle; |
| 54 |
} BDPluginStatus; |
| 55 |
|
| 56 |
typedef void* (*LoadFunc) (const gchar *so_name); |
| 57 |
|
| 58 |
/* KEEP THE ORDERING OF THESE ARRAYS MATCHING THE BDPluginName ENUM! */ |
| 59 |
static gchar * default_plugin_so[BD_PLUGIN_UNDEF] = { |
| 60 |
"libbd_lvm.so.@MAJOR_VER@", "libbd_btrfs.so.@MAJOR_VER@", |
| 61 |
"libbd_swap.so.@MAJOR_VER@", "libbd_loop.so.@MAJOR_VER@", |
| 62 |
"libbd_crypto.so.@MAJOR_VER@", "libbd_mpath.so.@MAJOR_VER@", |
| 63 |
"libbd_dm.so.@MAJOR_VER@", "libbd_mdraid.so.@MAJOR_VER@", |
| 64 |
"libbd_s390.so.@MAJOR_VER@", "libbd_part.so.@MAJOR_VER@", |
| 65 |
"libbd_fs.so.@MAJOR_VER@", "libbd_nvdimm.so.@MAJOR_VER@", |
| 66 |
"libbd_nvme.so.@MAJOR_VER@", |
| 67 |
#if defined(HAVE_SMART) || !defined(HAVE_SMARTMONTOOLS) |
| 68 |
"libbd_smart.so.@MAJOR_VER@", |
| 69 |
#else |
| 70 |
"libbd_smartmontools.so.@MAJOR_VER@", |
| 71 |
#endif |
| 72 |
}; |
| 73 |
static BDPluginStatus plugins[BD_PLUGIN_UNDEF] = { |
| 74 |
{{BD_PLUGIN_LVM, NULL}, NULL}, |
| 75 |
{{BD_PLUGIN_BTRFS, NULL}, NULL}, |
| 76 |
{{BD_PLUGIN_SWAP, NULL}, NULL}, |
| 77 |
{{BD_PLUGIN_LOOP, NULL}, NULL}, |
| 78 |
{{BD_PLUGIN_CRYPTO, NULL}, NULL}, |
| 79 |
{{BD_PLUGIN_MPATH, NULL}, NULL}, |
| 80 |
{{BD_PLUGIN_DM, NULL}, NULL}, |
| 81 |
{{BD_PLUGIN_MDRAID, NULL}, NULL}, |
| 82 |
{{BD_PLUGIN_S390, NULL}, NULL}, |
| 83 |
{{BD_PLUGIN_PART, NULL}, NULL}, |
| 84 |
{{BD_PLUGIN_FS, NULL}, NULL}, |
| 85 |
{{BD_PLUGIN_NVDIMM, NULL}, NULL}, |
| 86 |
{{BD_PLUGIN_NVME, NULL}, NULL}, |
| 87 |
{{BD_PLUGIN_SMART, NULL}, NULL}, |
| 88 |
}; |
| 89 |
static gchar* plugin_names[BD_PLUGIN_UNDEF] = { |
| 90 |
"lvm", "btrfs", "swap", "loop", "crypto", "mpath", "dm", "mdraid", "s390", "part", "fs", "nvdimm", "nvme", "smart" |
| 91 |
}; |
| 92 |
|
| 93 |
static void set_plugin_so_name (BDPlugin name, const gchar *so_name) { |
| 94 |
plugins[name].spec.so_name = so_name; |
| 95 |
} |
| 96 |
|
| 97 |
static gint config_file_cmp (gconstpointer a, gconstpointer b, gpointer user_data G_GNUC_UNUSED) { |
| 98 |
const gchar *name1 = (const gchar *) a; |
| 99 |
const gchar *name2 = (const gchar *) b; |
| 100 |
|
| 101 |
return g_strcmp0 (name1, name2); |
| 102 |
} |
| 103 |
|
| 104 |
static GSequence* get_config_files (GError **error) { |
| 105 |
GDir *dir = NULL; |
| 106 |
GSequence *ret = NULL; |
| 107 |
gchar *conf_dir_path = NULL; |
| 108 |
const gchar *dirent = NULL; |
| 109 |
|
| 110 |
conf_dir_path = g_strdup (g_getenv("LIBBLOCKDEV_CONFIG_DIR")); |
| 111 |
if (!conf_dir_path) |
| 112 |
conf_dir_path = g_strdup (DEFAULT_CONF_DIR_PATH); |
| 113 |
|
| 114 |
dir = g_dir_open (conf_dir_path, 0, error); |
| 115 |
if (!dir) { |
| 116 |
g_prefix_error (error, "Failed to get contents of the config dir (%s)", conf_dir_path); |
| 117 |
g_free (conf_dir_path); |
| 118 |
return NULL; |
| 119 |
} |
| 120 |
|
| 121 |
ret = g_sequence_new ((GDestroyNotify) g_free); |
| 122 |
|
| 123 |
dirent = g_dir_read_name(dir); |
| 124 |
while (dirent) { |
| 125 |
/* only process .cfg files from the directory */ |
| 126 |
if (g_str_has_suffix (dirent, ".cfg")) { |
| 127 |
dirent = g_build_filename (conf_dir_path, dirent, NULL); |
| 128 |
g_sequence_insert_sorted (ret, (gpointer) dirent, config_file_cmp, NULL); |
| 129 |
} |
| 130 |
dirent = g_dir_read_name(dir); |
| 131 |
} |
| 132 |
|
| 133 |
g_free (conf_dir_path); |
| 134 |
g_dir_close (dir); |
| 135 |
return ret; |
| 136 |
} |
| 137 |
|
| 138 |
static gboolean process_config_file (const gchar *config_file, GSList **plugins_sonames, GError **error) { |
| 139 |
GKeyFile *config = NULL; |
| 140 |
BDPlugin i = 0; |
| 141 |
gchar **sonames = NULL; |
| 142 |
gsize n_sonames = 0; |
| 143 |
|
| 144 |
config = g_key_file_new (); |
| 145 |
if (!g_key_file_load_from_file (config, config_file, G_KEY_FILE_NONE, error)) { |
| 146 |
g_key_file_free (config); |
| 147 |
return FALSE; |
| 148 |
} |
| 149 |
|
| 150 |
/* get sonames for each plugin (if specified) */ |
| 151 |
for (i=0; (i < BD_PLUGIN_UNDEF); i++) { |
| 152 |
sonames = g_key_file_get_string_list (config, plugin_names[i], "sonames", &n_sonames, error); |
| 153 |
if (!sonames) { |
| 154 |
/* no sonames specified or an error occurred */ |
| 155 |
if (*error) |
| 156 |
g_clear_error (error); |
| 157 |
} else { |
| 158 |
/* go through the sonames in the reversed order (because we prepend |
| 159 |
them to the list) */ |
| 160 |
for (; n_sonames > 0; n_sonames--) { |
| 161 |
plugins_sonames[i] = g_slist_prepend (plugins_sonames[i], sonames[n_sonames-1]); |
| 162 |
} |
| 163 |
/* we need to free only the array here not its items because those |
| 164 |
were put into the list above as pointers */ |
| 165 |
g_free (sonames); |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
g_key_file_free (config); |
| 170 |
|
| 171 |
return TRUE; |
| 172 |
} |
| 173 |
|
| 174 |
static gboolean load_config (GSequence *config_files, GSList **plugins_sonames, GError **error) { |
| 175 |
GSequenceIter *config_file_iter = NULL; |
| 176 |
gchar *config_file = NULL; |
| 177 |
|
| 178 |
/* process config files one after another in order */ |
| 179 |
config_file_iter = g_sequence_get_begin_iter (config_files); |
| 180 |
while (!g_sequence_iter_is_end (config_file_iter)) { |
| 181 |
config_file = (gchar *) g_sequence_get (config_file_iter); |
| 182 |
if (!process_config_file (config_file, plugins_sonames, error)) { |
| 183 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Cannot process the config file '%s': %s. Skipping.", |
| 184 |
config_file, (*error)->message); |
| 185 |
g_clear_error (error); |
| 186 |
} |
| 187 |
config_file_iter = g_sequence_iter_next (config_file_iter); |
| 188 |
} |
| 189 |
|
| 190 |
return TRUE; |
| 191 |
} |
| 192 |
|
| 193 |
static void unload_plugins (void) { |
| 194 |
if (plugins[BD_PLUGIN_LVM].handle && !unload_lvm (plugins[BD_PLUGIN_LVM].handle)) |
| 195 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the lvm plugin"); |
| 196 |
plugins[BD_PLUGIN_LVM].handle = NULL; |
| 197 |
|
| 198 |
if (plugins[BD_PLUGIN_BTRFS].handle && !unload_btrfs (plugins[BD_PLUGIN_BTRFS].handle)) |
| 199 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the btrfs plugin"); |
| 200 |
plugins[BD_PLUGIN_BTRFS].handle = NULL; |
| 201 |
|
| 202 |
if (plugins[BD_PLUGIN_SWAP].handle && !unload_swap (plugins[BD_PLUGIN_SWAP].handle)) |
| 203 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the swap plugin"); |
| 204 |
plugins[BD_PLUGIN_SWAP].handle = NULL; |
| 205 |
|
| 206 |
if (plugins[BD_PLUGIN_LOOP].handle && !unload_loop (plugins[BD_PLUGIN_LOOP].handle)) |
| 207 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the loop plugin"); |
| 208 |
plugins[BD_PLUGIN_LOOP].handle = NULL; |
| 209 |
|
| 210 |
if (plugins[BD_PLUGIN_CRYPTO].handle && !unload_crypto (plugins[BD_PLUGIN_CRYPTO].handle)) |
| 211 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the crypto plugin"); |
| 212 |
plugins[BD_PLUGIN_CRYPTO].handle = NULL; |
| 213 |
|
| 214 |
if (plugins[BD_PLUGIN_MPATH].handle && !unload_mpath (plugins[BD_PLUGIN_MPATH].handle)) |
| 215 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the mpath plugin"); |
| 216 |
plugins[BD_PLUGIN_MPATH].handle = NULL; |
| 217 |
|
| 218 |
if (plugins[BD_PLUGIN_DM].handle && !unload_dm (plugins[BD_PLUGIN_DM].handle)) |
| 219 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the dm plugin"); |
| 220 |
plugins[BD_PLUGIN_DM].handle = NULL; |
| 221 |
|
| 222 |
if (plugins[BD_PLUGIN_MDRAID].handle && !unload_mdraid (plugins[BD_PLUGIN_MDRAID].handle)) |
| 223 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the mdraid plugin"); |
| 224 |
plugins[BD_PLUGIN_MDRAID].handle = NULL; |
| 225 |
|
| 226 |
#if defined(__s390__) || defined(__s390x__) |
| 227 |
if (plugins[BD_PLUGIN_S390].handle && !unload_s390 (plugins[BD_PLUGIN_S390].handle)) |
| 228 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the s390 plugin"); |
| 229 |
plugins[BD_PLUGIN_S390].handle = NULL; |
| 230 |
#endif |
| 231 |
|
| 232 |
if (plugins[BD_PLUGIN_PART].handle && !unload_part (plugins[BD_PLUGIN_PART].handle)) |
| 233 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the part plugin"); |
| 234 |
plugins[BD_PLUGIN_PART].handle = NULL; |
| 235 |
|
| 236 |
if (plugins[BD_PLUGIN_FS].handle && !unload_fs (plugins[BD_PLUGIN_FS].handle)) |
| 237 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the fs plugin"); |
| 238 |
plugins[BD_PLUGIN_FS].handle = NULL; |
| 239 |
|
| 240 |
if (plugins[BD_PLUGIN_NVDIMM].handle && !unload_nvdimm (plugins[BD_PLUGIN_NVDIMM].handle)) |
| 241 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the nvdimm plugin"); |
| 242 |
plugins[BD_PLUGIN_NVDIMM].handle = NULL; |
| 243 |
|
| 244 |
if (plugins[BD_PLUGIN_NVME].handle && !unload_nvme (plugins[BD_PLUGIN_NVME].handle)) |
| 245 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the nvme plugin"); |
| 246 |
plugins[BD_PLUGIN_NVME].handle = NULL; |
| 247 |
|
| 248 |
if (plugins[BD_PLUGIN_SMART].handle && !unload_smart (plugins[BD_PLUGIN_SMART].handle)) |
| 249 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to close the smart plugin"); |
| 250 |
plugins[BD_PLUGIN_SMART].handle = NULL; |
| 251 |
} |
| 252 |
|
| 253 |
static void load_plugin_from_sonames (BDPlugin plugin, LoadFunc load_fn, void **handle, GSList *sonames) { |
| 254 |
while (!(*handle) && sonames) { |
| 255 |
*handle = load_fn (sonames->data); |
| 256 |
if (*handle) |
| 257 |
set_plugin_so_name(plugin, g_strdup (sonames->data)); |
| 258 |
sonames = g_slist_next (sonames); |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
static void do_load (GSList **plugins_sonames) { |
| 263 |
if (!plugins[BD_PLUGIN_LVM].handle && plugins_sonames[BD_PLUGIN_LVM]) |
| 264 |
load_plugin_from_sonames (BD_PLUGIN_LVM, load_lvm_from_plugin, &(plugins[BD_PLUGIN_LVM].handle), plugins_sonames[BD_PLUGIN_LVM]); |
| 265 |
if (!plugins[BD_PLUGIN_BTRFS].handle && plugins_sonames[BD_PLUGIN_BTRFS]) |
| 266 |
load_plugin_from_sonames (BD_PLUGIN_BTRFS, load_btrfs_from_plugin, &(plugins[BD_PLUGIN_BTRFS].handle), plugins_sonames[BD_PLUGIN_BTRFS]); |
| 267 |
if (!plugins[BD_PLUGIN_SWAP].handle && plugins_sonames[BD_PLUGIN_SWAP]) |
| 268 |
load_plugin_from_sonames (BD_PLUGIN_SWAP,load_swap_from_plugin, &(plugins[BD_PLUGIN_SWAP].handle), plugins_sonames[BD_PLUGIN_SWAP]); |
| 269 |
if (!plugins[BD_PLUGIN_LOOP].handle && plugins_sonames[BD_PLUGIN_LOOP]) |
| 270 |
load_plugin_from_sonames (BD_PLUGIN_LOOP, load_loop_from_plugin, &(plugins[BD_PLUGIN_LOOP].handle), plugins_sonames[BD_PLUGIN_LOOP]); |
| 271 |
if (!plugins[BD_PLUGIN_CRYPTO].handle && plugins_sonames[BD_PLUGIN_CRYPTO]) |
| 272 |
load_plugin_from_sonames (BD_PLUGIN_CRYPTO, load_crypto_from_plugin, &(plugins[BD_PLUGIN_CRYPTO].handle), plugins_sonames[BD_PLUGIN_CRYPTO]); |
| 273 |
if (!plugins[BD_PLUGIN_MPATH].handle && plugins_sonames[BD_PLUGIN_MPATH]) |
| 274 |
load_plugin_from_sonames (BD_PLUGIN_MPATH, load_mpath_from_plugin, &(plugins[BD_PLUGIN_MPATH].handle), plugins_sonames[BD_PLUGIN_MPATH]); |
| 275 |
if (!plugins[BD_PLUGIN_DM].handle && plugins_sonames[BD_PLUGIN_DM]) |
| 276 |
load_plugin_from_sonames (BD_PLUGIN_DM, load_dm_from_plugin, &(plugins[BD_PLUGIN_DM].handle), plugins_sonames[BD_PLUGIN_DM]); |
| 277 |
if (!plugins[BD_PLUGIN_MDRAID].handle && plugins_sonames[BD_PLUGIN_MDRAID]) |
| 278 |
load_plugin_from_sonames (BD_PLUGIN_MDRAID, load_mdraid_from_plugin, &(plugins[BD_PLUGIN_MDRAID].handle), plugins_sonames[BD_PLUGIN_MDRAID]); |
| 279 |
#if defined(__s390__) || defined(__s390x__) |
| 280 |
if (!plugins[BD_PLUGIN_S390].handle && plugins_sonames[BD_PLUGIN_S390]) |
| 281 |
load_plugin_from_sonames (BD_PLUGIN_S390, load_s390_from_plugin, &(plugins[BD_PLUGIN_S390].handle), plugins_sonames[BD_PLUGIN_S390]); |
| 282 |
#endif |
| 283 |
if (!plugins[BD_PLUGIN_PART].handle && plugins_sonames[BD_PLUGIN_PART]) |
| 284 |
load_plugin_from_sonames (BD_PLUGIN_PART, load_part_from_plugin, &(plugins[BD_PLUGIN_PART].handle), plugins_sonames[BD_PLUGIN_PART]); |
| 285 |
if (!plugins[BD_PLUGIN_FS].handle && plugins_sonames[BD_PLUGIN_FS]) |
| 286 |
load_plugin_from_sonames (BD_PLUGIN_FS, load_fs_from_plugin, &(plugins[BD_PLUGIN_FS].handle), plugins_sonames[BD_PLUGIN_FS]); |
| 287 |
if (!plugins[BD_PLUGIN_NVDIMM].handle && plugins_sonames[BD_PLUGIN_NVDIMM]) |
| 288 |
load_plugin_from_sonames (BD_PLUGIN_NVDIMM, load_nvdimm_from_plugin, &(plugins[BD_PLUGIN_NVDIMM].handle), plugins_sonames[BD_PLUGIN_NVDIMM]); |
| 289 |
if (!plugins[BD_PLUGIN_NVME].handle && plugins_sonames[BD_PLUGIN_NVME]) |
| 290 |
load_plugin_from_sonames (BD_PLUGIN_NVME, load_nvme_from_plugin, &(plugins[BD_PLUGIN_NVME].handle), plugins_sonames[BD_PLUGIN_NVME]); |
| 291 |
if (!plugins[BD_PLUGIN_SMART].handle && plugins_sonames[BD_PLUGIN_SMART]) |
| 292 |
load_plugin_from_sonames (BD_PLUGIN_SMART, load_smart_from_plugin, &(plugins[BD_PLUGIN_SMART].handle), plugins_sonames[BD_PLUGIN_SMART]); |
| 293 |
} |
| 294 |
|
| 295 |
static gboolean load_plugins (BDPluginSpec **require_plugins, gboolean reload, guint64 *num_loaded) { |
| 296 |
guint8 i = 0; |
| 297 |
gboolean requested_loaded = TRUE; |
| 298 |
GError *error = NULL; |
| 299 |
GSequence *config_files = NULL; |
| 300 |
GSList *plugins_sonames[BD_PLUGIN_UNDEF] = {0}; |
| 301 |
BDPlugin plugin_name = BD_PLUGIN_UNDEF; |
| 302 |
guint64 required_plugins_mask = 0; |
| 303 |
|
| 304 |
/* load config files first */ |
| 305 |
config_files = get_config_files (&error); |
| 306 |
if (config_files) { |
| 307 |
if (!load_config (config_files, plugins_sonames, &error)) |
| 308 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "Failed to load config files: %s. Using the built-in config", |
| 309 |
error->message); |
| 310 |
|
| 311 |
g_sequence_free (config_files); |
| 312 |
} else |
| 313 |
bd_utils_log_format (BD_UTILS_LOG_INFO, "Failed to load config files: %s. Using the built-in config", |
| 314 |
error->message); |
| 315 |
g_clear_error (&error); |
| 316 |
|
| 317 |
/* populate missing items with the built-in defaults */ |
| 318 |
for (i=0; i < BD_PLUGIN_UNDEF; i++) |
| 319 |
if (!plugins_sonames[i]) |
| 320 |
plugins_sonames[i] = g_slist_prepend (plugins_sonames[i], g_strdup (default_plugin_so[i])); |
| 321 |
|
| 322 |
#if !defined(__s390__) && !defined(__s390x__) |
| 323 |
/* do not load the s390 plugin by default if not on s390(x) */ |
| 324 |
g_slist_free_full (plugins_sonames[BD_PLUGIN_S390], (GDestroyNotify) g_free); |
| 325 |
plugins_sonames[BD_PLUGIN_S390] = NULL; |
| 326 |
#endif |
| 327 |
|
| 328 |
/* unload the previously loaded plugins if requested */ |
| 329 |
if (reload) { |
| 330 |
unload_plugins (); |
| 331 |
/* clean all so names and populate back those that are requested or the |
| 332 |
defaults */ |
| 333 |
for (i=0; i < BD_PLUGIN_UNDEF; i++) { |
| 334 |
g_free ((gchar *) plugins[i].spec.so_name); |
| 335 |
plugins[i].spec.so_name = NULL; |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
if (require_plugins) { |
| 340 |
/* set requested sonames */ |
| 341 |
for (i=0; *(require_plugins + i); i++) { |
| 342 |
plugin_name = require_plugins[i]->name; |
| 343 |
if (plugin_name >= BD_PLUGIN_UNDEF) |
| 344 |
continue; |
| 345 |
required_plugins_mask |= ((guint64) 1 << plugin_name); |
| 346 |
if (require_plugins[i]->so_name) { |
| 347 |
g_slist_free_full (plugins_sonames[plugin_name], (GDestroyNotify) g_free); |
| 348 |
plugins_sonames[plugin_name] = NULL; |
| 349 |
plugins_sonames[plugin_name] = g_slist_prepend(plugins_sonames[plugin_name], g_strdup (require_plugins[i]->so_name)); |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
/* now remove the defaults for plugins that are not required */ |
| 354 |
for (i=0; (i < BD_PLUGIN_UNDEF); i++) |
| 355 |
if (!(required_plugins_mask & ((guint64) 1 << i))) { |
| 356 |
/* plugin not required */ |
| 357 |
g_slist_free_full (plugins_sonames[i], (GDestroyNotify) g_free); |
| 358 |
plugins_sonames[i] = NULL; |
| 359 |
} |
| 360 |
} |
| 361 |
|
| 362 |
do_load (plugins_sonames); |
| 363 |
|
| 364 |
*num_loaded = 0; |
| 365 |
for (i=0; (i < BD_PLUGIN_UNDEF); i++) { |
| 366 |
/* if this plugin was required or all plugins were required, check if it |
| 367 |
was successfully loaded or not */ |
| 368 |
if (!require_plugins || (required_plugins_mask & ((guint64) 1 << i))) { |
| 369 |
#if !defined(__s390__) && !defined(__s390x__) |
| 370 |
if (!require_plugins && (i == BD_PLUGIN_S390)) |
| 371 |
/* do not check the s390 plugin on different archs unless |
| 372 |
explicitly required */ |
| 373 |
continue; |
| 374 |
#endif |
| 375 |
if (plugins[i].handle) |
| 376 |
(*num_loaded)++; |
| 377 |
else |
| 378 |
requested_loaded = FALSE; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
/* clear/free the config */ |
| 383 |
for (i=0; (i < BD_PLUGIN_UNDEF); i++) { |
| 384 |
if (plugins_sonames[i]) { |
| 385 |
g_slist_free_full (plugins_sonames[i], (GDestroyNotify) g_free); |
| 386 |
plugins_sonames[i] = NULL; |
| 387 |
} |
| 388 |
} |
| 389 |
|
| 390 |
return requested_loaded; |
| 391 |
} |
| 392 |
|
| 393 |
GQuark bd_init_error_quark (void) |
| 394 |
{ |
| 395 |
return g_quark_from_static_string ("g-bd-init-error-quark"); |
| 396 |
} |
| 397 |
|
| 398 |
/** |
| 399 |
* bd_init: |
| 400 |
* @require_plugins: (nullable) (array zero-terminated=1): %NULL-terminated list |
| 401 |
* of plugins that should be loaded (if no so_name is specified |
| 402 |
* for the plugin, the default is used) or %NULL to load all |
| 403 |
* plugins |
| 404 |
* @log_func: (nullable) (scope notified): logging function to use |
| 405 |
* @error: (out) (optional): place to store error (if any) |
| 406 |
* |
| 407 |
* Returns: whether the library was successfully initialized with all the |
| 408 |
* required or default (see @require_plugins) plugins or not |
| 409 |
* |
| 410 |
* Example of libblockdev initialization with 'fs' and 'lvm' plugins. Specific |
| 411 |
* version of the lvm plugin is required: |
| 412 |
* |
| 413 |
* |[<!-- language="C" --> |
| 414 |
* GError *error = NULL; |
| 415 |
* gboolean ret = FALSE; |
| 416 |
* BDPluginSpec fs_plugin = {BD_PLUGIN_FS, NULL}; |
| 417 |
* BDPluginSpec lvm_plugin = {BD_PLUGIN_LVM, "libbd_lvm.so.3"}; |
| 418 |
* |
| 419 |
* BDPluginSpec *plugins[] = {&fs_plugin, &lvm_plugin, NULL}; |
| 420 |
* |
| 421 |
* ret = bd_init (plugins, NULL, &error); |
| 422 |
* ]| |
| 423 |
*/ |
| 424 |
gboolean bd_init (BDPluginSpec **require_plugins, BDUtilsLogFunc log_func, GError **error) { |
| 425 |
gboolean success = TRUE; |
| 426 |
guint64 num_loaded = 0; |
| 427 |
|
| 428 |
g_mutex_lock (&init_lock); |
| 429 |
if (initialized) { |
| 430 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "bd_init() called more than once! Use bd_reinit() to reinitialize " |
| 431 |
"or bd_is_initialized() to get the current state."); |
| 432 |
g_mutex_unlock (&init_lock); |
| 433 |
return FALSE; |
| 434 |
} |
| 435 |
|
| 436 |
if (log_func && !bd_utils_init_logging (log_func, error)) { |
| 437 |
/* the error is already populated */ |
| 438 |
g_mutex_unlock (&init_lock); |
| 439 |
return FALSE; |
| 440 |
} |
| 441 |
|
| 442 |
if (!load_plugins (require_plugins, FALSE, &num_loaded)) { |
| 443 |
g_set_error_literal (error, BD_INIT_ERROR, BD_INIT_ERROR_PLUGINS_FAILED, |
| 444 |
"Failed to load plugins"); |
| 445 |
success = FALSE; |
| 446 |
} |
| 447 |
|
| 448 |
if (num_loaded == 0) { |
| 449 |
if (require_plugins && (*require_plugins == NULL)) |
| 450 |
/* requested to load no plugins (NULL is the first item in the |
| 451 |
array), none loaded -> OK */ |
| 452 |
initialized = TRUE; |
| 453 |
else |
| 454 |
initialized = FALSE; |
| 455 |
} else |
| 456 |
initialized = TRUE; |
| 457 |
|
| 458 |
g_mutex_unlock (&init_lock); |
| 459 |
|
| 460 |
return success; |
| 461 |
} |
| 462 |
|
| 463 |
/** |
| 464 |
* bd_ensure_init: |
| 465 |
* @require_plugins: (nullable) (array zero-terminated=1): %NULL-terminated list |
| 466 |
* of plugins that should be loaded (if no so_name is specified |
| 467 |
* for the plugin, the default is used) or %NULL to load all |
| 468 |
* plugins |
| 469 |
* @log_func: (nullable) (scope notified): logging function to use |
| 470 |
* @error: (out) (optional): place to store error (if any) |
| 471 |
* |
| 472 |
* Checks the state of the library and if it is uninitialized or not all the |
| 473 |
* @require_plugins plugins are available, tries to (re)initialize it. Otherwise |
| 474 |
* just returns early. The difference between: |
| 475 |
* |
| 476 |
* |[<!-- language="C" --> |
| 477 |
* if (!bd_is_initialized()) |
| 478 |
* bd_init(None, None, &error); |
| 479 |
* ]| |
| 480 |
* |
| 481 |
* and this function is that this function does the check and init in an atomic |
| 482 |
* way (holding the lock preventing other threads from doing changes in |
| 483 |
* between). |
| 484 |
* |
| 485 |
* Returns: whether the library was successfully initialized with all the |
| 486 |
* required or default (see @require_plugins) plugins or not either |
| 487 |
* before or by this call |
| 488 |
*/ |
| 489 |
gboolean bd_ensure_init (BDPluginSpec **require_plugins, BDUtilsLogFunc log_func, GError **error) { |
| 490 |
gboolean success = TRUE; |
| 491 |
BDPluginSpec **check_plugin = NULL; |
| 492 |
gboolean missing = FALSE; |
| 493 |
guint64 num_loaded = 0; |
| 494 |
BDPlugin plugin = BD_PLUGIN_UNDEF; |
| 495 |
|
| 496 |
g_mutex_lock (&init_lock); |
| 497 |
if (initialized) { |
| 498 |
if (require_plugins) |
| 499 |
for (check_plugin=require_plugins; !missing && *check_plugin; check_plugin++) |
| 500 |
missing = !bd_is_plugin_available((*check_plugin)->name); |
| 501 |
else |
| 502 |
/* all plugins requested */ |
| 503 |
for (plugin=BD_PLUGIN_LVM; !missing && plugin != BD_PLUGIN_UNDEF; plugin++) |
| 504 |
missing = !bd_is_plugin_available(plugin); |
| 505 |
|
| 506 |
if (!missing) { |
| 507 |
g_mutex_unlock (&init_lock); |
| 508 |
return TRUE; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
if (log_func && !bd_utils_init_logging (log_func, error)) { |
| 513 |
/* the error is already populated */ |
| 514 |
g_mutex_unlock (&init_lock); |
| 515 |
return FALSE; |
| 516 |
} |
| 517 |
|
| 518 |
if (!load_plugins (require_plugins, FALSE, &num_loaded)) { |
| 519 |
g_set_error_literal (error, BD_INIT_ERROR, BD_INIT_ERROR_PLUGINS_FAILED, |
| 520 |
"Failed to load plugins"); |
| 521 |
success = FALSE; |
| 522 |
} |
| 523 |
|
| 524 |
if (num_loaded == 0) { |
| 525 |
if (require_plugins && (*require_plugins == NULL)) |
| 526 |
/* requested to load no plugins (NULL is the first item in the |
| 527 |
array), none loaded -> OK */ |
| 528 |
initialized = TRUE; |
| 529 |
else |
| 530 |
initialized = FALSE; |
| 531 |
} else |
| 532 |
initialized = TRUE; |
| 533 |
|
| 534 |
g_mutex_unlock (&init_lock); |
| 535 |
|
| 536 |
return success; |
| 537 |
} |
| 538 |
|
| 539 |
/** |
| 540 |
* bd_try_init: |
| 541 |
* @request_plugins: (nullable) (array zero-terminated=1): %NULL-terminated list |
| 542 |
* of plugins that should be loaded (if no so_name is specified |
| 543 |
* for the plugin, the default is used) or %NULL to load all |
| 544 |
* plugins |
| 545 |
* @log_func: (nullable) (scope notified): logging function to use |
| 546 |
* @loaded_plugin_names: (optional) (out) (transfer container) (array zero-terminated=1): names |
| 547 |
* of the successfully loaded plugins |
| 548 |
* @error: (out) (optional): place to store error (if any) |
| 549 |
* |
| 550 |
* Returns: whether the library was successfully initialized with all the |
| 551 |
* required or default (see @require_plugins) plugins or not |
| 552 |
* |
| 553 |
* *UNLIKE IN CASE OF bd_init() AND bd_ensure_init(), FAILURE TO LOAD A PLUGIN |
| 554 |
* IS NOT CONSIDERED ERROR* |
| 555 |
*/ |
| 556 |
gboolean bd_try_init(BDPluginSpec **request_plugins, BDUtilsLogFunc log_func, |
| 557 |
gchar ***loaded_plugin_names, GError **error) { |
| 558 |
gboolean success = TRUE; |
| 559 |
guint64 num_loaded = 0; |
| 560 |
|
| 561 |
g_mutex_lock (&init_lock); |
| 562 |
if (initialized) { |
| 563 |
bd_utils_log_format (BD_UTILS_LOG_WARNING, "bd_try_init() called more than once! Use bd_reinit() " |
| 564 |
"to reinitialize or bd_is_initialized() to get the current state."); |
| 565 |
g_mutex_unlock (&init_lock); |
| 566 |
return FALSE; |
| 567 |
} |
| 568 |
|
| 569 |
if (log_func && !bd_utils_init_logging (log_func, error)) { |
| 570 |
/* the error is already populated */ |
| 571 |
g_mutex_unlock (&init_lock); |
| 572 |
return FALSE; |
| 573 |
} |
| 574 |
|
| 575 |
success = load_plugins (request_plugins, FALSE, &num_loaded); |
| 576 |
|
| 577 |
if (num_loaded == 0) { |
| 578 |
if (request_plugins && (*request_plugins == NULL)) |
| 579 |
/* requested to load no plugins (NULL is the first item in the |
| 580 |
array), none loaded -> OK */ |
| 581 |
initialized = TRUE; |
| 582 |
else |
| 583 |
initialized = FALSE; |
| 584 |
} else |
| 585 |
initialized = TRUE; |
| 586 |
|
| 587 |
if (loaded_plugin_names) |
| 588 |
*loaded_plugin_names = bd_get_available_plugin_names (); |
| 589 |
|
| 590 |
g_mutex_unlock (&init_lock); |
| 591 |
|
| 592 |
return success; |
| 593 |
} |
| 594 |
|
| 595 |
/** |
| 596 |
* bd_reinit: |
| 597 |
* @require_plugins: (nullable) (array zero-terminated=1): %NULL-terminated list |
| 598 |
* of plugins that should be loaded (if no so_name is specified |
| 599 |
* for the plugin, the default is used) or %NULL to load all |
| 600 |
* plugins |
| 601 |
* @reload: whether to reload the already loaded plugins or not |
| 602 |
* @log_func: (nullable) (scope notified): logging function to use or %NULL |
| 603 |
* to keep the old one |
| 604 |
* @error: (out) (optional): place to store error (if any) |
| 605 |
* |
| 606 |
* Returns: whether the library was successfully initialized or not |
| 607 |
* |
| 608 |
* If @reload is %TRUE all the plugins are closed and reloaded otherwise only |
| 609 |
* the missing plugins are loaded. |
| 610 |
*/ |
| 611 |
gboolean bd_reinit (BDPluginSpec **require_plugins, gboolean reload, BDUtilsLogFunc log_func, GError **error) { |
| 612 |
gboolean success = TRUE; |
| 613 |
guint64 num_loaded = 0; |
| 614 |
|
| 615 |
g_mutex_lock (&init_lock); |
| 616 |
if (log_func && !bd_utils_init_logging (log_func, error)) { |
| 617 |
/* the error is already populated */ |
| 618 |
g_mutex_unlock (&init_lock); |
| 619 |
return FALSE; |
| 620 |
} |
| 621 |
|
| 622 |
if (!load_plugins (require_plugins, reload, &num_loaded)) { |
| 623 |
g_set_error_literal (error, BD_INIT_ERROR, BD_INIT_ERROR_PLUGINS_FAILED, |
| 624 |
"Failed to load plugins"); |
| 625 |
success = FALSE; |
| 626 |
} else |
| 627 |
if (require_plugins && (*require_plugins == NULL) && reload) |
| 628 |
/* requested to just unload all plugins */ |
| 629 |
success = (num_loaded == 0); |
| 630 |
|
| 631 |
if (num_loaded == 0) { |
| 632 |
if (require_plugins && (*require_plugins == NULL)) |
| 633 |
/* requested to load no plugins (NULL is the first item in the |
| 634 |
array), none loaded -> OK */ |
| 635 |
initialized = TRUE; |
| 636 |
else |
| 637 |
initialized = FALSE; |
| 638 |
} else |
| 639 |
initialized = TRUE; |
| 640 |
|
| 641 |
g_mutex_unlock (&init_lock); |
| 642 |
return success; |
| 643 |
} |
| 644 |
|
| 645 |
/** |
| 646 |
* bd_try_reinit: |
| 647 |
* @require_plugins: (nullable) (array zero-terminated=1): %NULL-terminated list |
| 648 |
* of plugins that should be loaded (if no so_name is specified |
| 649 |
* for the plugin, the default is used) or %NULL to load all |
| 650 |
* plugins |
| 651 |
* @reload: whether to reload the already loaded plugins or not |
| 652 |
* @log_func: (nullable) (scope notified): logging function to use or %NULL |
| 653 |
* to keep the old one |
| 654 |
* @loaded_plugin_names: (optional) (out) (transfer container) (array zero-terminated=1): names of the successfully |
| 655 |
* loaded plugins |
| 656 |
* @error: (out) (optional): place to store error (if any) |
| 657 |
* |
| 658 |
* Returns: whether the library was successfully initialized or not |
| 659 |
* |
| 660 |
* If @reload is %TRUE all the plugins are closed and reloaded otherwise only |
| 661 |
* the missing plugins are loaded. |
| 662 |
* |
| 663 |
* *UNLIKE IN CASE OF bd_init() AND bd_ensure_init(), FAILURE TO LOAD A PLUGIN |
| 664 |
* IS NOT CONSIDERED ERROR* |
| 665 |
*/ |
| 666 |
gboolean bd_try_reinit (BDPluginSpec **require_plugins, gboolean reload, BDUtilsLogFunc log_func, |
| 667 |
gchar ***loaded_plugin_names, GError **error) { |
| 668 |
gboolean success = TRUE; |
| 669 |
guint64 num_loaded = 0; |
| 670 |
|
| 671 |
g_mutex_lock (&init_lock); |
| 672 |
if (log_func && !bd_utils_init_logging (log_func, error)) { |
| 673 |
/* the error is already populated */ |
| 674 |
g_mutex_unlock (&init_lock); |
| 675 |
return FALSE; |
| 676 |
} |
| 677 |
|
| 678 |
success = load_plugins (require_plugins, reload, &num_loaded); |
| 679 |
if (success && require_plugins && (*require_plugins == NULL) && reload) |
| 680 |
/* requested to just unload all plugins */ |
| 681 |
success = (num_loaded == 0); |
| 682 |
|
| 683 |
if (num_loaded == 0) { |
| 684 |
if (require_plugins && (*require_plugins == NULL)) |
| 685 |
/* requested to load no plugins (NULL is the first item in the |
| 686 |
array), none loaded -> OK */ |
| 687 |
initialized = TRUE; |
| 688 |
else |
| 689 |
initialized = FALSE; |
| 690 |
} else |
| 691 |
initialized = TRUE; |
| 692 |
|
| 693 |
if (loaded_plugin_names) |
| 694 |
*loaded_plugin_names = bd_get_available_plugin_names (); |
| 695 |
|
| 696 |
g_mutex_unlock (&init_lock); |
| 697 |
return success; |
| 698 |
} |
| 699 |
|
| 700 |
/** |
| 701 |
* bd_is_initialized: |
| 702 |
* |
| 703 |
* Returns: whether the library is initialized or not |
| 704 |
* |
| 705 |
* The library is considered initialized if some of the *init*() functions |
| 706 |
* was/were called and either at least one plugin is loaded or 0 plugins are |
| 707 |
* loaded after an explicit call that requested 0 plugins to be loaded. |
| 708 |
*/ |
| 709 |
gboolean bd_is_initialized (void) { |
| 710 |
gboolean is = FALSE; |
| 711 |
g_mutex_lock (&init_lock); |
| 712 |
is = initialized; |
| 713 |
g_mutex_unlock (&init_lock); |
| 714 |
return is; |
| 715 |
} |
| 716 |
|
| 717 |
/** |
| 718 |
* bd_get_available_plugin_names: |
| 719 |
* |
| 720 |
* Returns: (transfer container) (array zero-terminated=1): an array of string |
| 721 |
* names of plugins that are available |
| 722 |
*/ |
| 723 |
gchar** bd_get_available_plugin_names (void) { |
| 724 |
guint8 i = 0; |
| 725 |
guint8 num_loaded = 0; |
| 726 |
guint8 next = 0; |
| 727 |
|
| 728 |
for (i=0; i < BD_PLUGIN_UNDEF; i++) |
| 729 |
if (plugins[i].handle) |
| 730 |
num_loaded++; |
| 731 |
|
| 732 |
gchar **ret_plugin_names = g_new0 (gchar*, num_loaded + 1); |
| 733 |
for (i=0; i < BD_PLUGIN_UNDEF; i++) |
| 734 |
if (plugins[i].handle) { |
| 735 |
ret_plugin_names[next] = plugin_names[i]; |
| 736 |
next++; |
| 737 |
} |
| 738 |
ret_plugin_names[next] = NULL; |
| 739 |
|
| 740 |
return ret_plugin_names; |
| 741 |
} |
| 742 |
|
| 743 |
/** |
| 744 |
* bd_is_plugin_available: |
| 745 |
* @plugin: the queried plugin |
| 746 |
* |
| 747 |
* Returns: whether the given plugin is available or not |
| 748 |
*/ |
| 749 |
gboolean bd_is_plugin_available (BDPlugin plugin) { |
| 750 |
if (plugin < BD_PLUGIN_UNDEF) |
| 751 |
return plugins[plugin].handle != NULL; |
| 752 |
else |
| 753 |
return FALSE; |
| 754 |
} |
| 755 |
|
| 756 |
/** |
| 757 |
* bd_get_plugin_soname: |
| 758 |
* @plugin: the queried plugin |
| 759 |
* |
| 760 |
* Returns: (transfer full): name of the shared object loaded for the plugin or |
| 761 |
* %NULL if none is loaded |
| 762 |
*/ |
| 763 |
gchar* bd_get_plugin_soname (BDPlugin plugin) { |
| 764 |
if (plugin >= BD_PLUGIN_UNDEF) |
| 765 |
return NULL; |
| 766 |
|
| 767 |
if (plugins[plugin].handle) |
| 768 |
return g_strdup (plugins[plugin].spec.so_name); |
| 769 |
|
| 770 |
return NULL; |
| 771 |
} |
| 772 |
|
| 773 |
/** |
| 774 |
* bd_get_plugin_name: |
| 775 |
* @plugin: the queried plugin |
| 776 |
* |
| 777 |
* Returns: (transfer none): name of the plugin |
| 778 |
*/ |
| 779 |
gchar* bd_get_plugin_name (BDPlugin plugin) { |
| 780 |
if (plugin >= BD_PLUGIN_UNDEF) |
| 781 |
return NULL; |
| 782 |
|
| 783 |
return plugin_names[plugin]; |
| 784 |
} |