Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / isc-dhcp / omapip / errwarn.c
1 /* errwarn.c
2
3    Errors and warnings... */
4
5 /*
6  * Copyright (c) 1995 RadioMail Corporation.
7  * Copyright (c) 1996-2000 Internet Software Consortium.
8  * 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 The Internet Software Consortium nor the names
20  *    of its contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
24  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
25  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
26  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
28  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
31  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
33  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
34  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * 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.9 2000/09/29 20:01:49 mellon Exp $ Copyright (c) 1996 The Internet Software Consortium.  All rights reserved.\n";
46 #endif /* not lint */
47
48 #include <omapip/omapip_p.h>
49 #include <errno.h>
50
51 #ifdef DEBUG
52 int log_perror = -1;
53 #else
54 int log_perror = 1;
55 #endif
56 int log_priority;
57 void (*log_cleanup) (void);
58
59 #define CVT_BUF_MAX 1023
60 static char mbuf [CVT_BUF_MAX + 1];
61 static char fbuf [CVT_BUF_MAX + 1];
62
63 /* Log an error message, then exit... */
64
65 void log_fatal (const char * fmt, ... )
66 {
67   va_list list;
68
69   do_percentm (fbuf, fmt);
70
71   va_start (list, fmt);
72   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
73   va_end (list);
74
75 #ifndef DEBUG
76   syslog (log_priority | LOG_ERR, "%s", mbuf);
77 #endif
78
79   /* Also log it to stderr? */
80   if (log_perror) {
81           write (2, mbuf, strlen (mbuf));
82           write (2, "\n", 1);
83   }
84
85 #if !defined (NOMINUM)
86   log_error ("%s", "");
87   log_error ("If you did not get this software from ftp.isc.org, please");
88   log_error ("get the latest from ftp.isc.org and install that before");
89   log_error ("requesting help.");
90   log_error ("%s", "");
91   log_error ("If you did get this software from ftp.isc.org and have not");
92   log_error ("yet read the README, please read it before requesting help.");
93   log_error ("If you intend to request help from the dhcp-server@isc.org");
94   log_error ("mailing list, please read the section on the README about");
95   log_error ("submitting bug reports and requests for help.");
96   log_error ("%s", "");
97   log_error ("Please do not under any circumstances send requests for");
98   log_error ("help directly to the authors of this software - please");
99   log_error ("send them to the appropriate mailing list as described in");
100   log_error ("the README file.");
101   log_error ("%s", "");
102   log_error ("exiting.");
103 #endif
104   if (log_cleanup)
105           (*log_cleanup) ();
106   exit (1);
107 }
108
109 /* Log an error message... */
110
111 int log_error (const char * fmt, ...)
112 {
113   va_list list;
114
115   do_percentm (fbuf, fmt);
116
117   va_start (list, fmt);
118   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
119   va_end (list);
120
121 #ifndef DEBUG
122   syslog (log_priority | LOG_ERR, "%s", mbuf);
123 #endif
124
125   if (log_perror) {
126           write (2, mbuf, strlen (mbuf));
127           write (2, "\n", 1);
128   }
129
130   return 0;
131 }
132
133 /* Log a note... */
134
135 int log_info (const char *fmt, ...)
136 {
137   va_list list;
138
139   do_percentm (fbuf, fmt);
140
141   va_start (list, fmt);
142   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
143   va_end (list);
144
145 #ifndef DEBUG
146   syslog (log_priority | LOG_INFO, "%s", mbuf);
147 #endif
148
149   if (log_perror) {
150           write (2, mbuf, strlen (mbuf));
151           write (2, "\n", 1);
152   }
153
154   return 0;
155 }
156
157 /* Log a debug message... */
158
159 int log_debug (const char *fmt, ...)
160 {
161   va_list list;
162
163   do_percentm (fbuf, fmt);
164
165   va_start (list, fmt);
166   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
167   va_end (list);
168
169 #ifndef DEBUG
170   syslog (log_priority | LOG_DEBUG, "%s", mbuf);
171 #endif
172
173   if (log_perror) {
174           write (2, mbuf, strlen (mbuf));
175           write (2, "\n", 1);
176   }
177
178   return 0;
179 }
180
181 /* Find %m in the input string and substitute an error message string. */
182
183 void do_percentm (obuf, ibuf)
184      char *obuf;
185      const char *ibuf;
186 {
187         const char *s = ibuf;
188         char *p = obuf;
189         int infmt = 0;
190         const char *m;
191         int len = 0;
192
193         while (*s) {
194                 if (infmt) {
195                         if (*s == 'm') {
196 #ifndef __CYGWIN32__
197                                 m = strerror (errno);
198 #else
199                                 m = pWSAError ();
200 #endif
201                                 if (!m)
202                                         m = "<unknown error>";
203                                 len += strlen (m);
204                                 if (len > CVT_BUF_MAX)
205                                         goto out;
206                                 strcpy (p - 1, m);
207                                 p += strlen (p);
208                                 ++s;
209                         } else {
210                                 if (++len > CVT_BUF_MAX)
211                                         goto out;
212                                 *p++ = *s++;
213                         }
214                         infmt = 0;
215                 } else {
216                         if (*s == '%')
217                                 infmt = 1;
218                         if (++len > CVT_BUF_MAX)
219                                 goto out;
220                         *p++ = *s++;
221                 }
222         }
223       out:
224         *p = 0;
225 }
226
227 #ifdef NO_STRERROR
228 char *strerror (err)
229         int err;
230 {
231         extern char *sys_errlist [];
232         extern int sys_nerr;
233         static char errbuf [128];
234
235         if (err < 0 || err >= sys_nerr) {
236                 sprintf (errbuf, "Error %d", err);
237                 return errbuf;
238         }
239         return sys_errlist [err];
240 }
241 #endif /* NO_STRERROR */
242
243 #ifdef _WIN32
244 char *pWSAError ()
245 {
246   int err = WSAGetLastError ();
247
248   switch (err)
249     {
250     case WSAEACCES:
251       return "Permission denied";
252     case WSAEADDRINUSE:
253       return "Address already in use";
254     case WSAEADDRNOTAVAIL:
255       return "Cannot assign requested address";
256     case WSAEAFNOSUPPORT:
257       return "Address family not supported by protocol family";
258     case WSAEALREADY:
259       return "Operation already in progress";
260     case WSAECONNABORTED:
261       return "Software caused connection abort";
262     case WSAECONNREFUSED:
263       return "Connection refused";
264     case WSAECONNRESET:
265       return "Connection reset by peer";
266     case WSAEDESTADDRREQ:
267       return "Destination address required";
268     case WSAEFAULT:
269       return "Bad address";
270     case WSAEHOSTDOWN:
271       return "Host is down";
272     case WSAEHOSTUNREACH:
273       return "No route to host";
274     case WSAEINPROGRESS:
275       return "Operation now in progress";
276     case WSAEINTR:
277       return "Interrupted function call";
278     case WSAEINVAL:
279       return "Invalid argument";
280     case WSAEISCONN:
281       return "Socket is already connected";
282     case WSAEMFILE:
283       return "Too many open files";
284     case WSAEMSGSIZE:
285       return "Message too long";
286     case WSAENETDOWN:
287       return "Network is down";
288     case WSAENETRESET:
289       return "Network dropped connection on reset";
290     case WSAENETUNREACH:
291       return "Network is unreachable";
292     case WSAENOBUFS:
293       return "No buffer space available";
294     case WSAENOPROTOOPT:
295       return "Bad protocol option";
296     case WSAENOTCONN:
297       return "Socket is not connected";
298     case WSAENOTSOCK:
299       return "Socket operation on non-socket";
300     case WSAEOPNOTSUPP:
301       return "Operation not supported";
302     case WSAEPFNOSUPPORT:
303       return "Protocol family not supported";
304     case WSAEPROCLIM:
305       return "Too many processes";
306     case WSAEPROTONOSUPPORT:
307       return "Protocol not supported";
308     case WSAEPROTOTYPE:
309       return "Protocol wrong type for socket";
310     case WSAESHUTDOWN:
311       return "Cannot send after socket shutdown";
312     case WSAESOCKTNOSUPPORT:
313       return "Socket type not supported";
314     case WSAETIMEDOUT:
315       return "Connection timed out";
316     case WSAEWOULDBLOCK:
317       return "Resource temporarily unavailable";
318     case WSAHOST_NOT_FOUND:
319       return "Host not found";
320 #if 0
321     case WSA_INVALID_HANDLE:
322       return "Specified event object handle is invalid";
323     case WSA_INVALID_PARAMETER:
324       return "One or more parameters are invalid";
325     case WSAINVALIDPROCTABLE:
326       return "Invalid procedure table from service provider";
327     case WSAINVALIDPROVIDER:
328       return "Invalid service provider version number";
329     case WSA_IO_PENDING:
330       return "Overlapped operations will complete later";
331     case WSA_IO_INCOMPLETE:
332       return "Overlapped I/O event object not in signaled state";
333     case WSA_NOT_ENOUGH_MEMORY:
334       return "Insufficient memory available";
335 #endif
336     case WSANOTINITIALISED:
337       return "Successful WSAStartup not yet performer";
338     case WSANO_DATA:
339       return "Valid name, no data record of requested type";
340     case WSANO_RECOVERY:
341       return "This is a non-recoverable error";
342 #if 0
343     case WSAPROVIDERFAILEDINIT:
344       return "Unable to initialize a service provider";
345     case WSASYSCALLFAILURE:
346       return "System call failure";
347 #endif
348     case WSASYSNOTREADY:
349       return "Network subsystem is unavailable";
350     case WSATRY_AGAIN:
351       return "Non-authoritative host not found";
352     case WSAVERNOTSUPPORTED:
353       return "WINSOCK.DLL version out of range";
354     case WSAEDISCON:
355       return "Graceful shutdown in progress";
356 #if 0
357     case WSA_OPERATION_ABORTED:
358       return "Overlapped operation aborted";
359 #endif
360     }
361   return "Unknown WinSock error";
362 }
363 #endif /* _WIN32 */