From e56e7ed533fc7beb38d35ea9469b10299c46686e Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Tue, 7 May 2024 22:34:17 +0200 Subject: [PATCH] libdm: unlink checks for ENOENT --- device_mapper/ioctl/libdm-iface.c | 20 ++++++++++++-------- device_mapper/libdm-common.c | 9 ++++----- libdm/ioctl/libdm-iface.c | 20 ++++++++++++-------- libdm/libdm-common.c | 9 ++++----- 4 files changed, 32 insertions(+), 26 deletions(-) diff --git a/device_mapper/ioctl/libdm-iface.c b/device_mapper/ioctl/libdm-iface.c index ba5ad382c..dc7abb389 100644 --- a/device_mapper/ioctl/libdm-iface.c +++ b/device_mapper/ioctl/libdm-iface.c @@ -247,6 +247,16 @@ static int _control_device_number(uint32_t *major, uint32_t *minor) return 1; } +static int _control_unlink(const char *control) +{ + if (unlink(control) && (errno != ENOENT)) { + log_sys_error("unlink", control); + return -1; + } + + return 0; +} + /* * Returns 1 if it exists on returning; 0 if it doesn't; -1 if it's wrong. */ @@ -262,10 +272,7 @@ static int _control_exists(const char *control, uint32_t major, uint32_t minor) if (!S_ISCHR(buf.st_mode)) { log_verbose("%s: Wrong inode type", control); - if (!unlink(control)) - return 0; - log_sys_error("unlink", control); - return -1; + return _control_unlink(control); } if (major && buf.st_rdev != MKDEV(major, minor)) { @@ -273,10 +280,7 @@ static int _control_exists(const char *control, uint32_t major, uint32_t minor) "(%u, %u)", control, MAJOR(buf.st_mode), MINOR(buf.st_mode), major, minor); - if (!unlink(control)) - return 0; - log_sys_error("unlink", control); - return -1; + return _control_unlink(control); } return 1; diff --git a/device_mapper/libdm-common.c b/device_mapper/libdm-common.c index 9d672e833..de63eb424 100644 --- a/device_mapper/libdm-common.c +++ b/device_mapper/libdm-common.c @@ -1060,9 +1060,8 @@ static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor, if (info.st_rdev == dev) return 1; - if (unlink(path) < 0) { - log_error("Unable to unlink device node for '%s'", - dev_name); + if (unlink(path) && (errno != ENOENT)) { + log_sys_error("unlink", path); return 0; } } else if (_warn_if_op_needed(warn_if_udev_failed)) @@ -1106,8 +1105,8 @@ static int _rm_dev_node(const char *dev_name, int warn_if_udev_failed) "Falling back to direct node removal.", path); /* udev may already have deleted the node. Ignore ENOENT. */ - if (unlink(path) < 0 && errno != ENOENT) { - log_error("Unable to unlink device node for '%s'", dev_name); + if (unlink(path) && (errno != ENOENT)) { + log_sys_error("unlink", path); return 0; } diff --git a/libdm/ioctl/libdm-iface.c b/libdm/ioctl/libdm-iface.c index d9ba0849a..5bb385366 100644 --- a/libdm/ioctl/libdm-iface.c +++ b/libdm/ioctl/libdm-iface.c @@ -246,6 +246,16 @@ static int _control_device_number(uint32_t *major, uint32_t *minor) return 1; } +static int _control_unlink(const char *control) +{ + if (unlink(control) && (errno != ENOENT)) { + log_sys_error("unlink", control); + return -1; + } + + return 0; +} + /* * Returns 1 if it exists on returning; 0 if it doesn't; -1 if it's wrong. */ @@ -261,10 +271,7 @@ static int _control_exists(const char *control, uint32_t major, uint32_t minor) if (!S_ISCHR(buf.st_mode)) { log_verbose("%s: Wrong inode type", control); - if (!unlink(control)) - return 0; - log_sys_error("unlink", control); - return -1; + return _control_unlink(control); } if (major && buf.st_rdev != MKDEV(major, minor)) { @@ -272,10 +279,7 @@ static int _control_exists(const char *control, uint32_t major, uint32_t minor) "(%u, %u)", control, MAJOR(buf.st_mode), MINOR(buf.st_mode), major, minor); - if (!unlink(control)) - return 0; - log_sys_error("unlink", control); - return -1; + return _control_unlink(control); } return 1; diff --git a/libdm/libdm-common.c b/libdm/libdm-common.c index 363358802..d0e4c995d 100644 --- a/libdm/libdm-common.c +++ b/libdm/libdm-common.c @@ -1058,9 +1058,8 @@ static int _add_dev_node(const char *dev_name, uint32_t major, uint32_t minor, if (info.st_rdev == dev) return 1; - if (unlink(path) < 0) { - log_error("Unable to unlink device node for '%s'", - dev_name); + if (unlink(path) && (errno != ENOENT)) { + log_sys_error("unlink", path); return 0; } } else if (_warn_if_op_needed(warn_if_udev_failed)) @@ -1104,8 +1103,8 @@ static int _rm_dev_node(const char *dev_name, int warn_if_udev_failed) "Falling back to direct node removal.", path); /* udev may already have deleted the node. Ignore ENOENT. */ - if (unlink(path) < 0 && errno != ENOENT) { - log_error("Unable to unlink device node for '%s'", dev_name); + if (unlink(path) && (errno != ENOENT)) { + log_sys_error("unlink", path); return 0; }