|
|
4eda52 |
From 065209fc7a53d6f296f7fffd261f0a92fddc4485 Mon Sep 17 00:00:00 2001
|
|
|
4295f9 |
From: Yu Watanabe <watanabe.yu+github@gmail.com>
|
|
|
4295f9 |
Date: Wed, 1 Sep 2021 04:16:21 +0900
|
|
|
4295f9 |
Subject: [PATCH] udev-node: save information about device node and priority in
|
|
|
4295f9 |
symlink
|
|
|
4295f9 |
|
|
|
4295f9 |
Previously, we only store device IDs in /run/udev/links, and when
|
|
|
4295f9 |
creating/removing device node symlink, we create sd_device object
|
|
|
4295f9 |
corresponds to the IDs and read device node and priority from the
|
|
|
4295f9 |
object. That requires parsing uevent and udev database files.
|
|
|
4295f9 |
|
|
|
4295f9 |
This makes link_find_prioritized() get the most prioritzed device node
|
|
|
4295f9 |
without parsing the files.
|
|
|
4295f9 |
|
|
|
4295f9 |
(cherry picked from commit 377a83f0d80376456d9be203796f66f543a8b943)
|
|
|
4295f9 |
|
|
|
4eda52 |
Related: #2005024
|
|
|
4295f9 |
---
|
|
|
4295f9 |
src/udev/udev-node.c | 172 ++++++++++++++++++++++++++++++-------------
|
|
|
4295f9 |
1 file changed, 121 insertions(+), 51 deletions(-)
|
|
|
4295f9 |
|
|
|
4295f9 |
diff --git a/src/udev/udev-node.c b/src/udev/udev-node.c
|
|
|
4295f9 |
index 4496a2bd9b..5d6aae0bd4 100644
|
|
|
4295f9 |
--- a/src/udev/udev-node.c
|
|
|
4295f9 |
+++ b/src/udev/udev-node.c
|
|
|
4295f9 |
@@ -18,6 +18,7 @@
|
|
|
4295f9 |
#include "fs-util.h"
|
|
|
4295f9 |
#include "hexdecoct.h"
|
|
|
4295f9 |
#include "mkdir.h"
|
|
|
4295f9 |
+#include "parse-util.h"
|
|
|
4295f9 |
#include "path-util.h"
|
|
|
4295f9 |
#include "selinux-util.h"
|
|
|
4295f9 |
#include "smack-util.h"
|
|
|
4295f9 |
@@ -28,9 +29,9 @@
|
|
|
4295f9 |
#include "udev-node.h"
|
|
|
4295f9 |
#include "user-util.h"
|
|
|
4295f9 |
|
|
|
4295f9 |
-#define CREATE_LINK_MAX_RETRIES 128
|
|
|
4295f9 |
-#define LINK_UPDATE_MAX_RETRIES 128
|
|
|
4295f9 |
-#define TOUCH_FILE_MAX_RETRIES 128
|
|
|
4295f9 |
+#define CREATE_LINK_MAX_RETRIES 128
|
|
|
4295f9 |
+#define LINK_UPDATE_MAX_RETRIES 128
|
|
|
4295f9 |
+#define CREATE_STACK_LINK_MAX_RETRIES 128
|
|
|
4295f9 |
#define UDEV_NODE_HASH_KEY SD_ID128_MAKE(b9,6a,f1,ce,40,31,44,1a,9e,19,ec,8b,ae,f3,e3,2f)
|
|
|
4295f9 |
|
|
|
4295f9 |
static int create_symlink(const char *target, const char *slink) {
|
|
|
4295f9 |
@@ -175,39 +176,67 @@ static int link_find_prioritized(sd_device *dev, bool add, const char *stackdir,
|
|
|
4295f9 |
return r;
|
|
|
4295f9 |
|
|
|
4295f9 |
FOREACH_DIRENT_ALL(dent, dir, break) {
|
|
|
4295f9 |
- _cleanup_(sd_device_unrefp) sd_device *dev_db = NULL;
|
|
|
4295f9 |
- const char *devnode;
|
|
|
4295f9 |
- int db_prio = 0;
|
|
|
4295f9 |
+ _cleanup_free_ char *path = NULL, *buf = NULL;
|
|
|
4295f9 |
+ int tmp_prio;
|
|
|
4295f9 |
|
|
|
4295f9 |
- if (dent->d_name[0] == '\0')
|
|
|
4295f9 |
- break;
|
|
|
4295f9 |
if (dent->d_name[0] == '.')
|
|
|
4295f9 |
continue;
|
|
|
4295f9 |
|
|
|
4295f9 |
- log_device_debug(dev, "Found '%s' claiming '%s'", dent->d_name, stackdir);
|
|
|
4295f9 |
-
|
|
|
4295f9 |
- /* did we find ourself? */
|
|
|
4295f9 |
+ /* skip ourself */
|
|
|
4295f9 |
if (streq(dent->d_name, id))
|
|
|
4295f9 |
continue;
|
|
|
4295f9 |
|
|
|
4295f9 |
- if (sd_device_new_from_device_id(&dev_db, dent->d_name) < 0)
|
|
|
4295f9 |
- continue;
|
|
|
4295f9 |
+ path = path_join(stackdir, dent->d_name);
|
|
|
4295f9 |
+ if (!path)
|
|
|
4295f9 |
+ return -ENOMEM;
|
|
|
4295f9 |
|
|
|
4295f9 |
- if (sd_device_get_devname(dev_db, &devnode) < 0)
|
|
|
4295f9 |
- continue;
|
|
|
4295f9 |
+ if (readlink_malloc(path, &buf) >= 0) {
|
|
|
4295f9 |
+ char *devnode;
|
|
|
4295f9 |
|
|
|
4295f9 |
- if (device_get_devlink_priority(dev_db, &db_prio) < 0)
|
|
|
4295f9 |
- continue;
|
|
|
4295f9 |
+ /* New format. The devnode and priority can be obtained from symlink. */
|
|
|
4295f9 |
|
|
|
4295f9 |
- if (target && db_prio <= priority)
|
|
|
4295f9 |
- continue;
|
|
|
4295f9 |
+ devnode = strchr(buf, ':');
|
|
|
4295f9 |
+ if (!devnode || devnode == buf)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
|
|
|
4295f9 |
- log_device_debug(dev_db, "Device claims priority %i for '%s'", db_prio, stackdir);
|
|
|
4295f9 |
+ *(devnode++) = '\0';
|
|
|
4295f9 |
+ if (!path_startswith(devnode, "/dev"))
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
|
|
|
4295f9 |
- r = free_and_strdup(&target, devnode);
|
|
|
4295f9 |
- if (r < 0)
|
|
|
4295f9 |
- return r;
|
|
|
4295f9 |
- priority = db_prio;
|
|
|
4295f9 |
+ if (safe_atoi(buf, &tmp_prio) < 0)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (target && tmp_prio <= priority)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ r = free_and_strdup(&target, devnode);
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return r;
|
|
|
4295f9 |
+ } else {
|
|
|
4295f9 |
+ _cleanup_(sd_device_unrefp) sd_device *tmp_dev = NULL;
|
|
|
4295f9 |
+ const char *devnode;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ /* Old format. The devnode and priority must be obtained from uevent and
|
|
|
4295f9 |
+ * udev database files. */
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (sd_device_new_from_device_id(&tmp_dev, dent->d_name) < 0)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (device_get_devlink_priority(tmp_dev, &tmp_prio) < 0)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (target && tmp_prio <= priority)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (sd_device_get_devname(tmp_dev, &devnode) < 0)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ r = free_and_strdup(&target, devnode);
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return r;
|
|
|
4295f9 |
+ }
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ priority = tmp_prio;
|
|
|
4295f9 |
}
|
|
|
4295f9 |
|
|
|
4295f9 |
*ret = TAKE_PTR(target);
|
|
|
4295f9 |
@@ -256,10 +285,72 @@ toolong:
|
|
|
4295f9 |
return size - 1;
|
|
|
4295f9 |
}
|
|
|
4295f9 |
|
|
|
4295f9 |
+static int update_stack_directory(sd_device *dev, const char *dirname, bool add) {
|
|
|
4295f9 |
+ _cleanup_free_ char *filename = NULL, *data = NULL, *buf = NULL;
|
|
|
4295f9 |
+ const char *devname, *id;
|
|
|
4295f9 |
+ int priority, r;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ assert(dev);
|
|
|
4295f9 |
+ assert(dirname);
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ r = device_get_device_id(dev, &id;;
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return log_device_debug_errno(dev, r, "Failed to get device id: %m");
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ filename = path_join(dirname, id);
|
|
|
4295f9 |
+ if (!filename)
|
|
|
4295f9 |
+ return log_oom_debug();
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (!add) {
|
|
|
4295f9 |
+ if (unlink(filename) < 0 && errno != ENOENT)
|
|
|
4295f9 |
+ log_device_debug_errno(dev, errno, "Failed to remove %s, ignoring: %m", filename);
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ (void) rmdir(dirname);
|
|
|
4295f9 |
+ return 0;
|
|
|
4295f9 |
+ }
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ r = sd_device_get_devname(dev, &devname);
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return log_device_debug_errno(dev, r, "Failed to get device node: %m");
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ r = device_get_devlink_priority(dev, &priority);
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return log_device_debug_errno(dev, r, "Failed to get priority of device node symlink: %m");
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (asprintf(&data, "%i:%s", priority, devname) < 0)
|
|
|
4295f9 |
+ return log_oom_debug();
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (readlink_malloc(filename, &buf) >= 0 && streq(buf, data))
|
|
|
4295f9 |
+ return 0;
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (unlink(filename) < 0 && errno != ENOENT)
|
|
|
4295f9 |
+ log_device_debug_errno(dev, errno, "Failed to remove %s, ignoring: %m", filename);
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ for (unsigned j = 0; j < CREATE_STACK_LINK_MAX_RETRIES; j++) {
|
|
|
4295f9 |
+ /* This may fail with -ENOENT when the parent directory is removed during
|
|
|
4295f9 |
+ * creating the file by another udevd worker. */
|
|
|
4295f9 |
+ r = mkdir_p(dirname, 0755);
|
|
|
4295f9 |
+ if (r == -ENOENT)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return log_device_debug_errno(dev, r, "Failed to create directory %s: %m", dirname);
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ if (symlink(data, filename) < 0) {
|
|
|
4295f9 |
+ if (errno == ENOENT)
|
|
|
4295f9 |
+ continue;
|
|
|
4295f9 |
+ return log_device_debug_errno(dev, errno, "Failed to create symbolic link %s: %m", filename);
|
|
|
4295f9 |
+ }
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ return 0;
|
|
|
4295f9 |
+ }
|
|
|
4295f9 |
+
|
|
|
4295f9 |
+ return log_device_debug_errno(dev, SYNTHETIC_ERRNO(ELOOP), "Failed to create symbolic link %s: %m", filename);
|
|
|
4295f9 |
+}
|
|
|
4295f9 |
+
|
|
|
4295f9 |
/* manage "stack of names" with possibly specified device priorities */
|
|
|
4295f9 |
static int link_update(sd_device *dev, const char *slink_in, bool add) {
|
|
|
4295f9 |
- _cleanup_free_ char *slink = NULL, *filename = NULL, *dirname = NULL;
|
|
|
4295f9 |
- const char *slink_name, *id;
|
|
|
4295f9 |
+ _cleanup_free_ char *slink = NULL, *dirname = NULL;
|
|
|
4295f9 |
+ const char *slink_name;
|
|
|
4295f9 |
char name_enc[NAME_MAX+1];
|
|
|
4295f9 |
int i, r, retries;
|
|
|
4295f9 |
|
|
|
4295f9 |
@@ -279,35 +370,14 @@ static int link_update(sd_device *dev, const char *slink_in, bool add) {
|
|
|
4295f9 |
return log_device_debug_errno(dev, SYNTHETIC_ERRNO(EINVAL),
|
|
|
4295f9 |
"Invalid symbolic link of device node: %s", slink);
|
|
|
4295f9 |
|
|
|
4295f9 |
- r = device_get_device_id(dev, &id;;
|
|
|
4295f9 |
- if (r < 0)
|
|
|
4295f9 |
- return log_device_debug_errno(dev, r, "Failed to get device id: %m");
|
|
|
4295f9 |
-
|
|
|
4295f9 |
(void) udev_node_escape_path(slink_name, name_enc, sizeof(name_enc));
|
|
|
4295f9 |
- dirname = path_join("/run/udev/links/", name_enc);
|
|
|
4295f9 |
+ dirname = path_join("/run/udev/links", name_enc);
|
|
|
4295f9 |
if (!dirname)
|
|
|
4295f9 |
return log_oom_debug();
|
|
|
4295f9 |
|
|
|
4295f9 |
- filename = path_join(dirname, id);
|
|
|
4295f9 |
- if (!filename)
|
|
|
4295f9 |
- return log_oom_debug();
|
|
|
4295f9 |
-
|
|
|
4295f9 |
- if (!add) {
|
|
|
4295f9 |
- if (unlink(filename) < 0 && errno != ENOENT)
|
|
|
4295f9 |
- log_device_debug_errno(dev, errno, "Failed to remove %s, ignoring: %m", filename);
|
|
|
4295f9 |
-
|
|
|
4295f9 |
- (void) rmdir(dirname);
|
|
|
4295f9 |
- } else {
|
|
|
4295f9 |
- for (unsigned j = 0; j < TOUCH_FILE_MAX_RETRIES; j++) {
|
|
|
4295f9 |
- /* This may fail with -ENOENT when the parent directory is removed during
|
|
|
4295f9 |
- * creating the file by another udevd worker. */
|
|
|
4295f9 |
- r = touch_file(filename, /* parents= */ true, USEC_INFINITY, UID_INVALID, GID_INVALID, 0444);
|
|
|
4295f9 |
- if (r != -ENOENT)
|
|
|
4295f9 |
- break;
|
|
|
4295f9 |
- }
|
|
|
4295f9 |
- if (r < 0)
|
|
|
4295f9 |
- return log_device_debug_errno(dev, r, "Failed to create %s: %m", filename);
|
|
|
4295f9 |
- }
|
|
|
4295f9 |
+ r = update_stack_directory(dev, dirname, add);
|
|
|
4295f9 |
+ if (r < 0)
|
|
|
4295f9 |
+ return r;
|
|
|
4295f9 |
|
|
|
4295f9 |
/* If the database entry is not written yet we will just do one iteration and possibly wrong symlink
|
|
|
4295f9 |
* will be fixed in the second invocation. */
|