From 1106b6acd5b561f760d206ce311a69c5979f19af Mon Sep 17 00:00:00 2001 From: bluhm Date: Wed, 5 Apr 2017 21:30:04 +0000 Subject: [PATCH] The function logmsg() was used to generate local messages and to process incoming messages. Split this functionality into log_info() and logline(). Sort the parameters like they appear in the syslog line. OK millert@ --- usr.sbin/syslogd/syslogd.c | 70 +++++++++++++------------------------- 1 file changed, 24 insertions(+), 46 deletions(-) diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c index d8557b30094..00bd99da3e3 100644 --- a/usr.sbin/syslogd/syslogd.c +++ b/usr.sbin/syslogd/syslogd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: syslogd.c,v 1.237 2017/04/05 19:34:49 bluhm Exp $ */ +/* $OpenBSD: syslogd.c,v 1.238 2017/04/05 21:30:04 bluhm Exp $ */ /* * Copyright (c) 1983, 1988, 1993, 1994 @@ -110,7 +110,7 @@ const char ctty[] = _PATH_CONSOLE; /* - * Flags to logmsg(). + * Flags to logline(). */ #define IGN_CONS 0x001 /* don't print on console */ @@ -321,7 +321,7 @@ void markit(void); void fprintlog(struct filed *, int, char *); void init(void); void logevent(int, const char *); -void logmsg(int, char *, char *, int); +void logline(int, int, char *, char *); struct filed *find_dup(struct filed *); size_t parsepriority(const char *, int *); void printline(char *, char *); @@ -831,7 +831,7 @@ main(int argc, char *argv[]) to.tv_usec = 0; evtimer_add(ev_mark, &to); - logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: start", LocalHostName, ADDDATE); + log_info(LOG_INFO, "start"); log_debug("syslogd: started"); sigemptyset(&sigmask); @@ -1021,7 +1021,6 @@ reserve_accept4(int lfd, int event, struct event *ev, struct sockaddr *sa, socklen_t *salen, int flags) { struct timeval to = { 1, 0 }; - char ebuf[ERRBUFSIZE]; int afd; if (event & EV_TIMEOUT) { @@ -1040,9 +1039,7 @@ reserve_accept4(int lfd, int event, struct event *ev, afd = accept4(lfd, sa, salen, flags); if (afd == -1 && (errno == ENFILE || errno == EMFILE)) { - snprintf(ebuf, sizeof(ebuf), "syslogd: accept deferred: %s", - strerror(errno)); - logmsg(LOG_SYSLOG|LOG_WARNING, ebuf, LocalHostName, ADDDATE); + log_info(LOG_WARNING, "accept deferred: %s", strerror(errno)); /* * Disable the listen event and convert it to a timeout. * Pass the listen file descriptor to the callback. @@ -1076,7 +1073,7 @@ acceptcb(int lfd, short event, void *arg, int usetls) struct sockaddr_storage ss; socklen_t sslen; char hostname[NI_MAXHOST], servname[NI_MAXSERV]; - char *peername, ebuf[ERRBUFSIZE]; + char *peername; int fd; sslen = sizeof(ss); @@ -1139,9 +1136,8 @@ acceptcb(int lfd, short event, void *arg, int usetls) p->p_peername = peername; bufferevent_enable(p->p_bufev, EV_READ); - snprintf(ebuf, sizeof(ebuf), "syslogd: %s logger \"%s\" accepted", + log_info(LOG_INFO, "%s logger \"%s\" accepted", p->p_ctx ? "tls" : "tcp", peername); - logmsg(LOG_SYSLOG|LOG_INFO, ebuf, LocalHostName, ADDDATE); } /* @@ -1265,19 +1261,14 @@ void tcp_closecb(struct bufferevent *bufev, short event, void *arg) { struct peer *p = arg; - char ebuf[ERRBUFSIZE]; if (event & EVBUFFER_EOF) { - snprintf(ebuf, sizeof(ebuf), "syslogd: %s logger \"%s\" " - "connection close", p->p_ctx ? "tls" : "tcp", - p->p_peername); - logmsg(LOG_SYSLOG|LOG_INFO, ebuf, LocalHostName, ADDDATE); + log_info(LOG_INFO, "%s logger \"%s\" connection close", + p->p_ctx ? "tls" : "tcp", p->p_peername); } else { - snprintf(ebuf, sizeof(ebuf), "syslogd: %s logger \"%s\" " - "connection error: %s", p->p_ctx ? "tls" : "tcp", - p->p_peername, + log_info(LOG_NOTICE, "%s logger \"%s\" connection error: %s", + p->p_ctx ? "tls" : "tcp", p->p_peername, p->p_ctx ? tls_error(p->p_ctx) : strerror(errno)); - logmsg(LOG_SYSLOG|LOG_NOTICE, ebuf, LocalHostName, ADDDATE); } if (p->p_peername != hostname_unknown) @@ -1326,7 +1317,6 @@ void tcp_writecb(struct bufferevent *bufev, void *arg) { struct filed *f = arg; - char ebuf[ERRBUFSIZE]; /* * Successful write, connection to server is good, reset wait time. @@ -1336,13 +1326,11 @@ tcp_writecb(struct bufferevent *bufev, void *arg) if (f->f_un.f_forw.f_dropped > 0 && EVBUFFER_LENGTH(f->f_un.f_forw.f_bufev->output) < MAX_TCPBUF) { - snprintf(ebuf, sizeof(ebuf), - "syslogd: dropped %d message%s to loghost \"%s\"", + log_info(LOG_WARNING, "dropped %d message%s to loghost \"%s\"", f->f_un.f_forw.f_dropped, f->f_un.f_forw.f_dropped == 1 ? "" : "s", f->f_un.f_forw.f_loghost); f->f_un.f_forw.f_dropped = 0; - logmsg(LOG_SYSLOG|LOG_WARNING, ebuf, LocalHostName, ADDDATE); } } @@ -1355,12 +1343,11 @@ tcp_errorcb(struct bufferevent *bufev, short event, void *arg) char ebuf[ERRBUFSIZE]; if (event & EVBUFFER_EOF) - snprintf(ebuf, sizeof(ebuf), - "syslogd: loghost \"%s\" connection close", + snprintf(ebuf, sizeof(ebuf), "loghost \"%s\" connection close", f->f_un.f_forw.f_loghost); else snprintf(ebuf, sizeof(ebuf), - "syslogd: loghost \"%s\" connection error: %s", + "loghost \"%s\" connection error: %s", f->f_un.f_forw.f_loghost, f->f_un.f_forw.f_ctx ? tls_error(f->f_un.f_forw.f_ctx) : strerror(errno)); log_debug("%s", ebuf); @@ -1400,7 +1387,7 @@ tcp_errorcb(struct bufferevent *bufev, short event, void *arg) tcp_connect_retry(bufev, f); /* Log the connection error to the fresh buffer after reconnecting. */ - logmsg(LOG_SYSLOG|LOG_WARNING, ebuf, LocalHostName, ADDDATE); + log_info(LOG_WARNING, "%s", ebuf); } void @@ -1572,7 +1559,7 @@ printline(char *hname, char *msg) } line[MAXLINE] = *q = '\0'; - logmsg(pri, line, hname, 0); + logline(pri, 0, hname, line); } /* @@ -1603,7 +1590,7 @@ printsys(char *msg) while (*p && (c = *p++) != '\n' && q < &line[sizeof(line) - 4]) q = vis(q, c, 0, 0); - logmsg(pri, line, LocalHostName, flags); + logline(pri, flags, LocalHostName, line); } } @@ -1616,7 +1603,7 @@ vlogmsg(int pri, const char *proc, const char *fmt, va_list ap) l = snprintf(msg, sizeof(msg), "%s[%d]: ", proc, getpid()); if (l < sizeof(msg)) vsnprintf(msg + l, sizeof(msg) - l, fmt, ap); - logmsg(pri, msg, LocalHostName, ADDDATE); + logline(pri, ADDDATE, LocalHostName, msg); } struct timeval now; @@ -1626,14 +1613,14 @@ struct timeval now; * the priority. */ void -logmsg(int pri, char *msg, char *from, int flags) +logline(int pri, int flags, char *from, char *msg) { struct filed *f; int fac, msglen, prilev, i; char timestamp[33]; char prog[NAME_MAX+1]; - log_debug("logmsg: pri 0%o, flags 0x%x, from %s, msg %s", + log_debug("logline: pri 0%o, flags 0x%x, from %s, msg %s", pri, flags, from, msg); /* @@ -2162,20 +2149,15 @@ mark_timercb(int unused, short event, void *arg) void init_signalcb(int signum, short event, void *arg) { - char ebuf[ERRBUFSIZE]; - init(); - logmsg(LOG_SYSLOG|LOG_INFO, "syslogd: restart", - LocalHostName, ADDDATE); + log_info(LOG_INFO, "restart"); log_debug("syslogd: restarted"); if (tcpbuf_dropped > 0) { - snprintf(ebuf, sizeof(ebuf), - "syslogd: dropped %d message%s to remote loghost", + log_info(LOG_WARNING, "dropped %d message%s to remote loghost", tcpbuf_dropped, tcpbuf_dropped == 1 ? "" : "s"); tcpbuf_dropped = 0; - logmsg(LOG_SYSLOG|LOG_WARNING, ebuf, LocalHostName, ADDDATE); } } @@ -2190,7 +2172,6 @@ die(int signo) { struct filed *f; int was_initialized = Initialized; - char ebuf[ERRBUFSIZE]; Initialized = 0; /* Don't log SIGCHLDs */ SIMPLEQ_FOREACH(f, &Files, f_next) { @@ -2206,11 +2187,9 @@ die(int signo) Initialized = was_initialized; if (tcpbuf_dropped > 0) { - snprintf(ebuf, sizeof(ebuf), - "syslogd: dropped %d message%s to remote loghost", + log_info(LOG_WARNING, "dropped %d message%s to remote loghost", tcpbuf_dropped, tcpbuf_dropped == 1 ? "" : "s"); tcpbuf_dropped = 0; - logmsg(LOG_SYSLOG|LOG_WARNING, ebuf, LocalHostName, ADDDATE); } if (signo) @@ -2871,8 +2850,7 @@ markit(void) (void)gettimeofday(&now, NULL); MarkSeq += TIMERINTVL; if (MarkSeq >= MarkInterval) { - logmsg(LOG_INFO, "-- MARK --", - LocalHostName, ADDDATE|MARK); + logline(LOG_INFO, ADDDATE|MARK, LocalHostName, "-- MARK --"); MarkSeq = 0; } -- 2.20.1