Use new log macros and err.h fns

Signed-off-by: Joachim Nilsson <troglobit@gmail.com>
This commit is contained in:
Joachim Nilsson 2019-11-12 08:56:06 +01:00
parent 53c2d0e3d9
commit c66411e8c2
2 changed files with 23 additions and 35 deletions

View File

@ -42,8 +42,8 @@
#include <sys/socket.h> #include <sys/socket.h>
#include <sys/types.h> #include <sys/types.h>
#include "log.h"
#include "queue.h" #include "queue.h"
#include "syslogd.h"
struct sock { struct sock {
LIST_ENTRY(sock) link; LIST_ENTRY(sock) link;
@ -144,7 +144,7 @@ int socket_poll(struct timeval *timeout)
if (num <= 0) { if (num <= 0) {
/* Log all errors, except when signalled, ignore failures. */ /* Log all errors, except when signalled, ignore failures. */
if (num < 0 && EINTR != errno) if (num < 0 && EINTR != errno)
smclog(LOG_WARNING, "Failed select(): %s", strerror(errno)); WARN("Failed select(): %s", strerror(errno));
return num; return num;
} }

View File

@ -67,6 +67,7 @@ static char sccsid[] __attribute__((unused)) =
#include <utmp.h> #include <utmp.h>
#include <errno.h> #include <errno.h>
#include <err.h>
#include <fcntl.h> #include <fcntl.h>
#include <signal.h> #include <signal.h>
#include <sys/file.h> #include <sys/file.h>
@ -279,8 +280,7 @@ int main(int argc, char *argv[])
case 'l': case 'l':
if (LocalHosts) { if (LocalHosts) {
fprintf(stderr, "Only one -l argument allowed," warnx("Only one -l argument allowed, the first one is taken.");
"the first one is taken.\n");
break; break;
} }
LocalHosts = crunch_list(optarg); LocalHosts = crunch_list(optarg);
@ -476,7 +476,7 @@ int main(int argc, char *argv[])
init(); init();
if (pidfile(PidFile)) if (pidfile(PidFile))
flog(LOG_SYSLOG | LOG_ERR, "Failed writing %s", PidFile); ERR("Failed writing %s", PidFile);
continue; continue;
} }
@ -486,7 +486,7 @@ int main(int argc, char *argv[])
} }
if (nfds < 0) { if (nfds < 0) {
if (errno != EINTR) if (errno != EINTR)
logerror("select"); ERR("select()");
continue; continue;
} }
@ -1435,9 +1435,7 @@ void logrotate(struct filed *f)
f->f_file = open(f->f_un.f_fname, O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK | O_NOCTTY, 0644); f->f_file = open(f->f_un.f_fname, O_WRONLY | O_APPEND | O_CREAT | O_NONBLOCK | O_NOCTTY, 0644);
if (f->f_file < 0) { if (f->f_file < 0) {
f->f_type = F_UNUSED; f->f_type = F_UNUSED;
logerror("Failed re-opening log file after rotation"); ERR("Failed re-opening log file %s after rotation", f->f_un.f_fname);
errno = 0;
logerror(f->f_un.f_fname);
return; return;
} }
} }
@ -1645,18 +1643,16 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
logit("Failure resolving %s:%s: %s\n", host, serv, gai_strerror(err)); logit("Failure resolving %s:%s: %s\n", host, serv, gai_strerror(err));
logit("Retries: %d\n", f->f_prevcount); logit("Retries: %d\n", f->f_prevcount);
if (--f->f_prevcount < 0) { if (--f->f_prevcount < 0) {
flog(LOG_SYSLOG | LOG_WARN, "Still cannot find %s, " WARN("Still cannot find %s, giving up: %s",
"giving up: %s", host, gai_strerror(err)); host, gai_strerror(err));
logit("Giving up.\n");
f->f_type = F_UNUSED; f->f_type = F_UNUSED;
} else { } else {
flog(LOG_SYSLOG | LOG_WARN, "Still cannot find %s, " WARN("Still cannot find %s, will try again later: %s",
"will try again later: %s", host, gai_strerror(err)); host, gai_strerror(err));
logit("Left retries: %d\n", f->f_prevcount); logit("Left retries: %d\n", f->f_prevcount);
} }
} else { } else {
flog(LOG_SYSLOG | LOG_NOTICE, "Found %s, resuming operation.", host); NOTE("Found %s, resuming operation.", host);
logit("%s found, resuming.\n", host);
f->f_un.f_forw.f_addr = ai; f->f_un.f_forw.f_addr = ai;
f->f_prevcount = 0; f->f_prevcount = 0;
f->f_type = F_FORW; f->f_type = F_FORW;
@ -1719,11 +1715,9 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
break; break;
} }
if (err != -1) { if (err != -1) {
logit("INET sendto error: %d = %s.\n",
err, strerror(err));
f->f_type = F_FORW_SUSP; f->f_type = F_FORW_SUSP;
errno = err; errno = err;
logerror("sendto"); ERR("INET sendto()");
} }
} }
break; break;
@ -1783,7 +1777,7 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
f->f_file = open(f->f_un.f_fname, O_WRONLY | O_APPEND | O_NOCTTY); f->f_file = open(f->f_un.f_fname, O_WRONLY | O_APPEND | O_NOCTTY);
if (f->f_file < 0) { if (f->f_file < 0) {
f->f_type = F_UNUSED; f->f_type = F_UNUSED;
logerror(f->f_un.f_fname); ERR("Failed writing and re-opening %s", f->f_un.f_fname);
} else { } else {
untty(); untty();
goto again; goto again;
@ -1791,7 +1785,7 @@ void fprintlog(struct filed *f, struct buf_msg *buffer)
} else { } else {
f->f_type = F_UNUSED; f->f_type = F_UNUSED;
errno = e; errno = e;
logerror(f->f_un.f_fname); ERR("Failed writing to %s", f->f_un.f_fname);
} }
} else if (f->f_type == F_FILE && (f->f_flags & SYNC_FILE)) } else if (f->f_type == F_FILE && (f->f_flags & SYNC_FILE))
(void)fsync(f->f_file); (void)fsync(f->f_file);
@ -2005,7 +1999,7 @@ const char *cvthname(struct sockaddr_storage *f, int len)
} }
/* /*
* Base function for domark(), logerror(), etc. * Base function for domark(), ERR(), etc.
*/ */
void flog(int pri, char *fmt, ...) void flog(int pri, char *fmt, ...)
{ {
@ -2018,8 +2012,9 @@ void flog(int pri, char *fmt, ...)
(void)vsnprintf(buf, sizeof(buf), fmt, ap); (void)vsnprintf(buf, sizeof(buf), fmt, ap);
va_end(ap); va_end(ap);
(void)snprintf(proc_id, sizeof(proc_id), "%d", getpid()); logit("%s\n", buf);
(void)snprintf(proc_id, sizeof(proc_id), "%d", getpid());
memset(&buffer, 0, sizeof(buffer)); memset(&buffer, 0, sizeof(buffer));
buffer.hostname = LocalHostName; buffer.hostname = LocalHostName;
buffer.app_name = "syslogd"; buffer.app_name = "syslogd";
@ -2462,7 +2457,6 @@ static struct filed *cfline(char *line)
struct addrinfo *ai; struct addrinfo *ai;
struct filed *f; struct filed *f;
char buf[MAXLINE]; char buf[MAXLINE];
char xbuf[MAXLINE + 24];
char *p, *q, *bp; char *p, *q, *bp;
int ignorepri = 0; int ignorepri = 0;
int singlpri = 0; int singlpri = 0;
@ -2473,12 +2467,10 @@ static struct filed *cfline(char *line)
f = calloc(1, sizeof(*f)); f = calloc(1, sizeof(*f));
if (!f) { if (!f) {
logerror("Cannot allocate memory for log file"); ERR("Cannot allocate memory for log file");
return NULL; return NULL;
} }
errno = 0; /* keep strerror() stuff out of logerror messages */
/* default rotate from command line */ /* default rotate from command line */
f->f_rotatecount = RotateCnt; f->f_rotatecount = RotateCnt;
f->f_rotatesz = RotateSz; f->f_rotatesz = RotateSz;
@ -2517,8 +2509,7 @@ static struct filed *cfline(char *line)
} }
if (pri < 0) { if (pri < 0) {
(void)snprintf(xbuf, sizeof(xbuf), "unknown priority name \"%s\"", buf); ERRX("unknown priority name \"%s\"", buf);
logerror(xbuf);
free(f); free(f);
return NULL; return NULL;
@ -2560,8 +2551,7 @@ static struct filed *cfline(char *line)
} else { } else {
i = decode(buf, facilitynames); i = decode(buf, facilitynames);
if (i < 0) { if (i < 0) {
(void)snprintf(xbuf, sizeof(xbuf), "unknown facility name \"%s\"", buf); ERR("unknown facility name \"%s\"", buf);
logerror(xbuf);
free(f); free(f);
return NULL; return NULL;
@ -2627,8 +2617,7 @@ static struct filed *cfline(char *line)
err = nslookup(p, bp, &ai); err = nslookup(p, bp, &ai);
if (err) { if (err) {
flog(LOG_SYSLOG | LOG_WARN, "Cannot find %s, " WARN("Cannot find %s, will try again later: %s", p, gai_strerror(err));
"will try again later: %s", p, gai_strerror(err));
/* /*
* The host might be unknown due to an inaccessible * The host might be unknown due to an inaccessible
* nameserver (perhaps on the same host). We try to * nameserver (perhaps on the same host). We try to
@ -2663,8 +2652,7 @@ static struct filed *cfline(char *line)
if (f->f_file < 0) { if (f->f_file < 0) {
f->f_file = -1; f->f_file = -1;
logit("Error opening log file: %s\n", p); ERR("Error opening log file: %s", p);
logerror(p);
break; break;
} }
if (isatty(f->f_file)) { if (isatty(f->f_file)) {