xref: /minix/external/bsd/dhcp/dist/omapip/errwarn.c (revision 83ee113e)
1 /*	$NetBSD: errwarn.c,v 1.1.1.3 2014/07/12 11:57:58 spz Exp $	*/
2 /* errwarn.c
3 
4    Errors and warnings... */
5 
6 /*
7  * Copyright (c) 1995 RadioMail Corporation.
8  * Copyright (c) 2009,2014 by Internet Systems Consortium, Inc. ("ISC")
9  * Copyright (c) 2004,2007 by Internet Systems Consortium, Inc. ("ISC")
10  * Copyright (c) 1996-2003 by Internet Software Consortium
11  *
12  * Permission to use, copy, modify, and distribute this software for any
13  * purpose with or without fee is hereby granted, provided that the above
14  * copyright notice and this permission notice appear in all copies.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
17  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
18  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
19  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
20  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
21  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
22  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  *   Internet Systems Consortium, Inc.
25  *   950 Charter Street
26  *   Redwood City, CA 94063
27  *   <info@isc.org>
28  *   https://www.isc.org/
29  *
30  * This software was written for RadioMail Corporation by Ted Lemon
31  * under a contract with Vixie Enterprises.   Further modifications have
32  * been made for Internet Systems Consortium under a contract
33  * with Vixie Laboratories.
34  */
35 
36 #include <sys/cdefs.h>
37 __RCSID("$NetBSD: errwarn.c,v 1.1.1.3 2014/07/12 11:57:58 spz Exp $");
38 
39 #include "dhcpd.h"
40 
41 #include <omapip/omapip_p.h>
42 #include <errno.h>
43 #include <syslog.h>
44 
45 #ifdef DEBUG
46 int log_perror = -1;
47 #else
48 int log_perror = 1;
49 #endif
50 int log_priority;
51 void (*log_cleanup) (void);
52 
53 #define CVT_BUF_MAX 1023
54 static char mbuf [CVT_BUF_MAX + 1];
55 static char fbuf [CVT_BUF_MAX + 1];
56 
57 /* Log an error message, then exit... */
58 
log_fatal(const char * fmt,...)59 void log_fatal (const char * fmt, ... )
60 {
61   va_list list;
62 
63   do_percentm (fbuf, fmt);
64 
65   /* %Audit% This is log output. %2004.06.17,Safe%
66    * If we truncate we hope the user can get a hint from the log.
67    */
68   va_start (list, fmt);
69   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
70   va_end (list);
71 
72 #ifndef DEBUG
73   syslog (log_priority | LOG_ERR, "%s", mbuf);
74 #endif
75 
76   /* Also log it to stderr? */
77   if (log_perror) {
78 	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
79 	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
80   }
81 
82   log_error ("%s", "");
83   log_error ("If you think you have received this message due to a bug rather");
84   log_error ("than a configuration issue please read the section on submitting");
85   log_error ("bugs on either our web page at www.isc.org or in the README file");
86   log_error ("before submitting a bug.  These pages explain the proper");
87   log_error ("process and the information we find helpful for debugging..");
88   log_error ("%s", "");
89   log_error ("exiting.");
90 
91   if (log_cleanup)
92 	  (*log_cleanup) ();
93   exit (1);
94 }
95 
96 /* Log an error message... */
97 
log_error(const char * fmt,...)98 int log_error (const char * fmt, ...)
99 {
100   va_list list;
101 
102   do_percentm (fbuf, fmt);
103 
104   /* %Audit% This is log output. %2004.06.17,Safe%
105    * If we truncate we hope the user can get a hint from the log.
106    */
107   va_start (list, fmt);
108   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
109   va_end (list);
110 
111 #ifndef DEBUG
112   syslog (log_priority | LOG_ERR, "%s", mbuf);
113 #endif
114 
115   if (log_perror) {
116 	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
117 	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
118   }
119 
120   return 0;
121 }
122 
123 /* Log a note... */
124 
log_info(const char * fmt,...)125 int log_info (const char *fmt, ...)
126 {
127   va_list list;
128 
129   do_percentm (fbuf, fmt);
130 
131   /* %Audit% This is log output. %2004.06.17,Safe%
132    * If we truncate we hope the user can get a hint from the log.
133    */
134   va_start (list, fmt);
135   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
136   va_end (list);
137 
138 #ifndef DEBUG
139   syslog (log_priority | LOG_INFO, "%s", mbuf);
140 #endif
141 
142   if (log_perror) {
143 	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
144 	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
145   }
146 
147   return 0;
148 }
149 
150 /* Log a debug message... */
151 
log_debug(const char * fmt,...)152 int log_debug (const char *fmt, ...)
153 {
154   va_list list;
155 
156   do_percentm (fbuf, fmt);
157 
158   /* %Audit% This is log output. %2004.06.17,Safe%
159    * If we truncate we hope the user can get a hint from the log.
160    */
161   va_start (list, fmt);
162   vsnprintf (mbuf, sizeof mbuf, fbuf, list);
163   va_end (list);
164 
165 #ifndef DEBUG
166   syslog (log_priority | LOG_DEBUG, "%s", mbuf);
167 #endif
168 
169   if (log_perror) {
170 	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
171 	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
172   }
173 
174   return 0;
175 }
176 
177 /* Find %m in the input string and substitute an error message string. */
178 
do_percentm(obuf,ibuf)179 void do_percentm (obuf, ibuf)
180      char *obuf;
181      const char *ibuf;
182 {
183 	const char *s = ibuf;
184 	char *p = obuf;
185 	int infmt = 0;
186 	const char *m;
187 	int len = 0;
188 
189 	while (*s) {
190 		if (infmt) {
191 			if (*s == 'm') {
192 #ifndef __CYGWIN32__
193 				m = strerror (errno);
194 #else
195 				m = pWSAError ();
196 #endif
197 				if (!m)
198 					m = "<unknown error>";
199 				len += strlen (m);
200 				if (len > CVT_BUF_MAX)
201 					goto out;
202 				strcpy (p - 1, m);
203 				p += strlen (p);
204 				++s;
205 			} else {
206 				if (++len > CVT_BUF_MAX)
207 					goto out;
208 				*p++ = *s++;
209 			}
210 			infmt = 0;
211 		} else {
212 			if (*s == '%')
213 				infmt = 1;
214 			if (++len > CVT_BUF_MAX)
215 				goto out;
216 			*p++ = *s++;
217 		}
218 	}
219       out:
220 	*p = 0;
221 }
222 
223 #ifdef NO_STRERROR
strerror(err)224 char *strerror (err)
225 	int err;
226 {
227 	extern char *sys_errlist [];
228 	extern int sys_nerr;
229 	static char errbuf [128];
230 
231 	if (err < 0 || err >= sys_nerr) {
232 		sprintf (errbuf, "Error %d", err);
233 		return errbuf;
234 	}
235 	return sys_errlist [err];
236 }
237 #endif /* NO_STRERROR */
238 
239 #ifdef _WIN32
pWSAError()240 char *pWSAError ()
241 {
242   int err = WSAGetLastError ();
243 
244   switch (err)
245     {
246     case WSAEACCES:
247       return "Permission denied";
248     case WSAEADDRINUSE:
249       return "Address already in use";
250     case WSAEADDRNOTAVAIL:
251       return "Cannot assign requested address";
252     case WSAEAFNOSUPPORT:
253       return "Address family not supported by protocol family";
254     case WSAEALREADY:
255       return "Operation already in progress";
256     case WSAECONNABORTED:
257       return "Software caused connection abort";
258     case WSAECONNREFUSED:
259       return "Connection refused";
260     case WSAECONNRESET:
261       return "Connection reset by peer";
262     case WSAEDESTADDRREQ:
263       return "Destination address required";
264     case WSAEFAULT:
265       return "Bad address";
266     case WSAEHOSTDOWN:
267       return "Host is down";
268     case WSAEHOSTUNREACH:
269       return "No route to host";
270     case WSAEINPROGRESS:
271       return "Operation now in progress";
272     case WSAEINTR:
273       return "Interrupted function call";
274     case WSAEINVAL:
275       return "Invalid argument";
276     case WSAEISCONN:
277       return "Socket is already connected";
278     case WSAEMFILE:
279       return "Too many open files";
280     case WSAEMSGSIZE:
281       return "Message too long";
282     case WSAENETDOWN:
283       return "Network is down";
284     case WSAENETRESET:
285       return "Network dropped connection on reset";
286     case WSAENETUNREACH:
287       return "Network is unreachable";
288     case WSAENOBUFS:
289       return "No buffer space available";
290     case WSAENOPROTOOPT:
291       return "Bad protocol option";
292     case WSAENOTCONN:
293       return "Socket is not connected";
294     case WSAENOTSOCK:
295       return "Socket operation on non-socket";
296     case WSAEOPNOTSUPP:
297       return "Operation not supported";
298     case WSAEPFNOSUPPORT:
299       return "Protocol family not supported";
300     case WSAEPROCLIM:
301       return "Too many processes";
302     case WSAEPROTONOSUPPORT:
303       return "Protocol not supported";
304     case WSAEPROTOTYPE:
305       return "Protocol wrong type for socket";
306     case WSAESHUTDOWN:
307       return "Cannot send after socket shutdown";
308     case WSAESOCKTNOSUPPORT:
309       return "Socket type not supported";
310     case WSAETIMEDOUT:
311       return "Connection timed out";
312     case WSAEWOULDBLOCK:
313       return "Resource temporarily unavailable";
314     case WSAHOST_NOT_FOUND:
315       return "Host not found";
316 #if 0
317     case WSA_INVALID_HANDLE:
318       return "Specified event object handle is invalid";
319     case WSA_INVALID_PARAMETER:
320       return "One or more parameters are invalid";
321     case WSAINVALIDPROCTABLE:
322       return "Invalid procedure table from service provider";
323     case WSAINVALIDPROVIDER:
324       return "Invalid service provider version number";
325     case WSA_IO_PENDING:
326       return "Overlapped operations will complete later";
327     case WSA_IO_INCOMPLETE:
328       return "Overlapped I/O event object not in signaled state";
329     case WSA_NOT_ENOUGH_MEMORY:
330       return "Insufficient memory available";
331 #endif
332     case WSANOTINITIALISED:
333       return "Successful WSAStartup not yet performer";
334     case WSANO_DATA:
335       return "Valid name, no data record of requested type";
336     case WSANO_RECOVERY:
337       return "This is a non-recoverable error";
338 #if 0
339     case WSAPROVIDERFAILEDINIT:
340       return "Unable to initialize a service provider";
341     case WSASYSCALLFAILURE:
342       return "System call failure";
343 #endif
344     case WSASYSNOTREADY:
345       return "Network subsystem is unavailable";
346     case WSATRY_AGAIN:
347       return "Non-authoritative host not found";
348     case WSAVERNOTSUPPORTED:
349       return "WINSOCK.DLL version out of range";
350     case WSAEDISCON:
351       return "Graceful shutdown in progress";
352 #if 0
353     case WSA_OPERATION_ABORTED:
354       return "Overlapped operation aborted";
355 #endif
356     }
357   return "Unknown WinSock error";
358 }
359 #endif /* _WIN32 */
360