Custom device mapper logging is initialized during libdm library initialization in one place. --- src/ldm.c | 87 +++++++++++++++++++++++++------------------------------ 1 file changed, 40 insertions(+), 47 deletions(-) diff --git a/src/ldm.c b/src/ldm.c index f31a0aa..16934d6 100644 --- a/src/ldm.c +++ b/src/ldm.c @@ -236,6 +236,41 @@ ldm_error_get_type(void) return etype; } +/* We catch log messages generated by device mapper with errno != 0 and store + * them here */ +static int _dm_err_last_level = 0; +static const char *_dm_err_last_file = NULL; +static int _dm_err_last_line = 0; +static int _dm_err_last_errno = 0; +static char *_dm_err_last_msg = NULL; + +static void +_dm_log_fn(const int level, const char * const file, const int line, + const int dm_errno, const char *f, ...) +{ + if (dm_errno == 0) return; + + _dm_err_last_level = level; + _dm_err_last_file = file; + _dm_err_last_line = line; + + /* device-mapper doesn't set dm_errno usefully (it only seems to use + * EUNCLASSIFIED), so we capture errno directly and cross our fingers */ + _dm_err_last_errno = errno; + + if (_dm_err_last_msg) { + free(_dm_err_last_msg); + _dm_err_last_msg = NULL; + } + + va_list ap; + va_start(ap, f); + if (vasprintf(&_dm_err_last_msg, f, ap) == -1) { + g_error("vasprintf"); + } + va_end(ap); +} + /* Macros for exporting object properties */ #define EXPORT_PROP_STRING(object, klass, property) \ @@ -287,6 +322,9 @@ ldm_dispose(GObject * const object) if (ldm->priv->disk_groups) { g_array_unref(ldm->priv->disk_groups); ldm->priv->disk_groups = NULL; } + + /* Restore default logging function. */ + dm_log_with_errno_init(NULL); } static void @@ -295,6 +333,8 @@ ldm_init(LDM * const o) o->priv = LDM_GET_PRIVATE(o); bzero(o->priv, sizeof(*o->priv)); + /* Provide our logging function. */ + dm_log_with_errno_init(_dm_log_fn); dm_set_name_mangling_mode(DM_STRING_MANGLING_AUTO); dm_set_uuid_prefix(DM_UUID_PREFIX); } @@ -2430,14 +2470,6 @@ _dm_vol_uuid(const LDMVolumePrivate * const vol) return dm_uuid; } -/* We catch log messages generated by device mapper with errno != 0 and store - * them here */ -static int _dm_err_last_level = 0; -static const char *_dm_err_last_file = NULL; -static int _dm_err_last_line = 0; -static int _dm_err_last_errno = 0; -static char *_dm_err_last_msg = NULL; - struct dm_target { guint64 start; guint64 size; @@ -3005,33 +3037,6 @@ out: return name; } -static void -_dm_log_fn(const int level, const char * const file, const int line, - const int dm_errno, const char *f, ...) -{ - if (dm_errno == 0) return; - - _dm_err_last_level = level; - _dm_err_last_file = file; - _dm_err_last_line = line; - - /* device-mapper doesn't set dm_errno usefully (it only seems to use - * EUNCLASSIFIED), so we capture errno directly and cross our fingers */ - _dm_err_last_errno = errno; - - if (_dm_err_last_msg) { - free(_dm_err_last_msg); - _dm_err_last_msg = NULL; - } - - va_list ap; - va_start(ap, f); - if (vasprintf(&_dm_err_last_msg, f, ap) == -1) { - g_error("vasprintf"); - } - va_end(ap); -} - GString * ldm_volume_dm_get_name(const LDMVolume * const o) { @@ -3046,10 +3051,6 @@ ldm_volume_dm_create(const LDMVolume * const o, GString **created, const LDMVolumePrivate * const vol = o->priv; - /* We should really store the previous logging function and restore it - * afterwards, but the API doesn't allow this. */ - dm_log_with_errno_init(_dm_log_fn); - /* Check if the device already exists */ GString *uuid = _dm_vol_uuid(vol); if (_dm_find_tree_node_by_uuid(uuid->str, NULL, NULL, err)) { @@ -3082,8 +3083,6 @@ ldm_volume_dm_create(const LDMVolume * const o, GString **created, g_error("Unexpected volume type: %u", vol->type); } - dm_log_with_errno_init(NULL); - gboolean r = name != NULL; if (created) @@ -3102,10 +3101,6 @@ ldm_volume_dm_remove(const LDMVolume * const o, GString **removed, const LDMVolumePrivate * const vol = o->priv; - /* We should really store the previous logging function and restore it - * afterwards, but the API doesn't allow this. */ - dm_log_with_errno_init(_dm_log_fn); - gboolean r = FALSE; struct dm_tree *tree = NULL; @@ -3151,7 +3146,5 @@ out: else if (name) g_string_free(name, TRUE); - dm_log_with_errno_init(NULL); - return r; } -- 2.17.0