hammer - Fix bugs, fix serious snapshot bug, flush adjustments
[dragonfly.git] / sbin / dhclient / errwarn.c
blob100727b57103a456264d03dce0746dc677c5f251
1 /* $OpenBSD: src/sbin/dhclient/errwarn.c,v 1.17 2009/11/26 23:14:29 krw Exp $ */
3 /* Errors and warnings... */
5 /*
6 * Copyright (c) 1996 The Internet Software Consortium.
7 * All Rights Reserved.
8 * Copyright (c) 1995 RadioMail Corporation. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
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.
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.
43 #include <sys/types.h>
44 #include <sys/uio.h>
46 #include <errno.h>
47 #include <unistd.h>
49 #include "dhcpd.h"
51 static void do_percentm(char *obuf, size_t size, char *ibuf);
53 static char mbuf[1024];
54 static char fbuf[1024];
56 int warnings_occurred;
59 * Log an error message, then exit.
61 void
62 error(char *fmt, ...)
64 va_list list;
66 do_percentm(fbuf, sizeof(fbuf), fmt);
68 va_start(list, fmt);
69 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
70 va_end(list);
72 #ifndef DEBUG
73 syslog(LOG_ERR, "%s", mbuf);
74 #endif
76 /* Also log it to stderr? */
77 if (log_perror) {
78 write(STDERR_FILENO, mbuf, strlen(mbuf));
79 write(STDERR_FILENO, "\n", 1);
82 if (log_perror) {
83 fflush(stderr);
85 exit(1);
89 * Log a warning message...
91 int
92 warning(char *fmt, ...)
94 va_list list;
96 do_percentm(fbuf, sizeof(fbuf), fmt);
98 va_start(list, fmt);
99 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
100 va_end(list);
102 #ifndef DEBUG
103 syslog(LOG_ERR, "%s", mbuf);
104 #endif
106 if (log_perror) {
107 write(STDERR_FILENO, mbuf, strlen(mbuf));
108 write(STDERR_FILENO, "\n", 1);
111 return (0);
115 * Log a note...
118 note(char *fmt, ...)
120 va_list list;
122 do_percentm(fbuf, sizeof(fbuf), fmt);
124 va_start(list, fmt);
125 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
126 va_end(list);
128 #ifndef DEBUG
129 syslog(LOG_INFO, "%s", mbuf);
130 #endif
132 if (log_perror) {
133 write(STDERR_FILENO, mbuf, strlen(mbuf));
134 write(STDERR_FILENO, "\n", 1);
137 return (0);
140 #ifdef DEBUG
142 * Log a debug message...
145 debug(char *fmt, ...)
147 va_list list;
149 do_percentm(fbuf, sizeof(fbuf), fmt);
151 va_start(list, fmt);
152 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
153 va_end(list);
155 syslog(LOG_DEBUG, "%s", mbuf);
157 if (log_perror) {
158 write(STDERR_FILENO, mbuf, strlen(mbuf));
159 write(STDERR_FILENO, "\n", 1);
162 return (0);
164 #endif
166 * Find %m in the input string and substitute an error message string.
168 static void
169 do_percentm(char *obuf, size_t size, char *ibuf)
171 char ch;
172 char *s = ibuf;
173 char *t = obuf;
174 int prlen;
175 ssize_t fmt_left;
176 int saved_errno = errno;
179 * We wouldn't need this mess if printf handled %m, or if
180 * strerror() had been invented before syslog().
182 for (fmt_left = size; (ch = *s); ++s) {
183 if (ch == '%' && s[1] == 'm') {
184 ++s;
185 prlen = snprintf(t, fmt_left, "%s",
186 strerror(saved_errno));
187 if (prlen == -1)
188 prlen = 0;
189 else if (prlen >= fmt_left)
190 prlen = fmt_left - 1;
191 t += prlen;
192 fmt_left -= prlen;
193 } else {
194 if (fmt_left > 1) {
195 *t++ = ch;
196 fmt_left--;
200 *t = '\0';
204 parse_warn(char *fmt, ...)
206 va_list list;
207 static char spaces[] =
209 " "; /* 80 spaces */
210 struct iovec iov[6];
211 size_t iovcnt;
213 do_percentm(mbuf, sizeof(mbuf), fmt);
214 snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
215 va_start(list, fmt);
216 vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
217 va_end(list);
219 #ifndef DEBUG
220 syslog(LOG_ERR, "%s", mbuf);
221 syslog(LOG_ERR, "%s", token_line);
222 if (lexchar < 81)
223 syslog(LOG_ERR, "%*c", lexchar, '^');
224 #endif
226 if (log_perror) {
227 iov[0].iov_base = mbuf;
228 iov[0].iov_len = strlen(mbuf);
229 iov[1].iov_base = "\n";
230 iov[1].iov_len = 1;
231 iov[2].iov_base = token_line;
232 iov[2].iov_len = strlen(token_line);
233 iov[3].iov_base = "\n";
234 iov[3].iov_len = 1;
235 iovcnt = 4;
236 if (lexchar < 81) {
237 iov[4].iov_base = spaces;
238 iov[4].iov_len = lexchar - 1;
239 iov[5].iov_base = "^\n";
240 iov[5].iov_len = 2;
241 iovcnt += 2;
243 writev(STDERR_FILENO, iov, iovcnt);
245 warnings_occurred = 1;
246 return (0);