6 * Copyright 1987 by the Student Information Processing Board
7 * of the Massachusetts Institute of Technology
9 * Permission to use, copy, modify, and distribute this software and
10 * its documentation for any purpose is hereby granted, provided that
11 * the names of M.I.T. and the M.I.T. S.I.P.B. not be used in
12 * advertising or publicity pertaining to distribution of the software
13 * without specific, written prior permission. M.I.T. and the
14 * M.I.T. S.I.P.B. make no representations about the suitability of
15 * this software for any purpose. It is provided "as is" without
16 * express or implied warranty.
24 #ifdef HAVE_SYS_PRCTL_H
25 #include <sys/prctl.h>
27 #define PR_GET_DUMPABLE 3
29 #if (!defined(HAVE_PRCTL) && defined(linux))
30 #include <sys/syscall.h>
32 #ifdef HAVE_SEMAPHORE_H
33 #include <semaphore.h>
40 #include <sys/types.h>
43 #include "error_table.h"
47 #define THREAD_LOCAL static TLS
49 #define THREAD_LOCAL static
52 THREAD_LOCAL char buffer[25];
54 struct et_list * _et_list = (struct et_list *) NULL;
55 struct et_list * _et_dynamic_list = (struct et_list *) NULL;
58 #define COMERR_ATTR(x) __attribute__(x)
60 #define COMERR_ATTR(x)
64 static sem_t _et_lock;
65 static int _et_lock_initialized;
67 static void COMERR_ATTR((constructor)) setup_et_lock(void)
69 sem_init(&_et_lock, 0, 1);
70 _et_lock_initialized = 1;
73 static void COMERR_ATTR((destructor)) fini_et_lock(void)
75 sem_destroy(&_et_lock);
76 _et_lock_initialized = 0;
81 int et_list_lock(void)
84 if (!_et_lock_initialized)
86 return sem_wait(&_et_lock);
92 int et_list_unlock(void)
95 if (_et_lock_initialized)
96 return sem_post(&_et_lock);
101 typedef char *(*gettextf) (const char *);
103 static gettextf com_err_gettext = NULL;
105 gettextf set_com_err_gettext(gettextf new_proc)
107 gettextf x = com_err_gettext;
109 com_err_gettext = new_proc;
115 const char * error_message (errcode_t code)
123 offset = (int) (code & ((1<<ERRCODE_RANGE)-1));
124 table_num = code - offset;
126 #ifdef HAS_SYS_ERRLIST
127 if (offset < sys_nerr)
128 return(sys_errlist[offset]);
132 cp = strerror(offset);
140 for (et = _et_list; et; et = et->next) {
141 if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) {
142 /* This is the right table */
143 if (et->table->n_msgs <= offset) {
146 const char *msg = et->table->msgs[offset];
149 return (*com_err_gettext)(msg);
155 for (et = _et_dynamic_list; et; et = et->next) {
156 if ((et->table->base & 0xffffffL) == (table_num & 0xffffffL)) {
157 /* This is the right table */
158 if (et->table->n_msgs <= offset) {
161 const char *msg = et->table->msgs[offset];
164 return (*com_err_gettext)(msg);
172 strcpy (buffer, "Unknown code ");
174 strcat (buffer, error_table_name (table_num));
175 strcat (buffer, " ");
177 for (cp = buffer; *cp; cp++)
180 *cp++ = '0' + offset / 100;
184 if (started || offset >= 10) {
185 *cp++ = '0' + offset / 10;
188 *cp++ = '0' + offset;
194 * This routine will only return a value if the we are not running as
195 * a privileged process.
197 static char *safe_getenv(const char *arg)
199 if ((getuid() != geteuid()) || (getgid() != getegid()))
202 if (prctl(PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
205 #if (defined(linux) && defined(SYS_prctl))
206 if (syscall(SYS_prctl, PR_GET_DUMPABLE, 0, 0, 0, 0) == 0)
211 #if defined(HAVE_SECURE_GETENV)
212 return secure_getenv(arg);
213 #elif defined(HAVE___SECURE_GETENV)
214 return __secure_getenv(arg);
220 #define DEBUG_INIT 0x8000
221 #define DEBUG_ADDREMOVE 0x0001
223 static int debug_mask = 0;
224 static FILE *debug_f = 0;
226 static void init_debug(void)
228 char *dstr, *fn, *tmp;
231 if (debug_mask & DEBUG_INIT)
234 dstr = getenv("COMERR_DEBUG");
236 debug_mask = strtoul(dstr, &tmp, 0);
241 debug_mask |= DEBUG_INIT;
242 if (debug_mask == DEBUG_INIT)
245 fn = safe_getenv("COMERR_DEBUG_FILE");
247 debug_f = fopen(fn, "a");
249 debug_f = fopen("/dev/tty", "a");
251 fd = fileno(debug_f);
253 flags = fcntl(fd, F_GETFD);
255 fcntl(fd, F_SETFD, flags | FD_CLOEXEC);
258 debug_mask = DEBUG_INIT;
263 * New interface provided by krb5's com_err library
265 errcode_t add_error_table(const struct error_table * et)
269 if (!(el = (struct et_list *) malloc(sizeof(struct et_list))))
272 if (et_list_lock() != 0) {
278 el->next = _et_dynamic_list;
279 _et_dynamic_list = el;
282 if (debug_mask & DEBUG_ADDREMOVE)
283 fprintf(debug_f, "add_error_table: %s (0x%p)\n",
284 error_table_name(et->base),
292 * New interface provided by krb5's com_err library
294 errcode_t remove_error_table(const struct error_table * et)
297 struct et_list *el2 = 0;
299 if (et_list_lock() != 0)
302 el = _et_dynamic_list;
305 if (el->table->base == et->base) {
306 if (el2) /* Not the beginning of the list */
307 el2->next = el->next;
309 _et_dynamic_list = el->next;
311 if (debug_mask & DEBUG_ADDREMOVE)
313 "remove_error_table: %s (0x%p)\n",
314 error_table_name(et->base),
322 if (debug_mask & DEBUG_ADDREMOVE)
323 fprintf(debug_f, "remove_error_table FAILED: %s (0x%p)\n",
324 error_table_name(et->base),
331 * Variant of the interface provided by Heimdal's com_err library
334 add_to_error_table(struct et_list *new_table)
336 add_error_table(new_table->table);