Merge pull request #59 from xiaoxiang781216/nuttx

Port sysklogd to NuttX

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg 2022-11-09 16:28:04 +01:00 committed by GitHub
commit b234244680
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 118 additions and 5 deletions

View File

@ -54,6 +54,15 @@ PKG_PROG_PKG_CONFIG
AC_REPLACE_FUNCS([pidfile strlcpy strlcat utimensat]) AC_REPLACE_FUNCS([pidfile strlcpy strlcat utimensat])
AC_CONFIG_LIBOBJ_DIR([lib]) 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 # Check for other library functions
AC_CHECK_FUNCS([getprogname strtobytes]) AC_CHECK_FUNCS([getprogname strtobytes])

View File

@ -34,7 +34,6 @@
#ifndef _SYS_SYSLOG_H_ /* From NetBSD, for co-existance with C-library header */ #ifndef _SYS_SYSLOG_H_ /* From NetBSD, for co-existance with C-library header */
#define _SYS_SYSLOG_H_ #define _SYS_SYSLOG_H_
#include <features.h>
#include <stdarg.h> #include <stdarg.h>
/* /*
@ -80,6 +79,7 @@
/* mark "facility" */ /* mark "facility" */
#define INTERNAL_ALLPRI 0xFF /* Value to indicate all priorities in f_pmask */ #define INTERNAL_ALLPRI 0xFF /* Value to indicate all priorities in f_pmask */
#define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES << 3, 0) #define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES << 3, 0)
#undef CODE
typedef struct _code { typedef struct _code {
const char *c_name; const char *c_name;
int c_val; int c_val;

View File

@ -65,8 +65,9 @@ static char sccsid[] __attribute__((unused)) =
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#ifdef HAVE_UTMP_H
#include <utmp.h> #include <utmp.h>
#endif
#include <errno.h> #include <errno.h>
#include <err.h> #include <err.h>
#include <fnmatch.h> #include <fnmatch.h>
@ -83,7 +84,6 @@ static char sccsid[] __attribute__((unused)) =
#endif #endif
#include <arpa/inet.h> #include <arpa/inet.h>
#include <arpa/nameser.h>
#include <netdb.h> #include <netdb.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <resolv.h> #include <resolv.h>
@ -96,6 +96,10 @@ static char sccsid[] __attribute__((unused)) =
#include "timer.h" #include "timer.h"
#include "compat.h" #include "compat.h"
#ifndef MIN
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#endif
#define SecureMode (secure_opt > 0 ? secure_opt : secure_mode) #define SecureMode (secure_opt > 0 ? secure_opt : secure_mode)
char *CacheFile = _PATH_CACHE; char *CacheFile = _PATH_CACHE;
@ -866,8 +870,10 @@ static void create_inet_socket(struct peer *pe)
void untty(void) void untty(void)
{ {
#ifdef HAVE_SETSID
if (!Debug) if (!Debug)
setsid(); setsid();
#endif
} }
/* /*
@ -1397,7 +1403,97 @@ void printsys(char *msg)
} else if (*p == ' ') { } else if (*p == ' ') {
/* Linux /dev/kmsg continuation line w/ SUBSYSTEM= DEVICE=, skip */ /* Linux /dev/kmsg continuation line w/ SUBSYSTEM= DEVICE=, skip */
return; 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 */ /* kernel printf's come out on console */
buffer.flags |= IGN_CONS; buffer.flags |= IGN_CONS;
} }
@ -2062,6 +2158,7 @@ void endtty(int signo)
*/ */
void wallmsg(struct filed *f, struct iovec *iov, int iovcnt) void wallmsg(struct filed *f, struct iovec *iov, int iovcnt)
{ {
#ifdef HAVE_UTMP_H
static int reenter = 0; static int reenter = 0;
struct utmp *uptr; struct utmp *uptr;
struct utmp ut; struct utmp ut;
@ -2153,6 +2250,7 @@ void wallmsg(struct filed *f, struct iovec *iov, int iovcnt)
/* close the user login file */ /* close the user login file */
endutent(); endutent();
reenter = 0; reenter = 0;
#endif
} }
void reapchild(int signo) void reapchild(int signo)
@ -2422,6 +2520,7 @@ void die(int signo)
*/ */
static int waitdaemon(int maxwait) static int waitdaemon(int maxwait)
{ {
#ifdef HAVE_FORK
struct sigaction sa; struct sigaction sa;
pid_t pid, childpid; pid_t pid, childpid;
int status; int status;
@ -2470,6 +2569,7 @@ static int waitdaemon(int maxwait)
(void)close(fd); (void)close(fd);
} }
#endif /* HAVE_FORK */
return getppid(); return getppid();
} }
@ -2540,14 +2640,16 @@ static void signal_init(void)
SIGNAL(SIGQUIT, Debug ? die : SIG_IGN); SIGNAL(SIGQUIT, Debug ? die : SIG_IGN);
SIGNAL(SIGUSR1, Debug ? debug_switch : SIG_IGN); SIGNAL(SIGUSR1, Debug ? debug_switch : SIG_IGN);
SIGNAL(SIGUSR2, signal_rotate); SIGNAL(SIGUSR2, signal_rotate);
#ifdef SIGXFSZ
SIGNAL(SIGXFSZ, SIG_IGN); SIGNAL(SIGXFSZ, SIG_IGN);
#endif
SIGNAL(SIGHUP, reload); SIGNAL(SIGHUP, reload);
SIGNAL(SIGCHLD, reapchild); SIGNAL(SIGCHLD, reapchild);
} }
static void boot_time_init(void) static void boot_time_init(void)
{ {
#ifdef __linux__ #if defined(__linux__) || defined(__NuttX__)
struct sysinfo si; struct sysinfo si;
struct timeval tv; struct timeval tv;

View File

@ -49,7 +49,9 @@
#include "queue.h" #include "queue.h"
#include "syslog.h" #include "syslog.h"
#ifndef MAXLINE
#define MAXLINE 2048 /* maximum line length */ #define MAXLINE 2048 /* maximum line length */
#endif
#define MAXSVLINE MAXLINE /* maximum saved line length */ #define MAXSVLINE MAXLINE /* maximum saved line length */
#define DEFUPRI (LOG_USER | LOG_NOTICE) #define DEFUPRI (LOG_USER | LOG_NOTICE)
#define DEFSPRI (LOG_KERN | LOG_CRIT) #define DEFSPRI (LOG_KERN | LOG_CRIT)