diff --git a/lib/misc/lvm-wrappers.c b/lib/misc/lvm-wrappers.c index 3ab8a7135..84f61a6ed 100644 --- a/lib/misc/lvm-wrappers.c +++ b/lib/misc/lvm-wrappers.c @@ -17,6 +17,91 @@ #include #include +#ifdef UDEV_SYNC_SUPPORT +static const char _no_context_msg[] = "Udev library context not set."; +struct udev *_udev; + +int udev_init_library_context(void) +{ + if (_udev) + udev_unref(_udev); + + if (!(_udev = udev_new())) { + log_error("Failed to create udev library context."); + return 0; + } + + return 1; +} + +void udev_fin_library_context(void) +{ + udev_unref(_udev); + _udev = NULL; +} + +int udev_is_running(void) +{ + struct udev_queue *udev_queue; + int r; + + if (!_udev) { + log_debug(_no_context_msg); + goto bad; + } + + if (!(udev_queue = udev_queue_new(_udev))) { + log_debug("Could not get udev state."); + goto bad; + } + + r = udev_queue_get_udev_is_active(udev_queue); + udev_queue_unref(udev_queue); + + return r; + +bad: + log_debug("Assuming udev is not running."); + return 0; +} + +const char *udev_get_dev_dir(void) +{ + if (!_udev) { + log_debug(_no_context_msg); + return NULL; + } + + return udev_get_dev_path(_udev); +} + +struct udev* udev_get_library_context(void) +{ + return _udev; +} + +#else /* UDEV_SYNC_SUPPORT */ + +int udev_init_library_context(void) +{ + return 1; +} + +void udev_fin_library_context(void) +{ +} + +int udev_is_running(void) +{ + return 0; +} + +const char *udev_get_dev_dir(void) +{ + return NULL; +} +#endif + int lvm_getpagesize(void) { return getpagesize(); diff --git a/lib/misc/lvm-wrappers.h b/lib/misc/lvm-wrappers.h index 19a7f03cd..f5b381cd1 100644 --- a/lib/misc/lvm-wrappers.h +++ b/lib/misc/lvm-wrappers.h @@ -16,6 +16,16 @@ #ifndef _LVM_WRAPPERS_H #define _LVM_WRAPPERS_H +#ifdef UDEV_SYNC_SUPPORT +#include +struct udev *udev_get_library_context(void); +#endif + +int udev_init_library_context(void); +void udev_fin_library_context(void); +int udev_is_running(void); +const char *udev_get_dev_dir(void); + int lvm_getpagesize(void); /* diff --git a/make.tmpl.in b/make.tmpl.in index 5380b5e7b..cd8ae3590 100644 --- a/make.tmpl.in +++ b/make.tmpl.in @@ -38,7 +38,7 @@ CLDFLAGS += @CLDFLAGS@ LDDEPS += @LDDEPS@ LDFLAGS += @LDFLAGS@ LIB_SUFFIX = @LIB_SUFFIX@ -LVMINTERNAL_LIBS = -llvm-internal $(DL_LIBS) +LVMINTERNAL_LIBS = -llvm-internal $(UDEV_LIBS) $(DL_LIBS) DL_LIBS = @DL_LIBS@ PTHREAD_LIBS = @PTHREAD_LIBS@ READLINE_LIBS = @READLINE_LIBS@