From b3502b84c4766472059585bf0dd940abb03f5466 Mon Sep 17 00:00:00 2001 From: anjiahao Date: Mon, 22 Nov 2021 21:31:33 +0800 Subject: [PATCH] Port sysklogd to NuttX Signed-off-by: anjiahao Signed-off-by: Xiang Xiao --- configure.ac | 9 +++++ src/syslog.h | 2 +- src/syslogd.c | 110 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/syslogd.h | 2 + 4 files changed, 118 insertions(+), 5 deletions(-) diff --git a/configure.ac b/configure.ac index 161de19..2ce4a59 100644 --- a/configure.ac +++ b/configure.ac @@ -54,6 +54,15 @@ PKG_PROG_PKG_CONFIG AC_REPLACE_FUNCS([pidfile strlcpy strlcat utimensat]) AC_CONFIG_LIBOBJ_DIR([lib]) +# Check for utmp.h +AC_CHECK_HEADERS([utmp.h]) + +# Check for fork() +AC_CHECK_FUNCS([fork]) + +# Check for setsid() +AC_CHECK_FUNCS([setsid]) + # Check for other library functions AC_CHECK_FUNCS([getprogname strtobytes]) diff --git a/src/syslog.h b/src/syslog.h index e1c6f47..6b674b6 100644 --- a/src/syslog.h +++ b/src/syslog.h @@ -34,7 +34,6 @@ #ifndef _SYS_SYSLOG_H_ /* From NetBSD, for co-existance with C-library header */ #define _SYS_SYSLOG_H_ -#include #include /* @@ -80,6 +79,7 @@ /* mark "facility" */ #define INTERNAL_ALLPRI 0xFF /* Value to indicate all priorities in f_pmask */ #define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES << 3, 0) +#undef CODE typedef struct _code { const char *c_name; int c_val; diff --git a/src/syslogd.c b/src/syslogd.c index a292593..1b3c0ef 100644 --- a/src/syslogd.c +++ b/src/syslogd.c @@ -65,8 +65,9 @@ static char sccsid[] __attribute__((unused)) = #include #include #include +#ifdef HAVE_UTMP_H #include - +#endif #include #include #include @@ -83,7 +84,6 @@ static char sccsid[] __attribute__((unused)) = #endif #include -#include #include #include #include @@ -96,6 +96,10 @@ static char sccsid[] __attribute__((unused)) = #include "timer.h" #include "compat.h" +#ifndef MIN +#define MIN(x, y) ((x) < (y) ? (x) : (y)) +#endif + #define SecureMode (secure_opt > 0 ? secure_opt : secure_mode) char *CacheFile = _PATH_CACHE; @@ -866,8 +870,10 @@ static void create_inet_socket(struct peer *pe) void untty(void) { +#ifdef HAVE_SETSID if (!Debug) setsid(); +#endif } /* @@ -1397,7 +1403,97 @@ void printsys(char *msg) } else if (*p == ' ') { /* Linux /dev/kmsg continuation line w/ SUBSYSTEM= DEVICE=, skip */ return; - } else { + } +#ifdef __NuttX__ + else if (*p == '[') { + p++; +#ifdef CONFIG_SYSLOG_TIMESTAMP_FORMATTED + if (strptime(p, CONFIG_SYSLOG_TIMESTAMP_FORMAT, &buffer.timestamp.tm) == NULL) + return; + p = strchr(p, ']'); + if (p == NULL) + return; +#else + time_t sec = boot_time + strtoul(p ,&p, 0); + if (*p++ != '.') { + return; + } + localtime_r(&sec, &buffer.timestamp.tm); + buffer.timestamp.usec = atoi(p) * 1000; + p = strchr(p, ']'); + if (p == NULL) + return; +#endif + +#ifdef CONFIG_SMP + p = strchr(p, '['); + if (p == NULL) + return; + buffer.sd = ++p; + p = strchr(p, ']'); + if (p == NULL) + return; + *p++ = '\0'; +#endif + +#ifdef CONFIG_SYSLOG_PROCESSID + p = strchr(p, '['); + if (p == NULL) + return; + buffer.proc_id = ++p; + p = strchr(p, ']'); + if (p == NULL) + return; + + *p++ = '\0'; +#endif + +#ifdef CONFIG_SYSLOG_PRIORITY + static const char * PriorityNames[] = { + " EMERG", " ALERT", " CRIT", " ERROR", + " WARN", "NOTICE", " INFO", " DEBUG" + }; + p = strchr(p, '['); + if (p == NULL) + return; + p = p + 1; + + for (uint8_t i = 0; i <= LOG_DEBUG; i++) { + if (strncmp(p, PriorityNames[i], + strlen(PriorityNames[i])) == 0) { + buffer.pri = i; + p += strlen(PriorityNames[i]); + break; + } + } + p = strchr(p, ']'); + if (p == NULL) + return; + p += 2; +#endif + +#ifdef CONFIG_SYSLOG_PREFIX + p = strchr(p, '['); + if (p == NULL) + return; + buffer.hostname = p + 1; + p = strchr(p, ']'); + if (p == NULL) + return; + *p++ = '\0'; +#endif + +#if CONFIG_TASK_NAME_SIZE > 0 && defined(CONFIG_SYSLOG_PROCESS_NAME) + buffer.app_name = p; + p = strchr(p, ':'); + if (p == NULL) + return; + *(p + 1) = '\0'; + p += 2; +#endif + } +#endif + else { /* kernel printf's come out on console */ buffer.flags |= IGN_CONS; } @@ -2062,6 +2158,7 @@ void endtty(int signo) */ void wallmsg(struct filed *f, struct iovec *iov, int iovcnt) { +#ifdef HAVE_UTMP_H static int reenter = 0; struct utmp *uptr; struct utmp ut; @@ -2153,6 +2250,7 @@ void wallmsg(struct filed *f, struct iovec *iov, int iovcnt) /* close the user login file */ endutent(); reenter = 0; +#endif } void reapchild(int signo) @@ -2422,6 +2520,7 @@ void die(int signo) */ static int waitdaemon(int maxwait) { +#ifdef HAVE_FORK struct sigaction sa; pid_t pid, childpid; int status; @@ -2470,6 +2569,7 @@ static int waitdaemon(int maxwait) (void)close(fd); } +#endif /* HAVE_FORK */ return getppid(); } @@ -2540,14 +2640,16 @@ static void signal_init(void) SIGNAL(SIGQUIT, Debug ? die : SIG_IGN); SIGNAL(SIGUSR1, Debug ? debug_switch : SIG_IGN); SIGNAL(SIGUSR2, signal_rotate); +#ifdef SIGXFSZ SIGNAL(SIGXFSZ, SIG_IGN); +#endif SIGNAL(SIGHUP, reload); SIGNAL(SIGCHLD, reapchild); } static void boot_time_init(void) { -#ifdef __linux__ +#if defined(__linux__) || defined(__NuttX__) struct sysinfo si; struct timeval tv; diff --git a/src/syslogd.h b/src/syslogd.h index 934afd1..68ceafb 100644 --- a/src/syslogd.h +++ b/src/syslogd.h @@ -49,7 +49,9 @@ #include "queue.h" #include "syslog.h" +#ifndef MAXLINE #define MAXLINE 2048 /* maximum line length */ +#endif #define MAXSVLINE MAXLINE /* maximum saved line length */ #define DEFUPRI (LOG_USER | LOG_NOTICE) #define DEFSPRI (LOG_KERN | LOG_CRIT)