]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
errno-util: add ERRNO_IS_DEVICE_ABSENT() macro
authorLennart Poettering <lennart@poettering.net>
Thu, 24 Mar 2022 12:50:50 +0000 (13:50 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 24 Mar 2022 15:19:48 +0000 (16:19 +0100)
Inspired by: https://github.com/systemd/systemd/pull/22717#discussion_r834254495

src/basic/errno-util.h
src/home/homework-luks.c
src/rfkill/rfkill.c
src/udev/udev-builtin-btrfs.c

index 09abf0b7512dc3fb2686ea8b2e6a44fe10892c82..648de50eb497aeddbadc046d476e4214ac2c4e5a 100644 (file)
@@ -138,10 +138,18 @@ static inline bool ERRNO_IS_PRIVILEGE(int r) {
                       EPERM);
 }
 
-/* Three difference errors for "not enough disk space" */
+/* Three different errors for "not enough disk space" */
 static inline bool ERRNO_IS_DISK_SPACE(int r) {
         return IN_SET(abs(r),
                       ENOSPC,
                       EDQUOT,
                       EFBIG);
 }
+
+/* Three different errors for "this device does not quite exist" */
+static inline bool ERRNO_IS_DEVICE_ABSENT(int r) {
+        return IN_SET(abs(r),
+                      ENODEV,
+                      ENXIO,
+                      ENOENT);
+}
index 488cb30fe322aa1b0c2f5befa123d66eaaaed28b..5416d12fcf984e5456701794adfff80fb0958f7d 100644 (file)
@@ -495,7 +495,7 @@ static int acquire_open_luks_device(
                 return r;
 
         r = sym_crypt_init_by_name(&cd, setup->dm_name);
-        if (IN_SET(r, -ENODEV, -EINVAL, -ENOENT) && graceful)
+        if ((ERRNO_IS_DEVICE_ABSENT(r) || r == -EINVAL) && graceful)
                 return 0;
         if (r < 0)
                 return log_error_errno(r, "Failed to initialize cryptsetup context for %s: %m", setup->dm_name);
@@ -1631,7 +1631,7 @@ int home_deactivate_luks(UserRecord *h, HomeSetup *setup) {
                 cryptsetup_enable_logging(setup->crypt_device);
 
                 r = sym_crypt_deactivate_by_name(setup->crypt_device, setup->dm_name, 0);
-                if (IN_SET(r, -ENODEV, -EINVAL, -ENOENT)) {
+                if (ERRNO_IS_DEVICE_ABSENT(r) || r == -EINVAL) {
                         log_debug_errno(r, "LUKS device %s is already detached.", setup->dm_node);
                         we_detached = false;
                 } else if (r < 0)
index 656afa06ac8b4b551dba25271bc104b89cca6933..a833771d97f2b1d24b7299a2e6ef9e259b2b0e64 100644 (file)
@@ -80,7 +80,7 @@ static int find_device(
 
         r = sd_device_new_from_subsystem_sysname(&device, "rfkill", sysname);
         if (r < 0)
-                return log_full_errno(IN_SET(r, -ENOENT, -ENXIO, -ENODEV) ? LOG_DEBUG : LOG_ERR, r,
+                return log_full_errno(ERRNO_IS_DEVICE_ABSENT(r) ? LOG_DEBUG : LOG_ERR, r,
                                       "Failed to open device '%s': %m", sysname);
 
         r = sd_device_get_sysattr_value(device, "name", &name);
index a0093cb42347f3c6f6d496cdca1fa639fb146524..f9d4f1dd4ef4b56481780fa9580beb2a784e0513 100644 (file)
@@ -6,6 +6,7 @@
 #include <sys/ioctl.h>
 
 #include "device-util.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "string-util.h"
 #include "strxcpyx.h"
@@ -22,7 +23,7 @@ static int builtin_btrfs(sd_device *dev, sd_netlink **rtnl, int argc, char *argv
 
         fd = open("/dev/btrfs-control", O_RDWR|O_CLOEXEC);
         if (fd < 0) {
-                if (IN_SET(errno, ENOENT, ENXIO, ENODEV)) {
+                if (ERRNO_IS_DEVICE_ABSENT(errno)) {
                         /* Driver not installed? Then we aren't ready. This is useful in initrds that lack
                          * btrfs.ko. After the host transition (where btrfs.ko will hopefully become
                          * available) the device can be retriggered and will then be considered ready. */