summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-09-14 09:40:23 +0200
committerGitHub <noreply@github.com>2021-09-14 09:40:23 +0200
commitb83bbbac87670f8fd2fa12d9971393fca4d770bc (patch)
treeadf463fdd1b60c533103ed0e49f7758be93edb27 /src
parent8be102f8b8019a9bd7e445532cad632cbc6986d3 (diff)
parent2d3af41f0e837390b734253f5c4a99a9f33c53e3 (diff)
downloadsystemd-b83bbbac87670f8fd2fa12d9971393fca4d770bc.tar.gz
Merge pull request #20713 from yuwata/udev-watch-retry
udev-watch: retry to save watch handle on error
Diffstat (limited to 'src')
-rw-r--r--src/libsystemd/sd-device/device-private.c2
-rw-r--r--src/udev/udev-watch.c45
2 files changed, 42 insertions, 5 deletions
diff --git a/src/libsystemd/sd-device/device-private.c b/src/libsystemd/sd-device/device-private.c
index 16a2e9e1e4..37eda23a5f 100644
--- a/src/libsystemd/sd-device/device-private.c
+++ b/src/libsystemd/sd-device/device-private.c
@@ -680,7 +680,7 @@ int device_set_watch_handle(sd_device *device, int wd) {
assert(device);
- if (wd >= 0 && wd == device->watch_handle)
+ if (wd >= 0 && wd == device_get_watch_handle(device))
return 0;
device_remove_watch_handle(device);
diff --git a/src/udev/udev-watch.c b/src/udev/udev-watch.c
index 6032dfb4f6..87c4f09928 100644
--- a/src/udev/udev-watch.c
+++ b/src/udev/udev-watch.c
@@ -12,8 +12,13 @@
#include "dirent-util.h"
#include "fs-util.h"
#include "parse-util.h"
+#include "random-util.h"
#include "udev-watch.h"
+#define SAVE_WATCH_HANDLE_MAX_RETRIES 128
+#define MAX_RANDOM_DELAY (100 * USEC_PER_MSEC)
+#define MIN_RANDOM_DELAY ( 10 * USEC_PER_MSEC)
+
int udev_watch_restore(int inotify_fd) {
struct dirent *ent;
DIR *dir;
@@ -93,11 +98,43 @@ int udev_watch_begin(int inotify_fd, sd_device *dev) {
return ignore ? 0 : r;
}
- r = device_set_watch_handle(dev, wd);
- if (r < 0)
- return log_device_warning_errno(dev, r, "Failed to save watch handle in /run/udev/watch: %m");
+ for (unsigned i = 0; i < SAVE_WATCH_HANDLE_MAX_RETRIES; i++) {
+ if (i > 0) {
+ usec_t delay = MIN_RANDOM_DELAY + random_u64_range(MAX_RANDOM_DELAY - MIN_RANDOM_DELAY);
+
+ /* When the same handle is reused for different device node, we may fail to
+ * save the watch handle with -EEXIST. Let's consider the case of two workers A
+ * and B do the following:
+ *
+ * 1. A calls inotify_rm_watch()
+ * 2. B calls inotify_add_watch()
+ * 3. B calls device_set_watch_handle()
+ * 4. A calls device_set_watch_handle(-1)
+ *
+ * At step 3, the old symlinks to save the watch handle still exist. So,
+ * device_set_watch_handle() fails with -EEXIST. */
+
+ log_device_debug_errno(dev, r,
+ "Failed to save watch handle '%i' for %s in "
+ "/run/udev/watch, retrying in after %s: %m",
+ wd, devnode, FORMAT_TIMESPAN(delay, USEC_PER_MSEC));
+ (void) usleep(delay);
+ }
- return 0;
+ r = device_set_watch_handle(dev, wd);
+ if (r >= 0)
+ return 0;
+ if (r != -EEXIST)
+ break;
+ }
+
+ log_device_warning_errno(dev, r,
+ "Failed to save watch handle '%i' for %s in /run/udev/watch: %m",
+ wd, devnode);
+
+ (void) inotify_rm_watch(inotify_fd, wd);
+
+ return r;
}
int udev_watch_end(int inotify_fd, sd_device *dev) {