]>
Commit | Line | Data |
---|---|---|
d7837182 TL |
1 | /* errwarn.c |
2 | ||
3 | Errors and warnings... */ | |
4 | ||
5 | /* | |
f39b6e00 | 6 | * Copyright (c) 1995 RadioMail Corporation. |
4bd8800e | 7 | * Copyright (c) 1996-2000 Internet Software Consortium. |
d7837182 | 8 | * |
f39b6e00 TL |
9 | * Use is subject to license terms which appear in the file named |
10 | * ISC-LICENSE that should have accompanied this file when you | |
11 | * received it. If a file named ISC-LICENSE did not accompany this | |
12 | * file, or you are not sure the one you have is correct, you may | |
13 | * obtain an applicable copy of the license at: | |
d7837182 | 14 | * |
f39b6e00 | 15 | * http://www.isc.org/isc-license-1.0.html. |
d7837182 | 16 | * |
f39b6e00 TL |
17 | * This file is part of the ISC DHCP distribution. The documentation |
18 | * associated with this file is listed in the file DOCUMENTATION, | |
19 | * included in the top-level directory of this release. | |
20 | * | |
21 | * Support and other services are available for ISC products - see | |
22 | * http://www.isc.org for more information. | |
d7837182 TL |
23 | * |
24 | * This software was written for RadioMail Corporation by Ted Lemon | |
a2f303cd TL |
25 | * under a contract with Vixie Enterprises. Further modifications have |
26 | * been made for the Internet Software Consortium under a contract | |
27 | * with Vixie Laboratories. | |
d7837182 TL |
28 | */ |
29 | ||
30 | #ifndef lint | |
31 | static char copyright[] = | |
4bd8800e | 32 | "$Id: errwarn.c,v 1.2 2000/01/26 14:56:05 mellon Exp $ Copyright (c) 1996 The Internet Software Consortium. All rights reserved.\n"; |
d7837182 TL |
33 | #endif /* not lint */ |
34 | ||
4bd8800e | 35 | #include <omapip/omapip_p.h> |
d7837182 TL |
36 | #include <errno.h> |
37 | ||
4bd8800e TL |
38 | #ifdef DEBUG |
39 | int log_perror = -1; | |
40 | #else | |
41 | int log_perror = 1; | |
42 | #endif | |
43 | int log_priority; | |
44 | void (*log_cleanup) (void); | |
d7837182 TL |
45 | |
46 | static char mbuf [1024]; | |
47 | static char fbuf [1024]; | |
48 | ||
49 | /* Log an error message, then exit... */ | |
50 | ||
b1b7b521 | 51 | void log_fatal (ANSI_DECL(const char *) fmt, VA_DOTDOTDOT) |
d7837182 TL |
52 | KandR (char *fmt;) |
53 | va_dcl | |
54 | { | |
55 | va_list list; | |
d7837182 TL |
56 | |
57 | do_percentm (fbuf, fmt); | |
58 | ||
59 | VA_start (list, fmt); | |
60 | vsnprintf (mbuf, sizeof mbuf, fbuf, list); | |
61 | va_end (list); | |
55526112 | 62 | |
eb5cc81e | 63 | #ifndef DEBUG |
40950384 | 64 | syslog (log_priority | LOG_ERR, mbuf); |
eb5cc81e | 65 | #endif |
55526112 TL |
66 | |
67 | /* Also log it to stderr? */ | |
68 | if (log_perror) { | |
a6cbc1e4 TL |
69 | write (2, mbuf, strlen (mbuf)); |
70 | write (2, "\n", 1); | |
55526112 | 71 | } |
d7837182 | 72 | |
0c56f332 TL |
73 | syslog (LOG_CRIT, "exiting."); |
74 | if (log_perror) { | |
006c74da | 75 | fprintf (stderr, "exiting.\n"); |
0c56f332 TL |
76 | fflush (stderr); |
77 | } | |
4bd8800e TL |
78 | if (log_cleanup) |
79 | (*log_cleanup) (); | |
d7837182 TL |
80 | exit (1); |
81 | } | |
82 | ||
8ae2d595 | 83 | /* Log an error message... */ |
d7837182 | 84 | |
b1b7b521 | 85 | int log_error (ANSI_DECL (const char *) fmt, VA_DOTDOTDOT) |
d7837182 TL |
86 | KandR (char *fmt;) |
87 | va_dcl | |
88 | { | |
89 | va_list list; | |
90 | ||
91 | do_percentm (fbuf, fmt); | |
92 | ||
93 | VA_start (list, fmt); | |
94 | vsnprintf (mbuf, sizeof mbuf, fbuf, list); | |
95 | va_end (list); | |
55526112 | 96 | |
eb5cc81e | 97 | #ifndef DEBUG |
40950384 | 98 | syslog (log_priority | LOG_ERR, mbuf); |
eb5cc81e | 99 | #endif |
55526112 TL |
100 | |
101 | if (log_perror) { | |
a6cbc1e4 TL |
102 | write (2, mbuf, strlen (mbuf)); |
103 | write (2, "\n", 1); | |
55526112 TL |
104 | } |
105 | ||
d7837182 TL |
106 | return 0; |
107 | } | |
108 | ||
109 | /* Log a note... */ | |
110 | ||
b1b7b521 | 111 | int log_info (ANSI_DECL (const char *) fmt, VA_DOTDOTDOT) |
d7837182 TL |
112 | KandR (char *fmt;) |
113 | va_dcl | |
114 | { | |
115 | va_list list; | |
116 | ||
117 | do_percentm (fbuf, fmt); | |
118 | ||
119 | VA_start (list, fmt); | |
120 | vsnprintf (mbuf, sizeof mbuf, fbuf, list); | |
121 | va_end (list); | |
55526112 | 122 | |
eb5cc81e | 123 | #ifndef DEBUG |
40950384 | 124 | syslog (log_priority | LOG_INFO, mbuf); |
eb5cc81e | 125 | #endif |
55526112 TL |
126 | |
127 | if (log_perror) { | |
a6cbc1e4 TL |
128 | write (2, mbuf, strlen (mbuf)); |
129 | write (2, "\n", 1); | |
55526112 TL |
130 | } |
131 | ||
d7837182 TL |
132 | return 0; |
133 | } | |
134 | ||
135 | /* Log a debug message... */ | |
136 | ||
b1b7b521 | 137 | int log_debug (ANSI_DECL (const char *) fmt, VA_DOTDOTDOT) |
d7837182 TL |
138 | KandR (char *fmt;) |
139 | va_dcl | |
140 | { | |
141 | va_list list; | |
142 | ||
143 | do_percentm (fbuf, fmt); | |
144 | ||
145 | VA_start (list, fmt); | |
146 | vsnprintf (mbuf, sizeof mbuf, fbuf, list); | |
147 | va_end (list); | |
55526112 | 148 | |
eb5cc81e | 149 | #ifndef DEBUG |
40950384 | 150 | syslog (log_priority | LOG_DEBUG, mbuf); |
eb5cc81e | 151 | #endif |
55526112 TL |
152 | |
153 | if (log_perror) { | |
a6cbc1e4 TL |
154 | write (2, mbuf, strlen (mbuf)); |
155 | write (2, "\n", 1); | |
55526112 TL |
156 | } |
157 | ||
d7837182 TL |
158 | return 0; |
159 | } | |
160 | ||
161 | /* Find %m in the input string and substitute an error message string. */ | |
162 | ||
4bd8800e | 163 | void do_percentm (obuf, ibuf) |
d7837182 | 164 | char *obuf; |
b1b7b521 | 165 | const char *ibuf; |
d7837182 | 166 | { |
b1b7b521 | 167 | const char *s = ibuf; |
7c695fad TL |
168 | char *p = obuf; |
169 | int infmt = 0; | |
b1b7b521 | 170 | const char *m; |
d7837182 | 171 | |
7c695fad | 172 | while (*s) |
d7837182 | 173 | { |
7c695fad TL |
174 | if (infmt) |
175 | { | |
176 | if (*s == 'm') | |
177 | { | |
178 | #ifndef __CYGWIN32__ | |
179 | m = strerror (errno); | |
180 | #else | |
181 | m = pWSAError (); | |
182 | #endif | |
183 | if (!m) | |
184 | m = "<unknown error>"; | |
185 | strcpy (p - 1, m); | |
186 | p += strlen (p); | |
187 | ++s; | |
188 | } | |
189 | else | |
190 | *p++ = *s++; | |
191 | infmt = 0; | |
192 | } | |
193 | else | |
194 | { | |
195 | if (*s == '%') | |
196 | infmt = 1; | |
197 | *p++ = *s++; | |
198 | } | |
d7837182 | 199 | } |
7c695fad | 200 | *p = 0; |
d7837182 TL |
201 | } |
202 | ||
cd05ee6b TL |
203 | #ifdef NO_STRERROR |
204 | char *strerror (err) | |
205 | int err; | |
206 | { | |
207 | extern char *sys_errlist []; | |
208 | extern int sys_nerr; | |
209 | static char errbuf [128]; | |
210 | ||
211 | if (err < 0 || err >= sys_nerr) { | |
212 | sprintf (errbuf, "Error %d", err); | |
213 | return errbuf; | |
214 | } | |
215 | return sys_errlist [err]; | |
216 | } | |
217 | #endif /* NO_STRERROR */ | |
7c695fad TL |
218 | |
219 | #ifdef _WIN32 | |
220 | char *pWSAError () | |
221 | { | |
222 | int err = WSAGetLastError (); | |
223 | ||
224 | switch (err) | |
225 | { | |
226 | case WSAEACCES: | |
227 | return "Permission denied"; | |
228 | case WSAEADDRINUSE: | |
229 | return "Address already in use"; | |
230 | case WSAEADDRNOTAVAIL: | |
231 | return "Cannot assign requested address"; | |
232 | case WSAEAFNOSUPPORT: | |
233 | return "Address family not supported by protocol family"; | |
234 | case WSAEALREADY: | |
235 | return "Operation already in progress"; | |
236 | case WSAECONNABORTED: | |
237 | return "Software caused connection abort"; | |
238 | case WSAECONNREFUSED: | |
239 | return "Connection refused"; | |
240 | case WSAECONNRESET: | |
241 | return "Connection reset by peer"; | |
242 | case WSAEDESTADDRREQ: | |
243 | return "Destination address required"; | |
244 | case WSAEFAULT: | |
245 | return "Bad address"; | |
246 | case WSAEHOSTDOWN: | |
247 | return "Host is down"; | |
248 | case WSAEHOSTUNREACH: | |
249 | return "No route to host"; | |
250 | case WSAEINPROGRESS: | |
251 | return "Operation now in progress"; | |
252 | case WSAEINTR: | |
253 | return "Interrupted function call"; | |
254 | case WSAEINVAL: | |
255 | return "Invalid argument"; | |
256 | case WSAEISCONN: | |
257 | return "Socket is already connected"; | |
258 | case WSAEMFILE: | |
259 | return "Too many open files"; | |
260 | case WSAEMSGSIZE: | |
261 | return "Message too long"; | |
262 | case WSAENETDOWN: | |
263 | return "Network is down"; | |
264 | case WSAENETRESET: | |
265 | return "Network dropped connection on reset"; | |
266 | case WSAENETUNREACH: | |
267 | return "Network is unreachable"; | |
268 | case WSAENOBUFS: | |
269 | return "No buffer space available"; | |
270 | case WSAENOPROTOOPT: | |
271 | return "Bad protocol option"; | |
272 | case WSAENOTCONN: | |
273 | return "Socket is not connected"; | |
274 | case WSAENOTSOCK: | |
275 | return "Socket operation on non-socket"; | |
276 | case WSAEOPNOTSUPP: | |
277 | return "Operation not supported"; | |
278 | case WSAEPFNOSUPPORT: | |
279 | return "Protocol family not supported"; | |
280 | case WSAEPROCLIM: | |
281 | return "Too many processes"; | |
282 | case WSAEPROTONOSUPPORT: | |
283 | return "Protocol not supported"; | |
284 | case WSAEPROTOTYPE: | |
285 | return "Protocol wrong type for socket"; | |
286 | case WSAESHUTDOWN: | |
287 | return "Cannot send after socket shutdown"; | |
288 | case WSAESOCKTNOSUPPORT: | |
289 | return "Socket type not supported"; | |
290 | case WSAETIMEDOUT: | |
291 | return "Connection timed out"; | |
292 | case WSAEWOULDBLOCK: | |
293 | return "Resource temporarily unavailable"; | |
294 | case WSAHOST_NOT_FOUND: | |
295 | return "Host not found"; | |
296 | #if 0 | |
297 | case WSA_INVALID_HANDLE: | |
298 | return "Specified event object handle is invalid"; | |
299 | case WSA_INVALID_PARAMETER: | |
300 | return "One or more parameters are invalid"; | |
301 | case WSAINVALIDPROCTABLE: | |
302 | return "Invalid procedure table from service provider"; | |
303 | case WSAINVALIDPROVIDER: | |
304 | return "Invalid service provider version number"; | |
305 | case WSA_IO_PENDING: | |
306 | return "Overlapped operations will complete later"; | |
307 | case WSA_IO_INCOMPLETE: | |
308 | return "Overlapped I/O event object not in signaled state"; | |
309 | case WSA_NOT_ENOUGH_MEMORY: | |
310 | return "Insufficient memory available"; | |
311 | #endif | |
312 | case WSANOTINITIALISED: | |
313 | return "Successful WSAStartup not yet performer"; | |
314 | case WSANO_DATA: | |
315 | return "Valid name, no data record of requested type"; | |
316 | case WSANO_RECOVERY: | |
317 | return "This is a non-recoverable error"; | |
318 | #if 0 | |
319 | case WSAPROVIDERFAILEDINIT: | |
320 | return "Unable to initialize a service provider"; | |
321 | case WSASYSCALLFAILURE: | |
322 | return "System call failure"; | |
323 | #endif | |
324 | case WSASYSNOTREADY: | |
325 | return "Network subsystem is unavailable"; | |
326 | case WSATRY_AGAIN: | |
327 | return "Non-authoritative host not found"; | |
328 | case WSAVERNOTSUPPORTED: | |
329 | return "WINSOCK.DLL version out of range"; | |
330 | case WSAEDISCON: | |
331 | return "Graceful shutdown in progress"; | |
332 | #if 0 | |
333 | case WSA_OPERATION_ABORTED: | |
334 | return "Overlapped operation aborted"; | |
335 | #endif | |
336 | } | |
337 | return "Unknown WinSock error"; | |
338 | } | |
339 | #endif /* _WIN32 */ |