1*d131c537Schristos /* $NetBSD: log.c,v 1.3 2018/01/13 12:36:35 christos Exp $ */
2b985414bSchristos
3b985414bSchristos /*-
4b985414bSchristos * Copyright (c) 2017 The NetBSD Foundation, Inc.
5b985414bSchristos * Copyright (c) 2016 The DragonFly Project
6b985414bSchristos * Copyright (c) 2012 The FreeBSD Foundation
7b985414bSchristos * All rights reserved.
8b985414bSchristos *
9b985414bSchristos * This code is derived from software contributed to The NetBSD Foundation
10b985414bSchristos * by Tomohiro Kusumi <kusumi.tomohiro@gmail.com>.
11b985414bSchristos *
12b985414bSchristos * This software was developed by Edward Tomasz Napierala under sponsorship
13b985414bSchristos * from the FreeBSD Foundation.
14b985414bSchristos *
15b985414bSchristos * Redistribution and use in source and binary forms, with or without
16b985414bSchristos * modification, are permitted provided that the following conditions
17b985414bSchristos * are met:
18b985414bSchristos * 1. Redistributions of source code must retain the above copyright
19b985414bSchristos * notice, this list of conditions and the following disclaimer.
20b985414bSchristos * 2. Redistributions in binary form must reproduce the above copyright
21b985414bSchristos * notice, this list of conditions and the following disclaimer in the
22b985414bSchristos * documentation and/or other materials provided with the distribution.
23b985414bSchristos *
24b985414bSchristos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25b985414bSchristos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b985414bSchristos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b985414bSchristos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28b985414bSchristos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29b985414bSchristos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30b985414bSchristos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31b985414bSchristos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32b985414bSchristos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33b985414bSchristos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34b985414bSchristos * SUCH DAMAGE.
35b985414bSchristos *
36b985414bSchristos */
37b985414bSchristos #include <sys/cdefs.h>
38*d131c537Schristos __RCSID("$NetBSD: log.c,v 1.3 2018/01/13 12:36:35 christos Exp $");
39b985414bSchristos
40b985414bSchristos #include <errno.h>
41b985414bSchristos #include <stdarg.h>
42b985414bSchristos #include <stdio.h>
43b985414bSchristos #include <stdlib.h>
44b985414bSchristos #include <string.h>
45b985414bSchristos #include <syslog.h>
46b985414bSchristos #include <vis.h>
47b985414bSchristos
48b985414bSchristos #include "common.h"
49b985414bSchristos
50b985414bSchristos static int log_level = 0;
51b985414bSchristos static char *peer_name = NULL;
52b985414bSchristos static char *peer_addr = NULL;
53b985414bSchristos
54b985414bSchristos #define MSGBUF_LEN 1024
55b985414bSchristos
56b985414bSchristos void
log_init(int level)57b985414bSchristos log_init(int level)
58b985414bSchristos {
59b985414bSchristos
60b985414bSchristos log_level = level;
61b985414bSchristos openlog(getprogname(), LOG_NDELAY | LOG_PID, LOG_DAEMON);
62b985414bSchristos }
63b985414bSchristos
64b985414bSchristos void
log_set_peer_name(const char * name)65b985414bSchristos log_set_peer_name(const char *name)
66b985414bSchristos {
67b985414bSchristos
68b985414bSchristos /*
69b985414bSchristos * XXX: Turn it into assertion?
70b985414bSchristos */
71b985414bSchristos if (peer_name != NULL)
72b985414bSchristos log_errx(1, "%s called twice", __func__);
73b985414bSchristos if (peer_addr == NULL)
74b985414bSchristos log_errx(1, "%s called before log_set_peer_addr", __func__);
75b985414bSchristos
76b985414bSchristos peer_name = checked_strdup(name);
77b985414bSchristos }
78b985414bSchristos
79b985414bSchristos void
log_set_peer_addr(const char * addr)80b985414bSchristos log_set_peer_addr(const char *addr)
81b985414bSchristos {
82b985414bSchristos
83b985414bSchristos /*
84b985414bSchristos * XXX: Turn it into assertion?
85b985414bSchristos */
86b985414bSchristos if (peer_addr != NULL)
87b985414bSchristos log_errx(1, "%s called twice", __func__);
88b985414bSchristos
89b985414bSchristos peer_addr = checked_strdup(addr);
90b985414bSchristos }
91b985414bSchristos
9247cdb20dSchristos static __printflike(3, 0) void
log_common(int priority,int log_errno,const char * fmt,va_list ap)93b985414bSchristos log_common(int priority, int log_errno, const char *fmt, va_list ap)
94b985414bSchristos {
95b985414bSchristos static char msgbuf[MSGBUF_LEN];
96b985414bSchristos static char msgbuf_strvised[MSGBUF_LEN * 4 + 1];
97b985414bSchristos char *errstr;
98b985414bSchristos int ret;
99b985414bSchristos
100b985414bSchristos ret = vsnprintf(msgbuf, sizeof(msgbuf), fmt, ap);
101b985414bSchristos if (ret < 0) {
102b985414bSchristos fprintf(stderr, "%s: snprintf failed", getprogname());
103b985414bSchristos syslog(LOG_CRIT, "snprintf failed");
104*d131c537Schristos exit(EXIT_FAILURE);
105b985414bSchristos }
106b985414bSchristos
107b985414bSchristos ret = strnvis(msgbuf_strvised, sizeof(msgbuf_strvised), msgbuf, VIS_NL);
108b985414bSchristos if (ret < 0) {
109b985414bSchristos fprintf(stderr, "%s: strnvis failed", getprogname());
110b985414bSchristos syslog(LOG_CRIT, "strnvis failed");
111*d131c537Schristos exit(EXIT_FAILURE);
112b985414bSchristos }
113b985414bSchristos
114b985414bSchristos if (log_errno == -1) {
115b985414bSchristos if (peer_name != NULL) {
116b985414bSchristos fprintf(stderr, "%s: %s (%s): %s\n", getprogname(),
117b985414bSchristos peer_addr, peer_name, msgbuf_strvised);
118b985414bSchristos syslog(priority, "%s (%s): %s",
119b985414bSchristos peer_addr, peer_name, msgbuf_strvised);
120b985414bSchristos } else if (peer_addr != NULL) {
121b985414bSchristos fprintf(stderr, "%s: %s: %s\n", getprogname(),
122b985414bSchristos peer_addr, msgbuf_strvised);
123b985414bSchristos syslog(priority, "%s: %s",
124b985414bSchristos peer_addr, msgbuf_strvised);
125b985414bSchristos } else {
126b985414bSchristos fprintf(stderr, "%s: %s\n", getprogname(),
127b985414bSchristos msgbuf_strvised);
128b985414bSchristos syslog(priority, "%s", msgbuf_strvised);
129b985414bSchristos }
130b985414bSchristos
131b985414bSchristos } else {
132b985414bSchristos errstr = strerror(log_errno);
133b985414bSchristos
134b985414bSchristos if (peer_name != NULL) {
135b985414bSchristos fprintf(stderr, "%s: %s (%s): %s: %s\n", getprogname(),
136b985414bSchristos peer_addr, peer_name, msgbuf_strvised, errstr);
137b985414bSchristos syslog(priority, "%s (%s): %s: %s",
138b985414bSchristos peer_addr, peer_name, msgbuf_strvised, errstr);
139b985414bSchristos } else if (peer_addr != NULL) {
140b985414bSchristos fprintf(stderr, "%s: %s: %s: %s\n", getprogname(),
141b985414bSchristos peer_addr, msgbuf_strvised, errstr);
142b985414bSchristos syslog(priority, "%s: %s: %s",
143b985414bSchristos peer_addr, msgbuf_strvised, errstr);
144b985414bSchristos } else {
145b985414bSchristos fprintf(stderr, "%s: %s: %s\n", getprogname(),
146b985414bSchristos msgbuf_strvised, errstr);
147b985414bSchristos syslog(priority, "%s: %s",
148b985414bSchristos msgbuf_strvised, errstr);
149b985414bSchristos }
150b985414bSchristos }
151b985414bSchristos }
152b985414bSchristos
153*d131c537Schristos __dead void
log_err(int eval,const char * fmt,...)154b985414bSchristos log_err(int eval, const char *fmt, ...)
155b985414bSchristos {
156b985414bSchristos va_list ap;
157b985414bSchristos
158b985414bSchristos va_start(ap, fmt);
159b985414bSchristos log_common(LOG_CRIT, errno, fmt, ap);
160b985414bSchristos va_end(ap);
161b985414bSchristos
162b985414bSchristos exit(eval);
163b985414bSchristos }
164b985414bSchristos
165*d131c537Schristos __dead void
log_errx(int eval,const char * fmt,...)166b985414bSchristos log_errx(int eval, const char *fmt, ...)
167b985414bSchristos {
168b985414bSchristos va_list ap;
169b985414bSchristos
170b985414bSchristos va_start(ap, fmt);
171b985414bSchristos log_common(LOG_CRIT, -1, fmt, ap);
172b985414bSchristos va_end(ap);
173b985414bSchristos
174b985414bSchristos exit(eval);
175b985414bSchristos }
176b985414bSchristos
177b985414bSchristos void
log_warn(const char * fmt,...)178b985414bSchristos log_warn(const char *fmt, ...)
179b985414bSchristos {
180b985414bSchristos va_list ap;
181b985414bSchristos
182b985414bSchristos va_start(ap, fmt);
183b985414bSchristos log_common(LOG_WARNING, errno, fmt, ap);
184b985414bSchristos va_end(ap);
185b985414bSchristos }
186b985414bSchristos
187b985414bSchristos void
log_warnx(const char * fmt,...)188b985414bSchristos log_warnx(const char *fmt, ...)
189b985414bSchristos {
190b985414bSchristos va_list ap;
191b985414bSchristos
192b985414bSchristos va_start(ap, fmt);
193b985414bSchristos log_common(LOG_WARNING, -1, fmt, ap);
194b985414bSchristos va_end(ap);
195b985414bSchristos }
196b985414bSchristos
197b985414bSchristos void
log_debugx(const char * fmt,...)198b985414bSchristos log_debugx(const char *fmt, ...)
199b985414bSchristos {
200b985414bSchristos va_list ap;
201b985414bSchristos
202b985414bSchristos if (log_level == 0)
203b985414bSchristos return;
204b985414bSchristos
205b985414bSchristos va_start(ap, fmt);
206b985414bSchristos log_common(LOG_DEBUG, -1, fmt, ap);
207b985414bSchristos va_end(ap);
208b985414bSchristos }
209