From 6fa899db38d7f845209223110407cb1adbfc963f Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Thu, 24 Oct 2019 08:39:37 +0200 Subject: [PATCH] Misc, initial cleanup of syslog testing framework - Let syslog_tst.c use local syslog.c API, for wrapping - Add -DTESTING to build of syslog_tst & C:o - Enable CLOEXEC on each opened log file in syslog.c - Simplify code for readability Signed-off-by: Joachim Nilsson --- src/Makefile.am | 7 ++++--- src/syslog.c | 14 +++++++------- src/syslog_tst.c | 1 - 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/Makefile.am b/src/Makefile.am index 63ed17d..25d146c 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -34,7 +34,8 @@ klogd_CPPFLAGS = -DALLOW_KERNEL_LOGGING -D_BSD_SOURCE -D_DEFAULT_SOURCE logger_SOURCES = logger.c syslog.c logger_CPPFLAGS = -D_XOPEN_SOURCE=600 -D_BSD_SOURCE -D_GNU_SOURCE -D_DEFAULT_SOURCE -tsyslogd_SOURCES = syslogd.c pidfile.c pidfile.h -tsyslogd_CPPFLAGS = -D_BSD_SOURCE -D_DEFAULT_SOURCE -DTESTING +tsyslogd_SOURCES = $(syslogd_SOURCES) +tsyslogd_CPPFLAGS = $(syslogd_CPPFLAGS) -DTESTING -syslog_tst_SOURCES = syslog_tst.c +syslog_tst_SOURCES = syslog_tst.c syslog.c +syslog_tst_CPPFLAGS = -D_BSD_SOURCE -D_DEFAULT_SOURCE -DTESTING diff --git a/src/syslog.c b/src/syslog.c index 5db3e97..5b4cd4a 100644 --- a/src/syslog.c +++ b/src/syslog.c @@ -82,6 +82,9 @@ static char sccsid[] = "@(#)syslog.c 5.28 (Berkeley) 6/27/90"; #define _PATH_LOGNAME "/dev/log" +#ifndef TESTING +static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ +#endif static int LogFile = -1; /* fd for log */ static int connected; /* have done connect */ static int LogStat = 0; /* status bits, set by openlog() */ @@ -200,9 +203,6 @@ void vsyslog(int pri, const char *fmt, va_list ap) (void)close(fd); } -#ifndef TESTING -static struct sockaddr SyslogAddr; /* AF_UNIX address of local logger */ -#endif /* * OPENLOG -- open system log */ @@ -222,19 +222,19 @@ void openlog(const char *ident, int logstat, int logfac) #ifndef TESTING if (LogFile == -1) { SyslogAddr.sa_family = AF_UNIX; - strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, - sizeof(SyslogAddr.sa_data)); + strncpy(SyslogAddr.sa_data, _PATH_LOGNAME, sizeof(SyslogAddr.sa_data)); if (LogStat & LOG_NDELAY) { LogFile = socket(AF_UNIX, SOCK_DGRAM, 0); - /* fcntl(LogFile, F_SETFD, 1); */ + fcntl(LogFile, F_SETFD, 1); /* FD_CLOEXEC */ } } if (LogFile != -1 && !connected && connect(LogFile, &SyslogAddr, sizeof(SyslogAddr.sa_family) + strlen(SyslogAddr.sa_data)) != -1) + connected = 1; #else LogFile = fileno(stdout); + connected = 1; #endif - connected = 1; } /* diff --git a/src/syslog_tst.c b/src/syslog_tst.c index 86e209d..4b3b534 100644 --- a/src/syslog_tst.c +++ b/src/syslog_tst.c @@ -54,7 +54,6 @@ int main(int argc, char *argv[]) syslog(LOG_INFO, "Info log."); syslog(LOG_DEBUG, "Debug log."); closelog(); - return 0; } return 0;