]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
udev: gracefully handle ENODEV or friends in opening device node
authorYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 3 Jul 2025 13:46:38 +0000 (22:46 +0900)
committerLennart Poettering <lennart@poettering.net>
Fri, 4 Jul 2025 07:47:47 +0000 (09:47 +0200)
Fixes #38033.

src/udev/ata_id/ata_id.c
src/udev/cdrom_id/cdrom_id.c
src/udev/mtd_probe/mtd_probe.c
src/udev/udev-builtin-keyboard.c
src/udev/udev-builtin-uaccess.c
src/udev/udev-node.c
src/udev/udev-worker.c
src/udev/v4l_id/v4l_id.c

index eea04f70aabdba6489b66fde679ff6d66b0bf60d..6a3614acac0ea6edec95846aa067d382a3d57855 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "build.h"
 #include "device-nodes.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "log.h"
 #include "main-func.h"
@@ -411,8 +412,13 @@ static int run(int argc, char *argv[]) {
                 return r;
 
         fd = open(ASSERT_PTR(arg_device), O_RDONLY|O_NONBLOCK|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return log_error_errno(errno, "Cannot open %s: %m", arg_device);
+        if (fd < 0) {
+                bool ignore = ERRNO_IS_DEVICE_ABSENT(errno);
+                log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
+                               "Failed to open device node '%s'%s: %m",
+                               arg_device, ignore ? ", ignoring" : "");
+                return ignore ? 0 : -errno;
+        }
 
         if (disk_identify(fd, identify.byte, &peripheral_device_type) >= 0) {
                 /*
index a36f72e594aeceaa0fd74de0c7a7c3d7aa29fa55..d311f2b3222ec0c8b321fead11fe2af914b7d052 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "alloc-util.h"
 #include "build.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "main-func.h"
 #include "random-util.h"
@@ -966,8 +967,10 @@ static int run(int argc, char *argv[]) {
                 return r;
 
         r = open_drive(&c);
+        if (ERRNO_IS_NEG_DEVICE_ABSENT(r))
+                return 0;
         if (r < 0)
-                return r;
+                return log_warning_errno(r, "Failed to open device node '%s': %m", arg_node);
 
         /* same data as original cdrom_id */
         r = cd_capability_compat(&c);
index 1bee120912a02b0d92ec48fffe9d36af83241150..5899f5ae9c593232533365cf584088ed1d948716 100644 (file)
@@ -25,6 +25,7 @@
 #include <sys/ioctl.h>
 
 #include "build.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "log.h"
 #include "main-func.h"
@@ -73,8 +74,13 @@ static int run(int argc, char** argv) {
                 return r;
 
         mtd_fd = open(argv[1], O_RDONLY|O_CLOEXEC|O_NOCTTY);
-        if (mtd_fd < 0)
-                return log_error_errno(errno, "Failed to open: %m");
+        if (mtd_fd < 0) {
+                bool ignore = ERRNO_IS_DEVICE_ABSENT(errno);
+                log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
+                               "Failed to open device node '%s'%s: %m",
+                               argv[1], ignore ? ", ignoring" : "");
+                return ignore ? 0 : -errno;
+        }
 
         if (ioctl(mtd_fd, MEMGETINFO, &mtd_info) < 0)
                 return log_error_errno(errno, "MEMGETINFO ioctl failed: %m");
index e30d3cd97638a78f32e14059523a0c79a8303ee1..93a20639b8f78ef2b8607b1eae31a14b609cca02 100644 (file)
@@ -6,6 +6,7 @@
 #include <sys/ioctl.h>
 
 #include "device-util.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "parse-util.h"
 #include "stdio-util.h"
@@ -199,8 +200,13 @@ static int builtin_keyboard(UdevEvent *event, int argc, char *argv[]) {
 
                         if (fd < 0) {
                                 fd = sd_device_open(dev, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
-                                if (fd < 0)
-                                        return log_device_error_errno(dev, fd, "Failed to open device '%s': %m", node);
+                                if (fd < 0) {
+                                        bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
+                                        log_device_full_errno(dev, ignore ? LOG_DEBUG : LOG_WARNING, fd,
+                                                              "Failed to open device '%s'%s: %m",
+                                                              node, ignore ? ", ignoring" : "");
+                                        return ignore ? 0 : fd;
+                                }
                         }
 
                         (void) map_keycode(dev, fd, scancode, keycode);
@@ -216,8 +222,13 @@ static int builtin_keyboard(UdevEvent *event, int argc, char *argv[]) {
 
                         if (fd < 0) {
                                 fd = sd_device_open(dev, O_RDWR|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
-                                if (fd < 0)
-                                        return log_device_error_errno(dev, fd, "Failed to open device '%s': %m", node);
+                                if (fd < 0) {
+                                        bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
+                                        log_device_full_errno(dev, ignore ? LOG_DEBUG : LOG_WARNING, fd,
+                                                              "Failed to open device '%s'%s: %m",
+                                                              node, ignore ? ", ignoring" : "");
+                                        return ignore ? 0 : fd;
+                                }
                         }
 
                         if (has_abs == -1) {
index bd2f77c69d67b317cabdccb79af70000ce87a893..1f09ec36cd1ef71af61a57d443522cfd46b080b2 100644 (file)
@@ -121,8 +121,13 @@ static int builtin_uaccess(UdevEvent *event, int argc, char *argv[]) {
                 return 0;
 
         _cleanup_close_ int fd = sd_device_open(dev, O_CLOEXEC|O_PATH);
-        if (fd < 0)
-                return log_device_error_errno(dev, fd, "Failed to open device node: %m");
+        if (fd < 0) {
+                bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
+                log_device_full_errno(dev, ignore ? LOG_DEBUG : LOG_WARNING, fd,
+                                      "Failed to open device node%s: %m",
+                                      ignore ? ", ignoring" : "");
+                return ignore ? 0 : fd;
+        }
 
         const char *seat;
         r = device_get_seat(dev, &seat);
index a48061a1ea715e7222f7ecc367f5a7c011ffcf6d..849de60177a5e2806bea19b1ab4ada8a1d3a0f6c 100644 (file)
@@ -735,9 +735,11 @@ int static_node_apply_permissions(
 
         node_fd = open(devnode, O_PATH|O_CLOEXEC);
         if (node_fd < 0) {
-                if (errno != ENOENT)
-                        return log_error_errno(errno, "Failed to open %s: %m", devnode);
-                return 0;
+                bool ignore = ERRNO_IS_DEVICE_ABSENT(errno);
+                log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
+                               "Failed to open device node '%s'%s: %m",
+                               devnode, ignore ? ", ignoring" : "");
+                return ignore ? 0 : -errno;
         }
 
         if (fstat(node_fd, &stats) < 0)
index 4bcadfa1e75dd1eca78eb5ba7731c0129179d1de..2a7235589bbaf6c10d48095571739dc465de58de 100644 (file)
@@ -171,20 +171,25 @@ static int worker_mark_block_device_read_only(sd_device *dev) {
         if (r > 0)
                 return 0;
 
-        const char *val;
-        r = sd_device_get_devname(dev, &val);
+        const char *node;
+        r = sd_device_get_devname(dev, &node);
         if (r < 0)
                 return log_device_debug_errno(dev, r, "Failed to get device node: %m");
 
         _cleanup_close_ int fd = sd_device_open(dev, O_RDONLY|O_CLOEXEC|O_NONBLOCK|O_NOCTTY);
-        if (fd < 0)
-                return log_device_debug_errno(dev, fd, "Failed to open '%s', ignoring: %m", val);
+        if (fd < 0) {
+                bool ignore = ERRNO_IS_DEVICE_ABSENT(fd);
+                log_device_full_errno(dev, ignore ? LOG_DEBUG : LOG_WARNING, fd,
+                                      "Failed to open device node '%s'%s: %m",
+                                      node, ignore ? ", ignoring" : "");
+                return ignore ? 0 : fd;
+        }
 
         int state = 1;
         if (ioctl(fd, BLKROSET, &state) < 0)
-                return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", val);
+                return log_device_warning_errno(dev, errno, "Failed to mark block device '%s' read-only: %m", node);
 
-        log_device_info(dev, "Successfully marked block device '%s' read-only.", val);
+        log_device_info(dev, "Successfully marked block device '%s' read-only.", node);
         return 0;
 }
 
index 6c37d7c38d6c2c741202824634db5b8d724b51b9..cab1308d670cb345bc87fdc5f9b8dc20ff9ab185 100644 (file)
@@ -10,6 +10,7 @@
 #include <sys/ioctl.h>
 
 #include "build.h"
+#include "errno-util.h"
 #include "fd-util.h"
 #include "log.h"
 #include "main-func.h"
@@ -59,8 +60,13 @@ static int run(int argc, char *argv[]) {
                 return r;
 
         fd = open(arg_device, O_RDONLY|O_CLOEXEC|O_NOCTTY);
-        if (fd < 0)
-                return log_error_errno(errno, "Failed to open %s: %m", arg_device);
+        if (fd < 0) {
+                bool ignore = ERRNO_IS_DEVICE_ABSENT(errno);
+                log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
+                               "Failed to open device node '%s'%s: %m",
+                               arg_device, ignore ? ", ignoring" : "");
+                return ignore ? 0 : -errno;
+        }
 
         if (ioctl(fd, VIDIOC_QUERYCAP, &v2cap) == 0) {
                 int capabilities;