From: Yu Watanabe Date: Thu, 3 Jul 2025 13:46:38 +0000 (+0900) Subject: udev: gracefully handle ENODEV or friends in opening device node X-Git-Tag: v258-rc1~180 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=908b0ea15f24e687b4c743f9e43cfdf0ce3d465b;p=thirdparty%2Fsystemd.git udev: gracefully handle ENODEV or friends in opening device node Fixes #38033. --- diff --git a/src/udev/ata_id/ata_id.c b/src/udev/ata_id/ata_id.c index eea04f70aab..6a3614acac0 100644 --- a/src/udev/ata_id/ata_id.c +++ b/src/udev/ata_id/ata_id.c @@ -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) { /* diff --git a/src/udev/cdrom_id/cdrom_id.c b/src/udev/cdrom_id/cdrom_id.c index a36f72e594a..d311f2b3222 100644 --- a/src/udev/cdrom_id/cdrom_id.c +++ b/src/udev/cdrom_id/cdrom_id.c @@ -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); diff --git a/src/udev/mtd_probe/mtd_probe.c b/src/udev/mtd_probe/mtd_probe.c index 1bee120912a..5899f5ae9c5 100644 --- a/src/udev/mtd_probe/mtd_probe.c +++ b/src/udev/mtd_probe/mtd_probe.c @@ -25,6 +25,7 @@ #include #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"); diff --git a/src/udev/udev-builtin-keyboard.c b/src/udev/udev-builtin-keyboard.c index e30d3cd9763..93a20639b8f 100644 --- a/src/udev/udev-builtin-keyboard.c +++ b/src/udev/udev-builtin-keyboard.c @@ -6,6 +6,7 @@ #include #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) { diff --git a/src/udev/udev-builtin-uaccess.c b/src/udev/udev-builtin-uaccess.c index bd2f77c69d6..1f09ec36cd1 100644 --- a/src/udev/udev-builtin-uaccess.c +++ b/src/udev/udev-builtin-uaccess.c @@ -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); diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c index a48061a1ea7..849de60177a 100644 --- a/src/udev/udev-node.c +++ b/src/udev/udev-node.c @@ -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) diff --git a/src/udev/udev-worker.c b/src/udev/udev-worker.c index 4bcadfa1e75..2a7235589bb 100644 --- a/src/udev/udev-worker.c +++ b/src/udev/udev-worker.c @@ -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; } diff --git a/src/udev/v4l_id/v4l_id.c b/src/udev/v4l_id/v4l_id.c index 6c37d7c38d6..cab1308d670 100644 --- a/src/udev/v4l_id/v4l_id.c +++ b/src/udev/v4l_id/v4l_id.c @@ -10,6 +10,7 @@ #include #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;