summaryrefslogtreecommitdiffstats
path: root/udev.c
diff options
context:
space:
mode:
authorMateusz Grzonka <mateusz.grzonka@intel.com>2023-11-21 01:58:24 +0100
committerJes Sorensen <jes@trained-monkey.org>2023-11-21 17:12:06 +0100
commit9f376da6439b07dc93ae084ab576e133b9d8d839 (patch)
tree78092973d6591d33af1e0a0fb03c11d8458e1793 /udev.c
parentMdmonitor: Improve udev event handling (diff)
downloadmdadm-9f376da6439b07dc93ae084ab576e133b9d8d839.tar.xz
mdadm-9f376da6439b07dc93ae084ab576e133b9d8d839.zip
udev: Move udev_block() and udev_unblock() into udev.c
Add kernel style comments and better error handling. Signed-off-by: Mateusz Grzonka <mateusz.grzonka@intel.com> Signed-off-by: Kinga Tanska <kinga.tanska@intel.com> Signed-off-by: Jes Sorensen <jes@trained-monkey.org>
Diffstat (limited to 'udev.c')
-rw-r--r--udev.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/udev.c b/udev.c
index 2bac6921..bc4722b0 100644
--- a/udev.c
+++ b/udev.c
@@ -28,6 +28,8 @@
#include <syslog.h>
#include <libudev.h>
+static char *unblock_path;
+
/*
* udev_is_available() - Checks for udev in the system.
*
@@ -148,3 +150,45 @@ enum udev_status udev_wait_for_events(int seconds)
return UDEV_STATUS_TIMEOUT;
}
#endif
+
+/*
+ * udev_block() - Block udev from examining newly created arrays.
+ *
+ * When array is created, we don't want udev to examine it immediately.
+ * Function creates /run/mdadm/creating-mdXXX and expects that udev rule
+ * will notice it and act accordingly.
+ *
+ * Return:
+ * UDEV_STATUS_SUCCESS when successfully blocked udev
+ * UDEV_STATUS_ERROR on error
+ */
+enum udev_status udev_block(char *devnm)
+{
+ int fd;
+ char *path = xcalloc(1, BUFSIZ);
+
+ snprintf(path, BUFSIZ, "/run/mdadm/creating-%s", devnm);
+
+ fd = open(path, O_CREAT | O_RDWR, 0600);
+ if (!is_fd_valid(fd)) {
+ pr_err("Cannot block udev, error creating blocking file.\n");
+ pr_err("%s: %s\n", strerror(errno), path);
+ free(path);
+ return UDEV_STATUS_ERROR;
+ }
+
+ close(fd);
+ unblock_path = path;
+ return UDEV_STATUS_SUCCESS;
+}
+
+/*
+ * udev_unblock() - Unblock udev.
+ */
+void udev_unblock(void)
+{
+ if (unblock_path)
+ unlink(unblock_path);
+ free(unblock_path);
+ unblock_path = NULL;
+}