errwarn.c 8.94 KB
Newer Older
Ted Lemon's avatar
Ted Lemon committed
1
2
3
4
5
/* errwarn.c

   Errors and warnings... */

/*
Ted Lemon's avatar
Ted Lemon committed
6
 * Copyright (c) 1995 RadioMail Corporation.
Wlodzimierz Wencel's avatar
Wlodzimierz Wencel committed
7
 * Copyright (C) 2004-2022 Internet Systems Consortium, Inc. ("ISC")
8
 * Copyright (c) 1996-2003 by Internet Software Consortium
Ted Lemon's avatar
Ted Lemon committed
9
 *
10
11
12
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
Ted Lemon's avatar
Ted Lemon committed
13
 *
14
15
16
17
18
19
20
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Ted Lemon's avatar
Ted Lemon committed
21
 *
22
 *   Internet Systems Consortium, Inc.
Tomek Mrugalski's avatar
Tomek Mrugalski committed
23
24
 *   PO Box 360
 *   Newmarket, NH 03857 USA
25
 *   <info@isc.org>
26
 *   https://www.isc.org/
Ted Lemon's avatar
Ted Lemon committed
27
28
 *
 * This software was written for RadioMail Corporation by Ted Lemon
Ted Lemon's avatar
Ted Lemon committed
29
 * under a contract with Vixie Enterprises.   Further modifications have
30
 * been made for Internet Systems Consortium under a contract
Ted Lemon's avatar
Ted Lemon committed
31
 * with Vixie Laboratories.
Ted Lemon's avatar
Ted Lemon committed
32
33
 */

34
35
#include "dhcpd.h"

36
#include <omapip/omapip_p.h>
Ted Lemon's avatar
Ted Lemon committed
37
#include <errno.h>
38
#include <syslog.h>
Ted Lemon's avatar
Ted Lemon committed
39

40
41
42
43
44
45
#ifdef DEBUG
int log_perror = -1;
#else
int log_perror = 1;
#endif
void (*log_cleanup) (void);
Ted Lemon's avatar
Ted Lemon committed
46

47
48
49
#define CVT_BUF_MAX 1023
static char mbuf [CVT_BUF_MAX + 1];
static char fbuf [CVT_BUF_MAX + 1];
Ted Lemon's avatar
Ted Lemon committed
50
51
52

/* Log an error message, then exit... */

53
void log_fatal (const char * fmt, ... )
Ted Lemon's avatar
Ted Lemon committed
54
55
56
{
  va_list list;

57
  do_percentm (fbuf, sizeof fbuf, fmt);
Ted Lemon's avatar
Ted Lemon committed
58

59
60
61
  /* %Audit% This is log output. %2004.06.17,Safe%
   * If we truncate we hope the user can get a hint from the log.
   */
62
  va_start (list, fmt);
Ted Lemon's avatar
Ted Lemon committed
63
64
  vsnprintf (mbuf, sizeof mbuf, fbuf, list);
  va_end (list);
65

66
#ifndef DEBUG
67
  syslog (LOG_ERR, "%s", mbuf);
68
#endif
69
70
71

  /* Also log it to stderr? */
  if (log_perror) {
72
73
	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
74
  }
Ted Lemon's avatar
Ted Lemon committed
75

76
  log_error ("%s", "");
77
78
79
80
  log_error ("If you think you have received this message due to a bug rather");
  log_error ("than a configuration issue please read the section on submitting");
  log_error ("bugs on either our web page at www.isc.org or in the README file");
  log_error ("before submitting a bug.  These pages explain the proper");
81
  log_error ("process and the information we find helpful for debugging.");
82
  log_error ("%s", "");
83
  log_error ("exiting.");
84

85
86
  if (log_cleanup)
	  (*log_cleanup) ();
Ted Lemon's avatar
Ted Lemon committed
87
88
89
  exit (1);
}

90
/* Log an error message... */
Ted Lemon's avatar
Ted Lemon committed
91

92
int log_error (const char * fmt, ...)
Ted Lemon's avatar
Ted Lemon committed
93
94
95
{
  va_list list;

96
  do_percentm (fbuf, sizeof fbuf, fmt);
Ted Lemon's avatar
Ted Lemon committed
97

98
99
100
  /* %Audit% This is log output. %2004.06.17,Safe%
   * If we truncate we hope the user can get a hint from the log.
   */
101
  va_start (list, fmt);
Ted Lemon's avatar
Ted Lemon committed
102
103
  vsnprintf (mbuf, sizeof mbuf, fbuf, list);
  va_end (list);
104

105
#ifndef DEBUG
106
  syslog (LOG_ERR, "%s", mbuf);
107
#endif
108
109

  if (log_perror) {
110
111
	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
112
113
  }

Ted Lemon's avatar
Ted Lemon committed
114
115
116
117
118
  return 0;
}

/* Log a note... */

119
int log_info (const char *fmt, ...)
Ted Lemon's avatar
Ted Lemon committed
120
121
122
{
  va_list list;

123
  do_percentm (fbuf, sizeof fbuf, fmt);
Ted Lemon's avatar
Ted Lemon committed
124

125
126
127
  /* %Audit% This is log output. %2004.06.17,Safe%
   * If we truncate we hope the user can get a hint from the log.
   */
128
  va_start (list, fmt);
Ted Lemon's avatar
Ted Lemon committed
129
130
  vsnprintf (mbuf, sizeof mbuf, fbuf, list);
  va_end (list);
131

132
#ifndef DEBUG
133
  syslog (LOG_INFO, "%s", mbuf);
134
#endif
135
136

  if (log_perror) {
137
138
	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
139
140
  }

Ted Lemon's avatar
Ted Lemon committed
141
142
143
144
145
  return 0;
}

/* Log a debug message... */

146
int log_debug (const char *fmt, ...)
Ted Lemon's avatar
Ted Lemon committed
147
148
149
{
  va_list list;

150
  do_percentm (fbuf, sizeof fbuf, fmt);
Ted Lemon's avatar
Ted Lemon committed
151

152
153
154
  /* %Audit% This is log output. %2004.06.17,Safe%
   * If we truncate we hope the user can get a hint from the log.
   */
155
  va_start (list, fmt);
Ted Lemon's avatar
Ted Lemon committed
156
157
  vsnprintf (mbuf, sizeof mbuf, fbuf, list);
  va_end (list);
158

159
#ifndef DEBUG
160
  syslog (LOG_DEBUG, "%s", mbuf);
161
#endif
162
163

  if (log_perror) {
164
165
	  IGNORE_RET (write (STDERR_FILENO, mbuf, strlen (mbuf)));
	  IGNORE_RET (write (STDERR_FILENO, "\n", 1));
166
167
  }

Ted Lemon's avatar
Ted Lemon committed
168
169
170
171
172
  return 0;
}

/* Find %m in the input string and substitute an error message string. */

173
void do_percentm (obuf, obufsize, ibuf)
Ted Lemon's avatar
Ted Lemon committed
174
     char *obuf;
175
     size_t obufsize;
176
     const char *ibuf;
Ted Lemon's avatar
Ted Lemon committed
177
{
178
	const char *s = ibuf;
179
180
	char *p = obuf;
	int infmt = 0;
181
	const char *m;
182
	int len = 0;
Ted Lemon's avatar
Ted Lemon committed
183

184
185
186
	while (*s) {
		if (infmt) {
			if (*s == 'm') {
187
188
189
190
191
192
193
#ifndef __CYGWIN32__
				m = strerror (errno);
#else
				m = pWSAError ();
#endif
				if (!m)
					m = "<unknown error>";
194
				len += strlen (m);
195
				if (len > obufsize - 1)
196
					goto out;
197
198
199
				strcpy (p - 1, m);
				p += strlen (p);
				++s;
200
			} else {
201
				if (++len > obufsize - 1)
202
					goto out;
203
				*p++ = *s++;
204
			}
205
			infmt = 0;
206
		} else {
207
208
			if (*s == '%')
				infmt = 1;
209
			if (++len > obufsize - 1)
210
				goto out;
211
212
			*p++ = *s++;
		}
Ted Lemon's avatar
Ted Lemon committed
213
	}
214
      out:
215
	*p = 0;
Ted Lemon's avatar
Ted Lemon committed
216
217
}

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#ifdef NO_STRERROR
char *strerror (err)
	int err;
{
	extern char *sys_errlist [];
	extern int sys_nerr;
	static char errbuf [128];

	if (err < 0 || err >= sys_nerr) {
		sprintf (errbuf, "Error %d", err);
		return errbuf;
	}
	return sys_errlist [err];
}
#endif /* NO_STRERROR */
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354

#ifdef _WIN32
char *pWSAError ()
{
  int err = WSAGetLastError ();

  switch (err)
    {
    case WSAEACCES:
      return "Permission denied";
    case WSAEADDRINUSE:
      return "Address already in use";
    case WSAEADDRNOTAVAIL:
      return "Cannot assign requested address";
    case WSAEAFNOSUPPORT:
      return "Address family not supported by protocol family";
    case WSAEALREADY:
      return "Operation already in progress";
    case WSAECONNABORTED:
      return "Software caused connection abort";
    case WSAECONNREFUSED:
      return "Connection refused";
    case WSAECONNRESET:
      return "Connection reset by peer";
    case WSAEDESTADDRREQ:
      return "Destination address required";
    case WSAEFAULT:
      return "Bad address";
    case WSAEHOSTDOWN:
      return "Host is down";
    case WSAEHOSTUNREACH:
      return "No route to host";
    case WSAEINPROGRESS:
      return "Operation now in progress";
    case WSAEINTR:
      return "Interrupted function call";
    case WSAEINVAL:
      return "Invalid argument";
    case WSAEISCONN:
      return "Socket is already connected";
    case WSAEMFILE:
      return "Too many open files";
    case WSAEMSGSIZE:
      return "Message too long";
    case WSAENETDOWN:
      return "Network is down";
    case WSAENETRESET:
      return "Network dropped connection on reset";
    case WSAENETUNREACH:
      return "Network is unreachable";
    case WSAENOBUFS:
      return "No buffer space available";
    case WSAENOPROTOOPT:
      return "Bad protocol option";
    case WSAENOTCONN:
      return "Socket is not connected";
    case WSAENOTSOCK:
      return "Socket operation on non-socket";
    case WSAEOPNOTSUPP:
      return "Operation not supported";
    case WSAEPFNOSUPPORT:
      return "Protocol family not supported";
    case WSAEPROCLIM:
      return "Too many processes";
    case WSAEPROTONOSUPPORT:
      return "Protocol not supported";
    case WSAEPROTOTYPE:
      return "Protocol wrong type for socket";
    case WSAESHUTDOWN:
      return "Cannot send after socket shutdown";
    case WSAESOCKTNOSUPPORT:
      return "Socket type not supported";
    case WSAETIMEDOUT:
      return "Connection timed out";
    case WSAEWOULDBLOCK:
      return "Resource temporarily unavailable";
    case WSAHOST_NOT_FOUND:
      return "Host not found";
#if 0
    case WSA_INVALID_HANDLE:
      return "Specified event object handle is invalid";
    case WSA_INVALID_PARAMETER:
      return "One or more parameters are invalid";
    case WSAINVALIDPROCTABLE:
      return "Invalid procedure table from service provider";
    case WSAINVALIDPROVIDER:
      return "Invalid service provider version number";
    case WSA_IO_PENDING:
      return "Overlapped operations will complete later";
    case WSA_IO_INCOMPLETE:
      return "Overlapped I/O event object not in signaled state";
    case WSA_NOT_ENOUGH_MEMORY:
      return "Insufficient memory available";
#endif
    case WSANOTINITIALISED:
      return "Successful WSAStartup not yet performer";
    case WSANO_DATA:
      return "Valid name, no data record of requested type";
    case WSANO_RECOVERY:
      return "This is a non-recoverable error";
#if 0
    case WSAPROVIDERFAILEDINIT:
      return "Unable to initialize a service provider";
    case WSASYSCALLFAILURE:
      return "System call failure";
#endif
    case WSASYSNOTREADY:
      return "Network subsystem is unavailable";
    case WSATRY_AGAIN:
      return "Non-authoritative host not found";
    case WSAVERNOTSUPPORTED:
      return "WINSOCK.DLL version out of range";
    case WSAEDISCON:
      return "Graceful shutdown in progress";
#if 0
    case WSA_OPERATION_ABORTED:
      return "Overlapped operation aborted";
#endif
    }
  return "Unknown WinSock error";
}
#endif /* _WIN32 */