From: Yu Watanabe Date: Mon, 14 Oct 2024 21:22:24 +0000 (+0900) Subject: udev: do not try to lock whole block device on remove event X-Git-Tag: v257-rc1~224^2~1 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e8df18c9e171c87aebb2df8ac3bdd8f116236892;p=thirdparty%2Fsystemd.git udev: do not try to lock whole block device on remove event As another device may be created with the same device node while udevd is processing the remove event of the previous owner of the device node. This also adds comment why we skip watching device node on remove. --- diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c index c28c43b2af4..1b8e2b8dbd4 100644 --- a/src/udev/udev-watch.c +++ b/src/udev/udev-watch.c @@ -181,6 +181,9 @@ int udev_watch_begin(int inotify_fd, sd_device *dev) { assert(inotify_fd >= 0); assert(dev); + /* Ignore the request of watching the device node on remove event, as the device node specified by + * DEVNAME= has already been removed, and may already be assigned to another device. Consider the + * case e.g. a USB stick memory was unplugged and then another one is plugged. */ if (device_for_action(dev, SD_DEVICE_REMOVE)) return 0; diff --git a/src/udev/udev-worker.c b/src/udev/udev-worker.c index 7f22faccdc1..59f56f653cd 100644 --- a/src/udev/udev-worker.c +++ b/src/udev/udev-worker.c @@ -97,6 +97,12 @@ static int worker_lock_whole_disk(sd_device *dev, int *ret_fd) { * event handling; in the case udev acquired the lock, the external process can block until udev has * finished its event handling. */ + /* Do not try to lock device on remove event, as the device node specified by DEVNAME= has already + * been removed, and may already be assigned to another device. Consider the case e.g. a USB stick + * memory was unplugged and then another one is plugged. */ + if (device_for_action(dev, SD_DEVICE_REMOVE)) + goto nolock; + r = udev_get_whole_disk(dev, &dev_whole_disk, &val); if (r < 0) return r;