From 90913491392c026ec62e20fcd1360c8d5e192e72 Mon Sep 17 00:00:00 2001 From: Andreas Faerber Date: Sun, 27 Jan 2019 09:23:46 +0100 Subject: [PATCH] Add syslog output support to bootloader MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Broadcast console output as syslog debug messages, stripped of trailing newlines. Disabled by default. Patch from #6168. Change-Id: I1754381477dcde00bdcfa4482017daf5eb682cd3 Reviewed-on: https://review.haiku-os.org/c/912 Reviewed-by: Stephan Aßmus --- src/system/boot/loader/stdio.cpp | 64 +++++++++++++++++++++++++++++++- 1 file changed, 63 insertions(+), 1 deletion(-) diff --git a/src/system/boot/loader/stdio.cpp b/src/system/boot/loader/stdio.cpp index 267ddbd46f..07ae5ee4dc 100644 --- a/src/system/boot/loader/stdio.cpp +++ b/src/system/boot/loader/stdio.cpp @@ -6,6 +6,8 @@ #include #include +#include +#include #include #include @@ -20,6 +22,9 @@ //extern FILE *stdin; +//#define ENABLE_SYSLOG + + #undef errno int errno; @@ -31,6 +36,51 @@ _errnop(void) } +#ifdef ENABLE_SYSLOG + +static UDPSocket *sSyslogSocket = NULL; + +static void +sendToSyslog(const char *message, int length) +{ + // Lazy-initialize the socket + if (sSyslogSocket == NULL) { + // Check if the network stack has been initialized yet + if (NetStack::Default() != NULL) { + sSyslogSocket = new(std::nothrow) UDPSocket; + sSyslogSocket->Bind(INADDR_ANY, 60514); + } + } + + if (sSyslogSocket == NULL) + return; + + // Strip trailing newlines + while (length > 0) { + if (message[length - 1] != '\n' + && message[length - 1] != '\r') { + break; + } + length--; + } + if (length <= 0) + return; + + char buffer[1500]; + // same comment as in vfprintf applies... + const int facility = 0; // kernel + int severity = 7; // debug + int offset = snprintf(buffer, sizeof(buffer), + "<%d>1 - - Haiku - - - \xEF\xBB\xBF", + facility * 8 + severity); + length = std::min(length, (int)sizeof(buffer) - offset); + memcpy(buffer + offset, message, length); + sSyslogSocket->Send(INADDR_BROADCAST, 514, buffer, offset + length); +} + +#endif + + int vfprintf(FILE *file, const char *format, va_list list) { @@ -40,8 +90,12 @@ vfprintf(FILE *file, const char *format, va_list list) int length = vsnprintf(buffer, sizeof(buffer), format, list); length = std::min(length, (int)sizeof(buffer) - 1); - if (length > 0) + if (length > 0) { node->Write(buffer, length); +#ifdef ENABLE_SYSLOG + sendToSyslog(buffer, length); +#endif + } return length; } @@ -92,6 +146,10 @@ fputc(int c, FILE *file) // we only support direct console output right now... status = ((ConsoleNode *)file)->Write(&character, 1); +#ifdef ENABLE_SYSLOG + sendToSyslog(&character, 1); +#endif + if (status > 0) return character; @@ -108,6 +166,10 @@ fputs(const char *string, FILE *file) status_t status = ((ConsoleNode *)file)->Write(string, strlen(string)); fputc('\n', file); +#ifdef ENABLE_SYSLOG + sendToSyslog(string, strlen(string)); +#endif + return status; }