1 /* $OpenBSD: errwarn.c,v 1.7 2004/05/04 22:23:01 mickey Exp $ */
3 /* Errors and warnings... */
6 * SPDX-License-Identifier: BSD-3-Clause
8 * Copyright (c) 1996 The Internet Software Consortium.
10 * Copyright (c) 1995 RadioMail Corporation. All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of RadioMail Corporation, the Internet Software
22 * Consortium nor the names of its contributors may be used to endorse
23 * or promote products derived from this software without specific
24 * prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET
27 * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
29 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
37 * OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This software was written for RadioMail Corporation by Ted Lemon
40 * under a contract with Vixie Enterprises. Further modifications have
41 * been made for the Internet Software Consortium under a contract
42 * with Vixie Laboratories.
45 #include <sys/cdefs.h>
46 __FBSDID("$FreeBSD$");
52 static void do_percentm(char *obuf, size_t size, const char *ibuf);
54 static char mbuf[1024];
55 static char fbuf[1024];
57 int warnings_occurred;
60 * Log an error message, then exit.
63 error(const char *fmt, ...)
67 do_percentm(fbuf, sizeof(fbuf), fmt);
70 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
74 cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
77 /* Also log it to stderr? */
79 write(2, mbuf, strlen(mbuf));
83 cap_syslog(capsyslog, LOG_CRIT, "exiting.");
85 fprintf(stderr, "exiting.\n");
89 pidfile_remove(pidfile);
94 * Log a warning message...
97 warning(const char *fmt, ...)
101 do_percentm(fbuf, sizeof(fbuf), fmt);
104 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
108 cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
112 write(2, mbuf, strlen(mbuf));
123 note(const char *fmt, ...)
127 do_percentm(fbuf, sizeof(fbuf), fmt);
130 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
134 cap_syslog(capsyslog, log_priority | LOG_INFO, "%s", mbuf);
138 write(2, mbuf, strlen(mbuf));
146 * Log a debug message...
149 debug(const char *fmt, ...)
153 do_percentm(fbuf, sizeof(fbuf), fmt);
156 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
160 cap_syslog(capsyslog, log_priority | LOG_DEBUG, "%s", mbuf);
164 write(2, mbuf, strlen(mbuf));
172 * Find %m in the input string and substitute an error message string.
175 do_percentm(char *obuf, size_t size, const char *ibuf)
178 const char *s = ibuf;
182 int saved_errno = errno;
185 * We wouldn't need this mess if printf handled %m, or if
186 * strerror() had been invented before syslog().
188 for (fmt_left = size; (ch = *s); ++s) {
189 if (ch == '%' && s[1] == 'm') {
191 prlen = snprintf(t, fmt_left, "%s",
192 strerror(saved_errno));
193 if (prlen >= fmt_left)
194 prlen = fmt_left - 1;
208 parse_warn(const char *fmt, ...)
211 static char spaces[] =
215 do_percentm(mbuf, sizeof(mbuf), fmt);
216 snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
218 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
222 cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", mbuf);
223 cap_syslog(capsyslog, log_priority | LOG_ERR, "%s", token_line);
225 cap_syslog(capsyslog, log_priority | LOG_ERR,
226 "%s^", &spaces[sizeof(spaces) - lexchar]);
230 write(2, mbuf, strlen(mbuf));
232 write(2, token_line, strlen(token_line));
234 write(2, spaces, lexchar - 1);
238 warnings_occurred = 1;