]> git.ipfire.org Git - thirdparty/dhcp.git/blob - omapip/errwarn.c
Put some extra paranoia in logging subroutines.
[thirdparty/dhcp.git] / 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.5 2000/06/29 20:05:13 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 static char mbuf [1024];
60 static char fbuf [1024];
61
62 /* Log an error message, then exit... */
63
64 void log_fatal (const char * fmt, ... )
65 {
66 va_list list;
67
68 do_percentm (fbuf, fmt);
69
70 va_start (list, fmt);
71 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
72 va_end (list);
73
74 #ifndef DEBUG
75 syslog (log_priority | LOG_ERR, "%s", mbuf);
76 #endif
77
78 /* Also log it to stderr? */
79 if (log_perror) {
80 write (2, mbuf, strlen (mbuf));
81 write (2, "\n", 1);
82 }
83
84 syslog (LOG_CRIT, "exiting.");
85 if (log_perror) {
86 fprintf (stderr, "exiting.\n");
87 fflush (stderr);
88 }
89 if (log_cleanup)
90 (*log_cleanup) ();
91 exit (1);
92 }
93
94 /* Log an error message... */
95
96 int log_error (const char * fmt, ...)
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, "%s", 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 log_info (const char *fmt, ...)
121 {
122 va_list list;
123
124 do_percentm (fbuf, fmt);
125
126 va_start (list, fmt);
127 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
128 va_end (list);
129
130 #ifndef DEBUG
131 syslog (log_priority | LOG_INFO, "%s", mbuf);
132 #endif
133
134 if (log_perror) {
135 write (2, mbuf, strlen (mbuf));
136 write (2, "\n", 1);
137 }
138
139 return 0;
140 }
141
142 /* Log a debug message... */
143
144 int log_debug (const char *fmt, ...)
145 {
146 va_list list;
147
148 do_percentm (fbuf, fmt);
149
150 va_start (list, fmt);
151 vsnprintf (mbuf, sizeof mbuf, fbuf, list);
152 va_end (list);
153
154 #ifndef DEBUG
155 syslog (log_priority | LOG_DEBUG, "%s", mbuf);
156 #endif
157
158 if (log_perror) {
159 write (2, mbuf, strlen (mbuf));
160 write (2, "\n", 1);
161 }
162
163 return 0;
164 }
165
166 /* Find %m in the input string and substitute an error message string. */
167
168 void do_percentm (obuf, ibuf)
169 char *obuf;
170 const char *ibuf;
171 {
172 const char *s = ibuf;
173 char *p = obuf;
174 int infmt = 0;
175 const char *m;
176
177 while (*s)
178 {
179 if (infmt)
180 {
181 if (*s == 'm')
182 {
183 #ifndef __CYGWIN32__
184 m = strerror (errno);
185 #else
186 m = pWSAError ();
187 #endif
188 if (!m)
189 m = "<unknown error>";
190 strcpy (p - 1, m);
191 p += strlen (p);
192 ++s;
193 }
194 else
195 *p++ = *s++;
196 infmt = 0;
197 }
198 else
199 {
200 if (*s == '%')
201 infmt = 1;
202 *p++ = *s++;
203 }
204 }
205 *p = 0;
206 }
207
208 #ifdef NO_STRERROR
209 char *strerror (err)
210 int err;
211 {
212 extern char *sys_errlist [];
213 extern int sys_nerr;
214 static char errbuf [128];
215
216 if (err < 0 || err >= sys_nerr) {
217 sprintf (errbuf, "Error %d", err);
218 return errbuf;
219 }
220 return sys_errlist [err];
221 }
222 #endif /* NO_STRERROR */
223
224 #ifdef _WIN32
225 char *pWSAError ()
226 {
227 int err = WSAGetLastError ();
228
229 switch (err)
230 {
231 case WSAEACCES:
232 return "Permission denied";
233 case WSAEADDRINUSE:
234 return "Address already in use";
235 case WSAEADDRNOTAVAIL:
236 return "Cannot assign requested address";
237 case WSAEAFNOSUPPORT:
238 return "Address family not supported by protocol family";
239 case WSAEALREADY:
240 return "Operation already in progress";
241 case WSAECONNABORTED:
242 return "Software caused connection abort";
243 case WSAECONNREFUSED:
244 return "Connection refused";
245 case WSAECONNRESET:
246 return "Connection reset by peer";
247 case WSAEDESTADDRREQ:
248 return "Destination address required";
249 case WSAEFAULT:
250 return "Bad address";
251 case WSAEHOSTDOWN:
252 return "Host is down";
253 case WSAEHOSTUNREACH:
254 return "No route to host";
255 case WSAEINPROGRESS:
256 return "Operation now in progress";
257 case WSAEINTR:
258 return "Interrupted function call";
259 case WSAEINVAL:
260 return "Invalid argument";
261 case WSAEISCONN:
262 return "Socket is already connected";
263 case WSAEMFILE:
264 return "Too many open files";
265 case WSAEMSGSIZE:
266 return "Message too long";
267 case WSAENETDOWN:
268 return "Network is down";
269 case WSAENETRESET:
270 return "Network dropped connection on reset";
271 case WSAENETUNREACH:
272 return "Network is unreachable";
273 case WSAENOBUFS:
274 return "No buffer space available";
275 case WSAENOPROTOOPT:
276 return "Bad protocol option";
277 case WSAENOTCONN:
278 return "Socket is not connected";
279 case WSAENOTSOCK:
280 return "Socket operation on non-socket";
281 case WSAEOPNOTSUPP:
282 return "Operation not supported";
283 case WSAEPFNOSUPPORT:
284 return "Protocol family not supported";
285 case WSAEPROCLIM:
286 return "Too many processes";
287 case WSAEPROTONOSUPPORT:
288 return "Protocol not supported";
289 case WSAEPROTOTYPE:
290 return "Protocol wrong type for socket";
291 case WSAESHUTDOWN:
292 return "Cannot send after socket shutdown";
293 case WSAESOCKTNOSUPPORT:
294 return "Socket type not supported";
295 case WSAETIMEDOUT:
296 return "Connection timed out";
297 case WSAEWOULDBLOCK:
298 return "Resource temporarily unavailable";
299 case WSAHOST_NOT_FOUND:
300 return "Host not found";
301 #if 0
302 case WSA_INVALID_HANDLE:
303 return "Specified event object handle is invalid";
304 case WSA_INVALID_PARAMETER:
305 return "One or more parameters are invalid";
306 case WSAINVALIDPROCTABLE:
307 return "Invalid procedure table from service provider";
308 case WSAINVALIDPROVIDER:
309 return "Invalid service provider version number";
310 case WSA_IO_PENDING:
311 return "Overlapped operations will complete later";
312 case WSA_IO_INCOMPLETE:
313 return "Overlapped I/O event object not in signaled state";
314 case WSA_NOT_ENOUGH_MEMORY:
315 return "Insufficient memory available";
316 #endif
317 case WSANOTINITIALISED:
318 return "Successful WSAStartup not yet performer";
319 case WSANO_DATA:
320 return "Valid name, no data record of requested type";
321 case WSANO_RECOVERY:
322 return "This is a non-recoverable error";
323 #if 0
324 case WSAPROVIDERFAILEDINIT:
325 return "Unable to initialize a service provider";
326 case WSASYSCALLFAILURE:
327 return "System call failure";
328 #endif
329 case WSASYSNOTREADY:
330 return "Network subsystem is unavailable";
331 case WSATRY_AGAIN:
332 return "Non-authoritative host not found";
333 case WSAVERNOTSUPPORTED:
334 return "WINSOCK.DLL version out of range";
335 case WSAEDISCON:
336 return "Graceful shutdown in progress";
337 #if 0
338 case WSA_OPERATION_ABORTED:
339 return "Overlapped operation aborted";
340 #endif
341 }
342 return "Unknown WinSock error";
343 }
344 #endif /* _WIN32 */