13914ddf8SEdward Tomasz Napierala /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3abdd3945SEdward Tomasz Napierala *
43914ddf8SEdward Tomasz Napierala * Copyright (c) 2012 The FreeBSD Foundation
53914ddf8SEdward Tomasz Napierala *
63914ddf8SEdward Tomasz Napierala * This software was developed by Edward Tomasz Napierala under sponsorship
73914ddf8SEdward Tomasz Napierala * from the FreeBSD Foundation.
83914ddf8SEdward Tomasz Napierala *
93914ddf8SEdward Tomasz Napierala * Redistribution and use in source and binary forms, with or without
103914ddf8SEdward Tomasz Napierala * modification, are permitted provided that the following conditions
113914ddf8SEdward Tomasz Napierala * are met:
123914ddf8SEdward Tomasz Napierala * 1. Redistributions of source code must retain the above copyright
133914ddf8SEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer.
143914ddf8SEdward Tomasz Napierala * 2. Redistributions in binary form must reproduce the above copyright
153914ddf8SEdward Tomasz Napierala * notice, this list of conditions and the following disclaimer in the
163914ddf8SEdward Tomasz Napierala * documentation and/or other materials provided with the distribution.
173914ddf8SEdward Tomasz Napierala *
183914ddf8SEdward Tomasz Napierala * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
193914ddf8SEdward Tomasz Napierala * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
203914ddf8SEdward Tomasz Napierala * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
213914ddf8SEdward Tomasz Napierala * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
223914ddf8SEdward Tomasz Napierala * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
233914ddf8SEdward Tomasz Napierala * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
243914ddf8SEdward Tomasz Napierala * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
253914ddf8SEdward Tomasz Napierala * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
263914ddf8SEdward Tomasz Napierala * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
273914ddf8SEdward Tomasz Napierala * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
283914ddf8SEdward Tomasz Napierala * SUCH DAMAGE.
293914ddf8SEdward Tomasz Napierala *
303914ddf8SEdward Tomasz Napierala */
313914ddf8SEdward Tomasz Napierala
32925fd945SEdward Tomasz Napierala #include <sys/cdefs.h>
333914ddf8SEdward Tomasz Napierala #include <errno.h>
343914ddf8SEdward Tomasz Napierala #include <stdarg.h>
353914ddf8SEdward Tomasz Napierala #include <stdio.h>
363914ddf8SEdward Tomasz Napierala #include <stdlib.h>
373914ddf8SEdward Tomasz Napierala #include <string.h>
383914ddf8SEdward Tomasz Napierala #include <syslog.h>
393914ddf8SEdward Tomasz Napierala #include <vis.h>
403914ddf8SEdward Tomasz Napierala
413914ddf8SEdward Tomasz Napierala #include "common.h"
423914ddf8SEdward Tomasz Napierala
433914ddf8SEdward Tomasz Napierala static int log_level = 0;
443914ddf8SEdward Tomasz Napierala static char *peer_name = NULL;
453914ddf8SEdward Tomasz Napierala static char *peer_addr = NULL;
463914ddf8SEdward Tomasz Napierala
473914ddf8SEdward Tomasz Napierala #define MSGBUF_LEN 1024
483914ddf8SEdward Tomasz Napierala
493914ddf8SEdward Tomasz Napierala void
log_init(int level)503914ddf8SEdward Tomasz Napierala log_init(int level)
513914ddf8SEdward Tomasz Napierala {
523914ddf8SEdward Tomasz Napierala
533914ddf8SEdward Tomasz Napierala log_level = level;
543914ddf8SEdward Tomasz Napierala openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
553914ddf8SEdward Tomasz Napierala }
563914ddf8SEdward Tomasz Napierala
573914ddf8SEdward Tomasz Napierala void
log_set_peer_name(const char * name)583914ddf8SEdward Tomasz Napierala log_set_peer_name(const char *name)
593914ddf8SEdward Tomasz Napierala {
603914ddf8SEdward Tomasz Napierala
613914ddf8SEdward Tomasz Napierala /*
623914ddf8SEdward Tomasz Napierala * XXX: Turn it into assertion?
633914ddf8SEdward Tomasz Napierala */
643914ddf8SEdward Tomasz Napierala if (peer_name != NULL)
653914ddf8SEdward Tomasz Napierala log_errx(1, "%s called twice", __func__);
663914ddf8SEdward Tomasz Napierala if (peer_addr == NULL)
673914ddf8SEdward Tomasz Napierala log_errx(1, "%s called before log_set_peer_addr", __func__);
683914ddf8SEdward Tomasz Napierala
693914ddf8SEdward Tomasz Napierala peer_name = checked_strdup(name);
703914ddf8SEdward Tomasz Napierala }
713914ddf8SEdward Tomasz Napierala
723914ddf8SEdward Tomasz Napierala void
log_set_peer_addr(const char * addr)733914ddf8SEdward Tomasz Napierala log_set_peer_addr(const char *addr)
743914ddf8SEdward Tomasz Napierala {
753914ddf8SEdward Tomasz Napierala
763914ddf8SEdward Tomasz Napierala /*
773914ddf8SEdward Tomasz Napierala * XXX: Turn it into assertion?
783914ddf8SEdward Tomasz Napierala */
793914ddf8SEdward Tomasz Napierala if (peer_addr != NULL)
803914ddf8SEdward Tomasz Napierala log_errx(1, "%s called twice", __func__);
813914ddf8SEdward Tomasz Napierala
823914ddf8SEdward Tomasz Napierala peer_addr = checked_strdup(addr);
833914ddf8SEdward Tomasz Napierala }
843914ddf8SEdward Tomasz Napierala
853914ddf8SEdward Tomasz Napierala static void
log_common(int priority,int log_errno,const char * fmt,va_list ap)863914ddf8SEdward Tomasz Napierala log_common(int priority, int log_errno, const char *fmt, va_list ap)
873914ddf8SEdward Tomasz Napierala {
883914ddf8SEdward Tomasz Napierala static char msgbuf[MSGBUF_LEN];
893914ddf8SEdward Tomasz Napierala static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
90e19462c3SEdward Tomasz Napierala char *errstr;
913914ddf8SEdward Tomasz Napierala int ret;
923914ddf8SEdward Tomasz Napierala
933914ddf8SEdward Tomasz Napierala ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
943914ddf8SEdward Tomasz Napierala if (ret < 0) {
953914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: snprintf failed", getprogname());
963914ddf8SEdward Tomasz Napierala syslog(LOG_CRIT, "snprintf failed");
973914ddf8SEdward Tomasz Napierala exit(1);
983914ddf8SEdward Tomasz Napierala }
993914ddf8SEdward Tomasz Napierala
1003914ddf8SEdward Tomasz Napierala ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
1013914ddf8SEdward Tomasz Napierala if (ret < 0) {
1023914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: strnvis failed", getprogname());
1033914ddf8SEdward Tomasz Napierala syslog(LOG_CRIT, "strnvis failed");
1043914ddf8SEdward Tomasz Napierala exit(1);
1053914ddf8SEdward Tomasz Napierala }
1063914ddf8SEdward Tomasz Napierala
1073914ddf8SEdward Tomasz Napierala if (log_errno == -1) {
1083914ddf8SEdward Tomasz Napierala if (peer_name != NULL) {
1093914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
1103914ddf8SEdward Tomasz Napierala peer_addr, peer_name, msgbuf_strvised);
1113914ddf8SEdward Tomasz Napierala syslog(priority, "%s (%s): %s",
1123914ddf8SEdward Tomasz Napierala peer_addr, peer_name, msgbuf_strvised);
1133914ddf8SEdward Tomasz Napierala } else if (peer_addr != NULL) {
1143914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: %s: %s\n", getprogname(),
1153914ddf8SEdward Tomasz Napierala peer_addr, msgbuf_strvised);
1163914ddf8SEdward Tomasz Napierala syslog(priority, "%s: %s",
1173914ddf8SEdward Tomasz Napierala peer_addr, msgbuf_strvised);
1183914ddf8SEdward Tomasz Napierala } else {
1193914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: %s\n", getprogname(), msgbuf_strvised);
1203914ddf8SEdward Tomasz Napierala syslog(priority, "%s", msgbuf_strvised);
1213914ddf8SEdward Tomasz Napierala }
1223914ddf8SEdward Tomasz Napierala
1233914ddf8SEdward Tomasz Napierala } else {
124e19462c3SEdward Tomasz Napierala errstr = strerror(log_errno);
125e19462c3SEdward Tomasz Napierala
1263914ddf8SEdward Tomasz Napierala if (peer_name != NULL) {
1273914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
128e19462c3SEdward Tomasz Napierala peer_addr, peer_name, msgbuf_strvised, errstr);
1293914ddf8SEdward Tomasz Napierala syslog(priority, "%s (%s): %s: %s",
130e19462c3SEdward Tomasz Napierala peer_addr, peer_name, msgbuf_strvised, errstr);
1313914ddf8SEdward Tomasz Napierala } else if (peer_addr != NULL) {
1323914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
133e19462c3SEdward Tomasz Napierala peer_addr, msgbuf_strvised, errstr);
1343914ddf8SEdward Tomasz Napierala syslog(priority, "%s: %s: %s",
135e19462c3SEdward Tomasz Napierala peer_addr, msgbuf_strvised, errstr);
1363914ddf8SEdward Tomasz Napierala } else {
1373914ddf8SEdward Tomasz Napierala fprintf(stderr, "%s: %s: %s\n", getprogname(),
138e19462c3SEdward Tomasz Napierala msgbuf_strvised, errstr);
1393914ddf8SEdward Tomasz Napierala syslog(priority, "%s: %s",
140e19462c3SEdward Tomasz Napierala msgbuf_strvised, errstr);
1413914ddf8SEdward Tomasz Napierala }
1423914ddf8SEdward Tomasz Napierala }
1433914ddf8SEdward Tomasz Napierala }
1443914ddf8SEdward Tomasz Napierala
1453914ddf8SEdward Tomasz Napierala void
log_err(int eval,const char * fmt,...)1463914ddf8SEdward Tomasz Napierala log_err(int eval, const char *fmt, ...)
1473914ddf8SEdward Tomasz Napierala {
1483914ddf8SEdward Tomasz Napierala va_list ap;
1493914ddf8SEdward Tomasz Napierala
1503914ddf8SEdward Tomasz Napierala va_start(ap, fmt);
1513914ddf8SEdward Tomasz Napierala log_common(LOG_CRIT, errno, fmt, ap);
1523914ddf8SEdward Tomasz Napierala va_end(ap);
1533914ddf8SEdward Tomasz Napierala
1543914ddf8SEdward Tomasz Napierala exit(eval);
1553914ddf8SEdward Tomasz Napierala }
1563914ddf8SEdward Tomasz Napierala
1573914ddf8SEdward Tomasz Napierala void
log_errx(int eval,const char * fmt,...)1583914ddf8SEdward Tomasz Napierala log_errx(int eval, const char *fmt, ...)
1593914ddf8SEdward Tomasz Napierala {
1603914ddf8SEdward Tomasz Napierala va_list ap;
1613914ddf8SEdward Tomasz Napierala
1623914ddf8SEdward Tomasz Napierala va_start(ap, fmt);
1633914ddf8SEdward Tomasz Napierala log_common(LOG_CRIT, -1, fmt, ap);
1643914ddf8SEdward Tomasz Napierala va_end(ap);
1653914ddf8SEdward Tomasz Napierala
1663914ddf8SEdward Tomasz Napierala exit(eval);
1673914ddf8SEdward Tomasz Napierala }
1683914ddf8SEdward Tomasz Napierala
1693914ddf8SEdward Tomasz Napierala void
log_warn(const char * fmt,...)1703914ddf8SEdward Tomasz Napierala log_warn(const char *fmt, ...)
1713914ddf8SEdward Tomasz Napierala {
1723914ddf8SEdward Tomasz Napierala va_list ap;
1733914ddf8SEdward Tomasz Napierala
1743914ddf8SEdward Tomasz Napierala va_start(ap, fmt);
1753914ddf8SEdward Tomasz Napierala log_common(LOG_WARNING, errno, fmt, ap);
1763914ddf8SEdward Tomasz Napierala va_end(ap);
1773914ddf8SEdward Tomasz Napierala }
1783914ddf8SEdward Tomasz Napierala
1793914ddf8SEdward Tomasz Napierala void
log_warnx(const char * fmt,...)1803914ddf8SEdward Tomasz Napierala log_warnx(const char *fmt, ...)
1813914ddf8SEdward Tomasz Napierala {
1823914ddf8SEdward Tomasz Napierala va_list ap;
1833914ddf8SEdward Tomasz Napierala
1843914ddf8SEdward Tomasz Napierala va_start(ap, fmt);
1853914ddf8SEdward Tomasz Napierala log_common(LOG_WARNING, -1, fmt, ap);
1863914ddf8SEdward Tomasz Napierala va_end(ap);
1873914ddf8SEdward Tomasz Napierala }
1883914ddf8SEdward Tomasz Napierala
1893914ddf8SEdward Tomasz Napierala void
log_debugx(const char * fmt,...)1903914ddf8SEdward Tomasz Napierala log_debugx(const char *fmt, ...)
1913914ddf8SEdward Tomasz Napierala {
1923914ddf8SEdward Tomasz Napierala va_list ap;
1933914ddf8SEdward Tomasz Napierala
1943914ddf8SEdward Tomasz Napierala if (log_level == 0)
1953914ddf8SEdward Tomasz Napierala return;
1963914ddf8SEdward Tomasz Napierala
1973914ddf8SEdward Tomasz Napierala va_start(ap, fmt);
1983914ddf8SEdward Tomasz Napierala log_common(LOG_DEBUG, -1, fmt, ap);
1993914ddf8SEdward Tomasz Napierala va_end(ap);
2003914ddf8SEdward Tomasz Napierala }
201