1 /* $OpenBSD: src/sbin/dhclient/errwarn.c,v 1.17 2009/11/26 23:14:29 krw Exp $ */
3 /* Errors and warnings... */
6 * Copyright (c) 1996 The Internet Software Consortium.
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
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>
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.
66 do_percentm(fbuf
, sizeof(fbuf
), fmt
);
69 vsnprintf(mbuf
, sizeof(mbuf
), fbuf
, list
);
73 syslog(LOG_ERR
, "%s", mbuf
);
76 /* Also log it to stderr? */
78 write(STDERR_FILENO
, mbuf
, strlen(mbuf
));
79 write(STDERR_FILENO
, "\n", 1);
89 * Log a warning message...
92 warning(char *fmt
, ...)
96 do_percentm(fbuf
, sizeof(fbuf
), fmt
);
99 vsnprintf(mbuf
, sizeof(mbuf
), fbuf
, list
);
103 syslog(LOG_ERR
, "%s", mbuf
);
107 write(STDERR_FILENO
, mbuf
, strlen(mbuf
));
108 write(STDERR_FILENO
, "\n", 1);
122 do_percentm(fbuf
, sizeof(fbuf
), fmt
);
125 vsnprintf(mbuf
, sizeof(mbuf
), fbuf
, list
);
129 syslog(LOG_INFO
, "%s", mbuf
);
133 write(STDERR_FILENO
, mbuf
, strlen(mbuf
));
134 write(STDERR_FILENO
, "\n", 1);
142 * Log a debug message...
145 debug(char *fmt
, ...)
149 do_percentm(fbuf
, sizeof(fbuf
), fmt
);
152 vsnprintf(mbuf
, sizeof(mbuf
), fbuf
, list
);
155 syslog(LOG_DEBUG
, "%s", mbuf
);
158 write(STDERR_FILENO
, mbuf
, strlen(mbuf
));
159 write(STDERR_FILENO
, "\n", 1);
166 * Find %m in the input string and substitute an error message string.
169 do_percentm(char *obuf
, size_t size
, char *ibuf
)
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') {
185 prlen
= snprintf(t
, fmt_left
, "%s",
186 strerror(saved_errno
));
189 else if (prlen
>= fmt_left
)
190 prlen
= fmt_left
- 1;
204 parse_warn(char *fmt
, ...)
207 static char spaces
[] =
213 do_percentm(mbuf
, sizeof(mbuf
), fmt
);
214 snprintf(fbuf
, sizeof(fbuf
), "%s line %d: %s", tlname
, lexline
, mbuf
);
216 vsnprintf(mbuf
, sizeof(mbuf
), fbuf
, list
);
220 syslog(LOG_ERR
, "%s", mbuf
);
221 syslog(LOG_ERR
, "%s", token_line
);
223 syslog(LOG_ERR
, "%*c", lexchar
, '^');
227 iov
[0].iov_base
= mbuf
;
228 iov
[0].iov_len
= strlen(mbuf
);
229 iov
[1].iov_base
= "\n";
231 iov
[2].iov_base
= token_line
;
232 iov
[2].iov_len
= strlen(token_line
);
233 iov
[3].iov_base
= "\n";
237 iov
[4].iov_base
= spaces
;
238 iov
[4].iov_len
= lexchar
- 1;
239 iov
[5].iov_base
= "^\n";
243 writev(STDERR_FILENO
, iov
, iovcnt
);
245 warnings_occurred
= 1;