]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sbin/dhclient/errwarn.c
This commit was generated by cvs2svn to compensate for changes in r147338,
[FreeBSD/FreeBSD.git] / sbin / dhclient / errwarn.c
1 /*      $OpenBSD: errwarn.c,v 1.7 2004/05/04 22:23:01 mickey 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 <errno.h>
44
45 #include "dhcpd.h"
46
47 static void do_percentm(char *obuf, size_t size, char *ibuf);
48
49 static char mbuf[1024];
50 static char fbuf[1024];
51
52 int warnings_occurred;
53
54 /*
55  * Log an error message, then exit.
56  */
57 void
58 error(char *fmt, ...)
59 {
60         va_list list;
61
62         do_percentm(fbuf, sizeof(fbuf), fmt);
63
64         va_start(list, fmt);
65         vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
66         va_end(list);
67
68 #ifndef DEBUG
69         syslog(log_priority | LOG_ERR, "%s", mbuf);
70 #endif
71
72         /* Also log it to stderr? */
73         if (log_perror) {
74                 write(2, mbuf, strlen(mbuf));
75                 write(2, "\n", 1);
76         }
77
78         syslog(LOG_CRIT, "exiting.");
79         if (log_perror) {
80                 fprintf(stderr, "exiting.\n");
81                 fflush(stderr);
82         }
83         exit(1);
84 }
85
86 /*
87  * Log a warning message...
88  */
89 int
90 warning(char *fmt, ...)
91 {
92         va_list list;
93
94         do_percentm(fbuf, sizeof(fbuf), fmt);
95
96         va_start(list, fmt);
97         vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
98         va_end(list);
99
100 #ifndef DEBUG
101         syslog(log_priority | LOG_ERR, "%s", mbuf);
102 #endif
103
104         if (log_perror) {
105                 write(2, mbuf, strlen(mbuf));
106                 write(2, "\n", 1);
107         }
108
109         return (0);
110 }
111
112 /*
113  * Log a note...
114  */
115 int
116 note(char *fmt, ...)
117 {
118         va_list list;
119
120         do_percentm(fbuf, sizeof(fbuf), fmt);
121
122         va_start(list, fmt);
123         vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
124         va_end(list);
125
126 #ifndef DEBUG
127         syslog(log_priority | LOG_INFO, "%s", mbuf);
128 #endif
129
130         if (log_perror) {
131                 write(2, mbuf, strlen(mbuf));
132                 write(2, "\n", 1);
133         }
134
135         return (0);
136 }
137
138 /*
139  * Log a debug message...
140  */
141 int
142 debug(char *fmt, ...)
143 {
144         va_list list;
145
146         do_percentm(fbuf, sizeof(fbuf), fmt);
147
148         va_start(list, fmt);
149         vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
150         va_end(list);
151
152 #ifndef DEBUG
153         syslog(log_priority | LOG_DEBUG, "%s", mbuf);
154 #endif
155
156         if (log_perror) {
157                 write(2, mbuf, strlen(mbuf));
158                 write(2, "\n", 1);
159         }
160
161         return (0);
162 }
163
164 /*
165  * Find %m in the input string and substitute an error message string.
166  */
167 static void
168 do_percentm(char *obuf, size_t size, char *ibuf)
169 {
170         char ch;
171         char *s = ibuf;
172         char *t = obuf;
173         size_t prlen;
174         size_t fmt_left;
175         int saved_errno = errno;
176
177         /*
178          * We wouldn't need this mess if printf handled %m, or if
179          * strerror() had been invented before syslog().
180          */
181         for (fmt_left = size; (ch = *s); ++s) {
182                 if (ch == '%' && s[1] == 'm') {
183                         ++s;
184                         prlen = snprintf(t, fmt_left, "%s",
185                             strerror(saved_errno));
186                         if (prlen >= fmt_left)
187                                 prlen = fmt_left - 1;
188                         t += prlen;
189                         fmt_left -= prlen;
190                 } else {
191                         if (fmt_left > 1) {
192                                 *t++ = ch;
193                                 fmt_left--;
194                         }
195                 }
196         }
197         *t = '\0';
198 }
199
200 int
201 parse_warn(char *fmt, ...)
202 {
203         va_list list;
204         static char spaces[] =
205             "                                        "
206             "                                        "; /* 80 spaces */
207
208         do_percentm(mbuf, sizeof(mbuf), fmt);
209         snprintf(fbuf, sizeof(fbuf), "%s line %d: %s", tlname, lexline, mbuf);
210         va_start(list, fmt);
211         vsnprintf(mbuf, sizeof(mbuf), fbuf, list);
212         va_end(list);
213
214 #ifndef DEBUG
215         syslog(log_priority | LOG_ERR, "%s", mbuf);
216         syslog(log_priority | LOG_ERR, "%s", token_line);
217         if (lexline < 81)
218                 syslog(log_priority | LOG_ERR,
219                     "%s^", &spaces[sizeof(spaces) - lexchar]);
220 #endif
221
222         if (log_perror) {
223                 write(2, mbuf, strlen(mbuf));
224                 write(2, "\n", 1);
225                 write(2, token_line, strlen(token_line));
226                 write(2, "\n", 1);
227                 write(2, spaces, lexchar - 1);
228                 write(2, "^\n", 2);
229         }
230
231         warnings_occurred = 1;
232
233         return (0);
234 }