1 /* $OpenBSD: errwarn.c,v 1.12 2005/07/09 14:36:15 krw Exp $ */
2
3 /* Errors and warnings... */
4
5 /*
6 * Copyright (c) 1996 The Internet Software Consortium.
7 * All Rights Reserved.
8 * Copyright (c) 1995 RadioMail Corporation. All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 *
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of RadioMail Corporation, the Internet Software
20 * Consortium nor the names of its contributors may be used to endorse
21 * or promote products derived from this software without specific
22 * prior written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY RADIOMAIL CORPORATION, THE INTERNET
25 * SOFTWARE CONSORTIUM AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
26 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL RADIOMAIL CORPORATION OR CONTRIBUTORS
29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * This software was written for RadioMail Corporation by Ted Lemon
38 * under a contract with Vixie Enterprises. Further modifications have
39 * been made for the Internet Software Consortium under a contract
40 * with Vixie Laboratories.
41 */
42
43 #include <sys/types.h>
44 #include <sys/uio.h>
45 #include <unistd.h>
46 #include <errno.h>
47
48 #include "dhcpd.h"
49
50 static void do_percentm(char *obuf, size_t size, char *ibuf);
51
52 static char mbuf[1024];
53 static char fbuf[1024];
54
55 int warnings_occurred;
56
57 /*
58 * Log an error message, then exit.
59 */
60 void
error(char * fmt,...)61 error(char *fmt, ...)
62 {
63 va_list list;
64
65 do_percentm(fbuf, sizeof(fbuf), fmt);
66
67 va_start(list, fmt);
68 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
69 va_end(list);
70
71 #ifndef DEBUG
72 syslog(LOG_ERR, "%s", mbuf);
73 #endif
74
75 /* Also log it to stderr? */
76 if (log_perror) {
77 write(STDERR_FILENO, mbuf, strlen(mbuf));
78 write(STDERR_FILENO, "\n", 1);
79 }
80
81 syslog(LOG_CRIT, "exiting.");
82 if (log_perror) {
83 fprintf(stderr, "exiting.\n");
84 fflush(stderr);
85 }
86 exit(1);
87 }
88
89 /*
90 * Log a warning message...
91 */
92 int
warning(char * fmt,...)93 warning(char *fmt, ...)
94 {
95 va_list list;
96
97 do_percentm(fbuf, sizeof(fbuf), fmt);
98
99 va_start(list, fmt);
100 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
101 va_end(list);
102
103 #ifndef DEBUG
104 syslog(LOG_ERR, "%s", mbuf);
105 #endif
106
107 if (log_perror) {
108 write(STDERR_FILENO, mbuf, strlen(mbuf));
109 write(STDERR_FILENO, "\n", 1);
110 }
111
112 return (0);
113 }
114
115 /*
116 * Log a note...
117 */
118 int
note(char * fmt,...)119 note(char *fmt, ...)
120 {
121 va_list list;
122
123 do_percentm(fbuf, sizeof(fbuf), fmt);
124
125 va_start(list, fmt);
126 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
127 va_end(list);
128
129 #ifndef DEBUG
130 syslog(LOG_INFO, "%s", mbuf);
131 #endif
132
133 if (log_perror) {
134 write(STDERR_FILENO, mbuf, strlen(mbuf));
135 write(STDERR_FILENO, "\n", 1);
136 }
137
138 return (0);
139 }
140
141 /*
142 * Log a debug message...
143 */
144 int
debug(char * fmt,...)145 debug(char *fmt, ...)
146 {
147 va_list list;
148
149 do_percentm(fbuf, sizeof(fbuf), fmt);
150
151 va_start(list, fmt);
152 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
153 va_end(list);
154
155 #ifndef DEBUG
156 syslog(LOG_DEBUG, "%s", mbuf);
157 #endif
158
159 if (log_perror) {
160 write(STDERR_FILENO, mbuf, strlen(mbuf));
161 write(STDERR_FILENO, "\n", 1);
162 }
163
164 return (0);
165 }
166
167 /*
168 * Find %m in the input string and substitute an error message string.
169 */
170 static void
do_percentm(char * obuf,size_t size,char * ibuf)171 do_percentm(char *obuf, size_t size, char *ibuf)
172 {
173 char ch;
174 char *s = ibuf;
175 char *t = obuf;
176 int prlen;
177 size_t fmt_left;
178 int saved_errno = errno;
179
180 /*
181 * We wouldn't need this mess if printf handled %m, or if
182 * strerror() had been invented before syslog().
183 */
184 for (fmt_left = size; (ch = *s); ++s) {
185 if (ch == '%' && s[1] == 'm') {
186 ++s;
187 prlen = snprintf(t, fmt_left, "%s",
188 strerror(saved_errno));
189 if (prlen == -1)
190 prlen = 0;
191 else if (prlen >= fmt_left)
192 prlen = fmt_left - 1;
193 t += prlen;
194 fmt_left -= prlen;
195 } else {
196 if (fmt_left > 1) {
197 *t++ = ch;
198 fmt_left--;
199 }
200 }
201 }
202 *t = '\0';
203 }
204
205 int
parse_warn(char * fmt,...)206 parse_warn(char *fmt, ...)
207 {
208 va_list list;
209 static char spaces[] =
210 " "
211 " "; /* 80 spaces */
212 struct iovec iov[6];
213
214 do_percentm(mbuf, sizeof(mbuf), fmt);
215 snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
216 va_start(list, fmt);
217 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
218 va_end(list);
219
220 #ifndef DEBUG
221 syslog(LOG_ERR, "%s", mbuf);
222 syslog(LOG_ERR, "%s", token_line);
223 if (lexline < 81)
224 syslog(LOG_ERR,
225 "%s^", &spaces[sizeof(spaces) - lexchar]);
226 #endif
227
228 if (log_perror) {
229 iov[0].iov_base = mbuf;
230 iov[0].iov_len = strlen(mbuf);
231 iov[1].iov_base = "\n";
232 iov[1].iov_len = 1;
233 iov[2].iov_base = token_line;
234 iov[2].iov_len = strlen(token_line);
235 iov[3].iov_base = "\n";
236 iov[3].iov_len = 1;
237 iov[4].iov_base = spaces;
238 iov[4].iov_len = lexchar - 1;
239 iov[5].iov_base = "\n";
240 iov[5].iov_len = 1;
241 writev(STDERR_FILENO, iov, sizeof(iov)/sizeof(iov[0]));
242 }
243 warnings_occurred = 1;
244 return (0);
245 }
246