]> xenbits.xensource.com Git - unikraft/unikraft.git/commitdiff
lib/nolibc: Implement syslog functions
authorMarco Schlumpp <marco.schlumpp@gmail.com>
Tue, 29 Mar 2022 13:25:17 +0000 (15:25 +0200)
committerUnikraft <monkey@unikraft.io>
Thu, 4 May 2023 15:50:59 +0000 (15:50 +0000)
These functions forward the messages to the ukdebug library.

Signed-off-by: Marco Schlumpp <marco.schlumpp@gmail.com>
Reviewed-by: Razvan Deaconescu <razvand@unikraft.io>
Reviewed-by: Marc Rittinghaus <marc.rittinghaus@unikraft.io>
Approved-by: Marc Rittinghaus <marc.rittinghaus@unikraft.io>
Tested-by: Unikraft CI <monkey@unikraft.io>
GitHub-Closes: #627

lib/nolibc/Config.uk
lib/nolibc/Makefile.uk
lib/nolibc/exportsyms.uk
lib/nolibc/include/syslog.h [new file with mode: 0644]
lib/nolibc/syslog.c [new file with mode: 0644]

index a81aaece86936b6e3cfcaf810fd3493a8105092f..6d10da62772c0fe0c75df85373386464fff89f52 100644 (file)
@@ -12,4 +12,12 @@ if LIBNOLIBC
                        Assertions (`assert()` defined in `<assert.h>`) are mapped to `UK_ASSERT()`.
                        If selected, please note that libc assertions are also removed from the code
                        when assertions are disabled in libukdebug.
+
+       config LIBNOLIBC_SYSLOG
+               bool "Include syslog functions"
+               select LIBUKDEBUG
+               default n
+               help
+                       Provide implementation of syslog/openlog/closelog functions which use the
+                       ukdebug facility.
 endif
index 35e1e0f6a362aeded12d41faa8d84e22b1b992b3..94bea8e375dc40277190fba2c3c260aa497089bf 100644 (file)
@@ -45,6 +45,7 @@ LIBNOLIBC_SRCS-y += $(LIBNOLIBC_BASE)/sscanf.c
 LIBNOLIBC_SRCS-y += $(LIBNOLIBC_BASE)/asprintf.c
 LIBNOLIBC_SRCS-y += $(LIBNOLIBC_BASE)/random.c
 
+LIBNOLIBC_SRCS-$(CONFIG_LIBNOLIBC_SYSLOG) += $(LIBNOLIBC_BASE)/syslog.c
 
 LIBNOLIBC_SRCS-y += $(LIBNOLIBC_BASE)/qsort.c
 
index a283695fbd9c66afcb9b2a50ffa67d4ff9493bd5..7fef8490ae37d1270bad307bbe6802e9e1076e03 100644 (file)
@@ -103,6 +103,12 @@ htons
 nthos
 h_errno
 
+# syslog
+openlog
+closelog
+syslog
+vsyslog
+
 # random
 random
 srandom
diff --git a/lib/nolibc/include/syslog.h b/lib/nolibc/include/syslog.h
new file mode 100644 (file)
index 0000000..643e47f
--- /dev/null
@@ -0,0 +1,69 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/* Copyright (c) 2022, Unikraft GmbH and The Unikraft Authors.
+ * Licensed under the BSD-3-Clause License (the "License").
+ * You may not use this file except in compliance with the License.
+ */
+
+#ifndef __SYSLOG_H__
+#define __SYSLOG_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdarg.h"
+
+#define LOG_CONS       0x01
+#define LOG_NDELAY     0x02
+#define LOG_NOWAIT     0x04
+#define LOG_ODELAY     0x08
+#define LOG_PERROR     0x10
+#define LOG_PID                0x20
+
+#define LOG_AUTH       (1 << 3)
+#define LOG_AUTHPRIV   (2 << 3)
+#define LOG_CRON       (3 << 3)
+#define LOG_DAEMON     (4 << 3)
+#define LOG_FTP                (5 << 3)
+#define LOG_KERN       (6 << 3)
+#define LOG_LOCAL0     (7 << 3)
+#define LOG_LOCAL1     (8 << 3)
+#define LOG_LOCAL2     (9 << 3)
+#define LOG_LOCAL3     (10 << 3)
+#define LOG_LOCAL4     (11 << 3)
+#define LOG_LOCAL5     (12 << 3)
+#define LOG_LOCAL6     (13 << 3)
+#define LOG_LOCAL7     (14 << 3)
+#define LOG_LPR                (15 << 3)
+#define LOG_MAIL       (16 << 3)
+#define LOG_NEWS       (17 << 3)
+#define LOG_SYSLOG     (18 << 3)
+#define LOG_USER       (19 << 3)
+#define LOG_UUCP       (20 << 3)
+
+#define LOG_EMERG      0
+#define LOG_ALERT      1
+#define LOG_CRIT       2
+#define LOG_ERR                3
+#define LOG_WARNING    4
+#define LOG_NOTICE     5
+#define LOG_INFO       6
+#define LOG_DEBUG      7
+
+#define LOG_PRIMASK    0x7
+
+#define LOG_MAKEPRI(FAC, PRI) ((FAC) | (PRI))
+
+void openlog(const char *ident, int option, int facility);
+
+void syslog(int priority, const char *format, ...);
+
+void closelog(void);
+
+void vsyslog(int priority, const char *format, va_list ap);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SYSLOG_H__ */
diff --git a/lib/nolibc/syslog.c b/lib/nolibc/syslog.c
new file mode 100644 (file)
index 0000000..40b2e7f
--- /dev/null
@@ -0,0 +1,140 @@
+/* SPDX-License-Identifier: BSD-3-Clause */
+/*
+ * Authors: Marco Schlumpp <marco.schlumpp@gmail.com>
+ *
+ * Copyright (c) 2022, Karlsruhe Institute of Technology (KIT).
+ *                     All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the copyright holder nor the names of its
+ *    contributors may be used to endorse or promote products derived from
+ *    this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "syslog.h"
+
+#include <stdio.h>
+#include <uk/print.h>
+
+static int log_facility = LOG_USER;
+
+static const int level_map[] = {
+       KLVL_CRIT, /* LOG_EMERG */
+       KLVL_CRIT, /* LOG_ALERT */
+       KLVL_CRIT, /* LOG_CRIT */
+       KLVL_ERR,  /* LOG_ERR */
+       KLVL_WARN, /* LOG_WARNING */
+       KLVL_INFO, /* LOG_NOTICE */
+       KLVL_INFO, /* LOG_INFO */
+
+       /* This one maps to a different macro on unikraft */
+       /* KLVL_INFO, */ /* LOG_DEBUG */
+};
+
+static const char *facility_to_str(int facility)
+{
+       switch (facility) {
+       case LOG_AUTH:
+               return "AUTH";
+       case LOG_AUTHPRIV:
+               return "AUTHPRIV";
+       case LOG_CRON:
+               return "CRON";
+       case LOG_DAEMON:
+               return "DAEMON";
+       case LOG_FTP:
+               return "FTP";
+       case LOG_KERN:
+               return "KERN";
+       case LOG_LOCAL0:
+               return "LOCAL0";
+       case LOG_LOCAL1:
+               return "LOCAL1";
+       case LOG_LOCAL2:
+               return "LOCAL2";
+       case LOG_LOCAL3:
+               return "LOCAL3";
+       case LOG_LOCAL4:
+               return "LOCAL4";
+       case LOG_LOCAL5:
+               return "LOCAL5";
+       case LOG_LOCAL6:
+               return "LOCAL6";
+       case LOG_LOCAL7:
+               return "LOCAL7";
+       case LOG_LPR:
+               return "LPR";
+       case LOG_MAIL:
+               return "MAIL";
+       case LOG_NEWS:
+               return "NEWS";
+       case LOG_SYSLOG:
+               return "SYSLOG";
+       case LOG_USER:
+               return "USER";
+       case LOG_UUCP:
+               return "UUCP";
+       default:
+               return "???";
+       }
+}
+
+void openlog(const char *ident __unused, int option __unused, int facility)
+{
+       log_facility = facility;
+}
+
+void syslog(int priority, const char *format, ...)
+{
+       va_list ap;
+
+       va_start(ap, format);
+       vsyslog(priority, format, ap);
+       va_end(ap);
+}
+
+void closelog(void)
+{}
+
+void vsyslog(int priority, const char *format, va_list ap)
+{
+       int facility;
+
+       /* Prepare arguments */
+       facility = priority & ~LOG_PRIMASK;
+       if (facility == 0)
+               facility = log_facility;
+       priority &= LOG_PRIMASK;
+
+       /* Forward call */
+       if (priority == LOG_DEBUG) {
+               uk_printd("[%s] ", facility_to_str(facility));
+               uk_printd(format, ap);
+       } else {
+               priority = MAX(priority, 0);
+               priority = MIN(priority, (int)ARRAY_SIZE(level_map));
+               uk_printk(level_map[priority], "[%s] ",
+                         facility_to_str(facility));
+               uk_printk(level_map[priority], format, ap);
+       }
+}