Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / common / errwarn.c
1 /* errwarn.c
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 #ifndef lint
44 static char copyright[] =
45 "$Id: errwarn.c,v 1.15 1997/05/09 08:03:44 mellon Exp $ Copyright (c) 1996 The Internet Software Consortium.  All rights reserved.\n";
46 #endif /* not lint */
47
48 #include "dhcpd.h"
49 #include <errno.h>
50
51 static void do_percentm PROTO ((char *obuf, char *ibuf));
52
53 static char mbuf [1024];
54 static char fbuf [1024];
55
56 int warnings_occurred;
57
58 /* Log an error message, then exit... */
59
60 void error (ANSI_DECL(char *) fmt, VA_DOTDOTDOT)
61      KandR (char *fmt;)
62      va_dcl
63 {
64   va_list list;
65   extern int logged_in;
66
67   do_percentm (fbuf, fmt);
68
69   VA_start (list, fmt);
70   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
71   va_end (list);
72
73 #ifndef DEBUG
74   syslog (log_priority | LOG_ERR, mbuf);
75 #endif
76
77   /* Also log it to stderr? */
78   if (log_perror) {
79           write (2, mbuf, strlen (mbuf));
80           write (2, "\n", 1);
81   }
82
83   syslog (LOG_CRIT, "exiting.");
84   if (log_perror) {
85         fprintf (stderr, "exiting.\n");
86         fflush (stderr);
87   }
88   cleanup ();
89   exit (1);
90 }
91
92 /* Log a warning message... */
93
94 int warn (ANSI_DECL (char *) fmt, VA_DOTDOTDOT)
95      KandR (char *fmt;)
96      va_dcl
97 {
98   va_list list;
99
100   do_percentm (fbuf, fmt);
101
102   VA_start (list, fmt);
103   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
104   va_end (list);
105
106 #ifndef DEBUG
107   syslog (log_priority | LOG_ERR, mbuf);
108 #endif
109
110   if (log_perror) {
111           write (2, mbuf, strlen (mbuf));
112           write (2, "\n", 1);
113   }
114
115   return 0;
116 }
117
118 /* Log a note... */
119
120 int note (ANSI_DECL (char *) fmt, VA_DOTDOTDOT)
121      KandR (char *fmt;)
122      va_dcl
123 {
124   va_list list;
125
126   do_percentm (fbuf, fmt);
127
128   VA_start (list, fmt);
129   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
130   va_end (list);
131
132 #ifndef DEBUG
133   syslog (log_priority | LOG_INFO, mbuf);
134 #endif
135
136   if (log_perror) {
137           write (2, mbuf, strlen (mbuf));
138           write (2, "\n", 1);
139   }
140
141   return 0;
142 }
143
144 /* Log a debug message... */
145
146 int debug (ANSI_DECL (char *) fmt, VA_DOTDOTDOT)
147      KandR (char *fmt;)
148      va_dcl
149 {
150   va_list list;
151
152   do_percentm (fbuf, fmt);
153
154   VA_start (list, fmt);
155   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
156   va_end (list);
157
158 #ifndef DEBUG
159   syslog (log_priority | LOG_DEBUG, mbuf);
160 #endif
161
162   if (log_perror) {
163           write (2, mbuf, strlen (mbuf));
164           write (2, "\n", 1);
165   }
166
167   return 0;
168 }
169
170 /* Find %m in the input string and substitute an error message string. */
171
172 static void do_percentm (obuf, ibuf)
173      char *obuf;
174      char *ibuf;
175 {
176         char *s = ibuf;
177         char *p = obuf;
178         int infmt = 0;
179         char *m;
180
181         while (*s)
182         {
183                 if (infmt)
184                 {
185                         if (*s == 'm')
186                         {
187 #ifndef __CYGWIN32__
188                                 m = strerror (errno);
189 #else
190                                 m = pWSAError ();
191 #endif
192                                 if (!m)
193                                         m = "<unknown error>";
194                                 strcpy (p - 1, m);
195                                 p += strlen (p);
196                                 ++s;
197                         }
198                         else
199                                 *p++ = *s++;
200                         infmt = 0;
201                 }
202                 else
203                 {
204                         if (*s == '%')
205                                 infmt = 1;
206                         *p++ = *s++;
207                 }
208         }
209         *p = 0;
210 }
211
212
213 int parse_warn (ANSI_DECL (char *) fmt, VA_DOTDOTDOT)
214         KandR (char *fmt;)
215         va_dcl
216 {
217         va_list list;
218         static char spaces [] = "                                                                                ";
219         
220         do_percentm (mbuf, fmt);
221 #ifndef NO_SNPRINTF
222         snprintf (fbuf, sizeof fbuf, "%s line %d: %s",
223                   tlname, lexline, mbuf);
224 #else
225         sprintf (fbuf, "%s line %d: %s",
226                  tlname, lexline, mbuf);
227 #endif
228         
229         VA_start (list, fmt);
230         vsnprintf (mbuf, sizeof mbuf, fbuf, list);
231         va_end (list);
232
233 #ifndef DEBUG
234         syslog (log_priority | LOG_ERR, mbuf);
235         syslog (log_priority | LOG_ERR, token_line);
236         if (lexline < 81)
237                 syslog (log_priority | LOG_ERR,
238                         "%s^", &spaces [sizeof spaces - lexchar]);
239 #endif
240
241         if (log_perror) {
242                 write (2, mbuf, strlen (mbuf));
243                 write (2, "\n", 1);
244                 write (2, token_line, strlen (token_line));
245                 write (2, "\n", 1);
246                 write (2, spaces, lexchar - 1);
247                 write (2, "^\n", 2);
248         }
249
250         warnings_occurred = 1;
251
252         return 0;
253 }
254
255 #ifdef NO_STRERROR
256 char *strerror (err)
257         int err;
258 {
259         extern char *sys_errlist [];
260         extern int sys_nerr;
261         static char errbuf [128];
262
263         if (err < 0 || err >= sys_nerr) {
264                 sprintf (errbuf, "Error %d", err);
265                 return errbuf;
266         }
267         return sys_errlist [err];
268 }
269 #endif /* NO_STRERROR */
270
271 #ifdef _WIN32
272 char *pWSAError ()
273 {
274   int err = WSAGetLastError ();
275
276   switch (err)
277     {
278     case WSAEACCES:
279       return "Permission denied";
280     case WSAEADDRINUSE:
281       return "Address already in use";
282     case WSAEADDRNOTAVAIL:
283       return "Cannot assign requested address";
284     case WSAEAFNOSUPPORT:
285       return "Address family not supported by protocol family";
286     case WSAEALREADY:
287       return "Operation already in progress";
288     case WSAECONNABORTED:
289       return "Software caused connection abort";
290     case WSAECONNREFUSED:
291       return "Connection refused";
292     case WSAECONNRESET:
293       return "Connection reset by peer";
294     case WSAEDESTADDRREQ:
295       return "Destination address required";
296     case WSAEFAULT:
297       return "Bad address";
298     case WSAEHOSTDOWN:
299       return "Host is down";
300     case WSAEHOSTUNREACH:
301       return "No route to host";
302     case WSAEINPROGRESS:
303       return "Operation now in progress";
304     case WSAEINTR:
305       return "Interrupted function call";
306     case WSAEINVAL:
307       return "Invalid argument";
308     case WSAEISCONN:
309       return "Socket is already connected";
310     case WSAEMFILE:
311       return "Too many open files";
312     case WSAEMSGSIZE:
313       return "Message too long";
314     case WSAENETDOWN:
315       return "Network is down";
316     case WSAENETRESET:
317       return "Network dropped connection on reset";
318     case WSAENETUNREACH:
319       return "Network is unreachable";
320     case WSAENOBUFS:
321       return "No buffer space available";
322     case WSAENOPROTOOPT:
323       return "Bad protocol option";
324     case WSAENOTCONN:
325       return "Socket is not connected";
326     case WSAENOTSOCK:
327       return "Socket operation on non-socket";
328     case WSAEOPNOTSUPP:
329       return "Operation not supported";
330     case WSAEPFNOSUPPORT:
331       return "Protocol family not supported";
332     case WSAEPROCLIM:
333       return "Too many processes";
334     case WSAEPROTONOSUPPORT:
335       return "Protocol not supported";
336     case WSAEPROTOTYPE:
337       return "Protocol wrong type for socket";
338     case WSAESHUTDOWN:
339       return "Cannot send after socket shutdown";
340     case WSAESOCKTNOSUPPORT:
341       return "Socket type not supported";
342     case WSAETIMEDOUT:
343       return "Connection timed out";
344     case WSAEWOULDBLOCK:
345       return "Resource temporarily unavailable";
346     case WSAHOST_NOT_FOUND:
347       return "Host not found";
348 #if 0
349     case WSA_INVALID_HANDLE:
350       return "Specified event object handle is invalid";
351     case WSA_INVALID_PARAMETER:
352       return "One or more parameters are invalid";
353     case WSAINVALIDPROCTABLE:
354       return "Invalid procedure table from service provider";
355     case WSAINVALIDPROVIDER:
356       return "Invalid service provider version number";
357     case WSA_IO_PENDING:
358       return "Overlapped operations will complete later";
359     case WSA_IO_INCOMPLETE:
360       return "Overlapped I/O event object not in signaled state";
361     case WSA_NOT_ENOUGH_MEMORY:
362       return "Insufficient memory available";
363 #endif
364     case WSANOTINITIALISED:
365       return "Successful WSAStartup not yet performer";
366     case WSANO_DATA:
367       return "Valid name, no data record of requested type";
368     case WSANO_RECOVERY:
369       return "This is a non-recoverable error";
370 #if 0
371     case WSAPROVIDERFAILEDINIT:
372       return "Unable to initialize a service provider";
373     case WSASYSCALLFAILURE:
374       return "System call failure";
375 #endif
376     case WSASYSNOTREADY:
377       return "Network subsystem is unavailable";
378     case WSATRY_AGAIN:
379       return "Non-authoritative host not found";
380     case WSAVERNOTSUPPORTED:
381       return "WINSOCK.DLL version out of range";
382     case WSAEDISCON:
383       return "Graceful shutdown in progress";
384 #if 0
385     case WSA_OPERATION_ABORTED:
386       return "Overlapped operation aborted";
387 #endif
388     }
389   return "Unknown WinSock error";
390 }
391 #endif /* _WIN32 */