From 78158d134b056ceb974292e40a58e5f8a9af4cbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Thu, 19 Sep 2019 18:01:23 +0200 Subject: [PATCH] basic/log: put a ratelimit on our logging to /dev/kmsg See the inline comment for details. --- src/basic/log.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/basic/log.c b/src/basic/log.c index 8bcc18bc80..f26b592a6c 100644 --- a/src/basic/log.c +++ b/src/basic/log.c @@ -29,6 +29,7 @@ #include "parse-util.h" #include "proc-cmdline.h" #include "process-util.h" +#include "ratelimit.h" #include "signal-util.h" #include "socket-util.h" #include "stdio-util.h" @@ -459,6 +460,15 @@ static int write_to_kmsg( const char *func, const char *buffer) { + /* Set a ratelimit on the amount of messages logged to /dev/kmsg. This is mostly supposed to be a + * safety catch for the case where start indiscriminately logging in a loop. It will not catch cases + * where we log excessively, but not in a tight loop. + * + * Note that this ratelimit is per-emitter, so we might still overwhelm /dev/kmsg with multiple + * loggers. + */ + static thread_local RateLimit ratelimit = { 5 * USEC_PER_SEC, 200 }; + char header_priority[2 + DECIMAL_STR_MAX(int) + 1], header_pid[4 + DECIMAL_STR_MAX(pid_t) + 1]; struct iovec iovec[5] = {}; @@ -466,6 +476,9 @@ static int write_to_kmsg( if (kmsg_fd < 0) return 0; + if (!ratelimit_below(&ratelimit)) + return 0; + xsprintf(header_priority, "<%i>", level); xsprintf(header_pid, "["PID_FMT"]: ", getpid_cached());