Hi,
Instead of printing errno strings here and there, add a logerrorx()
that does not do that. Use it for anything that does not look like
a system call or library call around it.
Also add logerrorctx() that prints the TLS error instead.
ok?
bluhm
Index: usr.sbin/syslogd/syslogd.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.187
diff -u -p -U5 -r1.187 syslogd.c
--- usr.sbin/syslogd/syslogd.c 11 Sep 2015 12:42:12 -0000 1.187
+++ usr.sbin/syslogd/syslogd.c 11 Sep 2015 20:50:31 -0000
@@ -317,10 +317,13 @@ int decode(const char *, const CODE *);
void die(int);
void markit(void);
void fprintlog(struct filed *, int, char *);
void init(void);
void logerror(const char *);
+void logerrorx(const char *);
+void logerrorctx(const char *, struct tls *);
+void logerror_reason(const char *, const char *);
void logmsg(int, char *, char *, int);
struct filed *find_dup(struct filed *);
void printline(char *, char *);
void printsys(char *);
char *ttymsg(struct iovec *, int, char *, int);
@@ -448,27 +451,27 @@ main(int argc, char *argv[])
}
if (socket_bind("udp", NULL, "syslog", SecureMode,
&fd_udp, &fd_udp6) == -1) {
errno = 0;
- logerror("socket bind *");
+ logerrorx("socket bind *");
if (!Debug)
die(0);
}
fd_bind = -1;
if (bind_host && socket_bind("udp", bind_host, bind_port, 0,
&fd_bind, &fd_bind) == -1) {
errno = 0;
- logerror("socket bind udp");
+ logerrorx("socket bind udp");
if (!Debug)
die(0);
}
fd_listen = -1;
if (listen_host && socket_bind("tcp", listen_host, listen_port, 0,
&fd_listen, &fd_listen) == -1) {
errno = 0;
- logerror("socket listen tcp");
+ logerrorx("socket listen tcp");
if (!Debug)
die(0);
}
#ifndef SUN_LEN
@@ -515,11 +518,11 @@ main(int argc, char *argv[])
dprintf("LIOCSFD errno %d\n", errno);
}
close(pair[1]);
if (tls_init() == -1) {
- logerror("tls_init");
+ logerrorx("tls_init");
} else if ((tlsconfig = tls_config_new()) == NULL) {
logerror("tls_config_new");
} else if (NoVerify) {
tls_config_insecure_noverifycert(tlsconfig);
tls_config_insecure_noverifyname(tlsconfig);
@@ -532,18 +535,18 @@ main(int argc, char *argv[])
if ((fd = open(CAfile, O_RDONLY)) == -1) {
logerror("open CAfile");
} else if (fstat(fd, &sb) == -1) {
logerror("fstat CAfile");
} else if (sb.st_size > 1024*1024*1024) {
- logerror("CAfile larger than 1GB");
+ logerrorx("CAfile larger than 1GB");
} else if ((p = calloc(sb.st_size, 1)) == NULL) {
logerror("calloc CAfile");
} else if (read(fd, p, sb.st_size) != sb.st_size) {
logerror("read CAfile");
} else if (tls_config_set_ca_mem(tlsconfig, p, sb.st_size)
== -1) {
- logerror("tls_config_set_ca_mem");
+ logerrorx("tls_config_set_ca_mem");
} else {
dprintf("CAfile %s, size %lld\n", CAfile, sb.st_size);
}
free(p);
close(fd);
@@ -738,11 +741,11 @@ socket_bind(const char *proto, const cha
if ((error = getaddrinfo(host, port, &hints, &res0))) {
snprintf(ebuf, sizeof(ebuf), "getaddrinfo "
"proto %s, host %s, port %s: %s",
proto, host ? host : "*", port, gai_strerror(error));
- logerror(ebuf);
+ logerrorx(ebuf);
die(0);
}
for (res = res0; res; res = res->ai_next) {
switch (res->ai_family) {
@@ -1259,24 +1262,25 @@ tcp_connectcb(int fd, short event, void
if (f->f_type == F_FORWTLS) {
if ((f->f_un.f_forw.f_ctx = tls_client()) == NULL) {
snprintf(ebuf, sizeof(ebuf), "tls_client \"%s\"",
f->f_un.f_forw.f_loghost);
+ logerror(ebuf);
goto error;
}
if (tlsconfig &&
tls_configure(f->f_un.f_forw.f_ctx, tlsconfig) == -1) {
- snprintf(ebuf, sizeof(ebuf), "tls_configure "
- "\"%s\": %s", f->f_un.f_forw.f_loghost,
- tls_error(f->f_un.f_forw.f_ctx));
+ snprintf(ebuf, sizeof(ebuf), "tls_configure \"%s\"",
+ f->f_un.f_forw.f_loghost);
+ logerrorctx(ebuf, f->f_un.f_forw.f_ctx);
goto error;
}
if (tls_connect_socket(f->f_un.f_forw.f_ctx, s,
f->f_un.f_forw.f_host) == -1) {
snprintf(ebuf, sizeof(ebuf), "tls_connect_socket "
- "\"%s\": %s", f->f_un.f_forw.f_loghost,
- tls_error(f->f_un.f_forw.f_ctx));
+ "\"%s\"", f->f_un.f_forw.f_loghost);
+ logerrorctx(ebuf, f->f_un.f_forw.f_ctx);
goto error;
}
dprintf("tcp connect callback: tls context success\n");
buffertls_set(&f->f_un.f_forw.f_buftls, bufev,
@@ -1285,11 +1289,10 @@ tcp_connectcb(int fd, short event, void
}
return;
error:
- logerror(ebuf);
if (f->f_un.f_forw.f_ctx) {
tls_free(f->f_un.f_forw.f_ctx);
f->f_un.f_forw.f_ctx = NULL;
}
close(f->f_file);
@@ -1732,20 +1735,20 @@ fprintlog(struct filed *f, int flags, ch
!retryonce) {
f->f_file = priv_open_tty(f->f_un.f_fname);
retryonce = 1;
if (f->f_file < 0) {
f->f_type = F_UNUSED;
- logerror(f->f_un.f_fname);
+ logerrorx(f->f_un.f_fname);
} else
goto again;
} else if ((e == EPIPE || e == EBADF) &&
f->f_type == F_PIPE && !retryonce) {
f->f_file = priv_open_log(f->f_un.f_fname);
retryonce = 1;
if (f->f_file < 0) {
f->f_type = F_UNUSED;
- logerror(f->f_un.f_fname);
+ logerrorx(f->f_un.f_fname);
} else
goto again;
} else {
f->f_type = F_UNUSED;
f->f_file = -1;
@@ -1794,11 +1797,11 @@ wallmsg(struct filed *f, struct iovec *i
int i;
if (reenter++)
return;
if ((uf = priv_open_utmp()) == NULL) {
- logerror(_PATH_UTMP);
+ logerrorx(_PATH_UTMP);
reenter = 0;
return;
}
/* NOSTRICT */
while (fread((char *)&ut, sizeof(ut), 1, uf) == 1) {
@@ -1806,27 +1809,23 @@ wallmsg(struct filed *f, struct iovec *i
continue;
/* must use strncpy since ut_* may not be NUL terminated */
strncpy(line, ut.ut_line, sizeof(line) - 1);
line[sizeof(line) - 1] = '\0';
if (f->f_type == F_WALL) {
- if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL) {
- errno = 0; /* already in msg */
- logerror(p);
- }
+ if ((p = ttymsg(iov, 6, line, TTYMSGTIME)) != NULL)
+ logerrorx(p);
continue;
}
/* should we send the message to this user? */
for (i = 0; i < MAXUNAMES; i++) {
if (!f->f_un.f_uname[i][0])
break;
if (!strncmp(f->f_un.f_uname[i], ut.ut_name,
UT_NAMESIZE)) {
if ((p = ttymsg(iov, 6, line, TTYMSGTIME))
- != NULL) {
- errno = 0; /* already in msg */
- logerror(p);
- }
+ != NULL)
+ logerrorx(p);
break;
}
}
}
(void)fclose(uf);
@@ -1887,19 +1886,37 @@ init_signalcb(int signum, short event, v
/*
* Print syslogd errors some place.
*/
void
-logerror(const char *type)
+logerror(const char *message)
+{
+ logerror_reason(message, errno ? strerror(errno) : NULL);
+}
+
+void
+logerrorx(const char *message)
+{
+ logerror_reason(message, NULL);
+}
+
+void
+logerrorctx(const char *message, struct tls *ctx)
+{
+ logerror_reason(message, ctx ? tls_error(ctx) : NULL);
+}
+
+void
+logerror_reason(const char *message, const char *reason)
{
char ebuf[ERRBUFSIZE];
- if (errno)
+ if (reason)
(void)snprintf(ebuf, sizeof(ebuf), "syslogd: %s: %s",
- type, strerror(errno));
+ message, reason);
else
- (void)snprintf(ebuf, sizeof(ebuf), "syslogd: %s", type);
+ (void)snprintf(ebuf, sizeof(ebuf), "syslogd: %s", message);
errno = 0;
dprintf("%s\n", ebuf);
if (Startup)
fprintf(stderr, "%s\n", ebuf);
else
@@ -1936,12 +1953,11 @@ die(int signo)
if (signo) {
dprintf("syslogd: exiting on signal %d\n", signo);
(void)snprintf(ebuf, sizeof(ebuf), "exiting on signal %d",
signo);
- errno = 0;
- logerror(ebuf);
+ logerrorx(ebuf);
}
dprintf("[unpriv] syslogd child about to exit\n");
exit(0);
}
@@ -2105,11 +2121,11 @@ init(void)
/* make sure remaining buffers are freed */
while (!SIMPLEQ_EMPTY(&mb)) {
m = SIMPLEQ_FIRST(&mb);
SIMPLEQ_REMOVE_HEAD(&mb, f_next);
if (m->f_un.f_mb.f_rb != NULL) {
- logerror("Mismatched membuf");
+ logerrorx("Mismatched membuf");
ringbuf_free(m->f_un.f_mb.f_rb);
}
dprintf("Freeing membuf %p\n", m);
free(m);
@@ -2216,12 +2232,10 @@ cfline(char *line, char *progblock, char
struct timeval to;
dprintf("cfline(\"%s\", f, \"%s\", \"%s\")\n",
line, progblock, hostblock);
- errno = 0; /* keep strerror() stuff out of logerror messages */
-
if ((f = calloc(1, sizeof(*f))) == NULL) {
logerror("Couldn't allocate struct filed");
die(0);
}
for (i = 0; i <= LOG_NFACILITIES; i++)
@@ -2269,11 +2283,11 @@ cfline(char *line, char *progblock, char
pri = decode(buf, prioritynames);
if (pri < 0) {
(void)snprintf(ebuf, sizeof ebuf,
"unknown priority name \"%s\"", buf);
- logerror(ebuf);
+ logerrorx(ebuf);
free(f);
return (NULL);
}
}
@@ -2289,11 +2303,11 @@ cfline(char *line, char *progblock, char
i = decode(buf, facilitynames);
if (i < 0) {
(void)snprintf(ebuf, sizeof(ebuf),
"unknown facility name \"%s\"",
buf);
- logerror(ebuf);
+ logerrorx(ebuf);
free(f);
return (NULL);
}
f->f_pmask[i >> 3] = pri;
}
@@ -2313,17 +2327,17 @@ cfline(char *line, char *progblock, char
if ((strlcpy(f->f_un.f_forw.f_loghost, p,
sizeof(f->f_un.f_forw.f_loghost)) >=
sizeof(f->f_un.f_forw.f_loghost))) {
snprintf(ebuf, sizeof(ebuf), "loghost too long \"%s\"",
p);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
if (loghost_parse(++p, &proto, &host, &port) == -1) {
snprintf(ebuf, sizeof(ebuf), "bad loghost \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
if (proto == NULL)
proto = "udp";
ipproto = proto;
@@ -2335,18 +2349,18 @@ cfline(char *line, char *progblock, char
ipproto = proto;
} else if (strcmp(proto, "udp4") == 0) {
if (fd_udp == -1) {
snprintf(ebuf, sizeof(ebuf), "no udp4 \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
} else if (strcmp(proto, "udp6") == 0) {
if (fd_udp6 == -1) {
snprintf(ebuf, sizeof(ebuf), "no udp6 \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
} else if (strcmp(proto, "tcp") == 0 ||
strcmp(proto, "tcp4") == 0 || strcmp(proto, "tcp6") == 0) {
;
@@ -2357,34 +2371,34 @@ cfline(char *line, char *progblock, char
} else if (strcmp(proto, "tls6") == 0) {
ipproto = "tcp6";
} else {
snprintf(ebuf, sizeof(ebuf), "bad protocol \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
if (strlen(host) >= NI_MAXHOST) {
snprintf(ebuf, sizeof(ebuf), "host too long \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
if (port == NULL)
port = strncmp(proto, "tls", 3) == 0 ?
"syslog-tls" : "syslog";
if (strlen(port) >= NI_MAXSERV) {
snprintf(ebuf, sizeof(ebuf), "port too long \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
if (priv_getaddrinfo(ipproto, host, port,
(struct sockaddr*)&f->f_un.f_forw.f_addr,
sizeof(f->f_un.f_forw.f_addr)) != 0) {
snprintf(ebuf, sizeof(ebuf), "bad hostname \"%s\"",
f->f_un.f_forw.f_loghost);
- logerror(ebuf);
+ logerrorx(ebuf);
break;
}
f->f_file = -1;
if (strncmp(proto, "udp", 3) == 0) {
switch (f->f_un.f_forw.f_addr.ss_family) {
@@ -2441,11 +2455,11 @@ cfline(char *line, char *progblock, char
f->f_file = priv_open_tty(p);
else
f->f_file = priv_open_log(p);
if (f->f_file < 0) {
f->f_type = F_UNUSED;
- logerror(p);
+ logerrorx(p);
break;
}
if (isatty(f->f_file)) {
if (strcmp(p, ctty) == 0)
f->f_type = F_CONSOLE;
@@ -2496,11 +2510,11 @@ cfline(char *line, char *progblock, char
xf = find_dup(f);
/* Error on missing or non-unique name, or bad buffer length */
if (i == 0 || rb_len > MAX_MEMBUF || xf != NULL) {
f->f_type = F_UNUSED;
- logerror(p);
+ logerrorx(p);
break;
}
/* Set buffer length */
rb_len = MAXIMUM(rb_len, MIN_MEMBUF);
@@ -2639,11 +2653,11 @@ unix_socket(char *path, int type, mode_t
memset(&s_un, 0, sizeof(s_un));
s_un.sun_family = AF_UNIX;
if (strlcpy(s_un.sun_path, path, sizeof(s_un.sun_path)) >=
sizeof(s_un.sun_path)) {
snprintf(ebuf, sizeof(ebuf), "socket path too long: %s", path);
- logerror(ebuf);
+ logerrorx(ebuf);
die(0);
}
if ((fd = socket(AF_UNIX, type, 0)) == -1) {
logerror("socket");
@@ -2808,18 +2822,18 @@ ctlconn_readcb(int fd, short event, void
}
if (ctl_cmd_bytes < sizeof(ctl_cmd))
return;
if (ntohl(ctl_cmd.version) != CTL_VERSION) {
- logerror("Unknown client protocol version");
+ logerrorx("Unknown client protocol version");
ctlconn_cleanup();
return;
}
/* Ensure that logname is \0 terminated */
if (memchr(ctl_cmd.logname, '\0', sizeof(ctl_cmd.logname)) == NULL) {
- logerror("Corrupt ctlsock command");
+ logerrorx("Corrupt ctlsock command");
ctlconn_cleanup();
return;
}
*reply_text = '\0';
@@ -2883,11 +2897,11 @@ ctlconn_readcb(int fd, short event, void
}
}
strlcat(reply_text, "\n", MAX_MEMBUF);
break;
default:
- logerror("Unsupported ctlsock command");
+ logerrorx("Unsupported ctlsock command");
ctlconn_cleanup();
return;
}
reply_hdr->version = htonl(CTL_VERSION);
reply_hdr->flags = htonl(flags);
@@ -2913,11 +2927,11 @@ ctlconn_writecb(int fd, short event, voi
ssize_t n;
if (!(ctl_state == CTL_WRITING_REPLY ||
ctl_state == CTL_WRITING_CONT_REPLY)) {
/* Shouldn't be here! */
- logerror("ctlconn_write with bad ctl_state");
+ logerrorx("ctlconn_write with bad ctl_state");
ctlconn_cleanup();
return;
}
retry: