From 0175a4709457bd0dcd2bf478e1a5b27e8f9800df Mon Sep 17 00:00:00 2001 From: Costin Lupu Date: Wed, 14 Aug 2019 21:57:02 +0300 Subject: [PATCH] syslog.c: Import from osv Copied as is from the official osv git mirror https://github.com/cloudius-systems/osv.git, commit ee7a2cd4. Original file locations: * libc/syslog.c We had to decide between importing from musl or osv. We decided to pick the osv variant because it's simpler: it outputs to stdio. The musl implementation uses UNIX sockets, which we currently don't have in Unikraft. Moreover, the osv implementation was originally imported from musl and adapted. This is also the reason why we put the file under the musl-imported/ subdirectory. In the future, a better syslog implementation may replace this one and it might even become an internal library with fully fledged functionality, just like classic OSes. Signed-off-by: Costin Lupu Reviewed-by: Felipe Huici --- musl-imported/src/syslog.c | 73 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 musl-imported/src/syslog.c diff --git a/musl-imported/src/syslog.c b/musl-imported/src/syslog.c new file mode 100644 index 0000000..be7cfb6 --- /dev/null +++ b/musl-imported/src/syslog.c @@ -0,0 +1,73 @@ + +// adapted from musl's version, just writes to stdio + +#include +#include +#include +#include +#include +#include +#include + +static mutex_t lock; +static char log_ident[32]; +static int log_opt; +static int log_facility = LOG_USER; + +void openlog(const char *ident, int opt, int facility) +{ + LOCK(lock); + + if (ident) { + size_t n = strnlen(ident, sizeof log_ident - 1); + memcpy(log_ident, ident, n); + log_ident[n] = 0; + } else { + log_ident[0] = 0; + } + log_opt = opt; + log_facility = facility; + + UNLOCK(lock); +} + +void closelog(void) +{ +} + +void __syslog_chk(int priority, int flag, const char *message, ...) +{ + LOCK(lock); + + va_list ap; + va_start(ap, message); + + char timebuf[16]; + time_t now; + struct tm tm; + char buf[256]; + int pid; + int l, l2; + + if (!(priority & LOG_FACMASK)) priority |= log_facility; + + now = time(NULL); + gmtime_r(&now, &tm); + strftime(timebuf, sizeof timebuf, "%b %e %T", &tm); + + pid = (log_opt & LOG_PID) ? getpid() : 0; + l = snprintf(buf, sizeof buf, "<%d>%s %s%s%.0d%s: ", + priority, timebuf, log_ident, "["+!pid, pid, "]"+!pid); + l2 = vsnprintf(buf+l, sizeof buf - l, message, ap); + if (l2 >= 0) { + if (l2 >= sizeof buf - l) l = sizeof buf - 1; + else l += l2; + if (buf[l-1] != '\n') buf[l++] = '\n'; + fwrite(buf, 1, l, LOG_PRI(priority) >= LOG_ERR ? stderr : stdout); + } + + va_end(ap); + + UNLOCK(lock); +} + -- 2.39.5