Skip to content

eBPF-based security platform

log: keep errno across logging

Daniel Borkmann2b2cb382c770
diff --git a/util/log.c b/util/log.c
index f8cd51d1..05451bea 100644
--- a/util/log.c
+++ b/util/log.c
@@ -5,6 +5,7 @@
* syslog(3) wrappers under LOG_AUTHPRIV.
*/
+#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
@@ -49,15 +50,22 @@ static void escape(char *dst, size_t cap, const char *src)
dst[n] = '\0';
}
+/*
+ * Callers report a failure and then return -errno; syslog(3) and stdio
+ * may change errno on the way (no /dev/log in an early initrd), so it
+ * is kept for them.
+ */
static void emit(int priority, const char *fmt, va_list ap)
{
char msg[LOG_MSG_MAX], out[4 * LOG_MSG_MAX + 1];
+ int saved_errno = errno;
if (vstrsprintf(msg, sizeof(msg), fmt, ap) < 0)
strscpy(msg, fmt, sizeof(msg));
escape(out, sizeof(out), msg);
syslog(priority, "%s", out);
fprintf(stderr, "%s\n", out);
+ errno = saved_errno;
}
#define PR_DEFINE(name, prio) \