Commit 2791ce7c95 for frr
commit 2791ce7c955dde5ef0c28a1b437f032b52391230
Author: Rajesh Varatharaj <rvaratharaj@nvidia.com>
Date: Mon Nov 16 22:39:50 2026 +0000
lib: fix SIGBUS when zlog thread buffer cannot be allocated
zlog_tls_buffer_init() keeps each thread's log buffer in a file under
/var/tmp/frr and maps it. When posix_fallocate() fails for that file,
the code falls through to ftruncate(), which only sets the size, and
the file is mapped anyway. The mapping can then be sparse, and a log
message written to a part without blocks kills the daemon with SIGBUS
if the filesystem cannot supply one, for example when it is full.
Fall back to ftruncate() only when posix_fallocate() is not available,
or returns EOPNOTSUPP or EINVAL because the filesystem does not support
it. That is the case commit 6a3b431b8508 ("lib: fix TLS log buffer on
NetBSD") added the fallback for. On any other error, such as ENOSPC,
log it and use the anonymous buffer the function already has.
Fixes: #24442
Signed-off-by: Rajesh Varatharaj <rvaratharaj@nvidia.com>
diff --git a/lib/zlog.c b/lib/zlog.c
index c4b68e43a0..4f6ad24a5c 100644
--- a/lib/zlog.c
+++ b/lib/zlog.c
@@ -265,6 +265,7 @@ void zlog_tls_buffer_init(void)
struct zlog_tls *zlog_tls;
char mmpath[MAXPATHLEN];
int mmfd;
+ int err;
size_t i;
zlog_tls = zlog_tls_get();
@@ -288,12 +289,15 @@ void zlog_tls_buffer_init(void)
fchown(mmfd, zlog_uid, zlog_gid);
#ifdef HAVE_POSIX_FALLOCATE
- if (posix_fallocate(mmfd, 0, TLS_LOG_BUF_SIZE) != 0)
- /* note next statement is under above if() */
+ err = posix_fallocate(mmfd, 0, TLS_LOG_BUF_SIZE);
+#else
+ err = EOPNOTSUPP;
#endif
- if (ftruncate(mmfd, TLS_LOG_BUF_SIZE) < 0) {
- zlog_err("failed to allocate thread log buffer \"%s\": %s",
- mmpath, strerror(errno));
+ /* space cannot be reserved on this system or filesystem: only set the size */
+ if (err == EOPNOTSUPP || err == EINVAL)
+ err = ftruncate(mmfd, TLS_LOG_BUF_SIZE) < 0 ? errno : 0;
+ if (err != 0) {
+ zlog_err("failed to allocate thread log buffer \"%s\": %s", mmpath, strerror(err));
goto out_anon_unlink;
}