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