summaryrefslogtreecommitdiff
path: root/src/basic
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-09-19 18:01:23 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-09-20 16:05:53 +0200
commit78158d134b056ceb974292e40a58e5f8a9af4cbe (patch)
treeaeaec07ee82272893c6eb701f31c8876f36c4240 /src/basic
parent6123dfaa72589735c3e8e9b9b9e075a287f523ab (diff)
downloadsystemd-78158d134b056ceb974292e40a58e5f8a9af4cbe.tar.gz
basic/log: put a ratelimit on our logging to /dev/kmsg
See the inline comment for details.
Diffstat (limited to 'src/basic')
-rw-r--r--src/basic/log.c13
1 files changed, 13 insertions, 0 deletions
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());