From ad172d19d5ef8b5a3631a8484cc3d1a28dba26c2 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Wed, 2 Jun 2021 15:29:29 +0200 Subject: [PATCH] pid1: reduce log noise generated by devices with overly long sysfs paths This basically does what 2c905207db37c691d4abef868165ad5ea2dd0f4f did for mount units Fixes: #16161 --- src/core/device.c | 31 +++++++++++++++++++++++++++++-- src/systemd/sd-messages.h | 4 ++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/core/device.c b/src/core/device.c index d188b0cd222..03a2a9f22a2 100644 --- a/src/core/device.c +++ b/src/core/device.c @@ -3,6 +3,8 @@ #include #include +#include "sd-messages.h" + #include "alloc-util.h" #include "bus-error.h" #include "dbus-device.h" @@ -12,6 +14,7 @@ #include "log.h" #include "parse-util.h" #include "path-util.h" +#include "ratelimit.h" #include "serialize.h" #include "stat-util.h" #include "string-util.h" @@ -497,8 +500,32 @@ static int device_setup_unit(Manager *m, sd_device *dev, const char *path, bool } r = unit_name_from_path(path, ".device", &e); - if (r < 0) - return log_device_error_errno(dev, r, "Failed to generate unit name from device path: %m"); + if (r < 0) { + /* Let's complain about overly long device names only at most once every 5s or so. This is + * something we should mention, since relevant devices are not manageable by systemd, but not + * flood the log about. */ + static RateLimit rate_limit = { + .interval = 5 * USEC_PER_SEC, + .burst = 1, + }; + + /* If we cannot convert a device name to a unit name then let's ignore the device. So far, + * devices with such long names weren't really the kind you want to manage with systemd + * anyway, hence this shouldn't be a problem. */ + + if (r == -ENAMETOOLONG) + return log_struct_errno( + ratelimit_below(&rate_limit) ? LOG_WARNING : LOG_DEBUG, r, + "MESSAGE_ID=" SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR, + "DEVICE=%s", path, + LOG_MESSAGE("Device path '%s' too long to fit into unit name, ignoring device.", path)); + + return log_struct_errno( + ratelimit_below(&rate_limit) ? LOG_WARNING : LOG_DEBUG, r, + "MESSAGE_ID=" SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR, + "DEVICE=%s", path, + LOG_MESSAGE("Failed to generate valid unit name from device path '%s', ignoring device: %m", path)); + } u = manager_get_unit(m, e); if (u) { diff --git a/src/systemd/sd-messages.h b/src/systemd/sd-messages.h index 97ba02ffa83..aee0ddb6863 100644 --- a/src/systemd/sd-messages.h +++ b/src/systemd/sd-messages.h @@ -170,6 +170,10 @@ _SD_BEGIN_DECLARATIONS; SD_ID128_MAKE(1b,3b,b9,40,37,f0,4b,bf,81,02,8e,13,5a,12,d2,93) #define SD_MESSAGE_MOUNT_POINT_PATH_NOT_SUITABLE_STR \ SD_ID128_MAKE_STR(1b,3b,b9,40,37,f0,4b,bf,81,02,8e,13,5a,12,d2,93) +#define SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE \ + SD_ID128_MAKE(01,01,90,13,8f,49,4e,29,a0,ef,66,69,74,95,31,aa) +#define SD_MESSAGE_DEVICE_PATH_NOT_SUITABLE_STR \ + SD_ID128_MAKE_STR(01,01,90,13,8f,49,4e,29,a0,ef,66,69,74,95,31,aa) #define SD_MESSAGE_NOBODY_USER_UNSUITABLE SD_ID128_MAKE(b4,80,32,5f,9c,39,4a,7b,80,2c,23,1e,51,a2,75,2c) #define SD_MESSAGE_NOBODY_USER_UNSUITABLE_STR \ -- 2.47.3