1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
/*
* Copyright 1997,2000,2001,2004 by Massachusetts Institute of Technology
*
* Copyright 1987, 1988 by MIT Student Information Processing Board
*
* Permission to use, copy, modify, and distribute this software
* and its documentation for any purpose and without fee is
* hereby granted, provided that the above copyright notice
* appear in all copies and that both that copyright notice and
* this permission notice appear in supporting documentation,
* and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
* used in advertising or publicity pertaining to distribution
* of the software without specific, written prior permission.
* Furthermore if you modify this software you must label
* your software as modified software and not distribute it in such a
* fashion that it might be confused with the original M.I.T. software.
* M.I.T. and the M.I.T. S.I.P.B. make no representations about
* the suitability of this software for any purpose. It is
* provided "as is" without express or implied warranty.
*/
#include "autoconf.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <string.h>
#include "com_err.h"
#include "error_table.h"
#include "k5-platform.h"
#if !defined(HAVE_STRERROR) && !defined(SYS_ERRLIST_DECLARED)
extern char const * const sys_errlist[];
extern const int sys_nerr;
#endif
/*@null@*/ static struct et_list * _et_list = (struct et_list *) NULL;
/*@null@*//*@only@*/static struct dynamic_et_list * et_list_dynamic;
static k5_mutex_t et_list_lock = K5_MUTEX_PARTIAL_INITIALIZER;
static int terminated = 0; /* for debugging shlib fini sequence errors */
MAKE_INIT_FUNCTION(com_err_initialize);
MAKE_FINI_FUNCTION(com_err_terminate);
int com_err_initialize(void)
{
int err;
#ifdef SHOW_INITFINI_FUNCS
printf("com_err_initialize\n");
#endif
terminated = 0;
err = k5_mutex_finish_init(&et_list_lock);
if (err)
return err;
err = k5_mutex_finish_init(&com_err_hook_lock);
if (err)
return err;
err = k5_key_register(K5_KEY_COM_ERR, free);
if (err)
return err;
return 0;
}
void com_err_terminate(void)
{
struct dynamic_et_list *e, *enext;
if (! INITIALIZER_RAN(com_err_initialize) || PROGRAM_EXITING()) {
#ifdef SHOW_INITFINI_FUNCS
printf("com_err_terminate: skipping\n");
#endif
return;
}
#ifdef SHOW_INITFINI_FUNCS
printf("com_err_terminate\n");
#endif
k5_key_delete(K5_KEY_COM_ERR);
k5_mutex_destroy(&com_err_hook_lock);
k5_mutex_lock(&et_list_lock);
for (e = et_list_dynamic; e; e = enext) {
enext = e->next;
free(e);
}
k5_mutex_unlock(&et_list_lock);
k5_mutex_destroy(&et_list_lock);
terminated = 1;
}
#ifndef DEBUG_TABLE_LIST
#define dprintf(X)
#else
#define dprintf(X) printf X
#endif
const char * KRB5_CALLCONV
error_message(long code)
/*@modifies internalState@*/
{
unsigned long offset;
unsigned long l_offset;
struct et_list *et;
struct dynamic_et_list *det;
unsigned long table_num;
int started = 0;
unsigned int divisor = 100;
char *cp, *cp1;
const struct error_table *table;
int merr;
l_offset = (unsigned long)code & ((1<<ERRCODE_RANGE)-1);
offset = l_offset;
table_num = ((unsigned long)code - l_offset) & ERRCODE_MAX;
if (table_num == 0) {
#ifdef __sgi
system_error_code:
#endif
if (code == 0)
goto oops;
/* This could trip if int is 16 bits. */
if ((unsigned long)(int)code != code)
abort ();
#ifdef HAVE_STRERROR
cp = strerror((int) code);
if (cp)
return cp;
goto oops;
#else
#ifdef HAVE_SYS_ERRLIST
if (offset < sys_nerr)
return(sys_errlist[offset]);
else
goto oops;
#else
goto oops;
#endif /* HAVE_SYS_ERRLIST */
#endif /* HAVE_STRERROR */
}
#ifdef __sgi
/* Irix 6.5 uses a much bigger table than other UNIX systems
I've looked at, but the table is sparse. The sparse
entries start around 500, but sys_nerr is only 152. */
if (code > 0 && code <= 1600)
goto system_error_code;
#endif
if (CALL_INIT_FUNCTION(com_err_initialize))
return 0;
merr = k5_mutex_lock(&et_list_lock);
if (merr)
goto oops;
dprintf (("scanning static list for %x\n", table_num));
for (et = _et_list; et != NULL; et = et->next) {
if (et->table == NULL)
continue;
dprintf (("\t%x = %s\n", et->table->base & ERRCODE_MAX,
et->table->msgs[0]));
if ((et->table->base & ERRCODE_MAX) == table_num) {
table = et->table;
goto found;
}
}
dprintf (("scanning dynamic list for %x\n", table_num));
for (det = et_list_dynamic; det != NULL; det = det->next) {
dprintf (("\t%x = %s\n", det->table->base & ERRCODE_MAX,
det->table->msgs[0]));
if ((det->table->base & ERRCODE_MAX) == table_num) {
table = det->table;
goto found;
}
}
goto no_table_found;
found:
k5_mutex_unlock(&et_list_lock);
dprintf (("found it!\n"));
/* This is the right table */
/* This could trip if int is 16 bits. */
if ((unsigned long)(unsigned int)offset != offset)
goto no_table_found;
if (table->n_msgs <= (unsigned int) offset)
goto no_table_found;
return table->msgs[offset];
no_table_found:
k5_mutex_unlock(&et_list_lock);
#if defined(_WIN32)
/*
* WinSock errors exist in the 10000 and 11000 ranges
* but might not appear if WinSock is not initialized
*/
if (code >= WSABASEERR && code < WSABASEERR + 1100) {
table_num = 0;
offset = code;
divisor = WSABASEERR;
}
#endif
#ifdef _WIN32
{
LPVOID msgbuf;
if (! FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
NULL /* lpSource */,
(DWORD) code,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR) &msgbuf,
(DWORD) 0 /*sizeof(buffer)*/,
NULL /* va_list */ )) {
/*
* WinSock errors exist in the 10000 and 11000 ranges
* but might not appear if WinSock is not initialized
*/
if (code >= WSABASEERR && code < WSABASEERR + 1100) {
table_num = 0;
offset = code;
divisor = 10000;
}
goto oops;
} else {
char *buffer;
cp = k5_getspecific(K5_KEY_COM_ERR);
if (cp == NULL) {
cp = malloc(ET_EBUFSIZ);
if (cp == NULL) {
win32_no_specific:
return "Unknown error code";
}
if (k5_setspecific(K5_KEY_COM_ERR, cp) != 0) {
free(cp);
goto win32_no_specific;
}
}
buffer = cp;
strncpy(buffer, msgbuf, ET_EBUFSIZ);
buffer[ET_EBUFSIZ-1] = '\0';
cp = buffer + strlen(buffer) - 1;
if (*cp == '\n') *cp-- = '\0';
if (*cp == '\r') *cp-- = '\0';
if (*cp == '.') *cp-- = '\0';
LocalFree(msgbuf);
return buffer;
}
}
#endif
oops:
#if TARGET_OS_MAC
{
/* ComErr doesn't know about this error, ask the system */
/* Of course there's no way to tell if it knew what error it got */
return (strerror (code));
}
#endif
cp = k5_getspecific(K5_KEY_COM_ERR);
if (cp == NULL) {
cp = malloc(ET_EBUFSIZ);
if (cp == NULL) {
no_specific:
return "Unknown error code";
}
if (k5_setspecific(K5_KEY_COM_ERR, cp) != 0) {
free(cp);
goto no_specific;
}
}
cp1 = cp;
strcpy(cp, "Unknown code ");
cp += sizeof("Unknown code ") - 1;
if (table_num != 0L) {
(void) error_table_name_r(table_num, cp);
while (*cp != '\0')
cp++;
*cp++ = ' ';
}
while (divisor > 1) {
if (started != 0 || offset >= divisor) {
*cp++ = '0' + offset / divisor;
offset %= divisor;
started++;
}
divisor /= 10;
}
*cp++ = '0' + offset;
*cp = '\0';
return(cp1);
}
/*@-incondefs@*/ /* _et_list is global on unix but not in header annotations */
errcode_t KRB5_CALLCONV
add_error_table(/*@dependent@*/ const struct error_table * et)
/*@modifies _et_list,et_list_dynamic@*/
/*@=incondefs@*/
{
struct dynamic_et_list *del;
int merr;
if (CALL_INIT_FUNCTION(com_err_initialize))
return 0;
del = (struct dynamic_et_list *)malloc(sizeof(struct dynamic_et_list));
if (del == NULL)
return errno;
del->table = et;
merr = k5_mutex_lock(&et_list_lock);
if (merr) {
free(del);
return merr;
}
del->next = et_list_dynamic;
et_list_dynamic = del;
return k5_mutex_unlock(&et_list_lock);
}
/*@-incondefs@*/ /* _et_list is global on unix but not in header annotations */
errcode_t KRB5_CALLCONV
remove_error_table(const struct error_table * et)
/*@modifies _et_list,et_list_dynamic@*/
/*@=incondefs@*/
{
struct dynamic_et_list **del;
struct et_list **el;
int merr;
if (CALL_INIT_FUNCTION(com_err_initialize))
return 0;
#if !defined(ENABLE_THREADS) && defined(DEBUG_THREADS)
if (et_list_lock.os.initialized == 0 && terminated != 0) {
fprintf(stderr, "\n\n *** Function remove_error_table called after com_err library termination. ***\n *** Shared library termination code executed in incorrect order? ***\n\n");
abort();
}
#endif
merr = k5_mutex_lock(&et_list_lock);
if (merr)
return merr;
/* Remove the first occurrance we can find. Prefer dynamic
entries, but if there are none, check for a static one too. */
for (del = &et_list_dynamic; *del; del = &(*del)->next)
if ((*del)->table->base == et->base) {
/*@only@*/ struct dynamic_et_list *old = *del;
*del = old->next;
free (old);
return k5_mutex_unlock(&et_list_lock);
}
for (el = &_et_list; *el; el = &(*el)->next)
if ((*el)->table != NULL && (*el)->table->base == et->base) {
struct et_list *old = *el;
*el = old->next;
old->next = NULL;
old->table = NULL;
return k5_mutex_unlock(&et_list_lock);
}
k5_mutex_unlock(&et_list_lock);
return ENOENT;
}
int com_err_finish_init()
{
return CALL_INIT_FUNCTION(com_err_initialize);
}
|